New Tool Radar

Local AI

How to Use an Anthropic Client with Local Ollama

Point an Anthropic client at http://localhost:11434 and send a request to /v1/messages to get a local model's reply in Anthropic's message format.

Schematic diagram with three steps: point an Anthropic client at localhost:11434 with the placeholder key ollama, send a message to /v1/messages, then read the text blocks from the reply.

Ollama supports a subset of the Anthropic Messages API, so Anthropic clients and tools such as Claude Code can connect to Ollama. The local endpoint is a single address, POST /v1/messages on your own server, and the client needs only a change of base URL to talk to a local model instead of Anthropic’s servers. This is a different endpoint from the OpenAI-compatible /v1/chat/completions, which the code-calling guide covers: the request shape, the fields, and the shape of the answer are Anthropic’s, not OpenAI’s.

Send one request

Ollama’s Anthropic compatibility page gives the curl form for a local server. Use ollama as the placeholder API key, and pull a model first — the page’s example uses qwen3-coder.

curl -X POST http://localhost:11434/v1/messages -H "Content-Type: application/json" -H "x-api-key: ollama" -H "anthropic-version: 2023-06-01" -d '{ "model": "qwen3-coder", "max_tokens": 1024, "messages": [{ "role": "user", "content": "Hello, how are you?" }] }'

Field Value in the example Why it is there
model qwen3-coder An Ollama model tag, not a Claude model name
max_tokens 1024 The most tokens the reply may use
messages [{ "role": "user", "content": "Hello, how are you?" }] The conversation so far, in Anthropic’s shape
x-api-key ollama A placeholder: the local server does not validate API keys
anthropic-version 2023-06-01 The API version the request targets

From code, the same connection is a base URL change. The page’s Python example creates the client with base_url='http://localhost:11434' and api_key='ollama' — required by the client, but ignored by Ollama — then calls client.messages.create with the same model, max_tokens, and messages, and prints message.content[0].text. The Anthropic client appends /v1/messages to the base URL by itself.

Read the answer

Read the text blocks from content. The reply is a list of content blocks rather than a single answer string: text blocks hold the answer, while other block types can carry reasoning or tool requests. The supported response fields are id, type, role, model, content (text, tool_use, and thinking blocks), stop_reason (end_turn, max_tokens, or tool_use), and usage (input_tokens and output_tokens). The endpoint also supports messages, streaming, system prompts, multi-turn conversations, vision with base64 images, and tools with tool results.

Try it: ask the model how it is

Send the curl request above, then check the three results.

Check What you should see What it means
Answer text A content block of type text with the reply The local model answered in Anthropic’s format
Stop reason stop_reason of end_turn The model finished its reply normally
Token counts A usage object with input_tokens and output_tokens How much of the request and reply was counted

Expected result. We checked Ollama’s Anthropic compatibility documentation, which gives the curl and Python examples and the supported fields above. We did not send a request to /v1/messages on this machine, so we have no reply of our own; the checks above are the expected result.

Limits

This is a subset of the Anthropic API, not the whole of it. The documentation lists as not supported: the /v1/messages/count_tokens endpoint, tool_choice, metadata, prompt caching with cache_control blocks, the Batches API, citations, PDF document blocks, and server-sent errors during streaming. Token counts are approximations based on the underlying model’s tokenizer. The cloud endpoint is different again: direct cloud requests need a real bearer and the hosted endpoint rejects anthropic-version values older than 2023-06-01. And the shape is compatible, not the behaviour: a third-party guide puts it plainly — the API shape is compatible, but the model behaviour underneath is whatever that particular checkpoint actually does, so a local model will not reason like Claude. Starting Claude Code itself is a different task, done with ollama launch. We did not test any of this ourselves.

Sources

See all