feat(ai): add Claude Agent SDK integration for LLM analytics#477
feat(ai): add Claude Agent SDK integration for LLM analytics#477andrewm4894 wants to merge 6 commits intomasterfrom
Conversation
Add posthog.ai.claude_agent_sdk module that wraps claude_agent_sdk.query() to automatically emit $ai_generation, $ai_span, and $ai_trace events. - PostHogClaudeAgentProcessor with _GenerationTracker that reconstructs per-turn generation metrics from Anthropic StreamEvents - Two entry points: query() drop-in replacement and instrument() for configure-once reuse - Two-slot input tracking to correctly associate tool results with subsequent generations despite SDK message ordering - All instrumentation wrapped in try/except so PostHog errors never interrupt the underlying Claude Agent SDK query - 16 unit tests covering generation, multi-turn, fallback, tool spans, traces, privacy mode, personless mode, custom properties - Example scripts (simple_query.py, instrument_reuse.py)
posthog-python Compliance ReportDate: 2026-04-01 12:25:25 UTC ✅ All Tests Passed!0/0 tests passed |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 5a8383a167
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
- Honor per-call privacy override for $ai_input/$ai_output_choices in generation events (was only checking instance-level privacy) - Pass groups directly to _capture_event instead of fragile save/restore pattern on self._groups (thread-safe, exception-safe) - Fix tool span parent linkage: use tracker.current_span_id for in-progress generation instead of stale current_generation_span_id
Standalone script that tests posthog.ai.claude_agent_sdk integration. Supports single-shot and interactive modes. Requires local posthog-python with the claude_agent_sdk integration (PostHog/posthog-python#477). Usage: uv pip install -e ../posthog-python uv run --no-sync scripts/test_claude_agent_sdk.py uv run --no-sync scripts/test_claude_agent_sdk.py --interactive
…tions
Wraps ClaudeSDKClient to instrument receive_response() with the same
generation/span/trace tracking as query(). Supports multi-turn
conversations with full history — each turn emits its own $ai_generation
events, all linked by a shared $ai_trace_id. The $ai_trace event is
emitted on disconnect() to cover the entire session.
Usage:
async with PostHogClaudeSDKClient(options, posthog_client=ph) as client:
await client.query("Hello")
async for msg in client.receive_response():
...
await client.query("Follow up") # has conversation history
async for msg in client.receive_response():
...
Summary
posthog.ai.claude_agent_sdkmodule that wrapsclaude_agent_sdk.query()to automatically emit$ai_generation,$ai_span, and$ai_traceeventsquery()drop-in replacement andinstrument()for configure-once reuseHow it works
The Claude Agent SDK has no
TracingProcessorinterface like OpenAI Agents SDK. Instead, this integration wraps the async streaming iterator fromquery(), enablesinclude_partial_messages=Trueto receive raw AnthropicStreamEvents, and reconstructs per-turn$ai_generationevents frommessage_start/message_stopboundaries. Tool uses emit$ai_spanevents, andResultMessagetriggers a$ai_tracewith aggregate cost/latency.Test plan
uv run pytest posthog/test/ai/claude_agent_sdk/ -v)