How to use an Anthropic-compatible client with Ollama

Ollama can answer requests shaped like the Anthropic Messages API, which lets tools that expect Anthropic endpoints talk to a local Ollama server. The compatibility layer is useful when an application exposes only Anthropic-style base URL and token settings.

The local server still ignores the placeholder token, but many clients require one syntactically. Point the client at http://localhost:11434 and keep the model name set to an Ollama model that supports the requested behavior.

Compatibility does not make every Anthropic feature identical. Start with a minimal messages request, then add streaming, tools, or longer context after the basic route returns final text.

Steps to use an Anthropic-compatible client with Ollama:

  1. Set the Anthropic-compatible environment variables for local tools.
    $ export ANTHROPIC_AUTH_TOKEN=ollama
    $ export ANTHROPIC_BASE_URL=http://localhost:11434

    The token value is required by some clients but ignored by the local Ollama server.

  2. Send a minimal Messages API request.
    $ curl -s http://localhost:11434/v1/messages \
      -H 'Content-Type: application/json' \
      -H 'x-api-key: ollama' \
      -H 'anthropic-version: 2023-06-01' \
      -d '{
        "model": "gpt-oss:20b",
        "max_tokens": 80,
        "messages": [{"role":"user","content":"Return only OK."}]
      }'
    {"type":"message","content":[{"type":"thinking"},{"type":"text","text":"OK"}],"stop_reason":"end_turn"}
  3. Read final text from content entries with type text.
    $ python3 - <<'PY'
    content = [{"type":"thinking"}, {"type":"text", "text":"OK"}]
    print(''.join(part.get('text','') for part in content if part.get('type') == 'text'))
    PY
    OK
  4. Increase max_tokens if a thinking model stops before final text.
    $ curl -s http://localhost:11434/v1/messages -d '{"model":"gpt-oss:20b","max_tokens":20,"messages":[{"role":"user","content":"Return OK."}]}'
    {"content":[{"type":"thinking"}],"stop_reason":"max_tokens"}

    For gpt-oss models, too small a token cap can spend the budget on reasoning.

  5. Run the same client with the application model name after the smoke test passes.
    $ export ANTHROPIC_MODEL=gpt-oss:20b

    Use the model name from ollama list or api/tags.