A 401 here is about your credential or your base URL, never your model — a model problem returns 404 with a message naming the model. That single distinction resolves most of these in one curl.
The error
API Error: 401 {"type":"error","error":{"type":"authentication_error","message":"Missing or invalid API key"}}Causes and fixes at a glance
| Cause | Fix |
|---|---|
| ANTHROPIC_API_KEY set where ANTHROPIC_AUTH_TOKEN was meant | Use AUTH_TOKEN for a third-party base URL; API_KEY takes a one-time approval prompt. |
| Base URL carrying a /v1 path | Set the origin only — Claude Code appends /v1/messages itself. |
| Key revoked, or the account suspended | Both answer 401, never 403. Mint a fresh key and check the account. |
| A model the Messages endpoint does not serve | That returns 404 naming the model, not 401 — so it is a different fix. |
Print the three variables and check the base URL has no path
The most common cause is visible here. The base URL must be an origin — no /v1, no trailing path — because the client appends the endpoint itself. A base URL ending in /v1 produces a request to /v1/v1/messages.
env | grep -E '^ANTHROPIC_(BASE_URL|AUTH_TOKEN|API_KEY|MODEL)='
# Right: https://api.kunavo.com
# Wrong: https://api.kunavo.com/v1Call the endpoint directly, both ways
Kunavo's /v1/messages accepts the credential as either x-api-key or Authorization: Bearer, which is why Claude Code needs no plugin or proxy in front of it. If curl succeeds and the CLI does not, the problem is in your shell environment rather than on the server.
curl -s https://api.kunavo.com/v1/messages -H "x-api-key: $ANTHROPIC_AUTH_TOKEN" -H "anthropic-version: 2023-06-01" -H "content-type: application/json" -d '{"model":"claude-sonnet-5","max_tokens":8,
"messages":[{"role":"user","content":"hi"}]}'
# Same call, other header style — both are accepted:
# -H "Authorization: Bearer $ANTHROPIC_AUTH_TOKEN"Unset the variable you are not using
If both ANTHROPIC_API_KEY and ANTHROPIC_AUTH_TOKEN are set, the wrong one can win. Unset ANTHROPIC_API_KEY, start a new shell, and try again — a stale export in a shell profile outlives every other fix you try.
If the status is 404, stop debugging the key
A 404 whose message names the model means the credential was accepted and the model string was not. Fix ANTHROPIC_MODEL instead; nothing about the key is wrong. On a fresh setup the cause can be that nothing pins the model: Claude Code then sends its built-in default, the newest Opus, which Kunavo may not serve yet; its aliases follow Anthropic's newest models the same way, and for haiku, versions before 2.1.293 send a dated Haiku 4.5 id, which Kunavo does not serve — set ANTHROPIC_MODEL, ANTHROPIC_DEFAULT_OPUS_MODEL, ANTHROPIC_DEFAULT_SONNET_MODEL and ANTHROPIC_DEFAULT_HAIKU_MODEL to ids from GET /v1/models.
“Remote managed settings failed to load (authentication rejected (401))” is a different 401
This line is a warning about your organization's settings, not a failed model request. Claude Code fetches server-managed settings from api.anthropic.com directly, never through ANTHROPIC_BASE_URL; “authentication rejected (401)” means Anthropic refused the credential that fetch used, and “no remote policy applied” means the session runs without those settings because none were cached (https://code.claude.com/docs/en/errors, read October 22, 2026). Behind a gateway, the cause is Claude Code's request to Anthropic for the organization's policy being refused under the gateway's key. Claude Code 2.1.299, published on npm on October 14, 2026, has the two changelog entries quoted below about gateway keys reaching Anthropic: one lists feature flag, startup, fast mode, profile and feedback requests, and the other covers the server-managed settings request when a managed settings file sets ANTHROPIC_BASE_URL. Neither entry mentions a base URL set any other way; Anthropic's server-managed settings page says that exporting a non-default ANTHROPIC_BASE_URL in your shell skips the fetch. Check the version with claude --version, update if it is older than 2.1.299, and if your organization does not use server-managed settings, the warning does not affect the requests your session sends to the gateway. The words in the parentheses change with the cause, and Anthropic gives network error, request timed out and authentication rejected (401) as examples; reports also show access denied (403) and unexpected error. The end of the line says what the session runs on: “using cached policy” means the settings from the last successful fetch, and “no remote policy applied” means none. For a network cause, check that the machine can reach api.anthropic.com; for a 401 or 403, check your sign-in with /status (https://code.claude.com/docs/en/errors#remote-managed-settings-failed-to-load, read November 13, 2026).
- Fixed the server-managed settings request to Anthropic carrying a third-party gateway's API key when a managed settings file sets `ANTHROPIC_BASE_URL`
- Fixed feature flag, startup, fast mode, profile and feedback requests to Anthropic carrying the API key configured for a third-party gateway (`ANTHROPIC_BASE_URL`); Anthropic API keys are still sentIf you’re calling through Kunavo
A 401 from Kunavo narrows to five causes: no key arrived in either Authorization: Bearer or x-api-key, the key does not carry the sk-kn- prefix, it is an sk-kn- key Kunavo never issued (a typo or a truncated paste), it has been revoked, or the account is suspended. None of them answers 403, so the status code alone tells you which family you are in — and a valid key aimed at a model the Messages endpoint does not serve returns 404 with the model named in the message, not 401. That is the whole diagnostic tree.
Frequently asked questions
Why does my key work in curl but not in Claude Code?
Almost always a second variable set in a shell profile, or a base URL with a path on it. The endpoint accepts both header styles, so the difference is not the header.
ANTHROPIC_AUTH_TOKEN or ANTHROPIC_API_KEY?
AUTH_TOKEN for a third-party base URL — it is used immediately. API_KEY triggers a one-time approval prompt first, which is often mistaken for a failure.
Does a Claude subscription cover a custom base URL?
No. A subscription authenticates to the vendor's own endpoint; pointing the CLI elsewhere means that endpoint's credential, billed by that endpoint.
Does “Remote managed settings failed to load (authentication rejected (401))” mean my gateway key is wrong?
No. That fetch goes to api.anthropic.com, not to your gateway, so it says nothing about the key your model requests use. If Claude Code answers your prompts and the line ends with “no remote policy applied”, the session is simply running without your organization's server-managed settings, which matters only if your organization uses them.
What about “Remote managed settings failed to load (access denied (403))” or “(unexpected error)”?
It is the same warning with a different cause in the parentheses, and it concerns Claude Code's fetch of your organization's server-managed settings from api.anthropic.com, not the requests your session sends to the gateway. For a 401 or 403, check your sign-in with /status; for network error or request timed out, check that the machine can reach api.anthropic.com. If the line ends with “using cached policy”, the session runs on the last settings it fetched; if it ends with “no remote policy applied”, it runs without them.
Related guides
- Claude API 401 authentication_error / invalid x-api-key — every cause
- ANTHROPIC_AUTH_TOKEN vs ANTHROPIC_API_KEY — which one Claude Code actually reads
- Install Claude Code — the command for every OS, first login, and the errors people hit
- “Your organization has disabled Claude subscription access for Claude Code” — the three causes and what works
More error semantics live in the error reference; getting a key takes a minute via signing up and the authentication guide.