401 Invalid Authentication - Claude Code
Quick fix
Check the API key and authentication variables required by your provider, and confirm Claude Code's actual authentication field contains the correct key.
Symptom
Section titled “Symptom”API Error: 401 {"error":{"type":"authentication_error","message":"Invalid Authentication"},"type":"error"}These issues all show 401 authentication_error, but the causes differ. Reports involving Kimi For Coding describe mismatched authentication variables or fields, such as using `ANTHROPIC_API_KEY` instead of `ANTHROPIC_AUTH_TOKEN`, or entering the key only in the top-level `apiKey` while `env.ANTHROPIC_AUTH_TOKEN` remains empty. Reports involving MiniMax and OpenCode Zen describe Coding Plan keys, provider regions, or request URL configuration. Some users report that the CC Switch connectivity check succeeds while requests from Claude Code still return 401.
Issue #1046 reports that the native Claude Code app did not read `~/.claude/settings.json`, but the maintainer said they could not reproduce this on the latest version and suggested referring to issue #404. The available material does not provide one fix for all these cases; first check the key type, authentication field, and request URL for the provider you use.
Check the API key type and authentication field required by your provider. A Kimi For Coding user reported resolving 401 by using `ANTHROPIC_API_KEY`; another report found that Claude Code actually reads `env.ANTHROPIC_AUTH_TOKEN` and authentication fails when that field is empty.
If your Kimi For Coding configuration has both a top-level `apiKey` and an empty `env.ANTHROPIC_AUTH_TOKEN`, enter the key in `env.ANTHROPIC_AUTH_TOKEN`, or delete the old configuration and recreate it using the built-in preset. Then restart Claude Code.
// ~/.claude/settings.json{"env": {"ANTHROPIC_AUTH_TOKEN": "<你的 API Key>"}}MiniMax users confirmed using the Coding Plan key associated with their subscription and checking the provider configuration for the selected region. Issue comments include one user who resolved the problem by selecting the MiniMax en preset and another who did so by adjusting the request URL.
OpenCode Zen users reported resolving the issue by enabling the proxy and configuring the request URL to match the API format. The OpenAI Chat Completions format example must include `/v1/chat/completions`.
Affected Versions
Section titled “Affected Versions”Source Issues
Section titled “Source Issues”This page is distilled from 7 real issues
- #185kimi的ANTHROPIC_BASE_URL不是https://api.moonshot.cn/anthropic
- #219在claude code中配置了kimi-for-coding 对应信息提示401
- #544Macos minimax not working
- #879Windows 11 Claude 自定义配置 OpenCode Zen 的免费模型,报错。
- #890添加MiniMax报错401。API Error: 401{"type":"error","error":{"type":"authentication_error","message":"invalid api key"},"request_id":""} · Please run /login
- #1046[Bug] cc-switch 配置不生效于原生版 Claude Code (v2.1.42+)
- #2525[Bug] Kimi For Coding preset writes API key to apiKey field but leaves env.ANTHROPIC_AUTH_TOKEN empty, causing 401 in Claude Code
- Why does Claude Code still return 401 when the CC Switch speed test succeeds?
- In issue #2525, the CC Switch speed test succeeded using the top-level `apiKey`, but `env.ANTHROPIC_AUTH_TOKEN`, which Claude Code reads, was empty, so requests still returned 401.
- Which authentication variable should Kimi For Coding use?
- A user in issue #219 reported resolving 401 by switching to `ANTHROPIC_API_KEY`; issue #2525 says that the Claude Code configuration reads the key from `env.ANTHROPIC_AUTH_TOKEN`. Check that the authentication field in your actual configuration has a value.
- Which API key should MiniMax use?
- Issue comments recommend using a Coding Plan key; one user noted that subscribers to the English site should select the MiniMax en preset. Reports differ, so check that the key matches the selected preset and region.
- What should I do if the native Claude Code app does not work?
- Issue #1046 reports that the native app may not read `~/.claude/settings.json`, but the maintainer said they could not reproduce this on the latest version and suggested referring to issue #404. That issue does not provide a general fix that can be confirmed here.
Related problems
Section titled “Related problems”This is an unofficial community wiki with no affiliation to the cc-switch authors or the project itself. Its content is compiled from the project's public GitHub issues. This site distributes no software.