Messages (Anthropic)
POST /v1/messages — drop-in Anthropic-compatible endpoint, used by Claude Code and any Anthropic SDK.
The Messages endpoint speaks the Anthropic Messages wire format. Any client targeting https://api.anthropic.com/v1/messages can be redirected to Cocobox.
https://api.cocobox.io/v1/messages Authenticated with Authorization: Bearer sk-coco-… Request
| Field | Type | Description |
|---|---|---|
model required | string | Model alias or full id. See Models. |
messages required | Message[] | Alternating user / assistant turns. Each content may be a string or an array of content blocks. |
system optional | string | System prompt — equivalent to OpenAI's system role message. |
max_tokens required | integer | Maximum output tokens. Required by the Anthropic format. |
stream optional | boolean | If true, stream as SSE. Default: false |
temperature optional | number | Sampling temperature, 0–1. Default: 1 |
top_p optional | number | Nucleus sampling. |
stop_sequences optional | string[] | Up to 4 stop sequences. |
tools optional | Tool[] | Anthropic-format tool definitions ({ name, description, input_schema }). Passthrough — orchestrate yourself or use MCP. |
tool_choice optional | object | { "type": "auto" | "any" | "tool", "name": "..." }. |
metadata optional | object | { "user_id": "..." }. Forwarded to the provider for abuse monitoring. |
Response (non-streaming)
{
"id": "msg_01HF2X…",
"type": "message",
"role": "assistant",
"model": "sonnet",
"stop_reason": "end_turn",
"stop_sequence": null,
"usage": { "input_tokens": 24, "output_tokens": 18 },
"content": [
{ "type": "text", "text": "SELECT id, total FROM orders ORDER BY created_at DESC LIMIT 10;" }
]
}
Count tokens
A companion endpoint counts tokens for a request without spending credits.
https://api.cocobox.io/v1/messages/count_tokens Authenticated with Authorization: Bearer sk-coco-… {
"model": "sonnet",
"messages": [{ "role": "user", "content": "SELECT * FROM users;" }]
}
{ "input_tokens": 9 }
Streaming
Set "stream": true. Cocobox emits the standard Anthropic event sequence:
event: message_start
data: {"type":"message_start","message":{"id":"msg_…","model":"sonnet", … }}
event: content_block_start
data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"SELECT"}}
…
event: content_block_stop
data: {"type":"content_block_stop","index":0}
event: message_delta
data: {"type":"message_delta","delta":{"stop_reason":"end_turn","stop_sequence":null},"usage":{"output_tokens":18}}
event: message_stop
data: {"type":"message_stop"}
Examples
curl https://api.cocobox.io/v1/messages \
-H "x-api-key: $COCOBOX_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "Content-Type: application/json" \
-d '{
"model": "sonnet",
"max_tokens": 1024,
"messages": [
{ "role": "user", "content": "Write a CTE that ranks customers by lifetime spend." }
]
}' import Anthropic from '@anthropic-ai/sdk';
const client = new Anthropic({
apiKey: process.env.COCOBOX_API_KEY,
baseURL: 'https://api.cocobox.io',
});
const msg = await client.messages.create({
model: 'sonnet',
max_tokens: 1024,
messages: [{ role: 'user', content: 'SHOW DATABASES;' }],
});
console.log(msg.content[0].type === 'text' ? msg.content[0].text : ''); from anthropic import Anthropic
client = Anthropic(
api_key=os.environ["COCOBOX_API_KEY"],
base_url="https://api.cocobox.io",
)
msg = client.messages.create(
model="opus",
max_tokens=2048,
messages=[{"role": "user", "content": "Audit this schema for missing indexes: …"}],
)
print(msg.content[0].text) # Claude Code expects an Anthropic-compatible endpoint.
export ANTHROPIC_BASE_URL=https://api.cocobox.io
export ANTHROPIC_API_KEY=sk-coco-…
claude --model sonnet
Tools
Tools follow the Anthropic format. Like the OpenAI surface, tools are passthrough on /v1/messages — you receive tool_use blocks in the response and orchestrate the call yourself. For automatic tool execution against your databases, use MCP — or use a client (Claude Code, Cursor, Zed, opencode) that already integrates the Cocobox MCP server.
{
"model": "sonnet",
"max_tokens": 1024,
"messages": [{ "role": "user", "content": "How many active users last week?" }],
"tools": [
{
"name": "sql_run",
"description": "Run a read-only SQL query.",
"input_schema": {
"type": "object",
"properties": { "sql": { "type": "string" } },
"required": ["sql"]
}
}
]
}
Status codes
Identical to Chat completions. See Errors for the response envelope.