跪拜 Guibai
← Back to the summary

Run Claude Code on Ollama or DeepSeek — No Anthropic Bill Required

Personal use case: The claude model is really too expensive. Since Claude Code natively only supports the Anthropic API format, this article documents how to connect local models or other models (DeepSeek, etc.) to Claude Code.

Via Claude Code Router (CCR): Connect the Claude Code CLI to Ollama (local models) or DeepSeek (cloud models), enabling Claude Code to run with non-Anthropic models.


Table of Contents

  1. Principle Overview
  2. Installing Claude Code Router
  3. Configuring the Ollama Provider
  4. Configuring the DeepSeek Provider
  5. Configuring Claude Code to Connect to CCR
  6. Verifying the Connection
  7. Advanced: Routing Rules and Virtual Models
  8. Common Troubleshooting
  9. Reference Links

1. Principle Overview

Architecture Diagram

┌─────────────────┐      Anthropic Messages API       ┌──────────────────────────┐
│                 │  ──────────────────────────────▶   │                          │
│   Claude Code   │      /v1/messages                 │  Claude Code Router      │
│   (CLI / VSC)   │  ◀──────────────────────────────   │  (localhost:3456)        │
│                 │                                    │                          │
└─────────────────┘                                    └──────────┬───────────────┘
                                                                  │
                           ┌──────────────────────────────────────┼──────────────────────────┐
                           │                                      │                          │
                           ▼                                      ▼                          ▼
                  ┌──────────────────┐                  ┌──────────────────┐      ┌──────────────────┐
                  │   Ollama         │                  │   DeepSeek API   │      │   OpenAI API     │
                  │   (localhost)    │                  │   (Cloud)        │      │   (Other Compat) │
                  │   :11434/v1      │                  │   api.deepseek   │      │                  │
                  └──────────────────┘                  └──────────────────┘      └──────────────────┘

Workflow

  1. Claude Code natively only supports the Anthropic Messages API format (/v1/messages)
  2. Claude Code Router runs locally, exposing an Anthropic-compatible endpoint at http://127.0.0.1:3456
  3. By setting ANTHROPIC_BASE_URL=http://127.0.0.1:3456, Claude Code sends all API requests to CCR
  4. CCR performs protocol conversion internally: Translates the Anthropic Messages format into the OpenAI Chat Completions format (supported by both Ollama and DeepSeek)
  5. The response is then translated back from the OpenAI format to the Anthropic format and returned to Claude Code

Key Facts

Item Description
Protocol sent by Claude Code Anthropic Messages API (/v1/messages)
Protocol supported by Ollama OpenAI Chat Completions API (/v1/chat/completions)
Protocol supported by DeepSeek OpenAI Chat Completions API (/v1/chat/completions)
Role of CCR Protocol translation gateway, translating between the two formats
Authentication method Credentials passed via ANTHROPIC_AUTH_TOKEN or ANTHROPIC_API_KEY

2. Installing Claude Code Router

System Requirements

Download and Install

  1. Visit the GitHub Releases page
  2. Download the installation package for your system:
    • macOS: .dmg file
    • Windows: .exe installer
    • Linux: .AppImage file
  3. Install and launch the application

First Launch

After launching, CCR creates a configuration file at:


3. Configuring the Ollama Provider

Prerequisites

ollama pull llama3.1
ollama pull qwen2.5
ollama pull deepseek-r1:7b

Configuration via CCR Interface

  1. Open the CCR desktop application
  2. Go to Providers → Click Add Provider
  3. Fill in the following information:
Field Value
Provider Name Ollama
Endpoint (Base URL) http://127.0.0.1:11434/v1
Protocol openai_chat_completions
API Key Leave blank (or fill in ollama arbitrarily)
Models Enter the names of models you have pulled, e.g., llama3.1, qwen2.5
  1. Click Save

Why is the Ollama endpoint :11434/v1? Starting from version 0.1.32, Ollama natively supports an OpenAI-compatible API at the path /v1/chat/completions. CCR uses the openai_chat_completions protocol to convert Anthropic requests and send them to this endpoint.

Optional: Configuration via Deeplink

You can also add it quickly via a URL Scheme:

ccr://provider?name=Ollama&base_url=http%3A%2F%2F127.0.0.1%3A11434%2Fv1&api_key=ollama&models=llama3.1,qwen2.5&protocol=openai_chat_completions

Opening this link in a browser will cause CCR to pop up a confirmation dialog.

Testing the Ollama Connection

Verify that Ollama's OpenAI-compatible API is working correctly from the terminal:

curl http://127.0.0.1:11434/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "model": "llama3.1",
    "messages": [{"role": "user", "content": "Hello"}]
  }'

It should return a JSON response containing a choices field.


4. Configuring the DeepSeek Provider

Prerequisites

Configuration via CCR Interface

  1. Open the CCR desktop application
  2. Go to Providers → Click Add Provider
  3. Fill in the following information:
Field Value
Provider Name DeepSeek
Endpoint https://api.deepseek.com/v1
Protocol openai_chat_completions
API Key sk-xxxxxxxxxxxxxxxxxxxx (your DeepSeek API Key)
Models deepseek-chat, deepseek-reasoner
  1. Click Save

Optional: Configuration via Deeplink

ccr://provider?name=DeepSeek&base_url=https%3A%2F%2Fapi.deepseek.com%2Fv1&api_key=sk-xxxxxxxxx&models=deepseek-chat,deepseek-reasoner&protocol=openai_chat_completions

DeepSeek Model Description

Model ID Description Use Case
deepseek-chat DeepSeek V3 / Latest chat model Daily coding, code generation
deepseek-reasoner DeepSeek R1 reasoning model Complex reasoning, debugging analysis

5. Configuring Claude Code to Connect to CCR

5.1 Start the CCR Gateway

  1. In the CCR desktop application, go to Server
  2. Click Start to start the gateway service
  3. Confirm the status: both endpoints should be running:
    • Wrapper gateway: http://127.0.0.1:3456
    • Core gateway runtime: http://127.0.0.1:3457

💡 It is recommended to check Auto-start, so CCR will automatically start the gateway every time you boot up.

5.2 Create a Claude Code Configuration File

Method A: Global Configuration (Recommended)

Edit ~/.claude/settings.json (macOS/Linux) or %USERPROFILE%\.claude\settings.json (Windows):

{
  "env": {
    "ANTHROPIC_BASE_URL": "http://127.0.0.1:3456",
    "ANTHROPIC_API_KEY": "ccr-local"
  }
}

The value of ANTHROPIC_API_KEY can be any non-empty string — CCR, being a local gateway, does not validate this key, but Claude Code requires a value to bypass the login flow.

Method B: Project-Level Configuration

Create .claude/settings.local.json in the project root directory (this file is automatically gitignored):

{
  "env": {
    "ANTHROPIC_BASE_URL": "http://127.0.0.1:3456",
    "ANTHROPIC_API_KEY": "ccr-local"
  }
}

Method C: Shell Environment Variables (Temporary, Good for Testing)

export ANTHROPIC_BASE_URL=http://127.0.0.1:3456
export ANTHROPIC_API_KEY=ccr-local

5.3 Using CCR Profiles for Automatic Configuration (Recommended)

CCR provides a Profile feature for one-click configuration application:

  1. In CCR, go to Profiles
  2. Select Claude Code
  3. Select the target model (e.g., llama3.1 configured in your Ollama)
  4. Click Apply — this automatically sets the environment variables and opens Claude Code

5.4 Configuring CCR Routing

Set the following in the CCR Routing panel:

Routing Item Recommended Configuration
Default Provider Select your configured Ollama or DeepSeek
Default Model Select the specific model, e.g., llama3.1 or deepseek-chat

You can also configure routing rules for different workloads:

  • Background (background tasks) → Can use a cheaper model
  • Thinking (reasoning tasks) → Can use a more powerful model
  • Subagent (sub-agents) → Can use a more cost-effective model

6. Verifying the Connection

6.1 Quick Test (curl)

After configuring the environment variables, first test if the CCR gateway responds correctly using curl:

curl -X POST http://127.0.0.1:3456/v1/messages \
  -H "Authorization: Bearer ccr-local" \
  -H "anthropic-version: 2023-06-01" \
  -H "content-type: application/json" \
  -d '{"model": "llama3.1", "max_tokens": 50, "messages": [{"role": "user", "content": "Say hello"}]}'

Note: The model name here must be consistent with the model name you previously added in CCR. For DeepSeek, use deepseek-chat; for Ollama, use the model name you pulled, such as llama3.1.

If it returns JSON containing a content field, CCR is working correctly.

6.2 Launching Claude Code

cd your-project
claude

If everything is normal, Claude Code should skip the login screen and go directly into a session.

6.3 Checking Status

Run the following command in Claude Code:

/status

Check the following key information:

Status Item Expected Value
Anthropic base URL http://127.0.0.1:3456
API key Should display ANTHROPIC_API_KEY (not Login method)

6.4 Sending a Test Message

Try sending a simple message, for example:

Hello, what model are you running on?

If the CCR routing is configured correctly, you will receive a model reply, and you can see the request record in the CCR Dashboard.


7. Advanced: Routing Rules and Virtual Models

7.1 Multi-Model Routing

CCR supports routing different tasks to different models based on request characteristics:

Routing Condition Target Model Use Case
Default deepseek-chat Daily conversation and coding
Background llama3.1 (local Ollama) Background tasks, code search
Thinking deepseek-reasoner Tasks requiring deep reasoning
Long Context deepseek-chat Processing large files

Configuration method: Set it in CCR → RoutingAdd Routing Rule.

7.2 Virtual Models

You can create virtual model names to simplify model selection:

llama3.1 → llama3.1 on Ollama
default-coding → deepseek-chat
default-reasoning → deepseek-reasoner

Configure this in CCR → RoutingVirtual Models.

7.3 Fallback Routing

When the primary model is unavailable, CCR can automatically switch to a fallback model:

Primary:   deepseek-chat (DeepSeek)
Fallback:  llama3.1 (Ollama, runs locally, no network required)

Configure this in Routing → Edit routing rule → Configure Fallback model.

7.4 API Key Rotation

For API services like DeepSeek, you can configure multiple API Keys for load balancing and failover:

In Providers → Edit DeepSeek → API Key field, use commas to separate multiple keys:

sk-key1,sk-key2,sk-key3

CCR will automatically rotate to the next key when a request fails.


8. Common Troubleshooting

8.1 Claude Code Still Shows the Login Screen

Cause: The CCR gateway configuration is not being read by Claude Code.

Solution:

8.2 Connection Refused

Cause: The CCR gateway is not started.

Solution:

8.3 Model Returns Errors or No Response

Cause: There may be multiple possible issues.

Solution Steps:

For Ollama:

# Check Ollama service status
ollama list

# Confirm the model is loaded
ollama run llama3.1

# Check Ollama logs
ollama serve

For DeepSeek:

# Directly test the DeepSeek API
curl https://api.deepseek.com/v1/chat/completions \
  -H "Authorization: Bearer sk-your-key" \
  -H "Content-Type: application/json" \
  -d '{"model": "deepseek-chat", "messages": [{"role": "user", "content": "hi"}]}'

8.4 401 Authentication Error

Cause: The API Key is not being passed correctly.

Solution:

8.5 Protocol Conversion Error

Cause: CCR may encounter incompatible fields when converting between Anthropic Messages ↔ OpenAI Chat Completions.

Solution:

{
  "env": {
    "ANTHROPIC_BASE_URL": "http://127.0.0.1:3456",
    "ANTHROPIC_API_KEY": "ccr-local",
    "CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS": "1"
  }
}

8.6 Checking CCR Logs

The Network Logs panel in the CCR desktop application can view all passing requests and responses, making it the best tool for troubleshooting.

8.7 Resetting Configuration

If you need a complete reset:

# Back up existing configuration
cp ~/.claude-code-router/config.json ~/.claude-code-router/config.json.bak

# Delete configuration (CCR will recreate it on next launch)
rm ~/.claude-code-router/config.json

# Clear Claude Code cache
rm -rf ~/.claude/cache

9. Complete Configuration Example

Final ~/.claude/settings.json

{
  "env": {
    "ANTHROPIC_BASE_URL": "http://127.0.0.1:3456",
    "ANTHROPIC_API_KEY": "ccr-local",
    "CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS": "1",
    "CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": "1"
  }
}

Startup Sequence

The correct startup sequence is:

  1. Start Ollama (if using local models): ollama serve
  2. Start CCR and ensure the gateway is in Running status
  3. Start Claude Code: claude

10. Reference Links


Disclaimer: Claude Code Router is a third-party open-source project, not officially maintained by Anthropic. When using non-Anthropic models to run Claude Code, some native Claude Code features (such as ultra-long context, thinking capabilities, tool streaming, etc.) may be limited due to protocol conversion. Always use this in compliance with your organization's security policies.