aisdk-posthog weekly downloads statistics
PostHog LLM analytics integration for the Vercel AI SDK (OpenTelemetry-based)
aisdk-posthog has 1,291 recorded weekly downloads on npm, an increase of 141% over the past 30 days, ranking #2010 of 3,247 tracked listings.
About aisdk-posthog
aisdk-posthog
PostHog LLM analytics integration for the Vercel AI SDK, built on OpenTelemetry.
Maps the spans emitted by AI SDK v7's @ai-sdk/otel integration (invoke_agent, chat, execute_tool, ...) to PostHog LLM observability events: $ai_trace, $ai_generation, $ai_span. Requires ai ^7 (v6 users: stay on aisdk-posthog@0.2.x). Includes split input/output/total cost in USD via llm-info, a deterministic execution-trace ID derived from your own ID, and a streaming-aware buffer that fixes parent-child span relationships when the AI SDK's TransformStream boundaries break OTel context propagation.
Status: community-maintained. Not an official PostHog SDK.
Install
npm install aisdk-posthog ai @ai-sdk/otel
# or
pnpm add aisdk-posthog ai @ai-sdk/otel
ai (>=7) and @ai-sdk/otel are required peer dependencies. Node 22.12+.
Two ways to use it
The package supports two modes that compose freely. Mix and match per file.
Mode A — drop-in subpath (zero per-call boilerplate)
Register a default telemetry instance once at app boot, then change one import line per file. Every LLM call inside is auto-instrumented; tool calls trace automatically; sub-agents pick up the right functionId via subAgent().
// app/boot.ts — register once
import { createAISDKTelemetry, setDefaultTelemetry } from 'aisdk-posthog';
const telemetry = createAISDKTelemetry({
apiKey: process.env.POSTHOG_API_KEY!,
enabled: process.env.NODE_ENV === 'production',
getContext: ({ spanAttributes, executionUidByTraceId }) => {
const executionUid =
(spanAttributes['ai.telemetry.metadata.executionUid'] as
| string
| undefined) ?? executionUidByTraceId;
if (!executionUid) return undefined;
// …look up your user/workspace/chat from `executionUid`…
return {
distinctId: 'user_42',
groupId: 'workspace_99',
groupType: 'workspace_id',
};
},
});
setDefaultTelemetry(telemetry);
// anywhere else — only the import line changes
- import { generateText, streamText, ToolLoopAgent, tool } from 'ai';
+ import { generateText, streamText, ToolLoopAgent, tool } from 'aisdk-posthog/ai';
// call sites stay literally identical
await generateText({ model, prompt }); // auto-instrumented
await streamText({ model, messages }); // auto-instrumented
const agent = new ToolLoopAgent({ model, instructions, tools }); // auto-instrumented
If setDefaultTelemetry is never called or telemetry is disabled, the wrappers forward untouched — calls behave exactly like importing from 'ai' directly.
Mode B — per-call embedding (explicit, no globals)
Hold the instance and pass telemetry: telemetry.getTelemetry(...) per call. No subpath, no module-level state. Use this when you want fine-grained control over functionId per call site.
import { generateText } from 'ai';
import { telemetry } from './boot';
await generateText({
model,
prompt,
telemetry: telemetry.getTelemetry('chat-reply', {
executionUid,
}),
});
Mixing modes
Both modes coexist. Caller-supplied telemetry (or the deprecated experimental_telemetry alias) always wins over the auto-injected default, so you can use the subpath everywhere and override per call when you want a custom functionId:
import { generateText } from 'aisdk-posthog/ai';
import { telemetry } from './boot';
// Most calls auto-instrument with default config
await generateText({ model, prompt });
// One specific call wants a custom functionId
await generateText({
model,
prompt,
telemetry: telemetry.getTelemetry('special-case'),
});
Sub-agents (tools that call LLMs internally)
Wrap the tool with subAgent('name', tool({...})). Inside the wrapped tool, the AI SDK functions imported from the subpath automatically use 'name' as their functionId so the sub-agent shows up by name in PostHog. This works for both generateText patterns and ToolLoopAgent patterns.
import { subAgent } from 'aisdk-posthog';
import { generateText, ToolLoopAgent, tool, stepCountIs } from 'aisdk-posthog/ai';
import { z } from 'zod';
tools: {
research: subAgent('research', tool({
description: 'Research a topic',
inputSchema: z.object({ topic: z.string() }),
execute: async ({ topic }, { abortSignal }) => {
// Functions imported from 'aisdk-posthog/ai' read the current
// sub-agent name from AsyncLocalStorage and tag the span as
// `functionId: 'research'`. No telemetry threading.
const agent = new ToolLoopAgent({
model, instructions, tools: innerTools, stopWhen: stepCountIs(12),
});
return (await agent.generate({ prompt: topic, abortSignal })).text;
},
})),
}
For per-call mode, read the current sub-agent name yourself:
import { tool } from 'ai';
import { subAgent, currentSubAgentName } from 'aisdk-posthog';
import { generateText } from 'ai';
tools: {
research: subAgent('research', tool({
description, inputSchema,
execute: async ({ topic }, { abortSignal }) => {
return generateText({
model, prompt: `Research: ${topic}`,
telemetry: telemetry.getTelemetry(
currentSubAgentName() ?? 'fallback',
),
abortSignal,
});
},
})),
}
Wrapping a top-level execution
Use withExecutionTrace to anchor an entire request under one PostHog trace with a stable, deterministic traceId derived from your own execution ID:
import { randomUUID } from 'node:crypto';
const requestId = randomUUID();
await telemetry.withExecutionTrace(
requestId,
'chat.reply',
{ userId: req.user.id, channel: 'slack' },
async () => {
// every LLM call inside lands as a child of `chat.reply`
return generateText({ model, prompt }); // subpath: auto-instrumented
},
);…
Description from the npm listing.
- ai-sdk
- vercel-ai-sdk
- posthog
- analytics
- llm
- ai
- observability
- telemetry
- opentelemetry
README badge
Show your rank in a README or on your website. The badge updates automatically.
Charts
Images: 30d · 1y · all time · 1600×900 dark
Adoption over time
7/30/90-day growth
Ranking history
Listing placement
Listing placement
Filled points are observed values; hollow points come from Internet Archive snapshots of the listing page. Dashed segments span periods with no observations — values between points are not interpolated.
Each band is one page of 20. Weekly downloads on a linear scale. Placement is projected from weekly downloads, so it can differ from the marketplace's own sort order. Hover a dot to see the listing.
Recent observations
Full history (142)Similar npm AI SDKs plugins
All 20 aisdk-posthog alternativesData source
Weekly downloads (trailing 7 complete days) come from the official npm downloads API. Downloads include CI and mirrors and are not equivalent to installs or users. npm publishes no ratings.
Listing: https://www.npmjs.com/package/aisdk-posthog. First seen Nov 12, 2026; last successful collection Dec 6, 2026. See the methodology.