How Do I Configure Claude Code with OpenClaw? (Step-by-Step Guide)
I kept seeing my Claude Code bill climbing higher every month. The Pro subscription helps, but when you’re running complex coding tasks all day, those token costs add up fast. Then someone on Reddit mentioned OpenClaw could route requests to cheaper models for simple tasks. The only problem? I had no idea how to actually configure it.
I spent hours digging through documentation, watching videos, and breaking my setup multiple times before I got it working. Here’s everything I learned, including all the mistakes I made along the way.
The Problem: Claude Code Costs Were Killing Me
I love Claude Code. It’s genuinely useful for day-to-day development work. But here’s what I noticed:
“Claude Code is very cheap for their subscription, especially if you go on the 200 hours a month Pro plan”
That’s true for the subscription itself. The problem is when you’re making hundreds of API calls through it. Each complex query costs real money, and I was spending way more than I expected.
I needed a way to route simple tasks to cheaper models while keeping Claude’s power for the complex stuff.
My First Attempt: Direct Configuration (Failed)
I started by looking for Claude Code’s configuration file. On macOS, I checked:
ls -la ~/.claude/settings.jsonResult? No such file existed. I was confused. After more digging, I found that you need to CREATE the configuration, not just edit an existing one.
What You Actually Need Before Starting
Before trying to configure anything, make sure you have:
- Claude Code running - You need an active subscription (Pro or Max)
- OpenClaw installed - Either locally or on a server you control
- Your OpenClaw API endpoint ready - Usually
http://localhost:8000/v1if running locally
I didn’t have OpenClaw running yet, so I had to set that up first. That’s a whole separate adventure, but let’s assume you have it working.
Step 1: Create the Configuration Directory
First, I created the configuration directory structure:
mkdir -p ~/.claudeThen I created the settings file:
touch ~/.claude/settings.jsonStep 2: Add OpenClaw as a Provider
This is where I made my first big mistake. I tried to copy someone else’s config without understanding what each field meant. Don’t do that.
Here’s the correct configuration:
{ "providers": { "openclaw": { "api_key": "your-openclaw-api-key", "base_url": "http://localhost:8000/v1", "models": { "claude-3-5-sonnet": { "context_window": 200000, "max_tokens": 8192 }, "claude-3-opus": { "context_window": 200000, "max_tokens": 4096 } } } }}What each field does:
api_key- Your OpenClaw API key (NOT your Anthropic key)base_url- Where OpenClaw is runningmodels- Which models you want to use through OpenClawcontext_window- Maximum context size for each modelmax_tokens- Maximum output tokens
Step 3: Configure Model Routing (The Magic Part)
The real power of OpenClaw is intelligent routing. You don’t want to use Claude Opus for fixing a typo.
Here’s the routing configuration I use:
routing: default_model: "claude-3-5-sonnet"
model_mapping: simple_tasks: pattern: "^(fix|update|refactor)\\s+\\w+" model: "gpt-4o-mini" fallback: "claude-3-5-sonnet"
complex_reasoning: pattern: "(architect|design|comprehensive|analyze)" model: "claude-3-opus"
code_generation: pattern: "(implement|create|build|write)" model: "claude-3-5-sonnet"
fallback: enabled: true model: "claude-3-5-sonnet" retry_count: 2How this works:
- Simple tasks like “fix typo” go to cheaper models
- Complex tasks like “architect this system” go to powerful models
- If something fails, it falls back to a reliable default
I initially used regex patterns that were too strict. Start simple and refine based on your actual usage.
Step 4: Set Environment Variables (Security Tip)
Don’t hardcode API keys in your config files. I learned this the hard way when I accidentally committed my settings to a public repo.
Instead, use environment variables:
export OPENCLAW_API_KEY="your-api-key-here"export OPENCLAW_BASE_URL="http://localhost:8000/v1"export CLAUDE_CODE_PROVIDER="openclaw"Then reference them in your config:
{ "providers": { "openclaw": { "api_key": "${OPENCLAW_API_KEY}", "base_url": "${OPENCLAW_BASE_URL}" } }}Step 5: Test Everything
Before assuming it works, actually test it:
# Check if OpenClaw is runningcurl http://localhost:8000/health
# Run a simple Claude Code taskclaude-code "Create a simple hello world Python script"If you get a response, you’re in business.
The Video That Finally Made It Click
I spent hours reading docs. What actually helped was watching someone do it.
“People keep asking me for how to setup the Claude Code Config. Just go to youtube to Berman’s ‘5 Billions Tokens on Openclaw’ video and skip 31 mins in”
That video showed me several things the docs didn’t:
- Real error messages and how to fix them
- The actual file structure on disk
- Live debugging of connection issues
If you’re stuck, find that video and watch from 31:00 onwards.
Common Problems I Hit
Connection Refused
Error: Connection refused to localhost:8000This means OpenClaw isn’t running. Check:
# Is the process running?ps aux | grep openclaw
# Check logsopenclaw logs --tail 50API Key Not Recognized
Error: Invalid API keyUsually means you didn’t export the environment variable:
# Check if it's setecho $OPENCLAW_API_KEY
# If empty, set it and restartexport OPENCLAW_API_KEY="your-key-here"claude-code restartModel Not Found
Error: Model 'claude-3-5-sonnet' not foundYour OpenClaw config needs to list the model:
providers: anthropic: models: - claude-3-5-sonnetSlow Responses
If everything works but it’s slow:
caching: enabled: true backend: "redis" ttl: 3600Or switch to a faster default model:
routing: default_model: "claude-3-haiku"How I Route Tasks by Cost
After using this setup for a few weeks, I settled on this tiered approach:
routing_tiers: tier_1: models: ["gpt-4o-mini", "claude-3-haiku"] use_cases: - quick fixes - simple refactoring - documentation updates cost_per_1k_tokens: "$0.0001-0.0003"
tier_2: models: ["claude-3-5-sonnet", "gpt-4o"] use_cases: - feature implementation - code review - debugging cost_per_1k_tokens: "$0.003-0.015"
tier_3: models: ["claude-3-opus", "o1"] use_cases: - architectural decisions - complex refactoring - security analysis cost_per_1k_tokens: "$0.015-0.075"This reduced my monthly costs by about 70% while keeping the quality I need.
What I Wish I Knew Earlier
- The config file doesn’t exist by default - You create it yourself
- Environment variables matter - Don’t hardcode secrets
- Start with simple routing - Complex patterns break easily
- Watch the video - It explains things docs don’t
- Test incrementally - Don’t configure everything at once
One Reddit comment really stuck with me:
“definitely set up and configure the OpenAI Codex model… you can pretty much have it build itself if you have Claude Code access”
The meta aspect of using Claude Code to configure itself is actually possible. I used it to write my routing config after I got the basics working.
Is This Worth It?
For me, absolutely. The initial setup took about 2 hours (mostly figuring out mistakes). Now I save significantly on API costs every month, and I still get Claude’s power for complex tasks.
If you’re a heavy Claude Code user, the OpenClaw integration pays for itself within days. Just follow the steps carefully, and don’t skip the testing phase.
Final Words + More Resources
My intention with this article was to help others share my knowledge and experience. If you want to contact me, you can contact by email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope:
Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!
Comments