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

# Anthropic

> Instrument Claude API calls with automatic token counting and cost tracking using the AgentVista Anthropic adapter.

The `AnthropicAdapter` extracts telemetry from Anthropic `Message` responses — model name, input and output token counts, and cost in USD — and passes them directly into `agentvista.record()` or a traced run. Cost is calculated automatically from the model name using AgentVista's built-in pricing table; no manual configuration is required.

## Setup

<Steps>
  <Step title="Install the SDK with the Anthropic extra">
    ```bash theme={null}
    pip install agentvista[anthropic]
    ```

    This installs `agentvista` along with the `anthropic` package.
  </Step>

  <Step title="Initialize AgentVista">
    Call `agentvista.init()` once at application startup with your API key.

    ```python theme={null}
    import agentvista

    agentvista.init(api_key="av_xxxxx")
    ```
  </Step>

  <Step title="Import the adapter">
    ```python theme={null}
    from agentvista.adapters.anthropic import AnthropicAdapter

    adapter = AnthropicAdapter()
    ```
  </Step>
</Steps>

## Usage examples

<Tabs>
  <Tab title="Basic">
    Use `adapter.extract()` on any `anthropic.types.Message` response and unpack the result directly into `agentvista.record()`.

    ```python theme={null}
    import anthropic
    import agentvista
    from agentvista.adapters.anthropic import AnthropicAdapter

    agentvista.init(api_key="av_xxxxx")
    adapter = AnthropicAdapter()

    client = anthropic.Anthropic()
    response = client.messages.create(
        model="claude-sonnet-4-6",
        max_tokens=1024,
        messages=[{"role": "user", "content": "Qualify this lead..."}],
    )

    # Extract model, input_tokens, output_tokens, total_tokens, cost_usd
    telemetry = adapter.extract(response)
    agentvista.record(agent="lead-qualifier", success=True, **telemetry)
    ```

    `adapter.extract()` returns a dict with any of these keys present:

    | Key             | Type    | Description                                                        |
    | --------------- | ------- | ------------------------------------------------------------------ |
    | `model`         | `str`   | Model ID returned by the API (e.g. `"claude-sonnet-4-6-20260205"`) |
    | `input_tokens`  | `int`   | Prompt tokens plus any cache creation and cache read tokens        |
    | `output_tokens` | `int`   | Completion tokens                                                  |
    | `total_tokens`  | `int`   | Sum of input and output tokens                                     |
    | `cost_usd`      | `float` | Total cost in USD, rounded to 6 decimal places                     |
  </Tab>

  <Tab title="With tracing">
    Wrap your Claude call in `agentvista.run()` to record a full traced run with outcome signals.

    ```python theme={null}
    import anthropic
    import agentvista
    from agentvista.adapters.anthropic import AnthropicAdapter

    agentvista.init(api_key="av_xxxxx")
    adapter = AnthropicAdapter()
    client = anthropic.Anthropic()

    with agentvista.run("lead-qualifier") as r:
        response = client.messages.create(
            model="claude-sonnet-4-6",
            max_tokens=1024,
            messages=[{"role": "user", "content": "Qualify this lead..."}],
        )

        telemetry = adapter.extract(response)
        agentvista.record(agent="lead-qualifier", success=True, **telemetry)

        qualified = "yes" in response.content[0].text.lower()
        r.set_outcome(success=qualified, outcome="qualified" if qualified else "not-qualified")
    ```

    `r.set_outcome(success, outcome)` attaches a business-level result to the trace. The context manager automatically records duration and flushes the trace on exit.
  </Tab>

  <Tab title="Streaming">
    For streaming responses, call `stream.get_final_message()` to get the completed `Message` object and pass it to the adapter.

    ```python theme={null}
    import anthropic
    import agentvista
    from agentvista.adapters.anthropic import AnthropicAdapter

    agentvista.init(api_key="av_xxxxx")
    adapter = AnthropicAdapter()
    client = anthropic.Anthropic()

    with client.messages.stream(
        model="claude-sonnet-4-6",
        max_tokens=1024,
        messages=[{"role": "user", "content": "Summarize this document..."}],
    ) as stream:
        # Consume the stream however you need
        for text in stream.text_stream:
            print(text, end="", flush=True)

        # get_final_message() returns the complete Message with usage populated
        final_message = stream.get_final_message()

    telemetry = adapter.extract(final_message)
    agentvista.record(agent="summarizer", success=True, **telemetry)
    ```

    <Note>
      Pass `stream.get_final_message()` — not the stream object itself — to `adapter.extract()`. The stream object does not expose usage data directly.
    </Note>
  </Tab>
</Tabs>

## Supported models

<Note>
  Cost calculation is automatic for the following Claude models. If your model is not listed, `cost_usd` will be absent from the extracted telemetry; all other fields (tokens, model name) are still captured.

  | Model               | Input (per M tokens) | Output (per M tokens) |
  | ------------------- | -------------------- | --------------------- |
  | `claude-opus-4-6`   | \$5.00               | \$25.00               |
  | `claude-sonnet-4-6` | \$3.00               | \$15.00               |
  | `claude-sonnet-4-5` | \$3.00               | \$15.00               |
  | `claude-haiku-4-5`  | \$1.00               | \$5.00                |
  | `claude-opus-4-0`   | \$15.00              | \$75.00               |
  | `claude-haiku-3-5`  | \$0.80               | \$4.00                |
  | `claude-3-haiku`    | \$0.25               | \$1.25                |

  Models with prompt caching enabled (Opus 4.6, Sonnet 4.6, Sonnet 4.5, Haiku 4.5) also track `cache_creation_input_tokens` and `cache_read_input_tokens` from the Anthropic response and apply the correct cached read and cache write rates automatically.

  The adapter matches both full versioned IDs (e.g. `claude-sonnet-4-6-20260205`) and short aliases (e.g. `claude-sonnet-4-6`).
</Note>
