Skip to content

feat(aio): add Claude Agent SDK integration to the JS SDK #96009

Description

@bernatixer

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions