Trace Claude Agent SDK agents with Weave
- Python
- TypeScript
The Weave SDK autopatches the Claude Agent SDK for Python, letting you capture traces from your Claude agents with minimal setup.This guide shows how to initialize Weave and run a multi-turn Claude agent with MCP tools through
query(). Weave automatically traces the conversation, model calls, and tool calls end-to-end.Prerequisites
- A CoreWeave Forge account and API key set as a
WANDB_API_KEYenvironment variable. - An Anthropic API key set as an
ANTHROPIC_API_KEYenvironment variable. - Python 3.10+.
Install packages
Install the following packages in your developer environment. Theweave package captures traces, claude-agent-sdk provides the agent runtime, and the remaining packages support the example tool.
Initialize Weave in your code
- Python
- TypeScript
Add Each
weave.init to the project, update your CoreWeave Forge team and project names, and then build an agent the way you normally would. weave.init enables the autopatching that captures traces from the Claude Agent SDK.This example defines a wikipedia_search MCP tool and runs a three-turn conversation. Each turn is a separate query() call, but later turns pass resume with the first turn’s session ID so that all turns group as one conversation in the Weave Agents view. The first two turns trigger Wikipedia lookups, and the third uses the previous conversation context to produce a summary without a tool call.query() call produces an invoke_agent root span. Because the follow-up turns resume the same session, Weave stamps the same gen_ai.conversation.id on all spans and groups them as one conversation in the Agents view. ResultMessage.session_id carries the ID to reuse, and resume accepts None on the first turn, which starts a new session.Without resume, each turn starts its own session and appears as a separate conversation. ClaudeSDKClient is the exception: it holds one session open for the lifetime of the async with block, so turns you send through the same client are already grouped. Weave patches both paths.See your agent traces in the Agents view
After the script runs,weave.init() prints a link to your project. Open the Agents view to inspect:
- A session containing the conversation’s turns.
- Each turn rendered as an
invoke_agentspan with nestedchatandexecute_toolchildren. - The full input, model, output, token usage, and tool results at each step.