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

# Anthropic client

> Use Anthropic's official clients through Atlaso with one app user per request.

The Atlaso client library can scope Anthropic's official TypeScript and Python
clients. Inside each request, after your application identifies the user, call
`user.anthropic()`: it returns a copy of one shared Anthropic client with that user
attached. The Atlaso object builds the shared client on first use and keeps it
free of user headers. In TypeScript, pass the class once: `new Atlaso({ Anthropic })`.

Install `atlaso` with `@anthropic-ai/sdk` in TypeScript or `anthropic` in Python:

```bash theme={null}
npm install atlaso @anthropic-ai/sdk
pip install 'atlaso[anthropic]'
```

Set `ATLASO_KEY` on your server. To bring your own Anthropic client, build it
once from `anthropicOptions()` (Python `anthropic_options()`), which provide the
key and the gateway **origin**, and pass it per request: `user.anthropic(client)`.
Anthropic's clients append `/v1/messages` themselves, so the base URL must omit
`/v1`. The options also keep an unrelated `ANTHROPIC_AUTH_TOKEN` out of the request.

## A scoped call and its receipt

```typescript theme={null}
import Anthropic from "@anthropic-ai/sdk";
import { Atlaso, routing } from "atlaso";

const atlaso = new Atlaso({ Anthropic });
const user = atlaso.forUser("u1");
const result = await user.anthropic().messages.create({
  model: "anthropic/claude-sonnet-5.5",
  max_tokens: 512,
  messages: [{ role: "user", content: "Which units do I prefer?" }],
  ...routing({ order: ["anthropic"] }),
}).withResponse();
const tracked = atlaso.readReceipt(result);
console.log(tracked.data.content.map((b) => (b.type === "text" ? b.text : "")).join(""));
console.log(tracked.data.stop_reason); // "max_tokens" means the answer was cut off
console.log(tracked.receipt.memory?.memoryIds);
console.log(tracked.receipt.cost);
```

```python theme={null}
from atlaso import Atlaso, routing

atlaso = Atlaso()
user = atlaso.for_user("u1")
raw = user.anthropic().messages.with_raw_response.create(
    model="anthropic/claude-sonnet-5.5",
    max_tokens=512,
    messages=[{"role": "user", "content": "Which units do I prefer?"}],
    extra_body=routing(order=["anthropic"]),
)
tracked = atlaso.read_receipt(raw)
print("".join(b.text for b in tracked.data.content if b.type == "text"))
print(tracked.data.stop_reason)  # "max_tokens" means the answer was cut off
print(tracked.receipt.memory.memory_ids if tracked.receipt.memory else [])
print(tracked.receipt.cost)
```

A stop reason of `max_tokens` means the answer reached your `max_tokens` limit
and was cut off: raise the limit. `memoryIds` or `memory_ids` are facts put into
this request's prompt. They do
not prove the model used them. The cost is a decimal string when known. A
stream may end without cost; its receipt states why.

## Streams

The receipt reads the reply headers at stream start, then updates from
`message_delta.atlaso` and completes on `message_stop`. Consume the tracked
stream, not the original stream, to keep the receipt current. The tracker
passes every Anthropic event through unchanged.

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

const atlaso = new Atlaso({ Anthropic });
const raw = await atlaso.forUser("u1").anthropic().messages.create({
  model: "anthropic/claude-sonnet-5.5",
  max_tokens: 512,
  messages: [{ role: "user", content: "Which units do I prefer?" }],
  stream: true,
}).withResponse();
const tracked = atlaso.readReceipt(raw);
let stopReason: string | null = null;
for await (const event of tracked.stream) {
  if (event.type === "content_block_delta" && event.delta.type === "text_delta")
    process.stdout.write(event.delta.text);
  else if (event.type === "message_delta") stopReason = event.delta.stop_reason;
}
console.log(`\n${stopReason}`); // "max_tokens" means the answer was cut off
console.log((await tracked.done).cost);
```

```python theme={null}
import asyncio
from atlaso import AsyncAtlaso

async def answer() -> None:
    atlaso = AsyncAtlaso()
    raw = await atlaso.for_user("u1").anthropic().messages.with_raw_response.create(
        model="anthropic/claude-sonnet-5.5",
        max_tokens=512,
        messages=[{"role": "user", "content": "Which units do I prefer?"}],
        stream=True,
    )
    tracked = await atlaso.read_receipt(raw)
    stop_reason = None
    async for event in tracked.stream:
        if event.type == "content_block_delta" and event.delta.type == "text_delta":
            print(event.delta.text, end="")
        elif event.type == "message_delta":
            stop_reason = event.delta.stop_reason
    print(f"\n{stop_reason}")  # "max_tokens" means the answer was cut off
    print((await tracked.done()).cost)

asyncio.run(answer())
```

For a synchronous Python stream, use `Atlaso`, call
`messages.with_raw_response.create(..., stream=True)`, then iterate
`atlaso.read_receipt(raw).stream`. For an async raw response, `read_receipt(raw)` is awaited
for consistent behavior across Anthropic client versions.
For the `messages.stream()` helper, read its reply headers with
`receiptFromHeaders()` or `receipt_from_headers()` once the helper connects;
this provides a headers-only receipt.

## Guards and errors

`anthropic()` checks the gateway URL, user header, and credential on every
request. It refuses redirects. The shared base client is never changed.
TypeScript's Anthropic client wraps a locally refused request in its
`APIConnectionError`; `atlaso.classify(error)` returns the typed Atlaso
`ConfigurationError` information from its cause. Python surfaces
`ConfigurationError` directly. Use `onReceipt` and `onError` (`on_receipt` and
`on_error` in Python) for local callbacks; they send no telemetry.

The translated Messages route supports text, images, PDF documents, tools,
thinking, prompt caching, and structured output where the selected model
supports them. The gateway rejects unsupported fields with
`unsupported_parameter` and an Anthropic field path. Citations, server tools,
MCP servers, batches, and `count_tokens` are unavailable in this version;
`models.list()` is also unavailable through an Anthropic client. Errors carry
the normal Anthropic error envelope plus a gateway code and request ID. See
[gateway errors](https://docs.atlaso.ai/gateway/errors.md) for codes and retry guidance.


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