DEV Community

Jenny Met
Jenny Met

Posted on • Originally published at docs.crazyrouter.com

How to Configure Claude Code with a Third-Party OpenAI-Compatible API

Claude Code is a terminal-first coding assistant. It can read a repository, explain code, edit files, run commands, and help with refactors from inside your development workflow.

If you want to use it with a third-party API gateway, the setup is mostly environment variables. The part that often trips people up is this:

Claude Code is not configured like a normal OpenAI-compatible SDK.

OpenAI-compatible clients usually use a base URL like:

https://example.com/v1
Enter fullscreen mode Exit fullscreen mode

Claude Code uses the Anthropic Messages API style and expects the base URL to be the root domain:

https://example.com
Enter fullscreen mode Exit fullscreen mode

This article walks through the setup using Crazyrouter as the concrete example. Crazyrouter provides docs for both OpenAI-compatible clients and Anthropic-native tools, including a dedicated Claude Code setup guide and a general documentation introduction.

What you are configuring

Claude Code needs three broad things:

  1. the Claude Code CLI installed locally
  2. an API key from your third-party provider or gateway
  3. environment variables telling Claude Code where to send Anthropic API requests

For Crazyrouter, the relevant values are:

ANTHROPIC_BASE_URL=https://crazyrouter.com
ANTHROPIC_API_KEY=sk-your-token-here
Enter fullscreen mode Exit fullscreen mode

For the China-optimized API route, use:

ANTHROPIC_BASE_URL=https://cn.crazyrouter.com
ANTHROPIC_API_KEY=sk-your-token-here
Enter fullscreen mode Exit fullscreen mode

Notice there is no /v1 at the end of ANTHROPIC_BASE_URL.

That one detail is the difference between a clean setup and confusing errors like duplicated paths.

Prerequisites

You should have:

  • Git installed
  • Node.js 18 or newer
  • npm available
  • a Crazyrouter account and API token
  • model access for the Claude model you plan to use

The full official path is documented in the Crazyrouter Claude Code guide. This article focuses on the practical configuration flow.

Step 1: verify Git and Node.js

Claude Code is a coding tool, so make sure your local development environment is ready first.

git --version
node -v
npm -v
Enter fullscreen mode Exit fullscreen mode

You want Node.js 18+.

On macOS with Homebrew:

brew install git node
Enter fullscreen mode Exit fullscreen mode

On Ubuntu or Debian:

sudo apt update
sudo apt install -y git nodejs npm
Enter fullscreen mode Exit fullscreen mode

On Windows, install Git and Node.js LTS with your preferred installer or package manager, then verify in PowerShell:

git --version
node -v
npm -v
Enter fullscreen mode Exit fullscreen mode

Step 2: install Claude Code

Install the Claude Code CLI globally:

npm install -g @anthropic-ai/claude-code
Enter fullscreen mode Exit fullscreen mode

Then verify:

claude --version
which claude
Enter fullscreen mode Exit fullscreen mode

On Windows PowerShell:

claude --version
where.exe claude
Enter fullscreen mode Exit fullscreen mode

If the command is not found after installation, close and reopen your terminal so your PATH refreshes.

Step 3: create a dedicated API token

Do not reuse a production application token for a local coding assistant.

Create a separate token such as:

claude-code-local
Enter fullscreen mode Exit fullscreen mode

If your provider supports model allowlists and budgets, start narrow. For Crazyrouter, the Claude Code documentation suggests using Claude models such as:

claude-sonnet-4-6
claude-opus-4-6
Enter fullscreen mode Exit fullscreen mode

A dedicated token gives you:

  • easier rotation
  • clearer usage logs
  • separate budget control
  • less risk if a local environment leaks
  • simpler debugging when one tool breaks

Step 4: set temporary environment variables

Before writing anything permanently to your shell profile, test with temporary variables.

macOS or Linux:

export ANTHROPIC_BASE_URL=https://crazyrouter.com
export ANTHROPIC_API_KEY=sk-your-token-here

printf '%s\n' "$ANTHROPIC_BASE_URL"
Enter fullscreen mode Exit fullscreen mode

Windows PowerShell:

$env:ANTHROPIC_BASE_URL = "https://crazyrouter.com"
$env:ANTHROPIC_API_KEY = "sk-your-token-here"

Write-Output $env:ANTHROPIC_BASE_URL
Enter fullscreen mode Exit fullscreen mode

If you are using the China-optimized API route, set the root domain:

export ANTHROPIC_BASE_URL=https://cn.crazyrouter.com
Enter fullscreen mode Exit fullscreen mode

Again, do not add /v1.

Step 5: run Claude Code in a test repository

Create or open a small repository first. Avoid testing a new coding-agent setup inside an important production repo with uncommitted work.

mkdir claude-code-test
cd claude-code-test
git init
printf '# Claude Code test\n' > README.md
git add README.md
git commit -m "Initial commit"
Enter fullscreen mode Exit fullscreen mode

Then start Claude Code:

claude
Enter fullscreen mode Exit fullscreen mode

A simple first prompt:

Read this repository and suggest one small improvement. Do not edit files yet.
Enter fullscreen mode Exit fullscreen mode

This tests authentication, network routing, model access, and basic tool behavior without making changes.

Step 6: persist the variables after validation

Once the temporary setup works, make it persistent.

For Bash:

cat >> ~/.bashrc <<'EOF'
export ANTHROPIC_BASE_URL=https://crazyrouter.com
export ANTHROPIC_API_KEY=sk-your-token-here
EOF

source ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

For Zsh:

cat >> ~/.zshrc <<'EOF'
export ANTHROPIC_BASE_URL=https://crazyrouter.com
export ANTHROPIC_API_KEY=sk-your-token-here
EOF

source ~/.zshrc
Enter fullscreen mode Exit fullscreen mode

For Windows PowerShell user-level variables:

[Environment]::SetEnvironmentVariable("ANTHROPIC_BASE_URL", "https://crazyrouter.com", "User")
[Environment]::SetEnvironmentVariable("ANTHROPIC_API_KEY", "sk-your-token-here", "User")
Enter fullscreen mode Exit fullscreen mode

Close and reopen PowerShell after setting persistent variables.

For production-like workstations, consider using a password manager, local secret manager, or shell-specific encrypted environment management instead of writing raw keys to a plain-text profile.

Step 7: understand the base URL difference

This is the most important troubleshooting section.

Crazyrouter's API Endpoints docs describe the difference like this:

OpenAI-compatible SDKs / clients:
https://crazyrouter.com/v1

Claude Code / Anthropic-native clients:
https://crazyrouter.com
Enter fullscreen mode Exit fullscreen mode

Why?

Because different clients append paths differently.

An OpenAI-compatible SDK expects a base URL ending in /v1, then it appends paths such as:

/chat/completions
/models
/responses
Enter fullscreen mode Exit fullscreen mode

So the final URL becomes:

https://crazyrouter.com/v1/chat/completions
Enter fullscreen mode Exit fullscreen mode

Claude Code uses the Anthropic-native protocol. It appends the Anthropic request path itself. If you give it this incorrect value:

ANTHROPIC_BASE_URL=https://crazyrouter.com/v1
Enter fullscreen mode Exit fullscreen mode

You can end up with duplicated or invalid paths.

Correct:

ANTHROPIC_BASE_URL=https://crazyrouter.com
Enter fullscreen mode Exit fullscreen mode

Incorrect:

ANTHROPIC_BASE_URL=https://crazyrouter.com/v1
ANTHROPIC_BASE_URL=https://crazyrouter.com/v1/messages
Enter fullscreen mode Exit fullscreen mode

If your logs show paths like /v1/v1/messages or /v1/messages/v1/messages, reset the base URL to the root domain.

Step 8: keep OpenAI-compatible app config separate

You might also have a normal app that uses the OpenAI SDK through the same gateway.

That app should still use /v1:

import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.CRAZYROUTER_API_KEY,
  baseURL: "https://crazyrouter.com/v1",
});

const response = await client.chat.completions.create({
  model: "gpt-5.4",
  messages: [{ role: "user", content: "Hello" }],
});

console.log(response.choices[0].message.content);
Enter fullscreen mode Exit fullscreen mode

This is why I recommend separate environment variable names:

# For OpenAI-compatible apps
export CRAZYROUTER_API_KEY=sk-your-token-here
export OPENAI_BASE_URL=https://crazyrouter.com/v1

# For Claude Code
export ANTHROPIC_API_KEY=sk-your-claude-code-token-here
export ANTHROPIC_BASE_URL=https://crazyrouter.com
Enter fullscreen mode Exit fullscreen mode

Keeping these separate prevents one tool's base URL convention from breaking another tool.

Step 9: add a safety workflow for repositories

Coding agents are most useful when they can edit files, but you should still keep a safe workflow.

Before starting a session:

git status
Enter fullscreen mode Exit fullscreen mode

If you have uncommitted work, commit or stash it first:

git add .
git commit -m "Save work before Claude Code session"
Enter fullscreen mode Exit fullscreen mode

Or:

git stash push -m "before claude-code session"
Enter fullscreen mode Exit fullscreen mode

After Claude Code makes changes:

git diff
git status
npm test
Enter fullscreen mode Exit fullscreen mode

For Python projects:

pytest
Enter fullscreen mode Exit fullscreen mode

For Go projects:

go test ./...
Enter fullscreen mode Exit fullscreen mode

For Rust projects:

cargo test
Enter fullscreen mode Exit fullscreen mode

Treat the agent like a fast pair programmer: helpful, but still subject to review.

Troubleshooting checklist

If Claude Code does not work, check these in order.

1. Is the CLI installed?

claude --version
Enter fullscreen mode Exit fullscreen mode

If not found, reinstall or refresh your terminal PATH.

2. Are the environment variables visible in the same shell?

macOS or Linux:

echo "$ANTHROPIC_BASE_URL"
test -n "$ANTHROPIC_API_KEY" && echo "API key is set"
Enter fullscreen mode Exit fullscreen mode

PowerShell:

Write-Output $env:ANTHROPIC_BASE_URL
if ($env:ANTHROPIC_API_KEY) { Write-Output "API key is set" }
Enter fullscreen mode Exit fullscreen mode

Do not print the full API key in shared logs or screenshots.

3. Is the base URL correct?

For Claude Code:

https://crazyrouter.com
Enter fullscreen mode Exit fullscreen mode

Not:

https://crazyrouter.com/v1
Enter fullscreen mode Exit fullscreen mode

4. Does the token have model access?

Check whether the token is allowed to use the Claude model you selected. If you created a narrow token, this is easy to overlook.

5. Are you behind a proxy or firewall?

If your terminal cannot reach the API host, Claude Code cannot either. Test basic connectivity from the same machine and shell.

curl -I https://crazyrouter.com
Enter fullscreen mode Exit fullscreen mode

For the China-optimized API route:

curl -I https://cn.crazyrouter.com
Enter fullscreen mode Exit fullscreen mode

A recommended local setup

For regular use, I like this structure:

Token name: claude-code-local
Allowed models: Claude models only
Budget: small monthly cap at first
Base URL: https://crazyrouter.com
Storage: user shell profile or local secret manager
Test repo: small repository before production use
Review: always inspect git diff before committing
Enter fullscreen mode Exit fullscreen mode

Then keep your app token separate:

Token name: webapp-dev
Base URL: https://crazyrouter.com/v1
Used by: OpenAI-compatible SDK app
Enter fullscreen mode Exit fullscreen mode

This separation saves a surprising amount of debugging time.

Final thoughts

Configuring Claude Code with a third-party API gateway is not difficult, but it is easy to copy the wrong base URL from an OpenAI-compatible example.

Remember:

Claude Code uses the Anthropic-native root URL.
OpenAI-compatible SDKs usually use the /v1 URL.
Enter fullscreen mode Exit fullscreen mode

If you are using Crazyrouter, start with the Claude Code setup guide, then keep the API Endpoints reference nearby whenever you configure another client.

For the broader docs map, the Crazyrouter introduction links to quick start, tool integrations, text models, image generation, video generation, and AI-readable documentation.

Top comments (0)