Anthropic API 401 — Missing, Expired, or Misconfigured Key
// When OpenClaw calls Claude and the gateway logs show HTTP 401 from api.anthropic.com, the model never runs — agents hang, heartbeats fail, and Discord/Telegram may show generic 'provider error' messages. Unlike 429 rate limits, 401 means authentication failed outright: no billing on the account, a revoked or typo'd key, wrong sk-ant- prefix, or the key living in the wrong config field while an stale ANTHROPIC_API_KEY env var overrides openclaw.json. This unofficial guide walks through verifying the key at console.anthropic.com, placing it correctly under providers in ~/.openclaw/openclaw.json, and resolving env-vs-config mismatches that trip even experienced self-hosters.
🔍 Is This Your Issue?
✅ Fix 1 — Verify Key Format & Anthropic Console
# Confirm key shape (never paste full key in public logs)
echo "$ANTHROPIC_API_KEY" | head -c 12
# Expected prefix: sk-ant-api
# Test key directly (replace with your key once)
curl -s https://api.anthropic.com/v1/messages \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{"model":"claude-sonnet-4-20250514","max_tokens":10,"messages":[{"role":"user","content":"hi"}]}'# In browser: console.anthropic.com # 1. Settings → Billing — payment method active # 2. Settings → API Keys — key status Active # 3. Regenerate if unsure; revoke old keys # Note last 4 chars of new key for matching in config
✅ Fix 2 — Set Provider Key in openclaw.json
// ~/.openclaw/openclaw.json — providers section
{
"providers": [
{
"id": "anthropic",
"type": "anthropic",
"apiKey": "sk-ant-api03-XXXXXXXX"
}
],
"agents": {
"defaults": {
"provider": "anthropic",
"model": "claude-sonnet-4-20250514"
}
}
}# Or reference env var instead of inline key # openclaw.json: # "apiKeyEnv": "ANTHROPIC_API_KEY" # ~/.openclaw/.env ANTHROPIC_API_KEY=sk-ant-api03-XXXXXXXX # Validate config openclaw doctor openclaw gateway restart
✅ Fix 3 — Resolve ANTHROPIC_API_KEY Env Override
# Find env overrides env | grep -i ANTHROPIC grep -r ANTHROPIC ~/.config/systemd/user/ 2>/dev/null # Compare with config file grep -i anthropic ~/.openclaw/openclaw.json grep ANTHROPIC ~/.openclaw/.env 2>/dev/null
# Remove stale override from systemd (example) # Edit ~/.config/systemd/user/openclaw-gateway.service # Delete: Environment=ANTHROPIC_API_KEY=old-value systemctl --user daemon-reload systemctl --user restart openclaw-gateway # Confirm gateway sees correct key openclaw gateway status
Store Anthropic keys in either ~/.openclaw/.env OR openclaw.json providers — not both with different values. Env vars override config silently. After any change, restart the gateway and run a single test message before re-enabling heartbeats.
- • Create keys at console.anthropic.com with billing enabled — free-tier keys without credits often 401 immediately
- • Use sk-ant-api03- prefix keys; legacy formats or OpenAI-style sk- keys will never work with Anthropic endpoints
- • Keep ANTHROPIC_API_KEY out of systemd unit files unless you update them every rotation
- • Run `openclaw doctor` after editing providers — it surfaces missing keys before agents hit production channels
- • Never paste live keys into GitHub issues; rotate if accidentally exposed