Anthropic Messages API
Send non-streaming and streaming AIXHUB Anthropic-compatible Messages requests, inspect the response, and retry safely.
Use the Anthropic-compatible surface when the selected AIXHUB model route supports Messages. Raw HTTP calls use the complete endpoint POST https://api.aixhub.org/v1/messages; Anthropic SDKs and clients usually receive the root Base URL https://api.aixhub.org and append /v1/messages themselves.
| Setting | Value |
|---|---|
| Base URL for an Anthropic SDK or compatible client | https://api.aixhub.org |
| Complete HTTP endpoint | https://api.aixhub.org/v1/messages |
| Authentication | x-api-key: AIXHUB_API_KEY |
| Required version header | anthropic-version: 2023-06-01 |
| Example model | panel-anthropic-model-id copied from Model routes |
Create a dedicated AIXHUB API key before running the examples. Do not send an OpenAI Bearer header in place of x-api-key.
Bash request
read -rsp "AIXHUB API key: " AIXHUB_API_KEY && echo
export AIXHUB_API_KEY
export AIXHUB_MODEL="panel-anthropic-model-id"
curl --fail-with-body --silent --show-error \
https://api.aixhub.org/v1/messages \
--header "x-api-key: $AIXHUB_API_KEY" \
--header "anthropic-version: 2023-06-01" \
--header "content-type: application/json" \
--data "{\"model\":\"$AIXHUB_MODEL\",\"max_tokens\":16,\"messages\":[{\"role\":\"user\",\"content\":\"Reply with OK only.\"}]}"PowerShell request
$secureKey = Read-Host "AIXHUB API key" -AsSecureString
$env:AIXHUB_API_KEY = [System.Net.NetworkCredential]::new("", $secureKey).Password
$env:AIXHUB_MODEL = "panel-anthropic-model-id"
$headers = @{
"x-api-key" = $env:AIXHUB_API_KEY
"anthropic-version" = "2023-06-01"
}
$body = @{
model = $env:AIXHUB_MODEL
max_tokens = 16
messages = @(@{ role = "user"; content = "Reply with OK only." })
} | ConvertTo-Json -Depth 4 -Compress
$response = Invoke-RestMethod `
-Method Post `
-Uri "https://api.aixhub.org/v1/messages" `
-Headers $headers `
-ContentType "application/json" `
-Body $body
$response.content |
Where-Object { $_.type -eq "text" } |
Select-Object -ExpandProperty textReplace the model stand-in with an exact Messages-compatible route. After testing in a disposable shell, run unset AIXHUB_API_KEY AIXHUB_MODEL in Bash. In PowerShell, remove the environment values and every object that holds the derived key:
Remove-Item Env:AIXHUB_API_KEY, Env:AIXHUB_MODEL -ErrorAction SilentlyContinue
Remove-Variable headers, body, response, secureKey -ErrorAction SilentlyContinueRequest fields
| Field | Required | Meaning |
|---|---|---|
model | Yes | Exact Anthropic-compatible model ID copied from AIXHUB Model routes |
max_tokens | Yes | Maximum number of tokens to generate; it does not guarantee that many tokens will be used |
messages | Yes | Ordered user and assistant turns; the minimal example sends one user turn |
messages[].role | Yes | user or assistant; a system instruction belongs in the top-level system field |
messages[].content | Yes | A string or supported content-block array |
system | No | Top-level system instruction; do not insert a system role into messages |
stream | No | Set true to receive server-sent events instead of one JSON response |
temperature | No | Sampling control supported only when the selected route accepts it |
Start with the minimal fields. Add tools, images, prompt caching, or other optional features only after the route documentation confirms support and the basic request is visible in Usage.
Response example
The following JSON shows the stable shape to inspect. IDs and token counts are illustrative; do not hard-code them.
{
"id": "msg_01Example",
"type": "message",
"role": "assistant",
"model": "panel-anthropic-model-id",
"content": [
{
"type": "text",
"text": "OK"
}
],
"stop_reason": "end_turn",
"stop_sequence": null,
"usage": {
"input_tokens": 12,
"output_tokens": 1
}
}Read text blocks from content and inspect stop_reason before treating the output as complete. Preserve the HTTP status and any request ID header returned by the service when investigating a failure.
Streaming
Add "stream": true and disable curl buffering:
curl --no-buffer --fail-with-body --silent --show-error \
https://api.aixhub.org/v1/messages \
--header "x-api-key: $AIXHUB_API_KEY" \
--header "anthropic-version: 2023-06-01" \
--header "content-type: application/json" \
--data "{\"model\":\"$AIXHUB_MODEL\",\"max_tokens\":16,\"stream\":true,\"messages\":[{\"role\":\"user\",\"content\":\"Reply with OK only.\"}]}"A normal text stream follows an event sequence such as message_start, content_block_start, one or more content_block_delta events, content_block_stop, message_delta, and message_stop. ping events can appear between content events. Concatenate text only from text-delta content blocks in event order. Save the final message_delta.delta.stop_reason before message_stop.
Treat message_stop as the protocol end of the message, then inspect the saved stop_reason before accepting the output as complete. For this bounded OK test, expect end_turn; max_tokens means the output was truncated, while other reasons require application-specific handling. A stream can deliver an error event after the initial HTTP 200; in that case, keep the error, discard assumptions based on partial text, check Usage, and decide whether a new request is safe.
Errors and retries
| Observation | Action | Retry rule |
|---|---|---|
| 400 or invalid request | Check JSON shape, required fields, model ID, and protocol | Correct it before retrying |
| 401 | Confirm x-api-key and the complete dedicated key | Retry only after fixing authentication |
| 403 | Check key state, group, subscription, and balance; INSUFFICIENT_BALANCE is 403 | Retry only after the account state changes |
| 413 | Reduce prompt or attachment size | Retry the smaller request |
| 429 | Reduce rate and concurrency; honor Retry-After when present | Use capped backoff and a limited attempt count |
| 502 or 503 | Preserve time and request ID, then check Usage and service state | Retry a limited number of times after waiting |
SSE error event | Treat partial output as incomplete and preserve the event payload | Check acceptance and Usage before starting a new message |
A model ID or route problem is not always a 404. For any transient retry, allow at most three total attempts including the original, and honor Retry-After whenever the response actually returns it. Do not replay a 5xx, disconnect, or SSE error unless the application explicitly accepts duplicate output and charges, or can prove the prior generation was not accepted. See the error code reference for source classification and safe diagnostics.
Verification
The non-streaming example should print:
OKFor streaming, require message_stop and verify the final message_delta.delta.stop_reason is end_turn for this bounded test. Treat max_tokens as truncated rather than successful completion. Then open AIXHUB Usage and match the request time, selected model, and successful status. If Usage displays the protocol, confirm it is the Anthropic-compatible route.
Continue with the Anthropic Python SDK or configure Claude Code.
Official sources
Last verified: 2026-07-14
OpenAI Responses API
Send non-streaming and streaming Responses requests through AIXHUB with Bearer authentication, explicit completion checks, and bounded retry handling.
OpenAI Python SDK with AIXHUB
Create an isolated Python environment, call AIXHUB Responses, inspect the full object, stream safely, and manage SDK upgrades.