Claude Messages API
Inference Space Claude family models with support for Anthropic-native Messages and OpenAI-compatible APIs.
Inference Space provides Claude family conversational models, including the latest Opus, Sonnet, and Haiku generations. You can connect in either of two ways: the Anthropic-native Messages API or the OpenAI-compatible API. Both interfaces share the same account, API key, and billing system.
Overview
- Anthropic-native Messages:
POST https://ai.inf.space/v1/messages, suitable for applications already using the Anthropic SDK or Claude ecosystem tools. You can reuse native capabilities such asmessages,system,tools,thinking, andcache_controldirectly. - OpenAI-compatible:
POST https://ai.inf.space/v1/chat/completions, suitable for applications that already use an OpenAI-compatible client and want to switch to Claude without code changes. - The model family includes Claude Opus, Sonnet, and Haiku: Opus targets complex programming and deep reasoning, Sonnet targets general-purpose tasks and everyday coding, and Haiku targets high concurrency and fast responses.
Available model IDs, versions, and regions are shown in the console. We recommend managing the model ID as a configuration value so you can switch to new versions as the console is updated.
Authentication and Base URL
The API base URL is https://ai.inf.space; the browser console uses the same host. Both interfaces use the Authorization: Bearer header:
| Interface | Endpoint | Authentication header |
|---|---|---|
| Anthropic-native | https://ai.inf.space/v1/messages | Authorization: Bearer $AI_TOS_API_KEY |
| OpenAI-compatible | https://ai.inf.space/v1/chat/completions | Authorization: Bearer $AI_TOS_API_KEY |
Native Messages requests must also include anthropic-version: 2023-06-01 and content-type: application/json. Create an API key in the console, store it in the AI_TOS_API_KEY environment variable, and then make the request.
The gateway accepts only the Authorization: Bearer gk_... header (both the OpenAI and Anthropic paths use Bearer) and does not accept an incoming x-api-key. When using the Anthropic SDK, pass auth_token / authToken (which sends Authorization: Bearer) instead of api_key (which sends x-api-key and causes a 401). See Client Integrations for the complete configuration.
Calling Messages
This is the Anthropic-native interface. max_tokens is required and limits the maximum number of output tokens in a single response.
curl https://ai.inf.space/v1/messages \
-H "Authorization: Bearer $AI_TOS_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{
"model": "claude-sonnet-4-x",
"max_tokens": 1024,
"system": "You are a concise Chinese assistant.",
"messages": [
{ "role": "user", "content": "Introduce yourself in three sentences." }
]
}'import os, requests
resp = requests.post(
"https://ai.inf.space/v1/messages",
headers={
"Authorization": f"Bearer {os.environ['AI_TOS_API_KEY']}",
"anthropic-version": "2023-06-01",
"content-type": "application/json",
},
json={
"model": "claude-sonnet-4-x",
"max_tokens": 1024,
"system": "You are a concise Chinese assistant.",
"messages": [
{"role": "user", "content": "Introduce yourself in three sentences."}
],
},
)
print(resp.json())const resp = await fetch("https://ai.inf.space/v1/messages", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.AI_TOS_API_KEY}`,
"anthropic-version": "2023-06-01",
"content-type": "application/json",
},
body: JSON.stringify({
model: "claude-sonnet-4-x",
max_tokens: 1024,
system: "You are a concise Chinese assistant.",
messages: [{ role: "user", content: "Introduce yourself in three sentences." }],
}),
});
console.log(await resp.json());OpenAI-Compatible Calls
If you already have an OpenAI-compatible client, switch the base URL, API key, and model ID to use Claude. In this interface, the system prompt is passed as a message with role: "system".
curl https://ai.inf.space/v1/chat/completions \
-H "Authorization: Bearer $AI_TOS_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-sonnet-4-x",
"messages": [
{ "role": "system", "content": "You are a concise Chinese assistant." },
{ "role": "user", "content": "Introduce yourself in three sentences." }
]
}'Parameters
The following parameters apply to the Anthropic-native Messages API.
| Parameter | Type | Required | Description |
|---|---|---|---|
model | string | Yes | Model ID, such as claude-opus-4-x / claude-sonnet-4-x / claude-haiku-4-x; see the console for available models. |
messages | array | Yes | Array of conversation messages. Each item contains role (user / assistant) and content (a string or array of content blocks). |
max_tokens | integer | Yes | Maximum number of output tokens in a single response. |
system | string | No | System prompt that defines the model's role and global constraints. |
temperature | number | No | Sampling temperature in the range 0–1; higher values produce more varied output. |
top_p | number | No | Nucleus-sampling threshold; use it as an alternative to temperature. |
stop_sequences | array | No | Custom stop sequences. Generation stops when one is matched. |
stream | boolean | No | Whether to return an SSE stream; defaults to false. |
tools | array | No | List of tool (function) definitions used for tool calls. |
tool_choice | object | No | Tool-selection strategy, such as { "type": "auto" }, or a specific tool. |
thinking | object | No | Extended-thinking configuration. When enabled, the model outputs its reasoning process before the conclusion. |
The OpenAI-compatible API uses standard OpenAI fields such as messages, max_tokens, temperature, top_p, stop, stream, tools, and tool_choice.
Streaming
When stream: true is set, the response is returned in chunks as Server-Sent Events (SSE) for real-time rendering. The main event types are:
message_start: Starts a message and carries the initialmessagemetadata.content_block_delta: Content delta; text fragments are indelta.text.message_delta: Message-level delta carryingstop_reasonand cumulativeusage.message_stop: Ends the message.
curl https://ai.inf.space/v1/messages \
-H "Authorization: Bearer $AI_TOS_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{
"model": "claude-sonnet-4-x",
"max_tokens": 1024,
"stream": true,
"messages": [
{ "role": "user", "content": "Write a short poem about clouds." }
]
}'Tool Calls
Declare callable functions with tools. When needed, the model returns a tool_use content block. After the application executes the tool, add the result to the next messages turn as a tool_result block so the model can produce the final answer.
curl https://ai.inf.space/v1/messages \
-H "Authorization: Bearer $AI_TOS_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{
"model": "claude-sonnet-4-x",
"max_tokens": 1024,
"tools": [
{
"name": "get_weather",
"description": "Get the weather for a specified city",
"input_schema": {
"type": "object",
"properties": {
"city": { "type": "string", "description": "City name" }
},
"required": ["city"]
}
}
],
"messages": [
{ "role": "user", "content": "What's the weather like in Hefei today?" }
]
}'The returned tool_use block contains id, name, and input. After executing the tool, add the result and send another request using this structure:
{
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": "toolu_xxx",
"content": "Sunny, 26°C"
}
]
}Prompt Caching
For stable, repeatedly used content such as system prompts and long documents, add cache_control to a content block to use prompt caching, reduce input costs, and improve response speed.
{
"system": [
{
"type": "text",
"text": "(A large block of fixed business context and rules...)",
"cache_control": { "type": "ephemeral" }
}
]
}Writing to the cache produces cache_creation_input_tokens. Later cache hits are counted in cache_read_input_tokens, and cached input is billed at a lower price.
Responses and Usage
Key fields in a native Messages response:
| Field | Description |
|---|---|
content[] | Array of content blocks; type: text is text and type: tool_use is a tool call. |
stop_reason | Reason generation stopped, such as end_turn, max_tokens, tool_use, or stop_sequence. |
usage.input_tokens | Number of non-cached input tokens. |
usage.output_tokens | Number of output tokens. |
usage.cache_creation_input_tokens | Number of input tokens written to the cache. |
usage.cache_read_input_tokens | Number of input tokens served from the cache. |
{
"id": "msg_xxx",
"type": "message",
"role": "assistant",
"content": [
{ "type": "text", "text": "Hello, I am a Chinese assistant..." }
],
"stop_reason": "end_turn",
"usage": {
"input_tokens": 24,
"output_tokens": 38,
"cache_creation_input_tokens": 0,
"cache_read_input_tokens": 0
}
}Billing
The Claude family follows Inference Space LLM billing rules and is metered in tokens, in units of 1M tokens:
input_tokens(non-cached input),cached_input_tokens(cached input), andoutput_tokensare billed separately.- Cached input is billed at a lower price. When an explicit cache price exists, that price is used; otherwise, the lower discount for the same context tier applies.
- Context tiers are determined by the input tokens in the request:
≤128k,≤256k, and>256k. Each tier has different unit pricing. - Reasoning (extended thinking), tool results, and retrieved context are ultimately reflected in input or output tokens.
Exact prices, available models, and discounts are determined by console and organization pricing. Organization-specific prices, contract discounts, and current console prices take precedence.
Integration Recommendations
- Keep API keys on the server in production; do not expose them in browsers or client applications.
- Use a different key for each application to simplify auditing, quota management, and revocation.
- Estimate tokens from representative business samples before setting quotas and alerts; include tool calls, retrieved context, and thinking tokens in the budget.
- Manage the model ID as a configuration value so you can smoothly switch to new versions as the console is updated.
Responses API (OpenAI-Compatible)
OpenAI Responses passthrough at POST /v1/responses, plus optional stateful session subroutes for retrieval, cancellation, and input items.
Image Generation and Editing
OpenAI-compatible `/v1/images/generations` text-to-image and `/v1/images/edits` image-to-image, multi-image fusion, and mask-based inpainting.