API Error: 401 authentication_error - cc-switch
Quick fix
Check the API key field, Base URL, key type, and cc-switch version, so Claude Code actually reads a valid credential.
Symptom
Section titled “Symptom”API Error: 401 {"error":{"type":"authentication_error","message":"unauthenticated"},"type":"error"}This 401 has no single cause: the credential cc-switch wrote is not being accepted by Claude Code or the third-party endpoint. The common mechanisms:
1. Kimi For Coding: an early config or preset may write the key into the wrong field. What Claude Code actually reads is env.ANTHROPIC_AUTH_TOKEN or the ANTHROPIC_API_KEY environment variable. If cc-switch only wrote a top-level apiKey, or defaulted to ANTHROPIC_AUTH_TOKEN while the server wants ANTHROPIC_API_KEY, an empty or wrong credential goes out and you get a 401. Users confirm recovering by using ANTHROPIC_API_KEY instead of ANTHROPIC_AUTH_TOKEN.
2. MiniMax: the domestic and international endpoints and their matching provider types must be kept straight. With an English-site coding plan you cannot pick plain MiniMax — pick MiniMax en. The API key must also be a coding plan key, not a freshly generated ordinary key. Some older problems are also fixed by upgrading cc-switch or adding hasCompletedOnboarding: true to ~/.claude.json.
3. Native Claude Code: one issue argues the native install from v2.1.42+ does not read ~/.claude/settings.json and only reads system environment variables, but maintainers could not reproduce it. So this cause is inconsistent across versions and environments.
4. OpenCode Zen / OpenCode Go: proxy mode has to be on, with the right path for the endpoint format. Anthropic Messages and OpenAI Chat Completions differ in URL, proxy mode, and auth field requirements. Some users solved it by using ANTHROPIC_API_KEY alone with the proxy on.
Work out which provider you are on first: Kimi For Coding, MiniMax / MiniMax en, OpenCode Zen / Go, or a custom Anthropic-compatible endpoint. The 401 fix differs per provider.
Kimi For Coding: delete the existing provider/profile and recreate it from scratch with the built-in cc-switch Kimi For Coding preset, making sure the API key goes into the UI API Key field.
If it still 401s after recreating, check ~/.claude/settings.json: env.ANTHROPIC_AUTH_TOKEN must be non-empty and match your Kimi API key. If there is a top-level apiKey while env.ANTHROPIC_AUTH_TOKEN is empty, sync them by hand or delete the redundant top-level apiKey / base_url fields.
~/.claude/settings.json {"env": {"ANTHROPIC_BASE_URL": "https://api.kimi.com/coding/","ANTHROPIC_AUTH_TOKEN": "sk-kimi-..."}}If the server wants ANTHROPIC_API_KEY rather than ANTHROPIC_AUTH_TOKEN, switch the config to ANTHROPIC_API_KEY. Several users confirm this resolves Kimi-related 401s.
~/.claude/settings.json {"env": {"ANTHROPIC_BASE_URL": "https://api.kimi.com/coding/","ANTHROPIC_API_KEY": "sk-kimi-..."}}MiniMax: confirm the API key is a coding plan key, not a freshly generated ordinary key. If you are on the English / international site coding plan, pick MiniMax en when adding the provider in cc-switch rather than plain MiniMax.
Pick the MiniMax domestic or international endpoint according to where your subscription came from. One user solved it by changing the request address to https://api.minimaxi.com/anthropic; international users may need the api.minimax.io equivalent.
cc-switch 供应商配置 请求地址: https://api.minimaxi.com/anthropicIf the issue mentions the onboarding problem in older cc-switch, upgrade cc-switch, or add hasCompletedOnboarding: true to ~/.claude.json. Maintainers confirmed this is the correct fix for some MiniMax 401s.
~/.claude.json {"hasCompletedOnboarding": true}OpenCode Zen / OpenCode Go: turn cc-switch proxy mode on and confirm the request address carries the right path. The OpenAI Chat Completions format usually needs /v1/chat/completions; some users succeeded using ANTHROPIC_API_KEY with the proxy on.
cc-switch 供应商配置 请求地址: https://opencode.ai/zen/go/v1/chat/completions代理模式: 开启环境变量: ANTHROPIC_API_KEY=你的 KeyIf Claude Code is a native install and you suspect settings.json is not applying, check whether stale ANTHROPIC_AUTH_TOKEN, ANTHROPIC_BASE_URL, or ANTHROPIC_MODEL variables linger in your system environment — environment variables may take priority over the config file. This conclusion is disputed in the issues, so treat it as one thing to check rather than the fix.
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
- The cc-switch connection test passes — why does Claude Code still 401?
- The cc-switch speed test may use the top-level apiKey or its own request logic, while Claude Code reads env.ANTHROPIC_AUTH_TOKEN, ANTHROPIC_API_KEY, or system environment variables. Check whether the fields Claude Code actually reads are empty or wrong in the real config file.
- Should Kimi For Coding use ANTHROPIC_AUTH_TOKEN or ANTHROPIC_API_KEY?
- Users in the issues confirm switching to ANTHROPIC_API_KEY instead of ANTHROPIC_AUTH_TOKEN resolved it, while another issue says to make sure env.ANTHROPIC_AUTH_TOKEN is non-empty. If the built-in preset fails, check both fields are in sync and non-empty, and try ANTHROPIC_API_KEY.
- MiniMax reports invalid api key but my key is definitely right.
- Confirm you are using a coding plan key rather than an ordinary API key. On the English / international site coding plan, pick MiniMax en as the provider type; domestic users may need the api.minimaxi.com endpoint.
- What does hasCompletedOnboarding: true do?
- For some MiniMax 401s, maintainers confirmed that upgrading cc-switch or adding hasCompletedOnboarding: true to ~/.claude.json is the fix. It generally bypasses an auth check caused by the Claude Code first-run onboarding / login state.
- Does native Claude Code ignore settings.json?
- One issue argues the native build from v2.1.42+ reads only system environment variables and not ~/.claude/settings.json, but maintainers could not reproduce it. If you use a native install and the config does not apply, also check whether system environment variables override the cc-switch config.
- Why does OpenCode Zen / Go require the proxy?
- Users report the OpenAI Chat Completions format needs cc-switch proxy mode on with a path such as /v1/chat/completions. Configuring only the native Anthropic format, or getting the path wrong, can keep producing 401 or 404.
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.