The System One EventUtahGo To Event Page →

Docs

Set up Midium, use every part of it, run it for your team, and build on it.

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:

Shell
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#

NameTypeDescription
modelstringRequired. A Midium model's name or nickname (not a Claude model id).
messagesarrayRequired. 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.
systemstring or arraySystem prompt, as a string or text blocks. Midium: system-role messages inside messages are folded into it.
max_tokensintegerCap on the reply. Midium: the 8192 floor from Chat completions applies.
streambooleanStream Anthropic SSE events.
temperature, top_p, top_knumberSampling. Omitted fields use the model's recommended values.
stop_sequencesarrayStop sequences.
toolsarrayCustom 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_choiceobject{"type": "auto"}, {"type": "any"}, {"type": "none"} or {"type": "tool", "name": "…"}.
thinkingobject{"type": "disabled"} turns thinking off; enabled or adaptive turns it on. budget_tokens is ignored.
output_configobjectMidium: effort maps to reasoning_effort; format: {"type": "json_schema", "schema": {...}} constrains the reply to JSON (see Structured output).
metadataobjectAccepted and ignored, like service_tier, cache_control and any other field.

Response#

JSON
{
  "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_use block with an id starting toolu_, and stop_reason: "tool_use". Send the result in a tool_result block, as with Anthropic's API.
  • stop_reason is end_turn, tool_use or max_tokens.
  • Prompt tokens the model's cache served are reported as cache_read_input_tokens; input_tokens is the rest.
  • The model's thinking isn't returned: there are no thinking blocks 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_use block opens as soon as the tool's name is known. Its input arrives as one input_json_delta holding the whole JSON object, just before the block closes.
  • ping events keep the connection open during long prompts.
  • A failure partway sends an error event.

Count tokens#

POST /v1/messages/count_tokens takes the same body (max_tokens isn't needed) and returns {"input_tokens": n}:

Shell
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-key or Authorization: Bearer. anthropic-version and anthropic-beta headers are accepted and ignored.
  • Documents. Only plain-text document sources work. Send PDFs as extracted text.
  • Images. Images need a model that can see; see Images & vision.

Errors#

Errors use Anthropic's shape:

JSON
{"type": "error", "error": {"type": "invalid_request_error", "message": "tools.0: only custom tools with an input_schema are supported (got type 'bash_20250124')"}}
Statuserror.typeWhen
400invalid_request_errorBody isn't JSON, no messages, max_tokens below 1, or an unsupported block, tool or tool_choice.
401authentication_errorNo key, or a key this Mac doesn't know.
403permission_errorThe key's account is turned off.
404not_found_errorNo model by that name.
500api_errorGeneration failed.

See Errors & limits.

Something here wrong or out of date? Tell us.