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

# Anthropic

> Wrap the Anthropic client to meter and gate Messages API calls, streaming included.

## Set up

Wrap an instance of `Anthropic` from `@anthropic-ai/sdk`. The SDK recognizes it automatically.

```typescript theme={null}
import Anthropic from '@anthropic-ai/sdk';

const anthropic = stigg.wrap(new Anthropic());

// A regular call. Usage is reported when the response arrives.
await anthropic.messages.create({
  model: 'claude-opus-5',
  max_tokens: 1024,
  messages: [{ role: 'user', content: 'Summarize this ticket' }],
  stiggContext: { customerId: 'customer-acme', entitlement: { currencyId: 'credits' } },
});

// A streamed call. Usage is reported once, when the stream finishes.
const stream = anthropic.messages.stream({
  model: 'claude-opus-5',
  max_tokens: 1024,
  messages: [{ role: 'user', content: 'Summarize this ticket' }],
  stiggContext: { customerId: 'customer-acme', entitlement: { currencyId: 'credits' } },
});
for await (const event of stream) {
  // show the text to the user as it arrives
}
```

* Both ways of calling the Messages API are metered: `messages.create` for a regular call, and `messages.stream` for a streamed call, where the response arrives piece by piece. A streamed call is reported once, when the stream finishes, not once per piece.
* **A stream you stop early is still metered.** Anthropic reports a streamed call's final output count only at the end of the stream. If your code stops reading before then, for example when a user clicks "Stop", the SDK estimates the output from the text received so far, at about four characters per token, and marks the measurement as `estimated`. To bill only counts Anthropic reported, see [Your own mapper](/documentation/ai-agents/llm-wrappers/metering#your-own-mapper).
* **Optional: deny calls the balance cannot fully cover.** By default, the check before each call uses a requested usage of 1, which only asks whether anything is left. The real usage is reported after the call, so a small balance can go over by about one call. If a single call can be expensive, use `estimateFromMaxTokens(tokensPerUnit)`: the requested usage is then estimated from the call's `max_tokens`, the longest response it could produce, and access is denied if the balance cannot cover it. `tokensPerUnit` is how many tokens make one unit of the credits or feature you enforce. See [Gating calls](/documentation/ai-agents/llm-wrappers/gating#requested-usage) for how to set it up.

## What is reported

Every response from the Anthropic Messages API includes a `usage` object: the number of tokens the call consumed, split into input, output, and prompt cache reads and writes. Prompt caching lets Anthropic reuse the start of a prompt from an earlier call at a lower price. The wrapper reads these counts and reports them to Stigg as the metrics below. For details on each field, see Anthropic's [Messages API reference](https://platform.claude.com/docs/en/api/messages) and [prompt caching guide](https://platform.claude.com/docs/en/build-with-claude/prompt-caching).

| Method | Event name |
| - | - |
| `messages.create`, `messages.stream` | `anthropic.messages.create` |

| Metric | What it counts |
| - | - |
| `input_tokens` | Input tokens, not including cache reads or cache writes |
| `output_tokens` | Output tokens |
| `cache_read_input_tokens` | Input tokens read from the prompt cache |
| `cache_creation_input_tokens` | Input tokens written to the prompt cache |
| `total_tokens` | The sum of the four, added up by the SDK because Anthropic does not report a total |

Anthropic counts cache reads and cache writes **separately** from `input_tokens`, and the SDK reports each one as its own metric, plus `total_tokens`. Price the total, or the parts, but never both. If you want to charge for all of them together, for example by converting them into one credit balance with [weighted credits](/documentation/ai-agents/llm-wrappers/metering#weighted-credits), you can add them without counting any token twice. OpenAI is different: its input count already includes cached tokens.

Every usage field the vendor returns is also included, with a `raw.` prefix. See [How usage is reported](/documentation/ai-agents/llm-wrappers/supported-providers#how-usage-is-reported).


## Related topics

- [Amazon Bedrock](/documentation/ai-agents/llm-wrappers/providers/bedrock.md)
- [Supported providers and gateways](/documentation/ai-agents/llm-wrappers/supported-providers.md)
- [Gateways](/documentation/ai-agents/llm-wrappers/gateways.md)
- [Metering usage](/documentation/ai-agents/llm-wrappers/metering.md)
- [Agent SDK](/api-and-sdks/references/sdk/backend/graphql/agent-sdk.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.