Skip to main content

Anthropic compatibility

还没翻译译自英文(docs.ollama.com)本页 44 段都还没译

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_use blocks
    • tool_result blocks
    • thinking blocks
  • system (string or array)
  • stream
  • temperature
  • top_p
  • top_k
  • stop_sequences
  • tools
  • 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. 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-version values older than 2023-06-01. Use 2023-06-01 in requests.
  • Token counts are approximations based on the underlying model's tokenizer

Not supported

The following Anthropic API features are not currently supported:

FeatureDescription
/v1/messages/count_tokensToken counting endpoint
tool_choiceForcing specific tool use or disabling tools
metadataRequest metadata (user_id)
Prompt cachingcache_control blocks for caching prefixes
Batches API/v1/messages/batches for async batch processing
Citationscitations content blocks
PDF supportdocument content blocks with PDF files
Server-sent errorserror events during streaming (errors return HTTP status)

Partial support

FeatureStatus
Image contentBase64 images supported; URL images not supported
Extended thinkingBasic support; budget_tokens accepted but not enforced