Persistent Context Across Sessions for Every Agent – Captures everything your agent does during sessions, compresses it with AI, and injects relevant context back into future sessions. Works with Claude Code, OpenClaw, Codex, Gemini, Hermes, Copilot, OpenCode + More
TypeScript aiai-agentsai-memoryanthropicartificial-intelligencechromadbclaudeclaude-agent-sdkclaude-agentsclaude-codeclaude-code-pluginclaude-skillsembeddingslong-term-memorymem0memory-engineopenmemoryragsqlitesupermemory
/**
* Telemetry capture for the short-lived npx CLI (install / update / uninstall).
*
* The worker uses the posthog-node SDK with background batching; a CLI process
* exits seconds after the event, so instead of bundling the SDK into the npx
* binary this posts one event directly to the public ingestion endpoint with a
* hard 2s timeout. Same consent gate, same whitelist scrubber, same anonymous
* install ID as the worker transport (telemetry.ts).
*/
import { resolveTelemetryConsent, loadTelemetryConfig, getOrCreateInstallId, getOrCreateAnonymousDistinctId } from './consent.js';
import { scrubProperties } from './scrub.js';
import { getTelemetryApiKey, getTelemetryHost, buildBaseProperties, buildPersonSet } from './common.js';
import { observeTelemetryVolume } from './daily-volume.js';
const CAPTURE_TIMEOUT_MS = 2000;
/**
* Capture a single CLI event. Resolves when the event is sent, times out, or
* is skipped (no consent / no key). Never throws, never rejects — an install
* must finish identically with telemetry unreachable.
*/
export async function captureCliEvent(
event: string,
props?: Record<string, unknown>,
opts?: { person?: boolean; bypassVolume?: boolean }
): Promise<void> {
try {
if (!resolveTelemetryConsent(process.env, loadTelemetryConfig())) {
return;
}
const properties: Record<string, unknown> = scrubProperties({
...buildBaseProperties(),
...(props ?? {}),
});
// One timeout for every post this call makes, sent in parallel, so the
// first capture of a day (which also closes yesterday's rollups, one per
// feature and tool) still finishes within CAPTURE_TIMEOUT_MS.
const signal = AbortSignal.timeout(CAPTURE_TIMEOUT_MS);
const posts: Promise<void>[] = [];
if (!opts?.bypassVolume) {
const decision = observeTelemetryVolume(event, properties);
for (const closed of decision.closed) {
posts.push(postCaptureEvent(getTelemetryApiKey(), closed.event, {
...closed.properties,
$process_person_profile: false,
}, signal, getOrCreateAnonymousDistinctId()));
}
if (!decision.forward) {
await Promise.allSettled(posts);
return;
}
}
// Install / uninstall events build the anonymous person profile that
// powers retention and cohort insights. Worker and plugin events do not:
// `$set` on the install id makes every later event for that id bill as
// identified. See telemetry.ts.
if (opts?.person) {
properties.$set = buildPersonSet(properties);
} else {
properties.$process_person_profile = false;
}
if (process.env.CLAUDE_MEM_TELEMETRY_DEBUG === '1') {
process.stderr.write('[telemetry] ' + JSON.stringify({ event, properties }) + '\n');
} else {
posts.push(postCaptureEvent(
getTelemetryApiKey(),
event,
properties,
signal,
opts?.person ? getOrCreateInstallId() : getOrCreateAnonymousDistinctId(),
));
}
await Promise.allSettled(posts);
} catch {
// Telemetry must never break or slow the CLI. Swallow everything.
}
}
/**
* POST one event to the public ingestion endpoint.
*
* Deliberately no $geoip_disable: the raw capture API geolocates from the
* request IP at ingest (coarse country/region/city; the IP itself is
* discarded), matching the worker transport (telemetry.ts).
*/
async function postCaptureEvent(
apiKey: string,
event: string,
properties: Record<string, unknown>,
signal: AbortSignal,
distinctId: string,
): Promise<void> {
await fetch(`${getTelemetryHost()}/capture/`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
api_key: apiKey,
event,
distinct_id: distinctId,
properties,
}),
signal,
});
}