Claude Code · Troubleshooting
Claude Code API Error 401: invalid API key fixes
A 401 means the API received your request but could not authenticate it. The cause is almost always the key itself or which variable Claude Code is reading it from.
Last checked
The error text
API Error: 401 {"type":"error","error":{"type":"authentication_error","message":"..."}}Step-by-step fix
1. Copy the key again
Copy it fresh from the API Keys page and check for spaces or line breaks. Make sure the key was not deleted.
2. Use the right variable
ANTHROPIC_AUTH_TOKEN holds the aicodewith key; ANTHROPIC_API_KEY stays empty.
~/.claude/settings.json{ "env": { "ANTHROPIC_AUTH_TOKEN": "YOUR_API_KEY", "ANTHROPIC_API_KEY": "", "ANTHROPIC_BASE_URL": "https://api.aicodewith.ai" } }3. Look for leftovers
Run echo $ANTHROPIC_API_KEY and echo $ANTHROPIC_AUTH_TOKEN in a new terminal. Remove old exports from ~/.zshrc or ~/.bashrc.
4. Restart and check
Close every Claude Code session, start it again and run /status.
Other causes
| Symptom | Cause | Fix |
|---|---|---|
| 401 right after setup | Key pasted into ANTHROPIC_API_KEY | Move it to ANTHROPIC_AUTH_TOKEN |
| Worked yesterday, 401 today | Key deleted or rotated | Create a new key and update settings.json |
| 401 only in one terminal | That shell exports an old key | Unset it or open a clean terminal |
| Asked to /login instead | No key loaded; Claude Code is on the plan login | Check the settings.json path and JSON syntax |
Still failing?
- Check the model's page on aicodewith: it shows recent health for each channel.
- Look up the request in your dashboard usage records to see its model, status and cost.
- Email support@aicodewith.com with the time of the error, the model and the error text (never your full API key).
FAQ
What does Claude Code error 401 mean?
The request could not be authenticated: the key is wrong, deleted, or sent through the wrong variable.
Should I use ANTHROPIC_API_KEY or ANTHROPIC_AUTH_TOKEN?
Put the aicodewith key in ANTHROPIC_AUTH_TOKEN and set ANTHROPIC_API_KEY to an empty string.
Why does it work in one terminal but not another?
One shell probably still exports an old ANTHROPIC_API_KEY. Run echo $ANTHROPIC_API_KEY to check.
Can a low balance cause 401?
Balance problems show their own insufficient-balance error. Top up if the message mentions balance; otherwise check the key.
Is my key safe to share for support?
No. Never send the full key. Share the time of the error and the model instead.
Ready to try it?
Billing is per token from a prepaid balance. Rate and concurrency limits apply, and you can set daily, monthly or total spend caps on each API key.
Related
Sources
Claude and Claude Code are trademarks of Anthropic PBC. aicodewith is an independent platform and is not affiliated with or endorsed by Anthropic. Error formats are quoted from Claude Code and may change. Checked 2026-10-10.