Language
Inference Space Docs

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 as messages, system, tools, thinking, and cache_control directly.
  • 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:

InterfaceEndpointAuthentication header
Anthropic-nativehttps://ai.inf.space/v1/messagesAuthorization: Bearer $AI_TOS_API_KEY
OpenAI-compatiblehttps://ai.inf.space/v1/chat/completionsAuthorization: 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.

ParameterTypeRequiredDescription
modelstringYesModel ID, such as claude-opus-4-x / claude-sonnet-4-x / claude-haiku-4-x; 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.
systemstringNoSystem prompt that defines the model's role and global constraints.
temperaturenumberNoSampling temperature in the range 01; higher values produce more varied output.
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 used for tool calls.
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.

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 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 $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:

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_tokens (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.
  • 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.

On this page