PostHog / PostHog/posthog

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

Open
#96,009 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature/libraries llm-analytics team/llm-analytics
Dominant language
Python
Stars
39.9k
Forks
3.4k
Avg merge
6h 51m
Merged PRs (30d)
232

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in posthog-js/packages/ai and read posthog/ai/claude_agent_sdk/processor.py alongside the TypeScript Agent SDK stream surface, including includePartialMessages and the result message. Add the query wrapper, stateful client, and event instrumentation described in the issue, then update ai-observability/installation/claude-agent-sdk with a TypeScript tab. Done means generations, tool spans, query traces, metrics, and costs are captured without interrupting the underlying query.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
ai, documentation
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
63/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.