Skip to main content

Alternative Model Providers for Claude Code

Introduction​

Claude Code can be configured to use alternative model providers beyond the default Anthropic API. This tutorial covers how to set up OpenRouter for cloud-based model routing and Ollama for local model inference.


OpenRouter​

OpenRouter offers access to the same models from multiple providers and can automatically route requests between them, giving you access to various models including Claude, GPT, and other providers through a unified API.

You Will Need​

  • Claude Code installed
  • An OpenRouter account and API key

1. Create OpenRouter Preset (Optional)​

OpenRouter can route your requests automatically or you can create custom presets for specific routing behavior.

Option A: Automatic High-Throughput Routing​

Navigate to Settings > Routing in your OpenRouter dashboard to enable automatic routing to the provider with the highest throughput.

Option B: Custom Preset​

Create a preset with your own custom name/slug for more control:

  1. Go to Settings > Presets in your OpenRouter dashboard
  2. Click "Create Preset"
  3. Configure your preferred providers and routing rules
  4. Save with a memorable slug (e.g., cerebras-glm-4-7-cerebras)

2. Configure Environment​

Set the following environment variables to point Claude Code at the OpenRouter API:

export ANTHROPIC_BASE_URL=https://openrouter.ai/api
export ANTHROPIC_AUTH_TOKEN=YOUR_OPENROUTER_KEY
export ANTHROPIC_API_KEY=""
note

The ANTHROPIC_API_KEY is intentionally left empty. The ANTHROPIC_AUTH_TOKEN is used for authentication with OpenRouter.

3. Start Claude Code​

Once your environment is configured, start Claude Code with your desired model.

Using a Custom Preset​

claude --model @preset/cerebras-glm-4-7-cerebras

Using Standard Models​

Anthropic Claude:

claude --model anthropic/claude-sonnet-4.5

Other Providers:

claude --model z-ai/glm-4.7
claude --model openai/gpt-oss-120b

Available Models​

OpenRouter provides access to models from many providers. Check the OpenRouter models page for a complete list of available models and their capabilities.

Troubleshooting​

Authentication Errors​

Ensure your ANTHROPIC_AUTH_TOKEN contains a valid OpenRouter API key and that ANTHROPIC_API_KEY is set to an empty string.

Model Not Found​

Verify the model identifier matches the format used by OpenRouter (e.g., provider/model-name). Check the OpenRouter dashboard for the exact model identifiers.


Ollama​

Ollama lets you run large language models locally on your machine. You can configure Claude Code to use Ollama as a backend for local inference.

You Will Need​

  • Claude Code installed
  • Ollama installed and running locally
  • At least one model pulled (e.g., ollama pull llama3)

Configure Environment​

Set the following environment variables to point Claude Code at your local Ollama instance:

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

The ANTHROPIC_AUTH_TOKEN is set to ollama as a placeholder. Ollama doesn't require authentication for local requests.

Start Claude Code​

Once configured, start Claude Code with your desired local model:

claude --model glm-4.7:cloud

Use the model name as it appears in ollama list. For example:

claude --model llama3:latest
claude --model codellama:7b

Troubleshooting​

Connection Refused​

Ensure Ollama is running. Start it with:

ollama serve

Model Not Found​

Pull the model first with ollama pull <model-name>, then try again.


For more information about Claude Code, visit the official documentation.