Top.Mail.Ru
General icon
General
Галерея icon
Галерея
Unitool Labs icon
Unitool Labs
Блог icon
Блог
MCP icon
MCP

Anthropic-compatible API (/v1/messages)

HTTP
POST https://api.unitool.ai/v1/messages
POST https://api.unitool.ai/v1/messages/count_tokens

/v1/messages speaks the Anthropic Messages format for Claude models. The official Anthropic SDKs and Claude Code work with it: set the base URL to https://api.unitool.ai and pass your ut_live_... key in x-api-key or Authorization: Bearer. The key needs the inference scope.

Claude models only. Other models answer 400 invalid_request_error: call them through /v1/chat/completions. Model ids come from the catalog, e.g. claude-sonnet-5-5. A dated name (claude-opus-5-5-20261001) points to the same model; the answer echoes the name you sent.

Claude Code

curl
export ANTHROPIC_BASE_URL=https://api.unitool.ai
export ANTHROPIC_AUTH_TOKEN=ut_live_...
export ANTHROPIC_API_KEY=""
export ANTHROPIC_MODEL=claude-fable-5-2
export ANTHROPIC_DEFAULT_HAIKU_MODEL=claude-haiku-5-5

Take model names from GET /v1/models. Every request reserves the input plus max_tokens of output on the balance, and Claude Code asks for the model's maximum output. Keep a balance that covers it or lower the limit with CLAUDE_CODE_MAX_OUTPUT_TOKENS. Otherwise the answer is 402 billing_error, and the message names the max_tokens the balance pays for. Details in Billing.

Anthropic SDK

Python
import os
import anthropic

client = anthropic.Anthropic(base_url="https://api.unitool.ai", api_key=os.environ["UNITOOL_API_KEY"])
message = client.messages.create(
    model="claude-sonnet-5-5",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hi!"}],
)
print(message.content[0].text, message.usage)

with client.messages.stream(model="claude-sonnet-5-5", max_tokens=1024,
                            messages=[{"role": "user", "content": "Write a haiku about APIs"}]) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)
TypeScript
import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic({ baseURL: "https://api.unitool.ai", apiKey: process.env.UNITOOL_API_KEY });
const message = await client.messages.create({
  model: "claude-sonnet-5-5",
  max_tokens: 1024,
  messages: [{ role: "user", content: "Hi!" }],
});
console.log(message.content, message.usage);

const stream = client.messages.stream({ model: "claude-sonnet-5-5", max_tokens: 1024, messages: [{ role: "user", content: "Write a haiku about APIs" }] });
stream.on("text", (text) => process.stdout.write(text));
await stream.finalMessage();

Supported

  • Streaming with Anthropic events: message_start, content blocks, message_delta with stop_reason and the full usage, message_stop. An event: ping arrives every 15 seconds.
  • Your own tools (tools of type custom, tool_choice: auto, any, tool, none), tool_use and tool_result with text and images.
  • Extended thinking (thinking: enabled with budget_tokens, adaptive, disabled) with signatures. Send thinking and redacted_thinking blocks back unchanged with tool results. output_config.effort takes a level the model has.
  • Images (base64 and URL), PDF (base64 and https), plain-text documents.
  • system as a string or text blocks, stop_sequences, temperature and top_p (for models that take them), structured output output_config.format (json_schema).
  • usage the Anthropic way: input_tokens (input that touched no cache), cache_read_input_tokens, cache_creation_input_tokens, cache_creation.ephemeral_5m_input_tokens and ephemeral_1h_input_tokens, output_tokens, plus our cost_tokens (balance tokens charged) and estimated.
  • stop_reason: end_turn, max_tokens, tool_use, refusal.

Prompt caching is automatic: the gateway places the cache points (tools, system, the tail of the conversation). Your cache_control marks are accepted but not used. Cache reads and writes are billed at the model's cache rates.

The only server-side tool that works is web search: the web_search_20250305 tool turns search on. The answer is the model's text and usage.server_tool_use.web_search_requests, without search result blocks or citations. max_uses above 5 is lowered to 5; allowed_domains and blocked_domains are refused.

Not supported

WhatAnswer
Server-side tools (code execution and other non-custom tools, except web search), mcp_servers, container, document citations, unknown block types400 invalid_request_error naming the field
metadata, top_k, service_tier, tool_choice.disable_parallel_tool_use and other unknown fieldsDropped, listed in the x-unitool-ignored-params header
The anthropic-beta headerHas no effect; the values sent are listed in x-unitool-ignored-betas

Other differences from Anthropic:

  • stop_reason is never stop_sequence: a stop on stop_sequences comes back as end_turn.
  • tool_result.is_error is not passed to the model.
  • In a stream, a thinking block's signature may arrive after the following blocks. SDKs assemble blocks by index, so the final message is right.
  • GET /v1/models returns the list in the OpenAI shape.
  • anthropic-version is accepted, not required.

count_tokens

POST /v1/messages/count_tokens takes the same body and returns {"input_tokens": N}: our estimate of the input, the one the balance hold uses. The call is free and creates nothing. It is an estimate, not the vendor's count.

Money

The same as chat: a hold for the input and max_tokens, the charge by actual usage, a disconnect stops the generation and only the generated part is paid, a cut on our side before the first token is free. The price is in usage.cost_tokens, in the x-unitool-cost-tokens header (non-streamed) and in the receipt by the id from the request-id header.

Errors

Errors on this path use Anthropic's envelope, refusals of the key and of the rate limit included:

JSON
{ "type": "error", "error": { "type": "billing_error", "message": "The balance does not cover max_tokens=64000: it pays for up to 6120 output tokens. Lower max_tokens or top up." }, "request_id": "req_01JE..." }
HTTPerror.type
400invalid_request_error
401authentication_error
402billing_error: the message names the max_tokens the balance pays for
403permission_error
404not_found_error
413request_too_large
429rate_limit_error, with retry-after
500, 502api_error
503overloaded_error: we answer 503, not 529; SDKs retry both
504timeout_error

There are no code, param or details fields in this envelope: the message names the reason, and the codes are explained in Errors. An error mid-stream arrives as an event: error with the same body, then the stream closes.

This page as Markdown

We use cookies

By continuing to use the site you agree to our Privacy Policy.