# Claude through the AI gateway

```ts
import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic({
  baseURL: process.env.AI_GATEWAY_URL,
  apiKey: process.env.AI_GATEWAY_TOKEN,
});

const { data: message, response } = await client.messages
  .create({
    model: "claude-opus-5-5",
    max_tokens: 16000,
    messages: [{ role: "user", content: "Summarise this shipment note." }],
  })
  .withResponse();

const usage = JSON.parse(response.headers.get("x-spaceboy-usage") ?? "{}");
console.log(message.stop_reason, usage.costUsd);
```

A deployed app calls Anthropic's Claude models through the platform's AI
gateway with no Anthropic account. The gateway's `/messages` route (also
`/v1/messages`, the SDK's path) speaks the Anthropic Messages API, so the
official SDK works unchanged: base URL `AI_GATEWAY_URL`, API key
`AI_GATEWAY_TOKEN`. Both are set on deployed environments of projects with
the AI capability that run on the gateway (the default, unless both your
own OpenAI and ElevenLabs keys are connected). Local runs and sandboxes do
not have them. Calls are billed from the organization's prepaid credits.

## Models and prices

Anthropic's list prices in US dollars per million tokens, no markup:

| Model | Input | Output |
|---|---|---|
| `claude-fable-5-2` | 8.00 | 40.00 |
| `claude-opus-5-5` | 4.00 | 20.00 |
| `claude-sonnet-5-5` | 2.00 | 10.00 |
| `claude-haiku-5-5`, prompt up to 100K tokens | 0.10 | 0.50 |
| `claude-haiku-5-5`, prompt over 100K tokens | 0.50 | 2.50 |

The prompt is input plus cache-write plus cache-read tokens; over 100K the
whole Haiku request is billed at the higher rate. Prompt-cache reads and
writes are both billed at the model's normal input rate, a deliberate
simplification: cached reads cost more here than on your own Anthropic
account, and cache writes a little less.

## What the gateway forwards

- The request body as you send it. The gateway checks only the model;
  fields it does not know are passed on, and Anthropic judges the rest
  (a missing `max_tokens` is Anthropic's own 400).
- Your `anthropic-version` and `anthropic-beta` headers.
- The response, streamed or not. A refusal is a normal response with
  `stop_reason: "refusal"`; check `stop_reason` and it is billed like any
  other.

## What each call cost

Non-streamed responses carry an `x-spaceboy-usage` header. A stream ends
with one extra event after `message_stop`:

```text
event: spaceboy.usage
data: {"type":"spaceboy.usage","usage":{"endpoint":"MESSAGES","model":"claude-haiku-5-5","unit":"TOKENS","quantity":1500,"inputTokens":1000,"outputTokens":500,"costMicros":350,"costUsd":0.00035}}
```

`inputTokens` includes cache reads and writes; `costMicros` is what was
debited, in millionths of a dollar. The SDK's stream helper passes the
event through without using it, so `finalMessage()` still works; iterate
the raw events to read it.

## Errors

| Status | Code | Meaning |
|---|---|---|
| 400 | `model_not_allowed` | Not one of the models above. Claude ids are also refused on the OpenAI routes. |
| 401 | `missing_token` / `invalid_token` | `AI_GATEWAY_TOKEN` missing or wrong; redeploy to refresh it. |
| 402 | `out_of_credits` | Top up credits. |
| 502 | `upstream_key_rejected` | Platform-side key problem, not your request. |
| 503 | `gateway_not_configured` | Claude is not enabled on this platform yet. |

Anything else Anthropic returns (a 400 for a bad body, a 429) is passed
through unchanged and not billed. Credits and billing:
[Pricing and billing](https://spaceboy.ai/docs/agents/pricing-and-billing.md).
