> ## Documentation Index
> Fetch the complete documentation index at: https://docs.zespan.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Anthropic

> Trace Anthropic Claude API calls automatically in TypeScript and Python — messages, tool use, and streaming — with wrapAnthropic() or patch_anthropic().

<Note>
  Available for: **Python** and **TypeScript**.
</Note>

Wrap your `Anthropic` client instance with `wrapAnthropic()` (TypeScript), or patch the `anthropic` module with `patch_anthropic()` (Python). Every `messages.create` call is traced automatically, including tool use and streaming responses.

## Installation

<CodeGroup>
  ```bash TypeScript theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  npm install @zespan/sdk @anthropic-ai/sdk
  ```

  ```bash Python theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  pip install zespan anthropic
  ```
</CodeGroup>

## Setup

<CodeGroup>
  ```typescript TypeScript theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  import Anthropic from "@anthropic-ai/sdk";
  import { zespan } from "@zespan/sdk";

  zespan.init({ apiKey: process.env.ZESPAN_API_KEY! });

  const anthropic = zespan.wrapAnthropic(new Anthropic());
  ```

  ```python Python theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  import os
  import zespan

  zespan.init(api_key=os.environ["ZESPAN_API_KEY"])
  zespan.patch_anthropic()

  import anthropic  # import after patching
  client = anthropic.Anthropic()
  ```
</CodeGroup>

In TypeScript, `wrapAnthropic()` returns a wrapped client instance — pass it to any function that makes Anthropic calls, and use it exactly like the original client. In Python, `patch_anthropic()` monkey-patches `anthropic.resources.messages.Messages.create` in place, so `anthropic.Anthropic()` (constructed after patching) is traced automatically with no wrapping step.

## Example

<CodeGroup>
  ```typescript TypeScript theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  const message = await anthropic.messages.create({
    model: "claude-sonnet-4-6",
    max_tokens: 1024,
    messages: [{ role: "user", content: "Explain observability in one paragraph." }],
  });
  ```

  ```python Python theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  message = client.messages.create(
      model="claude-sonnet-4-6",
      max_tokens=1024,
      messages=[{"role": "user", "content": "Explain observability in one paragraph."}],
  )
  ```
</CodeGroup>

## What gets captured

| Field         | Details                                                             |
| ------------- | ------------------------------------------------------------------- |
| Model         | `claude-opus-4-7`, `claude-sonnet-4-6`, `claude-haiku-4-5`, etc.    |
| Input tokens  | From `usage.input_tokens`                                           |
| Output tokens | From `usage.output_tokens`                                          |
| Cost          | Calculated from token counts and Anthropic pricing                  |
| Latency       | Total time from request to first token (streaming) or full response |
| Stop reason   | `end_turn`, `max_tokens`, `tool_use`, `stop_sequence`               |

## Tool use

Tool calls and tool results are captured as child spans under the message span, in both languages.

<CodeGroup>
  ```typescript TypeScript theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  const message = await anthropic.messages.create({
    model: "claude-sonnet-4-6",
    max_tokens: 1024,
    tools: [
      {
        name: "get_weather",
        description: "Get current weather",
        input_schema: {
          type: "object",
          properties: { location: { type: "string" } },
          required: ["location"],
        },
      },
    ],
    messages: [{ role: "user", content: "What's the weather in Paris?" }],
  });
  ```

  ```python Python theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  message = client.messages.create(
      model="claude-sonnet-4-6",
      max_tokens=1024,
      tools=[
          {
              "name": "get_weather",
              "description": "Get current weather",
              "input_schema": {
                  "type": "object",
                  "properties": {"location": {"type": "string"}},
                  "required": ["location"],
              },
          }
      ],
      messages=[{"role": "user", "content": "What's the weather in Paris?"}],
  )
  ```
</CodeGroup>

## Streaming

Streaming responses are traced end-to-end in both languages. Token counts accumulate from `message_delta` events.

<CodeGroup>
  ```typescript TypeScript theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  const stream = anthropic.messages.stream({
    model: "claude-haiku-4-5",
    max_tokens: 512,
    messages: [{ role: "user", content: "Count to ten." }],
  });

  for await (const chunk of stream) {
    if (chunk.type === "content_block_delta") {
      process.stdout.write(chunk.delta.text ?? "");
    }
  }
  ```

  ```python Python theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  stream = client.messages.create(
      model="claude-haiku-4-5",
      max_tokens=512,
      stream=True,
      messages=[{"role": "user", "content": "Count to ten."}],
  )

  for event in stream:
      if event.type == "content_block_delta" and event.delta.type == "text_delta":
          print(event.delta.text, end="")
  ```
</CodeGroup>

<Note>
  In Python, `patch_anthropic()` only patches `messages.create()` — including calls made with `stream=True`. It does not patch the separate `messages.stream()` context-manager helper, so use `create(..., stream=True)` and iterate over the raw typed events (`event.type`, e.g. `content_block_delta`) as shown above to get traced streaming.
</Note>

<Note>
  Prompt and completion text is stored by default with PII redaction applied before transmission. Set `storePrompts: false` (`store_prompts=False` in Python) in `zespan.init()` to disable prompt storage.
</Note>

## Next steps

* [Agent tracing](/sdk/agent-tracing) — trace multi-turn Claude conversations as agents
* [Guardrails](/sdk/guardrails) — enforce safety policies on Claude outputs
