Anthropic Messages
POST /v1/messages — Anthropic's Messages API on local models, with streaming and tools. Run Claude Code or the Anthropic SDKs against the models on this Mac.
For developers
POST /v1/messages speaks Anthropic's Messages API, so Claude Code, the Anthropic SDKs and other Anthropic-wire agents can run on the models on this Mac. Midium translates each request into a chat completion and the reply back, so the same models, tool calling, sampling defaults and thinking handling apply. Streaming and tools both work.
Run Claude Code on a local model#
Point Claude Code at Midium with three environment variables, then start it:
export ANTHROPIC_BASE_URL=http://127.0.0.1:9100
export ANTHROPIC_API_KEY=YOUR_API_KEY
export ANTHROPIC_MODEL="YOUR_MODEL"
claude
Use a chat model that handles tools well; Claude Code calls tools on almost every turn. Midium must stay open while Claude Code uses it.
To give Claude Code Midium's sub-agents and your vaults instead (keeping Claude's own models), use Add to Claude Code on the Code page. See MCP servers.
Request#
curl http://127.0.0.1:9100/v1/messages \
-H "x-api-key: YOUR_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "Content-Type: application/json" \
-d '{
"model": "YOUR_MODEL",
"max_tokens": 1024,
"system": "You are a concise assistant.",
"messages": [{"role": "user", "content": "Name three uses for a paperclip."}]
}'
The base URL has no /v1: Anthropic SDKs add /v1/messages themselves.
Parameters#
| Name | Type | Description |
|---|---|---|
model | string | Required. A Midium model's name or nickname (not a Claude model id). |
messages | array | Required. user and assistant turns. Content is a string or a list of blocks: text, image (base64 or URL source), document (plain-text source only), tool_use, tool_result. thinking blocks are accepted and dropped. |
system | string or array | System prompt, as a string or text blocks. Midium: system-role messages inside messages are folded into it. |
max_tokens | integer | Cap on the reply. Midium: the 8192 floor from Chat completions applies. |
stream | boolean | Stream Anthropic SSE events. |
temperature, top_p, top_k | number | Sampling. Omitted fields use the model's recommended values. |
stop_sequences | array | Stop sequences. |
tools | array | Custom tools with an input_schema. A web_search server tool turns on Midium's web search (needs a Brave Search key). Other server tools are refused. |
tool_choice | object | {"type": "auto"}, {"type": "any"}, {"type": "none"} or {"type": "tool", "name": "…"}. |
thinking | object | {"type": "disabled"} turns thinking off; enabled or adaptive turns it on. budget_tokens is ignored. |
output_config | object | Midium: effort maps to reasoning_effort; format: {"type": "json_schema", "schema": {...}} constrains the reply to JSON (see Structured output). |
metadata | object | Accepted and ignored, like service_tier, cache_control and any other field. |
Response#
{
"id": "msg_4be7c3a91f0d2e88a1b6c5d4",
"type": "message",
"role": "assistant",
"model": "YOUR_MODEL",
"content": [
{"type": "text", "text": "Holding papers, resetting a device, and as a makeshift hook."}
],
"stop_reason": "end_turn",
"stop_sequence": null,
"usage": {
"input_tokens": 24,
"output_tokens": 19,
"cache_creation_input_tokens": 0,
"cache_read_input_tokens": 0
}
}
- A tool call comes back as a
tool_useblock with anidstartingtoolu_, andstop_reason: "tool_use". Send the result in atool_resultblock, as with Anthropic's API. stop_reasonisend_turn,tool_useormax_tokens.- Prompt tokens the model's cache served are reported as
cache_read_input_tokens;input_tokensis the rest. - The model's thinking isn't returned: there are no
thinkingblocks in the reply.
Streaming#
With "stream": true you get Anthropic's event sequence: message_start, ping, then content_block_start / content_block_delta / content_block_stop per block, message_delta (with stop_reason and usage), and message_stop.
- Text streams as
text_deltas. - A
tool_useblock opens as soon as the tool's name is known. Its input arrives as oneinput_json_deltaholding the whole JSON object, just before the block closes. pingevents keep the connection open during long prompts.- A failure partway sends an
errorevent.
Count tokens#
POST /v1/messages/count_tokens takes the same body (max_tokens isn't needed) and returns {"input_tokens": n}:
curl http://127.0.0.1:9100/v1/messages/count_tokens \
-H "x-api-key: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"model": "YOUR_MODEL", "messages": [{"role": "user", "content": "Hello"}]}'
It's an estimate: the model's own tokenizer over the prompt's text (about 4 characters a token when the model has no tokenizer file), plus a few tokens per message and a flat 256 per image. The chat template isn't rendered.
Notes#
- Auth. Send the key as
x-api-keyorAuthorization: Bearer.anthropic-versionandanthropic-betaheaders are accepted and ignored. - Documents. Only plain-text
documentsources work. Send PDFs as extracted text. - Images. Images need a model that can see; see Images & vision.
Errors#
Errors use Anthropic's shape:
{"type": "error", "error": {"type": "invalid_request_error", "message": "tools.0: only custom tools with an input_schema are supported (got type 'bash_20250124')"}}
| Status | error.type | When |
|---|---|---|
400 | invalid_request_error | Body isn't JSON, no messages, max_tokens below 1, or an unsupported block, tool or tool_choice. |
401 | authentication_error | No key, or a key this Mac doesn't know. |
403 | permission_error | The key's account is turned off. |
404 | not_found_error | No model by that name. |
500 | api_error | Generation failed. |
See Errors & limits.
Something here wrong or out of date? Tell us.
