Context
The Claude Agent SDK integration for LLM Analytics only exists in the Python SDK (posthog.ai.claude_agent_sdk, added in #53010). TypeScript users building on @anthropic-ai/claude-agent-sdk have no way to get traces into LLM Analytics today.
@posthog/ai currently covers Anthropic, OpenAI, OpenAI Agents, Gemini, LangChain, Vercel AI SDK and Google ADK. The existing Anthropic wrapper does not help here: the Agent SDK runs Claude Code itself, so requests never pass through the Anthropic client.
This has been asked for by a customer.
What
A claude-agent-sdk integration in posthog-js' packages/ai, mirroring the Python one:
- a
query() drop-in replacement for the SDK's query()
- a stateful client equivalent for multi-turn conversations
$ai_generation per LLM turn (model, tokens, cache metrics, cost, latency, input/output)
$ai_span per tool use
$ai_trace per query, with aggregate cost and latency
Why it should be small
The Python integration does not patch HTTP. It consumes the Agent SDK's own message stream: message_start to message_stop stream events delimit one generation, tool blocks become spans, and the result message closes the trace. The TypeScript SDK exposes the same surface, with includePartialMessages: true for the per-turn stream events and total_cost_usd plus usage on the result message. So the port is mostly a translation of posthog/ai/claude_agent_sdk/processor.py.
Reference: PostHog/posthog-python#477.
Notes
- Wrap all instrumentation in try/catch, so PostHog failures never interrupt the underlying query.
- Docs page
ai-observability/installation/claude-agent-sdk is Python-only and needs a TypeScript tab.
Context
The Claude Agent SDK integration for LLM Analytics only exists in the Python SDK (
posthog.ai.claude_agent_sdk, added in #53010). TypeScript users building on@anthropic-ai/claude-agent-sdkhave no way to get traces into LLM Analytics today.@posthog/aicurrently covers Anthropic, OpenAI, OpenAI Agents, Gemini, LangChain, Vercel AI SDK and Google ADK. The existing Anthropic wrapper does not help here: the Agent SDK runs Claude Code itself, so requests never pass through the Anthropic client.This has been asked for by a customer.
What
A
claude-agent-sdkintegration inposthog-js'packages/ai, mirroring the Python one:query()drop-in replacement for the SDK'squery()$ai_generationper LLM turn (model, tokens, cache metrics, cost, latency, input/output)$ai_spanper tool use$ai_traceper query, with aggregate cost and latencyWhy it should be small
The Python integration does not patch HTTP. It consumes the Agent SDK's own message stream:
message_starttomessage_stopstream events delimit one generation, tool blocks become spans, and the result message closes the trace. The TypeScript SDK exposes the same surface, withincludePartialMessages: truefor the per-turn stream events andtotal_cost_usdplus usage on the result message. So the port is mostly a translation ofposthog/ai/claude_agent_sdk/processor.py.Reference: PostHog/posthog-python#477.
Notes
ai-observability/installation/claude-agent-sdkis Python-only and needs a TypeScript tab.