Anthropic SDK / Messages format

Call the Claude-native /v1/messages endpoint with the official Anthropic SDK. This is also how Claude Code connects.

Besides the OpenAI format, AI Nexus offers the Anthropic-native POST /v1/messages endpoint. You can use the official Anthropic SDK directly, and it is also how Claude Code connects.

Pick a model that supports this endpoint

/v1/messages works with models that support the Claude-native format. Pick a model on the model square that supports /v1/messages and use its model ID. For other models, use OpenAI SDK / Chat Completions or the Responses API.

Differences from the OpenAI format

OpenAI formatAnthropic format
Path/v1/chat/completions/v1/messages
Key goes inAuthorization: Bearer <Key>x-api-key: <Key>; Authorization: Bearer <Key> also works
Version headernoneanthropic-version: 2023-06-01
max_tokensoptionalrequired
SDK base URLhttps://ai.myaiexp.com/v1https://ai.myaiexp.com (no /v1)
ModelsModels that support Chat CompletionsModels that support the Claude-native format

The most common mistake

The Anthropic SDK appends /v1/messages to the base URL itself, so don't include /v1 in the base URL. Otherwise requests go to /v1/v1/messages.

Example

Replace your-model-id in the samples with a model ID from the model square (or call GET /v1/models).

anthropic_messages.py
import os

import anthropic

client = anthropic.Anthropic(
    api_key=os.environ["MYAIEXP_API_KEY"],
    base_url="https://ai.myaiexp.com",  # note: the Anthropic SDK base_url has no /v1
)

message = client.messages.create(
    model="your-model-id",  # replace with a model ID from the model square
    max_tokens=256,
    messages=[{"role": "user", "content": "Say hello in one sentence."}],
)

print(message.content[0].text)

Install the SDK:

pip install anthropic

If your tool only supports Bearer authentication, replace x-api-key: $MYAIEXP_API_KEY in the curl with Authorization: Bearer $MYAIEXP_API_KEY; in the Anthropic SDK this is the auth_token / authToken option.

Extended thinking

Models that support extended thinking can think before they answer: send the thinking parameter in the request. thinking is a standard parameter of the Anthropic Messages API and is passed through as defined in Anthropic's API. For its format, refer to Anthropic's documentation; which models support extended thinking is listed on the model square.

Notes:

  • Thinking is returned as type: "thinking" content blocks before the answer. Don't read content[0] blindly; filter for type == "text".
  • Thinking tokens count as output tokens and are billed as output. Leave enough room in max_tokens, or the response may be cut off mid-thought.
  • Thinking takes time, so pair it with streaming.

Improve cache hit rate

For multi-turn conversations or long system prompts, add cache_control to content blocks to enable prompt caching; repeated input is billed at the vendor's cache-read list price. See Prompt caching.

Choose the right model

  • /v1/messages only accepts IDs of models that support the Claude-native format; other models return an error. Pick a model on the model square that supports /v1/messages and use its model ID.
  • Model IDs are case-sensitive. The models your key can use are those returned by GET /v1/models; the full list and prices are on the model square.
  • If you set a model restriction when creating the key, make sure the model you need is in the allowed list. See API Keys.

Supported features

FeatureNotes
Basic chatSupported, with either x-api-key or Bearer auth
Streaming ("stream": true)Supported, standard Anthropic SSE events; see Streaming
Tool use (tools / tool_use)Supported; see Function calling
Image inputSupported; see Multimodal
Prompt caching (cache_control)Supported; see Prompt caching
Extended thinkingthinking parameter (model-dependent); see above

This service is provided for learning and research purposes only. Please follow each model provider's usage policies.

On this page