claude -p, or when you have an existing Claude Code wrapper that shells out
to the claude binary. The trace shape is an explicit AGENT span with the
prompt as input and Claude Code output as output.
Anthropic renamed the Claude Code SDK to the Claude Agent SDK. If your app
imports
@anthropic-ai/claude-agent-sdk or claude_agent_sdk, use
Claude Agent SDK traces. If your app
invokes the claude binary directly, use the subprocess pattern below.Install
TypeScript CLI Invocation
Resolve theclaude binary once, then wrap each non-interactive invocation with
agentSpan(). Set CLAUDE_BIN when Claude Code is installed outside PATH or
when you want to pin a specific executable.
TypeScript
Python CLI Invocation
Use the same pattern in Python when a worker, job, or automation script shells out to Claude Code.Python
Agent SDK Query Loop
If you are using Claude Code programmatically through the Agent SDK, trace the SDK’squery() loop instead of tracing a raw subprocess.
What To Look For
- An AGENT span named
claude-code.invocationfor direct CLI subprocess work agent.name=ClaudeCodeandgen_ai.system=anthropicon the span- Prompt input and stdout output captured on the span
- Error status and exception details if the subprocess exits non-zero
- Agent SDK query spans when using the
claude_agent_sdkpackage