Skip to main content
Use this guide when your application invokes Claude Code as a CLI process, such as 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 the claude 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’s query() loop instead of tracing a raw subprocess.

What To Look For

  • An AGENT span named claude-code.invocation for direct CLI subprocess work
  • agent.id=claude-code-repo-worker for stable Agents dashboard grouping
  • agent.name=ClaudeCode and gen_ai.system=anthropic on 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_sdk package