Messages (Anthropic format)
/v1/messagesAnthropic Messages API shapeSome tools speak only the Anthropic Messages protocol: Claude Code, the Anthropic SDKs, and apps built on
them. This endpoint accepts that request shape and answers in that response shape, so those tools work with an
OSMEXO key. It serves every text model on OSMEXO, not only Claude: the same request can run on
anthropic/claude-opus-5.5, z-ai/glm-5.4 or any other id from /v1/models.
Pricing, limits, retries and errors are the same as for Chat Completions.
Connect a client
Anthropic clients add /v1/messages themselves, so give them the base URL without /v1:
| Setting | Value |
|---|---|
| Base URL | __GATEWAY_URL__ |
| API key | your sk-llm-… key |
| Model | an OSMEXO id such as anthropic/claude-sonnet-5.5, or Anthropic's own name for a Claude model |
The key is accepted in either header: x-api-key: sk-llm-… (what Anthropic SDKs send) or
Authorization: Bearer sk-llm-…. The anthropic-version header is accepted and ignored.
For Claude Code, see Coding agents.
Request
curl "https://api.osmexo.com/v1/messages" \
-H "x-api-key: $OSMEXO_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "Content-Type: application/json" \
-d '{
"model": "anthropic/claude-sonnet-5.5",
"max_tokens": 1024,
"system": "You are a terse assistant.",
"messages": [
{ "role": "user", "content": "What is the capital of France?" }
]
}'With the Anthropic SDK, change only the base URL and the key:
from anthropic import Anthropic
client = Anthropic(base_url="https://api.osmexo.com", api_key="sk-llm-…")
message = client.messages.create(
model="anthropic/claude-sonnet-5.5",
max_tokens=1024,
messages=[{"role": "user", "content": "What is the capital of France?"}],
)
print(message.content[0].text)Supported fields
| Field | Notes |
|---|---|
model, required | An OSMEXO id (vendor/name). Anthropic's names for Claude models also work: claude-opus-5-5 and dated forms such as claude-opus-5-5-20260401 are matched to anthropic/claude-opus-5.5. |
max_tokens, required | If it is above the model's output limit, the request runs at that limit instead of failing. |
messages, required | user and assistant turns. Content is a string or an array of blocks, see below. A system turn inside messages is accepted too. |
system | A string or an array of text blocks. |
stream | Anthropic event stream, see Streaming. |
temperature, top_p | |
stop_sequences | Up to 4 are used. |
tools | Your own tools: { name, description, input_schema }. |
tool_choice | auto, any, none or { type: "tool", name }. disable_parallel_tool_use is honoured. |
metadata.user_id | Stored as the end-user id. |
Content blocks
| Block | In a request | In a response |
|---|---|---|
text | Yes. A cache_control marker on it is kept, so prompt caching works. | Yes |
image | Yes, with a base64 or url source, on models with image input. | No |
tool_use | Yes, in assistant turns. | Yes |
tool_result | Yes, in user turns. Text content is passed to the model. | No |
thinking | Accepted and left out: earlier reasoning is not sent back to the model. | Yes, on reasoning models, with an empty signature. |
document and other blocks | 400 invalid_request_error. | No |
Not supported: the thinking request setting (the model decides for itself), tools that Anthropic runs on its
side (web search, code execution and similar; they are left out of the request), message batches and files.
Response
{
"id": "msg_01M9…",
"type": "message",
"role": "assistant",
"model": "anthropic/claude-sonnet-5.5",
"content": [{ "type": "text", "text": "Paris." }],
"stop_reason": "end_turn",
"stop_sequence": null,
"usage": {
"input_tokens": 24,
"output_tokens": 2,
"cache_read_input_tokens": 0,
"cache_creation_input_tokens": 0
}
}| Field | Notes |
|---|---|
model | The model name you sent. |
stop_reason | end_turn, max_tokens, tool_use or refusal. |
usage.input_tokens | Fresh input only. Cached input is counted apart, in cache_read_input_tokens and cache_creation_input_tokens, as Anthropic does. |
What the request cost is in the X-LLM-Cost-Micro response header and in the dashboard; see
response headers.
Tool calls
The model answers with a tool_use block and stop_reason: "tool_use". Send the result back as a
tool_result block in the next user turn:
{
"role": "user",
"content": [
{ "type": "tool_result", "tool_use_id": "call_1", "content": "18°C, sunny" }
]
}Streaming
With "stream": true the response is the Anthropic event stream. Every frame has an event: name and a JSON
data: object whose type repeats that name:
event: message_start
event: content_block_start (text, thinking or tool_use)
event: content_block_delta (text_delta, thinking_delta or input_json_delta)
event: content_block_stop
event: message_delta (stop_reason and the final usage)
event: message_stopThere is no [DONE] frame on a successful stream; message_stop ends it. A failure after the first event
arrives as event: error. Before the first event, a failure is a normal JSON error with its status code.
Count tokens
/v1/messages/count_tokensestimateTakes the same body (without max_tokens) and returns { "input_tokens": n }. The number is an estimate
from the size of the request, about four characters per token, not the model's own tokenizer. No model is
called and nothing is charged. Use usage on a real response for the exact count.
Errors
The status codes and their meaning are the same as everywhere else, see Errors. The body uses Anthropic's shape, so Anthropic SDKs raise their usual exceptions:
{
"type": "error",
"error": {
"type": "invalid_request_error",
"code": "validation_failed",
"message": "max_tokens is required and must be positive",
"param": "max_tokens",
"request_id": "req_01M9…"
}
}| HTTP | error.type |
|---|---|
| 400 | invalid_request_error |
| 401 | authentication_error |
| 402 | billing_error |
| 403 | permission_error |
| 404 | not_found_error |
| 413 | request_too_large |
| 429 | rate_limit_error |
| 503 | overloaded_error |
| other 5xx | api_error |
error.code keeps the OSMEXO code (for example insufficient_credits), which is the one to act on.