# Anthropic-compatible API for Claude

Source: [Anthropic-compatible API for Claude](<https://docs.gptunnel.ru/en/api/anthropic-messages/>)

<a id="_top"></a>
Authenticate requests with your [API key](<https://docs.gptunnel.ru/en/#get-an-api-key>).

The `/v1/messages` endpoint accepts the **Anthropic Messages API** format. To connect the Anthropic SDK or Claude Code, set the GPTunneL base URL and your API key.

<a id="claude-request"></a>
## Claude Request

`POST https://gptunnel.ru/v1/messages`

### Request headers

| Parameter | Type | Description |
| --- | --- | --- |
| `x-api-key` (required) | `string` | Pass the API key as the value of x-api-key. Alternatively, send the header Authorization: Bearer YOUR\_API\_KEY. |
| `anthropic-version` | `string` | API Anthropic version, transmitted as it is |
| `content-type` (required) | `string` | application/json |

Send a standard Anthropic request body, including fields such as `model`, `messages`, `system`, `tools`, `tool_choice`, and `stream`. GPTunneL validates the fields it uses and forwards the rest, with the exceptions described below.

<a id="example-request"></a>
### Example request

Set the `GPTUNNEL_API_KEY` environment variable. This example requests `claude-5.5-haiku`.

<a id="tab-panel-0-0"></a>
**Python**

```python
import json
import os
from urllib.request import Request, urlopen

body = {
    'model': 'claude-5.5-haiku',
    'max_tokens': 64,
    'messages': [{'role': 'user', 'content': 'Сколько будет 2 + 2? Ответь цифрой.'}],
}
request = Request(
    'https://gptunnel.ru/v1/messages',
    method='POST',
    headers={
        'x-api-key': os.environ['GPTUNNEL_API_KEY'],
        'anthropic-version': '2023-06-01',
        'Content-Type': 'application/json',
    },
    data=json.dumps(body).encode('utf-8'),
)
with urlopen(request, timeout=30) as response:
    print(json.load(response)['content'])
```

<a id="tab-panel-0-1"></a>
**cURL**

```bash
curl --fail-with-body --silent --show-error \
  --url 'https://gptunnel.ru/v1/messages' \
  --header "x-api-key: $GPTUNNEL_API_KEY" \
  --header 'anthropic-version: 2023-06-01' \
  --json '{
    "model": "claude-5.5-haiku",
    "max_tokens": 64,
    "messages": [{"role": "user", "content": "Сколько будет 2 + 2? Ответь цифрой."}]
  }'
```

<a id="tab-panel-0-2"></a>
**JavaScript (Node.js)**

```javascript
const response = await fetch('https://gptunnel.ru/v1/messages', {
  method: 'POST',
  headers: {
    'x-api-key': process.env.GPTUNNEL_API_KEY,
    'anthropic-version': '2023-06-01',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    model: 'claude-5.5-haiku',
    max_tokens: 64,
    messages: [{ role: 'user', content: 'Сколько будет 2 + 2? Ответь цифрой.' }],
  }),
})
if (!response.ok) throw new Error(await response.text())
console.log((await response.json()).content)
```

<a id="example-response"></a>
### Example response

The response text is in the `content` array. In the `model` field, Claude may use its own spelling of the model ID. Here is an excerpt from a verified response.

```json
{
  "type": "message",
  "model": "claude-haiku-5-5",
  "content": [{ "type": "text", "text": "4" }],
  "stop_reason": "end_turn",
  "usage": { "input_tokens": 30, "output_tokens": 3 }
}
```

 <a id="только-модели-claude"></a>
 <a id="accessibility-of-format"></a>
 
<a id="format-availability"></a>
## Format availability

The native route serves Claude models. Other models can use the [Anthropic-compatible format](<https://docs.gptunnel.ru/en/api/#call-formats>) when it is enabled for that model. If a model is unavailable on `/v1/messages`, check the [catalog for your key](<https://docs.gptunnel.ru/en/api/models/>) and confirm which formats the model supports. For ordinary chat requests, use [Chat Completions](<https://docs.gptunnel.ru/en/api/chat-completions/>).

<a id="what-we-change-in-the-body-of-a-request"></a>
## What we change in the body of a request

GPTunneL forwards the request body to Anthropic with these changes:

| Change | Reason |
| --- | --- |
| GPTunneL replaces `metadata` | Anthropic accepts only `user_id` within this object. GPTunneL uses its own hash. |
| GPTunneL removes `service_tier` | The gateway does not offer Anthropic’s paid priority tier. |
| When thinking is disabled, GPTunneL removes `clear_thinking_*` strategies from `context_management.edits` and drops `context_management` if no strategies remain | Anthropic requires enabled or adaptive `thinking` for these strategies and rejects an empty `edits` array. |
| For Haiku, GPTunneL removes `thinking: {"type": "adaptive"}` and `output_config` | Haiku returns `400` for these options. |
| GPTunneL removes `code-execution-*` values from `anthropic-beta` | The gateway does not support `code_execution`. |
| GPTunneL removes the `[1m]` suffix from the model ID | Claude Code adds this suffix for extended context. |

The gateway also removes internal routing fields: `provider`, `route`, `models`, `transforms`, `preset`, and `plugins`.

Other fields are forwarded as supplied. Anthropic may return `400` for an unknown field. The request field allowlist applies only to the OpenAI-compatible [`/v1/chat/completions`](<https://docs.gptunnel.ru/en/api/chat-completions/>) endpoint.

<a id="streaming-response"></a>
## Streaming response

Set `"stream": true` in the request body. A stream tested with `claude-5.5-haiku` returned `message_start`, then `content_block_start`, `content_block_delta`, and `content_block_stop`, followed by `message_delta` and `message_stop`. You may also receive `ping` events. Read the text from `content_block_delta` and finish reading the message on `message_stop`.

<a id="token-counting"></a>
## Token counting

`POST https://gptunnel.ru/v1/messages/count_tokens`

Returns an estimate of the number of input tokens in the request body.

> **Caution: Approximate token count**
>
> GPTunneL calculates this estimate locally. This method is free and does not send the request to Anthropic. Text uses the `cl100k_base` tokenizer. Images and PDF files use a fixed estimate because their sizes are unavailable at this step. Use the estimate for budgeting. The actual charge is based on `usage` in the model response.

<a id="tools"></a>
## Tools

Custom tools defined with `name` and `input_schema` are forwarded unchanged. Supported Anthropic tools include `web_search`, `web_fetch`, `computer`, `bash`, and `text_editor`.

Requests that include `code_execution` return HTTP `400`. Anthropic bills code execution by container runtime, while the gateway bills by token usage. For the same reason, GPTunneL removes `code-execution-*` values from the `anthropic-beta` header and forwards the other beta flags unchanged.

 <a id="structured-conclusion"></a>
 
<a id="structured-output"></a>
## Structured output

GPTunneL forwards the schema in `output_config` to Claude unchanged. Supply a schema that meets Anthropic’s requirements, including `additionalProperties` where required.

Use this format to request strict JSON directly. On the OpenAI-compatible [`/v1/chat/completions`](<https://docs.gptunnel.ru/en/api/chat-completions/>) endpoint, GPTunneL translates `response_format` into Anthropic options. The `json_object` mode has no native Anthropic equivalent.

For Haiku models, GPTunneL removes `output_config` because Anthropic would return HTTP `400`.

<a id="prompt-caching"></a>
## Prompt caching

Your client sets cache breakpoints with `cache_control`. The response’s `usage` object includes `cache_creation_input_tokens` and `cache_read_input_tokens`. Reading from the cache costs less than standard input. Writing to the cache costs more.

For business-account API keys, the organization owner or administrator can enable **Prompt caching in API** under **Business account → API**. The option is off by default. While it is off, the gateway removes `cache_control` and bills the full prompt at the normal input rate. Caching is always available for personal accounts.

<a id="errors"></a>
## Errors

Errors follow the Anthropic format. The HTTP status is accompanied by an `error` object containing `type` and `message`. Anthropic SDKs handle this format directly.

If you receive HTTP `403`, check which models your key is allowed to use.

Both message requests and token-count requests can return HTTP `503` with `error.type: "overloaded_error"` when the service is temporarily unavailable. Wait before retrying.

See [API error troubleshooting](<https://docs.gptunnel.ru/en/getting-started/api-errors/>) for more HTTP statuses and errors from other endpoints.

> **Tip: Claude Code**
>
> GPTunneL automatically strips the `[1m]` suffix that Claude Code adds to model IDs for extended context.
