Blog / Claude Code Custom API Setup
Claude Code Tutorial

How to Connect Claude Code to a Custom API Provider (Step-by-Step)

I spent two weeks testing different setups to find the most reliable way to use Claude Code with third-party API providers. Here's exactly what worked — and what didn't.

C

CaelLee

June 20, 2025 · 12 min read

TL;DR

Why Use a Custom API Provider?

Here's the thing: Claude Code is incredible, but the official Anthropic API pricing adds up fast when you're using it 8 hours a day. I was burning through $50-80/week on my personal projects alone.

Third-party API providers offer the same models at significantly lower prices — sometimes 40-60% cheaper — because they negotiate volume discounts and optimize their infrastructure. The catch? You need to configure Claude Code to point to their endpoints instead of the default Anthropic API.

That's where most tutorials fall short. They show you the happy path and skip the 15 different errors you'll hit along the way. I'm going to show you everything.

What You'll Need

Step 1: Get Your API Key

First, sign up with a provider that supports the Anthropic Messages API format. Not all providers do — many only support OpenAI's format, which won't work with Claude Code directly.

After signing up, navigate to the API Keys section and generate a new key. Copy it somewhere safe — you'll need it in a moment.

Pro tip: Start with a small balance ($5-10) to test the connection before committing to a larger top-up. Most providers offer instant activation.

Step 2: Install CC Switch (Recommended)

CC Switch is a CLI tool that manages Claude Code's configuration for you. It handles the tricky parts — like switching between different providers and storing credentials securely.

npm install -g cc-switch

Then run the setup wizard:

cc-switch setup

When prompted, select "Anthropic" as the protocol and paste your API key.

Manual Configuration (Without CC Switch)

If you prefer to configure manually, set these environment variables:

# For macOS/Linux (add to ~/.bashrc or ~/.zshrc)
export ANTHROPIC_BASE_URL="https://api.provider.com/llm-api"
export ANTHROPIC_API_KEY="sk-your-api-key-here"

# For Windows PowerShell
$env:ANTHROPIC_BASE_URL = "https://api.provider.com/llm-api"
$env:ANTHROPIC_API_KEY = "sk-your-api-key-here"

Common mistake: Don't add /v1 to the Base URL when using the Anthropic protocol. The /v1 suffix is for OpenAI-compatible endpoints only. If you get a "404 Not Found" error, this is almost always the cause.

Step 3: Verify the Connection

Before diving into a real project, test the connection with a simple prompt:

claude "Say hello and tell me which model you're running"

If everything is configured correctly, you should see a response like:

Hello! I'm running on claude-sonnet-4-20250514.

Troubleshooting Common Errors

I ran into these errors during my testing. Here's how to fix each one:

Error Cause Fix
404 Not Found Wrong Base URL format Remove /v1 from URL
401 Unauthorized Invalid API key Regenerate key
429 Too Many Requests Rate limit exceeded Wait or upgrade plan
Connection refused Network/firewall issue Check proxy settings

Step 4: Start Coding

Once the connection is verified, you're ready to go. Navigate to your project directory and start Claude Code:

cd my-project
claude

Claude Code will use your custom API provider for all requests. The experience is identical to using the official API — same features, same speed, same Agent mode.

What I'd Do Differently

Looking back at my two weeks of testing, here's what I wish I'd known from the start:

  1. Always test with a small balance first. I lost $20 on a provider that turned out to have terrible latency.
  2. Use CC Switch from day one. Manually editing environment variables gets old fast when you're switching between providers.
  3. Check the provider's status page before blaming your config. Half the "connection errors" I debugged were actually provider outages.
  4. Keep a backup API key. When your primary provider goes down at 2 AM, you'll thank yourself.

Common Mistakes to Avoid

C

CaelLee

Full-stack developer with 8+ years of experience. Currently researching AI-powered developer tools and API infrastructure. I test everything I write about.

View all posts →

Related Posts