Add logfire.instrument_claude_agent_sdk()#1799
Merged
alexmojaki merged 148 commits intomainfrom Mar 27, 2026
Merged
Conversation
…remove unused collected list
…_logfire_prompt assignments
…esults, hook edge cases
Reset _logfire_prompt at the start of patched_query() so it doesn't carry over from a previous call when prompt is None. Also remove unused scaffolding: _logfire_start_time, _logfire_streamed_input, _next_start_time, and mark_next_start() — all stored but never read.
alexmojaki
commented
Mar 25, 2026
alexmojaki
commented
Mar 25, 2026
alexmojaki
commented
Mar 25, 2026
alexmojaki
commented
Mar 25, 2026
alexmojaki
commented
Mar 25, 2026
Deploying logfire-docs with
|
| Latest commit: |
b096c58
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://b46dbff3.logfire-docs.pages.dev |
| Branch Preview URL: | https://instrument-claude-agent-sdk.logfire-docs.pages.dev |
original_init sets self.options to ClaudeAgentOptions() when no options are passed. The kwargs extraction would miss this, skipping hook injection.
alexmojaki
commented
Mar 25, 2026
alexmojaki
commented
Mar 27, 2026
…tion Tool responses from the SDK are already structured data. Removing the str() conversion lets them serialize as proper JSON objects in both the execute_tool span attribute and conversation history input messages. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…hook Consistency with post_tool_use_hook and post_tool_use_failure_hook which already access input_data inside the error handler. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Prevents stale references (including conversation history) from persisting in thread-local storage after receive_response completes. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Chat spans now include gen_ai.system_instructions so each span is
self-contained. Span name is updated to 'chat {model}' via update_name
when the model is known.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Cross-async-context detach always fails with OTel error logs, and keeping context attached doesn't help (tool code runs in yet another async context). Now: attach root context, _start() the span to capture the parent link, immediately detach. No context left attached means no cross-context detach errors. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
The model comes from AssistantMessage (not ResultMessage), so we store it on the turn tracker and set it on the root span when the result arrives. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Replaces scattered thread-local attributes and the global _active_tool_spans dict with a single _ConversationState object. This fixes cross-thread interference where two conversations would share the same _active_tool_spans dict. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
logfire.instrument_claude_agent_sdk()
…fire into instrument-claude-agent-sdk
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
logfire.instrument_claude_agent_sdk()for native OpenTelemetry instrumentation of the Claude Agent SDKClaudeSDKClientto create conversation, turn, and tool spansPreToolUse,PostToolUse,PostToolUseFailure) for tool call tracingthreading.local()for context propagation (workaround for anyio breaking contextvars)Test plan
tests/otel_integrations/test_claude_agent_sdk.pycovering all code pathslogfire/_internal/integrations/claude_agent_sdk.pytest_logfire_api.pyupdated to include the new integration🤖 Generated with Claude Code
Summary by cubic
Adds native
logfire.instrument_claude_agent_sdk()tracing for the Claude Agent SDK using OTel GenAI semconv. Createsinvoke_agent,chat {model}, andexecute_tool {tool_name}spans with provider, messages (incl. reasoning), per‑turn usage/cost, conversation ID, and error details.New Features
claude_agent_sdk.ClaudeSDKClientvia SDK hooks (scope suffixclaude_agent_sdk); returns a context manager to revert; API exposed inlogfireandlogfire_api(stub usesnullcontext()).invoke_agent; siblingchat {model}per assistant turn; siblingexecute_tool {tool_name}for tool calls.gen_ai.provider.name,gen_ai.input.messages,gen_ai.output.messages,gen_ai.system_instructions, reasoning viaReasoningPart(ThinkingBlock), per‑turngen_ai.usage.*(incl. cache),gen_ai.response.model,gen_ai.conversation.id,operation.cost, anderror.type; sets error level on failures..query(); onlyClaudeSDKClientis instrumented; instances created after instrumentation get full tool tracing. Docs updated to uselogfire.instrument_claude_agent_sdk().Bug Fixes
claude_agent_sdkat module import; useHookMatcher;isinstancechecks for SDK block types; replacegetattrwith direct attribute access on typed SDK blocks; tighten types; remove dead code; add coverage pragmas; fixsys.modulesleak.claudeCLI with--record-claude-cassettes; add dev dependencyclaude-agent-sdk; CI/resource warning fixes.Written for commit 09a9ce9. Summary will update on new commits.