Anthropic compatibility
Revised
Ollama supports a subset of the Anthropic Messages API, so you can connect Anthropic clients and tools such as Claude Code to it. This page lists the supported endpoints, request fields, and limits.1?1
Direct cloud access
Set your API key in OLLAMA_API_KEY.X43 You don't need to install Ollama.2?2
export OLLAMA_API_KEY="your_api_key"
Send a request with bearer authentication:
curl https://ollama.com/v1/messages \
-H "Authorization: Bearer $OLLAMA_API_KEY" \
-H "Content-Type: application/json" \
-H "anthropic-version: 2023-06-01" \
-d '{
"model": "gemma4:31b",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": "Say hello in one sentence."
}
]
}'
Read text blocks from content. The cloud endpoint requires Authorization: Bearer and doesn't accept x-api-key alone.3
Compatible models support messages, streaming, and function calling. Tool-choice controls, deferred tools, and hosted web search aren't4 fully supported.?3
Local server usage
These examples connect to your local Ollama server. Use ollama as the placeholder API key.X30 To use cloud models through this server, sign in to Ollama.
Environment variables
To use Ollama with tools that expect the Anthropic API (like Claude Code), set these environment variables:X6
export ANTHROPIC_AUTH_TOKEN=ollama # required but ignored
export ANTHROPIC_BASE_URL=http://localhost:11434
Simple /v1/messages example
import anthropic
client = anthropic.Anthropic(
base_url='http://localhost:11434',
api_key='ollama', # required but ignored
)
message = client.messages.create(
model='qwen3-coder',
max_tokens=1024,
messages=[
{'role': 'user', 'content': 'Hello, how are you?'}
]
)
print(message.content[0].text)
import Anthropic from "@anthropic-ai/sdk";
const anthropic = new Anthropic({
baseURL: "http://localhost:11434",
apiKey: "ollama", // required but ignored
});
const message = await anthropic.messages.create({
model: "qwen3-coder",
max_tokens: 1024,
messages: [{ role: "user", content: "Hello, how are you?" }],
});
console.log(message.content[0].text);
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?" }]
}'
Streaming example
import anthropic
client = anthropic.Anthropic(
base_url='http://localhost:11434',
api_key='ollama',
)
with client.messages.stream(
model='qwen3-coder',
max_tokens=1024,
messages=[{'role': 'user', 'content': 'Count from 1 to 10'}]
) as stream:
for text in stream.text_stream:
print(text, end='', flush=True)
import Anthropic from "@anthropic-ai/sdk";
const anthropic = new Anthropic({
baseURL: "http://localhost:11434",
apiKey: "ollama",
});
const stream = await anthropic.messages.stream({
model: "qwen3-coder",
max_tokens: 1024,
messages: [{ role: "user", content: "Count from 1 to 10" }],
});
for await (const event of stream) {
if (
event.type === "content_block_delta" &&
event.delta.type === "text_delta"
) {
process.stdout.write(event.delta.text);
}
}
curl -X POST http://localhost:11434/v1/messages \
-H "Content-Type: application/json" \
-d '{
"model": "qwen3-coder",
"max_tokens": 1024,
"stream": true,
"messages": [{ "role": "user", "content": "Count from 1 to 10" }]
}'
Tool calling example
import anthropic
client = anthropic.Anthropic(
base_url='http://localhost:11434',
api_key='ollama',
)
message = client.messages.create(
model='qwen3-coder',
max_tokens=1024,
tools=[
{
'name': 'get_weather',
'description': 'Get the current weather in a location',
'input_schema': {
'type': 'object',
'properties': {
'location': {
'type': 'string',
'description': 'The city and state, e.g. San Francisco, CA'
}
},
'required': ['location']
}
}
],
messages=[{'role': 'user', 'content': "What's the weather in San Francisco?"}]
)
for block in message.content:
if block.type == 'tool_use':
print(f'Tool: {block.name}')
print(f'Input: {block.input}')
import Anthropic from "@anthropic-ai/sdk";
const anthropic = new Anthropic({
baseURL: "http://localhost:11434",
apiKey: "ollama",
});
const message = await anthropic.messages.create({
model: "qwen3-coder",
max_tokens: 1024,
tools: [
{
name: "get_weather",
description: "Get the current weather in a location",
input_schema: {
type: "object",
properties: {
location: {
type: "string",
description: "The city and state, e.g. San Francisco, CA",
},
},
required: ["location"],
},
},
],
messages: [{ role: "user", content: "What's the weather in San Francisco?" }],
});
for (const block of message.content) {
if (block.type === "tool_use") {
console.log("Tool:", block.name);
console.log("Input:", block.input);
}
}
curl -X POST http://localhost:11434/v1/messages \
-H "Content-Type: application/json" \
-d '{
"model": "qwen3-coder",
"max_tokens": 1024,
"tools": [
{
"name": "get_weather",
"description": "Get the current weather in a location",
"input_schema": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state"
}
},
"required": ["location"]
}
}
],
"messages": [{ "role": "user", "content": "What is the weather in San Francisco?" }]
}'
Using with Claude Code
See the Claude Code guide for Ollama Launch and direct cloud setup.X9
Endpoints
/v1/messages
Supported features
- Messages
- Streaming
- System prompts
- Multiturn5 conversations
- Vision (images)
- Tools (function calling)
- Tool results
- Thinking/extended thinking
Supported request fields
-
model -
max_tokens -
messages- Text
content - Image
content(base64) - Array of content blocks
-
tool_useblocks -
tool_resultblocks (includingtool_referenceblocks from client-side tool search) -
thinkingblocks
- Text
-
system(string or array) -
stream -
temperature -
top_p -
top_k -
stop_sequences -
tools-
defer_loading(a deferred tool is offered to the model only after atool_referenceloads it)
-
-
thinking -
output_config-
effort(model-defined names)
-
-
tool_choice -
metadata
Supported response fields
-
id -
type -
role -
model -
content(text, tool_use, thinking blocks) -
stop_reason(end_turn, max_tokens, tool_use) -
usage(input_tokens, output_tokens)
Streaming events
-
message_start -
content_block_start -
content_block_delta(text_delta, input_json_delta, thinking_delta) -
content_block_stop -
message_delta -
message_stop -
ping -
errorX19
Use /api/show to discover a model's supported thinking values and default. thinking.type: "enabled" and "disabled" act as explicit on/off controls. Models that can't turn off6 thinking use their lowest supported level for "disabled". When a model uses its metadata to resolve named levels, supported output_config.effort names are applied exactly and unsupported names resolve to the model default. Models without metadata keep the existing compatibility behavior, which trims and lowercases effort names and maps "xhigh" to "high".X20
Models
Ollama supports both local and cloud models. See the currently supported models7.
Local models
Pull a local model before use:
ollama pull qwen3-coder
Cloud models
Browse the cloud model catalog. Through a signed-in Ollama server, use a cloud name such as gemma4:cloud without a separate pull.X4 For direct cloud requests, use the hosted identifier, such as gemma4:31b.
Default model names on a local server
For tooling that relies on default Anthropic model names such as claude-3-5-sonnet, use ollama cp to copy an existing model name:
ollama cp qwen3-coder claude-3-5-sonnet
Afterward, you can specify the new model name in the model field:8
curl http://localhost:11434/v1/messages \
-H "Content-Type: application/json" \
-d '{
"model": "claude-3-5-sonnet",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": "Hello!"
}
]
}'
Differences from the Anthropic API
Behavior differences
- The local server doesn't9 validate API keys. Direct cloud inference requires a valid bearer token.
- The hosted endpoint rejects
anthropic-versionvalues older than2023-06-01. Use2023-06-01in requests. - Token counts are approximations based on the underlying model's tokenizer.10
Not supported
The following Anthropic API features aren't11 currently supported:
| Feature | Description |
|---|---|
/v1/messages/count_tokens |
Token counting endpoint |
tool_choice |
Forcing specific tool use or disabling tools |
metadata |
Request metadata (user_id) |
| Prompt caching | cache_control blocks for caching prefixes |
| Batches API | /v1/messages/batches for async batch processing |
| Citations | citations content blocks |
| PDF support | document content blocks with PDF files |
| Server-sent errors | error events during streaming (errors return HTTP status) |
Partial support
| Feature | Status |
|---|---|
| Image content | Base64 images supported; URL images not supported |
| Extended thinking | Basic support; budget_tokens accepted but not enforced |
Notes on this page
1 Wording Audit: improved A reference opens with one sentence on what it is and what the page lists, not with an instruction.
Was:
Connect Anthropic clients and tools such as Claude Code to Ollama. Ollama supports a subset of the Anthropic Messages API.type-ref-austereAuditor: The opening now identifies the page’s purpose and what readers can find on it.
2 Wording Audit: improved Replaced a fragment with a full sentence addressed to the reader.
Was:
No Ollama installation required.ContractionsAuditor: The fragment becomes a clear sentence with the same meaning.
3 Wording Audit: improved Joined the two clauses without a semicolon and used the contraction.
Was:
The cloud endpoint requiresAuthorization: Bearer; it does not acceptx-api-keyalone.SemicolonContractionsAuditor: The sentence reads more smoothly and keeps the authentication requirement intact.
4 Wording Audit: improved Used the contraction.
Was:
are notContractionsAuditor: The contraction makes the wording more concise without changing its meaning.
5 Wording Audit: improved Closed the prefix.
Was:
Multi-turnClosedPrefixesAuditor: “Multiturn” is a concise equivalent of “multi-turn.”
6 Wording Audit: neutral Used "can't turn off" instead of "cannot disable".
Was:
cannot disableTurnOnContractionsAuditor: “Can’t turn off” conveys the same limitation as “cannot disable.”
7 Wording Audit: improved Link text now names the destination instead of "here"; the link target is unchanged.
Was:
hereLinkTextlink-descriptive-textAuditor: The link text names its destination while preserving the link target.
8 Wording Audit: improved Used the active voice and wrote "Afterward".
Was:
Afterwards, this new model name can be specified in themodelfield:prin-plain-wordsAuditor: The sentence uses a more direct construction and preserves the instruction.
9 Wording Audit: improved Used the contraction.
Was:
does notContractionsAuditor: The contraction makes the wording more concise without changing its meaning.
10 Wording Audit: improved Added the closing period that the other two items in the list have.
Was:
- Token counts are approximations based on the underlying model's tokenizerlist-parallel-structureAuditor: Adding the period makes the list punctuation consistent.
11 Wording Audit: improved Used the contraction.
Was:
are notContractionsAuditor: The contraction makes the wording more concise without changing its meaning.
Questions for the owner
?1 Which Ollama version does this page describe? A reference should state it, and the page gives none.
About: “Ollama supports a subset of the Anthropic Messages API”
?2 The example key
your_api_keyis not in the angle-bracket placeholder form; should it be<your-api-key>?About: “No Ollama installation required.”
?3 The page says tool-choice controls are "not fully supported", but the endpoint list and the Not supported table say
tool_choiceis not supported at all; which is right?About: “Tool-choice controls, deferred tools, and hosted web search are not fully supported.”
X4 Whether cloud models must be pulled. Cloud models and Anthropic compatibility say cloud models need no download or pull, while VS Code tells the reader to pull one. Does the VS Code extension need
ollama pullfor a cloud model to appear, or should that step be removed?Pages: Run cloud models, Anthropic compatibility, Use Ollama models in VS Code Chat
X6 Anthropic compatibility setup omits ANTHROPIC_API_KEY. The Claude Code page sets ANTHROPIC_API_KEY to an empty string in its local and cloud setups, but the Anthropic compatibility page's environment variables for Claude Code leave it out. Is clearing ANTHROPIC_API_KEY required for Claude Code, and if so should the Anthropic compatibility page include it?
Pages: Anthropic compatibility, Claude Code
X9 Anchor to Claude Code direct-cloud section does not match the heading. Two pages link to #connect-directly-to-ollama-cloud, but the Claude Code heading is "Connect directly to Ollama's cloud", whose generated slug includes "ollamas". Should the heading get an explicit anchor id, or should both links be updated to the generated slug?
Pages: Quickstart, Anthropic compatibility, Claude Code
X19 How errors are reported during streaming. The Errors page says a mid-stream error arrives as an object in the stream with the status unchanged; Anthropic compatibility lists the
errorevent as supported and also lists server-senterrorevents as not supported because errors return an HTTP status. Does the Anthropic-compatible endpoint emiterrorevents mid-stream, and how should that relate to the Errors page?Pages: Errors, Anthropic compatibility
X20 Mapping of the "xhigh" effort for models without metadata. For models without thinking metadata, OpenAI compatibility maps "xhigh" to "max", while Anthropic compatibility maps it to "high". Is the difference between the two compatibility layers intended, or should one mapping be corrected?
X30 Placeholder API key for local servers varies. Pages tell readers to use
ollama,not-needed, or an empty value as the placeholder key for the same local server. Should one placeholder value be used across pages where the tool allows it?Pages: Anthropic compatibility, Run Droid with Ollama, Run Copilot CLI with Ollama, Hermes Agent
X43 API key setup repeated on five pages. Creating the key and exporting OLLAMA_API_KEY is repeated in Quickstart, Creating an API key, Authentication and both compatibility pages; only Quickstart gives the PowerShell form. Which page should own API key setup, with the others linking to it?
Pages: Quickstart, Create an API key, Authentication, OpenAI compatibility, Anthropic compatibility
Original
Connect Anthropic clients and tools such as Claude Code to Ollama. Ollama supports a subset of the Anthropic Messages API.
Direct cloud access
Set your API key in OLLAMA_API_KEY. No Ollama installation required.
export OLLAMA_API_KEY="your_api_key"
Send a request with bearer authentication:
curl https://ollama.com/v1/messages \
-H "Authorization: Bearer $OLLAMA_API_KEY" \
-H "Content-Type: application/json" \
-H "anthropic-version: 2023-06-01" \
-d '{
"model": "gemma4:31b",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": "Say hello in one sentence."
}
]
}'
Read text blocks from content. The cloud endpoint requires Authorization: Bearer; it does not accept x-api-key alone.
Compatible models support messages, streaming, and function calling. Tool-choice controls, deferred tools, and hosted web search are not fully supported.
Local server usage
These examples connect to your local Ollama server. Use ollama as the placeholder API key. To use cloud models through this server, sign in to Ollama.
Environment variables
To use Ollama with tools that expect the Anthropic API (like Claude Code), set these environment variables:
export ANTHROPIC_AUTH_TOKEN=ollama # required but ignored
export ANTHROPIC_BASE_URL=http://localhost:11434
Simple /v1/messages example
import anthropic
client = anthropic.Anthropic(
base_url='http://localhost:11434',
api_key='ollama', # required but ignored
)
message = client.messages.create(
model='qwen3-coder',
max_tokens=1024,
messages=[
{'role': 'user', 'content': 'Hello, how are you?'}
]
)
print(message.content[0].text)
import Anthropic from "@anthropic-ai/sdk";
const anthropic = new Anthropic({
baseURL: "http://localhost:11434",
apiKey: "ollama", // required but ignored
});
const message = await anthropic.messages.create({
model: "qwen3-coder",
max_tokens: 1024,
messages: [{ role: "user", content: "Hello, how are you?" }],
});
console.log(message.content[0].text);
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?" }]
}'
Streaming example
import anthropic
client = anthropic.Anthropic(
base_url='http://localhost:11434',
api_key='ollama',
)
with client.messages.stream(
model='qwen3-coder',
max_tokens=1024,
messages=[{'role': 'user', 'content': 'Count from 1 to 10'}]
) as stream:
for text in stream.text_stream:
print(text, end='', flush=True)
import Anthropic from "@anthropic-ai/sdk";
const anthropic = new Anthropic({
baseURL: "http://localhost:11434",
apiKey: "ollama",
});
const stream = await anthropic.messages.stream({
model: "qwen3-coder",
max_tokens: 1024,
messages: [{ role: "user", content: "Count from 1 to 10" }],
});
for await (const event of stream) {
if (
event.type === "content_block_delta" &&
event.delta.type === "text_delta"
) {
process.stdout.write(event.delta.text);
}
}
curl -X POST http://localhost:11434/v1/messages \
-H "Content-Type: application/json" \
-d '{
"model": "qwen3-coder",
"max_tokens": 1024,
"stream": true,
"messages": [{ "role": "user", "content": "Count from 1 to 10" }]
}'
Tool calling example
import anthropic
client = anthropic.Anthropic(
base_url='http://localhost:11434',
api_key='ollama',
)
message = client.messages.create(
model='qwen3-coder',
max_tokens=1024,
tools=[
{
'name': 'get_weather',
'description': 'Get the current weather in a location',
'input_schema': {
'type': 'object',
'properties': {
'location': {
'type': 'string',
'description': 'The city and state, e.g. San Francisco, CA'
}
},
'required': ['location']
}
}
],
messages=[{'role': 'user', 'content': "What's the weather in San Francisco?"}]
)
for block in message.content:
if block.type == 'tool_use':
print(f'Tool: {block.name}')
print(f'Input: {block.input}')
import Anthropic from "@anthropic-ai/sdk";
const anthropic = new Anthropic({
baseURL: "http://localhost:11434",
apiKey: "ollama",
});
const message = await anthropic.messages.create({
model: "qwen3-coder",
max_tokens: 1024,
tools: [
{
name: "get_weather",
description: "Get the current weather in a location",
input_schema: {
type: "object",
properties: {
location: {
type: "string",
description: "The city and state, e.g. San Francisco, CA",
},
},
required: ["location"],
},
},
],
messages: [{ role: "user", content: "What's the weather in San Francisco?" }],
});
for (const block of message.content) {
if (block.type === "tool_use") {
console.log("Tool:", block.name);
console.log("Input:", block.input);
}
}
curl -X POST http://localhost:11434/v1/messages \
-H "Content-Type: application/json" \
-d '{
"model": "qwen3-coder",
"max_tokens": 1024,
"tools": [
{
"name": "get_weather",
"description": "Get the current weather in a location",
"input_schema": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state"
}
},
"required": ["location"]
}
}
],
"messages": [{ "role": "user", "content": "What is the weather in San Francisco?" }]
}'
Using with Claude Code
See the Claude Code guide for Ollama Launch and direct cloud setup.
Endpoints
/v1/messages
Supported features
- Messages
- Streaming
- System prompts
- Multi-turn conversations
- Vision (images)
- Tools (function calling)
- Tool results
- Thinking/extended thinking
Supported request fields
-
model -
max_tokens -
messages- Text
content - Image
content(base64) - Array of content blocks
-
tool_useblocks -
tool_resultblocks (includingtool_referenceblocks from client-side tool search) -
thinkingblocks
- Text
-
system(string or array) -
stream -
temperature -
top_p -
top_k -
stop_sequences -
tools-
defer_loading(a deferred tool is offered to the model only after atool_referenceloads it)
-
-
thinking -
output_config-
effort(model-defined names)
-
-
tool_choice -
metadata
Supported response fields
-
id -
type -
role -
model -
content(text, tool_use, thinking blocks) -
stop_reason(end_turn, max_tokens, tool_use) -
usage(input_tokens, output_tokens)
Streaming events
-
message_start -
content_block_start -
content_block_delta(text_delta, input_json_delta, thinking_delta) -
content_block_stop -
message_delta -
message_stop -
ping -
error
Use /api/show to discover a model's supported thinking values and default. thinking.type: "enabled" and "disabled" act as explicit on/off controls. Models that cannot disable thinking use their lowest supported level for "disabled". When a model uses its metadata to resolve named levels, supported output_config.effort names are applied exactly and unsupported names resolve to the model default. Models without metadata keep the existing compatibility behavior, which trims and lowercases effort names and maps "xhigh" to "high".
Models
Ollama supports both local and cloud models. See the currently supported models here.
Local models
Pull a local model before use:
ollama pull qwen3-coder
Cloud models
Browse the cloud model catalog. Through a signed-in Ollama server, use a cloud name such as gemma4:cloud without a separate pull. For direct cloud requests, use the hosted identifier, such as gemma4:31b.
Default model names on a local server
For tooling that relies on default Anthropic model names such as claude-3-5-sonnet, use ollama cp to copy an existing model name:
ollama cp qwen3-coder claude-3-5-sonnet
Afterwards, this new model name can be specified in the model field:
curl http://localhost:11434/v1/messages \
-H "Content-Type: application/json" \
-d '{
"model": "claude-3-5-sonnet",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": "Hello!"
}
]
}'
Differences from the Anthropic API
Behavior differences
- The local server does not validate API keys. Direct cloud inference requires a valid bearer token.
- The hosted endpoint rejects
anthropic-versionvalues older than2023-06-01. Use2023-06-01in requests. - Token counts are approximations based on the underlying model's tokenizer
Not supported
The following Anthropic API features are not currently supported:
| Feature | Description |
|---|---|
/v1/messages/count_tokens |
Token counting endpoint |
tool_choice |
Forcing specific tool use or disabling tools |
metadata |
Request metadata (user_id) |
| Prompt caching | cache_control blocks for caching prefixes |
| Batches API | /v1/messages/batches for async batch processing |
| Citations | citations content blocks |
| PDF support | document content blocks with PDF files |
| Server-sent errors | error events during streaming (errors return HTTP status) |
Partial support
| Feature | Status |
|---|---|
| Image content | Base64 images supported; URL images not supported |
| Extended thinking | Basic support; budget_tokens accepted but not enforced |
What Changed
The page already had a reference shape, so the changes are small. The opening now says what the page lists, one link whose text was "here" now names its destination, and wording was tightened (contractions, a closed prefix, a missing period, a typo-like phrase). Open questions cover the version the page describes, the placeholder key format, and a wording gap on tool choice, and several inventory conflicts are attached where they occur.
Content type: reference. The page lists the endpoints, request fields, response fields, and limits of Ollama's Anthropic-compatible API for lookup, with a few setup examples.
Automated check, original: 1 errors, 2 warnings, 6 suggestions. Revised: 0 errors, 0 warnings, 0 suggestions.
Independent audit (GPT-6): improved. The revision is clearer and easier to scan, with the listed wording changes preserving meaning. It also leaves unresolved a support-status conflict that the instructions said to flag, and the revision adds a brief description of what the page lists.
What the revision missed:
- The page says deferred tools are “not fully supported” but also marks
defer_loadingas supported; the revision should ask the owner to resolve this conflict rather than leave it unflagged. - Question q2 quotes “No Ollama installation required,” which was removed in the revision, instead of identifying the current
your_api_keyplaceholder whose format it asks about. - The added opening says the page lists supported endpoints, but it describes one endpoint,
/v1/messages; this is a minor imprecision in the new framing.
Wording (11)
- 1 A reference opens with one sentence on what it is and what the page lists, not with an instruction. improved
- 2 Replaced a fragment with a full sentence addressed to the reader. improved
- 3 Joined the two clauses without a semicolon and used the contraction. improved
- 4 Used the contraction. improved
- 5 Closed the prefix. improved
- 6 Used "can't turn off" instead of "cannot disable". neutral
- 7 Link text now names the destination instead of "here"; the link target is unchanged. improved
- 8 Used the active voice and wrote "Afterward". improved
- 9 Used the contraction. improved
- 10 Added the closing period that the other two items in the list have. improved
- 11 Used the contraction. improved