If you landed here you probably Googled something like “OpenClaw Claude Code integration setup for autonomous coding”. This post is the straight‑shot guide I wish existed last week when I wired Claude’s Code models into my OpenClaw agent. No marketing gloss — just what worked, what broke, and the exact config you can paste.
Why wire Claude Code into OpenClaw at all?
OpenClaw already talks to GPT-4o, Mixtral, Gemini 1.5, you name it. Claude Code (I’m using claude-3-code-200k-20240601) is different for two reasons:
- It handles 200K context windows without chunking. You can dump an entire monorepo into memory.
- The pricing is usage-based with a cheap output token rate. For auto-generated pull requests that matters more than input.
Combined with OpenClaw’s shell + browser tools you get a reasonably autonomous junior dev that never sleeps. My use case: kick off refactors from a Telegram chat while commuting, let the agent push a PR, and review it later on desktop.
Prerequisites and cost quick sheet
Versions I’m running
- OpenClaw 0.47.2 (Node 22.4.0)
- OpenClaw Gateway 0.9.6
- claw-daemon 0.9.4
- anthropic sdk 0.19.1
- Claude model:
claude-3-code-200k-20240601
Cost model
As of 2024-06-20:
- Input: $15 / 1 M tokens
- Output: $3 / 1 M tokens
- A typical 1 500-line PR costs me ~$0.04. Cheaper than GPT-4o for long context.
ClawCloud passes the Anthropic invoice straight through. No surcharge. If you’re self-hosting you bring your own key and pay Anthropic directly.
Step 1 — Generate an Anthropic API key and scope it down
Head to Anthropic’s console → API Keys → New Secret Key. Name it openclaw-workbot so you remember what you gave permissions to.
- For now, Anthropic keys are project-wide. You can’t limit to a single model, but you can rotate keys anytime.
- Copy the key; you won’t see it again.
Export it locally while we work:
export ANTHROPIC_API_KEY=sk-ant-replace-me
Step 2 — Wire Claude Code into the OpenClaw Gateway
The Gateway owns provider config. Whether you run on ClawCloud or locally, the file lives in $HOME/.claw/gateway.toml.
Add the Claude block:
[[providers]]
name = "anthropic"
enabled = true
api_key = "${ANTHROPIC_API_KEY}"
[providers.models]
claude-code = "claude-3-code-200k-20240601"
default = "claude-code"
If you’re on ClawCloud’s web UI: Settings → Providers → “Add Provider” → Anthropic. Paste key and select Claude 3 Code 200k from the dropdown.
Common mistake: mis-matching model id
Anthropic keeps renaming SKUs. Run npx openclaw models list anthropic to see the exact identifier available in your region.
Step 3 — Give the agent permission to start sessions
OpenClaw’s RBAC lives in permissions.yaml. Without an explicit allow, Claude calls are blocked by default. Example snippet that grants only the code model:
subjects:
- "agent:build-bot"
resources:
- "model:claude-code"
actions:
- "invoke"
If you skip this, you’ll see ERR_POLICY_DENY in logs and nothing else.
Reload without downtime
RBAC is hot-reloaded. Just kill -HUP $(pgrep openclaw-gateway) or hit “Reload” on the web.
Step 4 — Define the autonomous coding workflow
OpenClaw workflows live in ./flows. Below is the trimmed version of my personal build-and-pr.flow.ts. Note the explicit provider='anthropic' selection so the gateway doesn’t round-robin to GPT-4o.
export default defineFlow(async (ctx) => {
const branch = `auto/${Date.now()}`;
await ctx.shell.run(`git checkout -b ${branch}`);
const diff = await ctx.llm.generate({
provider: 'anthropic',
model: 'claude-code',
system: `You are an autonomous coding agent.
When you're done, write the diff to disk, run tests, and commit.`,
messages: ctx.memory.last(50),
});
await ctx.fs.writeFiles(diff.files);
await ctx.shell.run('npm test');
await ctx.shell.run('git add . && git commit -m "feat: automated patch"');
await ctx.git.push(branch);
await ctx.github.pullRequest({
title: 'Automated patch',
body: diff.changelog,
branch,
});
return `PR opened from ${branch}`;
});
Why not let Claude run git itself?
I tried. It’s flaky. The built-in ctx.git helper wraps the GitHub API and handles auth token rotation, rate limits, and the SSH key OpenClaw already knows. Less surface, fewer headaches.
Step 5 — Connect your messaging channel for remote triggers
I travel with a minimalist iPhone. No SSH, no laptop. Triggering builds over iMessage is the killer feature.
- Under Integrations → Channels enable iMessage (or WhatsApp, Telegram… the syntax is identical).
- Define a command in
message-commands.yaml:
commands:
- name: "/build "
flow: "build-and-pr"
parameters:
repo: string
ticket: string
Now text the bot:
/build github.com/org/app TKT-4321
The gateway turns that into a flow.run() call with parameters.
Step 6 — Monitor progress without tailing logs
Two options:
1. Streaming updates to chat
Add this to the flow after each long-running step:
await ctx.chat.reply(`✅ Tests passed (${elapsed}s)`);
For Claude output specifically, enable function onToken:
await ctx.llm.generate({
provider: 'anthropic',
model: 'claude-code',
messages,
onToken: (token) => ctx.chat.stream(token),
});
2. The Gateway dashboard
openclaw gateway ui launches localhost:5314 (or your ClawCloud subdomain). The Sessions tab now shows Anthropic latency, token counts, and cost in real time.
Pro tip: toggle only show paid calls to see if Claude is silently failing and falling back to the 8K free completions bucket.
Step 7 — Automatic pull request creation and reviewer tagging
The earlier flow pushes and opens a PR, but you probably want reviewers assigned. Append:
await ctx.github.request('POST /repos/{owner}/{repo}/pulls/{pull_number}/requested_reviewers', {
owner: 'acme',
repo: 'web',
pull_number: pr.number,
reviewers: ['lead-dev'],
});
GitHub token comes from the GITHUB_TOKEN secret you already used for the gateway’s GitHub integration. No extra work.
Handling large PRs
Claude sometimes dumps >800 lines. GitHub will refuse to render the diff in review UI. Force split commits:
if (diff.size > 500) {
await ctx.git.reset('--soft', 'HEAD~1');
await ctx.shell.run('git add -p');
await ctx.git.commit('chore: split huge diff');
await ctx.git.push(branch);
}
Yes, this looks gnarly but it keeps the PR readable.
Step 8 — Security snapshots and audit trail
You just gave a cloud LLM write access to your repo. Trade-offs exist.
- OpenClaw logs every Claude request with
hash(requestBody)— not the raw text — unless you flipLOG_SENSITIVE=1. - Anthropic retains prompts for 30 days for abuse detection. If that worries you, create a second repo mirror and cherry-pick after human review.
- Use GitHub’s CODEOWNERS so any PR from
bot-buildrequires one approval. - Rotate your Anthropic key monthly. I run
aws events put-rule+ Lambda to auto-create a new key and update Claw secret.
Step 9 — Debugging the inevitable issues
"model not found"
The Anthropic SDK emits this when the model ID is wrong. Run:
npx anthropic models list | grep code
Stuck at "awaiting input"
Means your flow forgot to reply to the system message. Claude acts like it’s in a chat unless you set system properly or supply a final user message.
Half-written files
Claude sometimes drops a file footer. Guard with SHA-256:
for (f of diff.files) {
const before = await ctx.fs.sha(f.path);
await ctx.fs.writeFile(f.path, f.content);
const after = await ctx.fs.sha(f.path);
if (before === after) throw new Error('write failed');
}
Real-world run: shipping a hotfix from a hiking trail
Last Saturday I was 12 km into the Vienna Woods when an on-call alert pinged. Null pointer in our Node service. No laptop, 3G only.
- Telegram to bot:
/build github.com/pspdfkit/parse-json HOT-634 - Bot replied Parsing repo... Claude context: 48 948 tokens
- 13 min later: Tests passed (219 of 219). Pushing branch auto/1718871483723
- PR link popped up. I skimmed the diff on phone — legit.
- Tagged my colleague to merge. Outage closed before I left the forest.
Total cost: $0.06 Claude + $0.005 ClawCloud compute. Cheaper than the bus ticket back.
This isn’t magic. Claude still hallucinates. I still squash and rebase. But for mechanical patches it’s quicker than remote-desktoping into the office Mac mini.
Takeaway and next steps
If you want autonomous coding that survives 200K-token prompts, Claude Code is the current sweet spot. Wire it into OpenClaw once, lock down the key, and you can ship from a train, trail, or couch. Next stop: set up scheduled weekend refactors with ctx.cron() so Monday morning starts with fresh PRs.