How to Make Claude Code Use Other Models

4 months ago 3

Recently, I’ve been experimenting with various agent projects. Most of them support using any model provider that follows the OpenAI-compatible API format. However, Claude Code—the most well-known among this type—can only use its own models. While their models are quite good, they’re too expensive for my use case, which is purely exploratory or for side projects, not for production.

After some searching, I couldn’t find any guides on how to make Claude Code work with unofficial models, so I decided to document my approach.

The core of this method is a project called Claude Bridge. The implementation, like solving many computer science problems, adds a middle layer. Specifically, it patches Node’s fetch method to intercept all requests to Anthropic’s official API, transforms them into a standard OpenAI format (actually a unified format defined by the author), forwards them to the designated provider, and then transforms the streaming response back into Claude’s SSE format.

Install dependencies

# Official Claude Code npm install -g @anthropic-ai/claude-code # Claude Bridge npm install -g @mariozechner/claude-bridge

Prepare API key helper script

This step is inspired by a Claude Code GitHub issue: How can I use my API key without signing in?, and the article Setting up Claude-Code with API Key.

By default, Claude Code only works by logging into a Claude account (redirecting to the official login page), and doesn’t support direct API Key usage. But there is a workaround—just set up an apiKeyHelper.

First, add the following to ~/.claude/settings.json (create the file if it doesn’t exist):

{ "apiKeyHelper": "~/.claude/anthropic_key.sh" }

Then, create the script ~/.claude/anthropic_key.sh with the following content:

echo "sk_your_anthropic_api_key"

(Since we’ll be using a third-party model provider, this can be any string.)

Finally, make the script executable:

chmod +x ~/.claude/anthropic_key.sh

Run Claude Code

Replace the parameters with your target provider and model. The first openai is the provider format—you can also use gemini, etc. For details, refer to the Claude Bridge README.

claude-bridge openai {{model_name}} --baseURL {{base_url}} --apiKey {{api_key}}

 Known limitations

  • Token counting may be inaccurate
  • Image input is not supported
  • Web search/fetch is not available
  • Some parts of thinking/reasoning may fail to parse properly

The article is translated from https://nekonull.me/share/claude-code-3rd-party-model/ and original authored by @jerrylususu 

Read Entire Article