跳过至正文
Ollama 提供了对 Anthropic Messages API 的兼容性,以帮助将现有应用程序连接到 Ollama,包括 Claude Code 等工具。

用法

环境变量

要将 Ollama 与需要 Anthropic API 的工具(如 Claude Code)配合使用,请设置以下环境变量
export ANTHROPIC_AUTH_TOKEN=ollama  # required but ignored
export ANTHROPIC_BASE_URL=http://localhost:11434

简单的 /v1/messages 示例

basic.py
import anthropic

client = anthropic.Anthropic(
    base_url='https://: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)

流式传输示例

streaming.py
import anthropic

client = anthropic.Anthropic(
    base_url='https://: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)

工具调用示例

tools.py
import anthropic

client = anthropic.Anthropic(
    base_url='https://: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}')

与 Claude Code 配合使用

Claude Code 可以配置为使用 Ollama 作为其后端。 对于编程用例,推荐使用 glm-4.7minimax-m2.1qwen3-coder 等模型。 在使用前下载模型:
ollama pull qwen3-coder
注意:Qwen 3 coder 是一个 30B 参数的模型,至少需要 24GB 的 VRAM 才能流畅运行。若要获得更长的上下文长度,则需要更多显存。
ollama pull glm-4.7:cloud

快速设置

ollama launch claude
这将提示您选择一个模型,自动配置并启动 Claude Code。若要在不启动的情况下进行配置:
ollama launch claude --config

手动设置

设置环境变量并运行 Claude Code
ANTHROPIC_AUTH_TOKEN=ollama ANTHROPIC_BASE_URL=https://:11434 claude --model qwen3-coder
或者在您的 shell 配置文件中设置环境变量
export ANTHROPIC_AUTH_TOKEN=ollama
export ANTHROPIC_BASE_URL=http://localhost:11434
然后使用任何 Ollama 模型运行 Claude Code
claude --model qwen3-coder

端点

/v1/messages

支持的功能

  • 消息 (Messages)
  • 流式输出
  • 系统提示词 (System prompts)
  • 多轮对话
  • 视觉 (图像)
  • 工具 (函数调用)
  • 工具结果
  • 思考/深度思考 (Thinking/extended thinking)

支持的请求字段

  • model
  • max_tokens
  • messages
    • 文本 content
    • 图像 content (base64)
    • 内容块数组 (Array of content blocks)
    • tool_use
    • tool_result
    • thinking
  • system (字符串或数组)
  • stream
  • temperature
  • top_p
  • top_k
  • stop_sequences
  • tools
  • thinking
  • tool_choice
  • metadata

支持的响应字段

  • id
  • type
  • role
  • model
  • content (text, tool_use, thinking 块)
  • stop_reason (end_turn, max_tokens, tool_use)
  • usage (input_tokens, output_tokens)

流式传输事件

  • message_start
  • content_block_start
  • content_block_delta (text_delta, input_json_delta, thinking_delta)
  • content_block_stop
  • message_delta
  • message_stop
  • ping
  • error

模型

Ollama 同时支持本地和云端模型。

本地模型

在使用前拉取本地模型
ollama pull qwen3-coder
推荐的本地模型
  • qwen3-coder - 非常适合编程任务
  • gpt-oss:20b - 强大的通用模型

云端模型

云端模型无需拉取即可立即使用
  • glm-4.7:cloud - 高性能云端模型
  • minimax-m2.1:cloud - 快速云端模型

默认模型名称

对于依赖默认 Anthropic 模型名称(如 claude-3-5-sonnet)的工具,请使用 ollama cp 复制现有模型名称
ollama cp qwen3-coder claude-3-5-sonnet
之后,可以在 model 字段中指定这个新的模型名称
curl https://:11434/v1/messages \
    -H "Content-Type: application/json" \
    -d '{
        "model": "claude-3-5-sonnet",
        "max_tokens": 1024,
        "messages": [
            {
                "role": "user",
                "content": "Hello!"
            }
        ]
    }'

与 Anthropic API 的区别

行为差异

  • 接受 API 密钥但不进行验证
  • 接受 anthropic-version 标头但不使用
  • Token 计数是基于底层模型分词器 (tokenizer) 的近似值

不支持

目前不支持以下 Anthropic API 功能
功能描述
/v1/messages/count_tokensToken 计数端点
tool_choice强制使用特定工具或禁用工具
metadata请求元数据 (user_id)
提示词缓存 (Prompt caching)用于缓存前缀的 cache_control
Batches API用于异步批处理的 /v1/messages/batches
引用 (Citations)citations 内容块
PDF 支持包含 PDF 文件的 document 内容块
服务器发送的错误流式传输期间的 error 事件(错误会返回 HTTP 状态码)

部分支持

功能状态
图像内容支持 Base64 图像;不支持 URL 图像
深度思考 (Extended thinking)基本支持;接受 budget_tokens 但不强制执行