Skip to content

Ollama 提供與 Anthropic Messages API 的相容性,以協助將現有應用程式連接到 Ollama,包括像 Claude Code 這樣的工具。

使用方法

環境變數

若要與預期使用 Anthropic API 的工具(例如 Claude Code)搭配使用 Ollama,請設定以下環境變數:

shell
export ANTHROPIC_AUTH_TOKEN=ollama  # required but ignored
export ANTHROPIC_BASE_URL=http://localhost:11434

簡單的 /v1/messages 範例

流式傳輸 (Streaming) 範例

工具調用 (Tool calling) 範例

與 Claude Code 搭配使用

Claude Code 可配置為使用 Ollama 作為其後端。

推薦模型

對於程式開發場景,推薦使用 glm-4.7minimax-m2.1qwen3-coder 等模型。

使用前先下載模型:

shell
ollama pull qwen3-coder

注意:Qwen 3 coder 是一個 30B 參數模型,需要至少 24GB 的 VRAM 才能順暢執行。較長的上下文長度則需要更多記憶體。

shell
ollama pull glm-4.7:cloud

快速設定

shell
ollama launch claude

這將提示您選擇模型、自動配置 Claude Code 並啟動它。若要在不啟動的情況下進行配置:

shell
ollama launch claude --config

手動設定

設定環境變數並執行 Claude Code:

shell
ANTHROPIC_AUTH_TOKEN=ollama ANTHROPIC_BASE_URL=http://localhost:11434 claude --model qwen3-coder

或者在您的 Shell 設定檔中設定環境變數:

shell
export ANTHROPIC_AUTH_TOKEN=ollama
export ANTHROPIC_BASE_URL=http://localhost:11434

然後使用任何 Ollama 模型執行 Claude Code:

shell
claude --model qwen3-coder

端點 (Endpoints)

/v1/messages

支援功能

  • [x] Messages
  • [x] Streaming
  • [x] System prompts
  • [x] Multi-turn conversations
  • [x] Vision (images)
  • [x] Tools (function calling)
  • [x] Tool results
  • [x] Thinking/extended thinking

支援的請求欄位

  • [x] model
  • [x] max_tokens
  • [x] messages
    • [x] Text content
    • [x] Image content (base64)
    • [x] Array of content blocks
    • [x] tool_use blocks
    • [x] tool_result blocks
    • [x] thinking blocks
  • [x] system (string or array)
  • [x] stream
  • [x] temperature
  • [x] top_p
  • [x] top_k
  • [x] stop_sequences
  • [x] tools
  • [x] thinking
  • [ ] tool_choice
  • [ ] metadata

支援的響應欄位

  • [x] id
  • [x] type
  • [x] role
  • [x] model
  • [x] content (text, tool_use, thinking blocks)
  • [x] stop_reason (end_turn, max_tokens, tool_use)
  • [x] usage (input_tokens, output_tokens)

流式傳輸事件 (Streaming events)

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

模型

Ollama 支持本地與雲端模型。

本地模型

使用前先下載本地模型:

shell
ollama pull qwen3-coder

推薦的本地模型:

  • qwen3-coder - 非常適合程式開發任務
  • gpt-oss:20b - 強大的通用型模型

雲端模型

雲端模型可立即使用,無需下載:

  • glm-4.7:cloud - 高性能雲端模型
  • minimax-m2.1:cloud - 快速的雲端模型

預設模型名稱

對於依賴 Anthropic 預設模型名稱(例如 claude-3-5-sonnet)的工具,請使用 ollama cp 來複製現有的模型名稱:

shell
ollama cp qwen3-coder claude-3-5-sonnet

之後,可以在 model 欄位中指定此新模型名稱:

shell
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!"
            }
        ]
    }'

與 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
Citationscitations 內容區塊
PDF support帶有 PDF 文件的 document 內容區塊
Server-sent errors流式傳輸期間的 error 事件(錯誤會返回 HTTP 狀態碼)

部分支援

功能狀態
Image content支持 Base64 圖片;不支援 URL 圖片
Extended thinking基本支持;接受但未強制執行 budget_tokens