> ## Documentation Index
> Fetch the complete documentation index at: https://wb-21fd5541-docs-hivemind-launch.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Claude Agent SDK

> Trace an agent built with the Claude Agent SDK using Weave.

<Note>
  Weave for Agents is in public preview. Features, APIs, and the Agents view UI may change before general availability.
</Note>

The [Claude Agent SDK](https://github.com/anthropics/claude-agent-sdk-python) is a Python SDK for building agent applications with Claude.

Weave automatically traces Claude Agent SDK calls, including agent queries, model responses, tool use, and multi-turn conversations. Weave displays the captured data in the **Agents** view of your project.

## Trace Claude Agent SDK agents with Weave

The Weave SDK autopatches the Claude Agent SDK, allowing you to capture traces from your Claude agents with minimal setup.

This doc shows how to initialize Weave and then run a Claude agent with MCP tools through `ClaudeSDKClient` so that Weave automatically traces the conversation, model calls, and tool calls end-to-end.

### Prerequisites

* A W\&B account and [API key](https://wandb.ai/authorize) set as a `WANDB_API_KEY` environment variable
* An Anthropic API key set as an `ANTHROPIC_API_KEY` environment variable
* Python 3.10+

### Install packages

Install the following packages in your developer environment:

```bash theme={null}
pip install weave claude-agent-sdk
```

### Initialize Weave in your code

Add `weave.init` to the project, update your W\&B team and project names, and then build an agent the way you normally would.

The following code creates a Claude agent with two MCP math tools and runs it while Weave captures its traces.

```python lines theme={null}
import anyio
import weave

from claude_agent_sdk import (
    ClaudeAgentOptions,
    ClaudeSDKClient,
    create_sdk_mcp_server,
    tool,
)

weave.init("[YOUR-TEAM]/[YOUR-PROJECT]")


@tool("add", "Add two numbers", {"a": float, "b": float})
async def add(args: dict) -> dict:
    return {"content": [{"type": "text", "text": str(args["a"] + args["b"])}]}


@tool("multiply", "Multiply two numbers", {"a": float, "b": float})
async def multiply(args: dict) -> dict:
    return {"content": [{"type": "text", "text": str(args["a"] * args["b"])}]}


math_server = create_sdk_mcp_server(
    name="math",
    version="1.0.0",
    tools=[add, multiply],
)


async def main():
    options = ClaudeAgentOptions(
        mcp_servers={"math": math_server},
        allowed_tools=["mcp__math__add", "mcp__math__multiply"],
    )

    async with ClaudeSDKClient(options=options) as client:
        await client.query("Using the math tools, compute (3 + 7) * 2.")

        async for message in client.receive_response():
            print(message)


anyio.run(main) 
```

`weave.init()` prints a link to your project when it runs.

For details on viewing Agents data in Weave, see [View agent activity](/weave/guides/tracking/view-agent-activity).
