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 format | Anthropic format | |
|---|---|---|
| Path | /v1/chat/completions | /v1/messages |
| Key goes in | Authorization: Bearer <Key> | x-api-key: <Key>; Authorization: Bearer <Key> also works |
| Version header | none | anthropic-version: 2023-06-01 |
max_tokens | optional | required |
| SDK base URL | https://ai.myaiexp.com/v1 | https://ai.myaiexp.com (no /v1) |
| Models | Models that support Chat Completions | Models 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).
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 anthropicIf 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 readcontent[0]blindly; filter fortype == "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/messagesonly 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/messagesand 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
| Feature | Notes |
|---|---|
| Basic chat | Supported, 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 input | Supported; see Multimodal |
Prompt caching (cache_control) | Supported; see Prompt caching |
| Extended thinking | thinking parameter (model-dependent); see above |
Function calling
Let the model decide whether to call functions you define, with a complete two-round example. Works via Chat Completions, and also via the Claude-native Messages format.
Prompt caching (improve cache hit rate)
When calling the Claude-native /v1/messages endpoint, add cache_control to content blocks to enable prompt caching and cut the cost and latency of repeated input.
This service is provided for learning and research purposes only. Please follow each model provider's usage policies.