Claude Agent SDK: per-turn generations with reasoning (transcript-based integration) + mapping for claude_code.* OTel spans #18422
hamed-hashemi2
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Problem
With the documented Claude Agent SDK integration (
openinference-instrumentation-claude-agent-sdk), aClaudeSDKClientrun appears in Langfuse as one AGENT observation (first prompt + final output + total tokens) plus TOOL observations. There are no per-turn GENERATION observations, so you can't see what the model received on each turn, what it replied, the reasoning (extended thinking), or which tools it chose. For the same run, LangSmith shows one LLM run per turn including reasoning, which it rebuilds from the Claude Code session transcript.What I tested
Versions: Python, langfuse 4.15.6, claude-agent-sdk 0.2.159 (bundled Claude Code 2.1.281), openinference-instrumentation-claude-agent-sdk 0.1.18 / 0.1.20, Amazon Bedrock, Claude Sonnet 4.5.
openinference-instrumentation-anthropic, but the Python Agent SDK doesn't use theanthropicpackage (calls happen in the CLI subprocess), so that yields 0 LLM spans.TRACEPARENTfrom the Langfuse span, exported straight to/api/public/otel): spans arrive and nest correctly, andclaude_code.llm_requestis typed as GENERATION with the right model. But input, output and usage are empty; everything lands in metadata (attributes.new_context,attributes.response.model_output,attributes.input_tokens,attributes.output_tokens,attributes.cache_read_tokens,attributes.cache_creation_tokens).langfuse.observation.input/output/usage_details: generations render correctly with tokens and cost. Reasoning is still missing because Claude Code redacts thinking in all telemetry (see anthropics/claude-code#).Requests
receive_response(), read the session transcript (~/.claude/projects/<cwd>/<session_id>.jsonl) and emit one GENERATION per assistant turn with full input messages, output text, thinking, tool calls and usage. This is the only route to reasoning visibility today.claude_code.*spans at OTel ingestion:claude_code.llm_request→ input (new_context,user_system_prompt/system_prompt_preview), output (response.model_output), usage (input_tokens,output_tokens,cache_read_tokens,cache_creation_tokens);claude_code.tool→ input (tool_input), output (new_context). Claude Code is a common source of OTel traces and currently needs a custom collector or relay to display properly.mcp-python-sdkTOOL observations, which currently attach to the trace root alongside the OpenInference tool spans.Happy to share the relay script and a sanitized trace export.
Additional information
No response
All reactions