Language
Inference Space Docs

Messages API (Anthropic-Native)

Anthropic-native POST /v1/messages for Claude family models, with support for system, tools, thinking, prompt caching, and SSE streaming.

Inference Space provides an Anthropic-native Messages API at POST /v1/messages, covering the latest Claude Opus, Sonnet, and Haiku generations. Applications that already use the Anthropic SDK or Claude ecosystem tools such as Claude Code and Claude SDK can reuse native capabilities including messages, system, tools, thinking, and cache_control.

Private deployments use the same API; only the base URL needs to be replaced → Enterprise Private Deployment.

For the OpenAI-compatible interface to Claude, see Chat Completions API. Both interfaces share the same account, API key, and billing system.

Overview

  • Endpoint: POST https://ai.inf.space/v1/messages (Anthropic-native Messages protocol)
  • Base URL: https://ai.inf.space (the browser console and API share this host; use the documented API path)
  • Model family: Claude Opus (complex programming and deep reasoning), Sonnet (general-purpose tasks and everyday coding), and Haiku (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. See Model List.

Authentication and Base URL

The API base URL is https://ai.inf.space. Authentication headers:

Authorization: Bearer gk_xxxxxxxxxxxxxxxx
anthropic-version: 2023-06-01
content-type: application/json
  • Use Authorization: Bearer <gk_...> for authentication. Both the OpenAI and Anthropic paths use Bearer. Create API keys beginning with gk_ in the console.
  • The gateway does not accept an incoming x-api-key; it reads only the Authorization header, which must include the Bearer prefix. Otherwise it returns 401.
  • Native Messages requests must also include anthropic-version: 2023-06-01.

Keep the API key on the server in production; do not expose it in a browser.

Provider Selection

/v1/messages defaults to the anthropic provider. This is the Anthropic protocol path: clients such as Claude Code and Claude SDK send an Anthropic-shaped request without a provider field when connecting through ANTHROPIC_BASE_URL. To specify another provider, use the X-Provider header or body.provider; either takes precedence over the default.

Unlike the strict routing of /v1/chat/completions and /v1/responses, /v1/messages is the only path that defaults to anthropic when no provider is specified.

Calling Messages

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 $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
from anthropic import Anthropic

client = Anthropic(
    base_url="https://ai.inf.space",
    auth_token=os.environ["TOS_API_KEY"],  # → Authorization: Bearer gk_...
)

msg = client.messages.create(
    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(msg.content[0].text)
import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic({
  baseURL: "https://ai.inf.space",
  authToken: process.env.TOS_API_KEY, // → Authorization: Bearer gk_...
});

const msg = await client.messages.create({
  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(msg.content[0].text);

The official Anthropic SDK's api_key / apiKey sends an x-api-key header by default, which the gateway does not accept and which results in a 401. Use auth_token / authToken instead to send Authorization: Bearer, and set base_url to https://ai.inf.space. See Client Integrations for the complete configuration.

Parameters

ParameterTypeRequiredDescription
modelstringYesModel ID, such as Claude Opus, Sonnet, or Haiku; see the console for available models.
messagesarrayYesArray of conversation messages. Each item contains role (user / assistant) and content (a string or array of content blocks).
max_tokensintegerYesMaximum number of output tokens in a single response.
systemstring / arrayNoSystem prompt that defines the model's role and global constraints.
temperaturenumberNoSampling temperature.
top_pnumberNoNucleus-sampling threshold; use it as an alternative to temperature.
stop_sequencesarrayNoCustom stop sequences. Generation stops when one is matched.
streambooleanNoWhether to return an SSE stream; defaults to false.
toolsarrayNoList of tool (function) definitions.
tool_choiceobjectNoTool-selection strategy, such as { "type": "auto" }, or a specific tool.
thinkingobjectNoExtended-thinking configuration. When enabled, the model outputs its reasoning process before the conclusion.

Use cache_control on content blocks to use prompt caching.

anthropic-beta Header

When the client does not provide an anthropic-beta header, the gateway injects the following defaults:

anthropic-beta: oauth-2025-04-20,interleaved-thinking-2025-05-14,context-management-2025-06-27

If the client provides its own anthropic-beta header, such as the beta matrix negotiated by the Claude Code CLI, the gateway preserves the client's value exactly and does not override it.

Streaming

When stream: true is set, the response is returned in chunks as Server-Sent Events (SSE). The main event types are:

  • message_start: Starts a message and carries the initial message metadata.
  • content_block_delta: Content delta; text fragments are in delta.text.
  • message_delta: Message-level delta carrying stop_reason and cumulative usage.
  • message_stop: Ends the message.
curl https://ai.inf.space/v1/messages \
  -H "Authorization: Bearer $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 $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:

FieldDescription
content[]Array of content blocks; type: text is text and type: tool_use is a tool call.
stop_reasonReason generation stopped, such as end_turn, max_tokens, tool_use, or stop_sequence.
usage.input_tokensNumber of non-cached input tokens.
usage.output_tokensNumber of output tokens.
usage.cache_creation_input_tokensNumber of input tokens written to the cache.
usage.cache_read_input_tokensNumber 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, and output_tokens are 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.
  • 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.

On this page