Claude Code Your apiKeyHelper script is failing
Claude Code ran your apiKeyHelper command and got no key back. Tell the three failure types apart with /status, then fix the script to print only the key.
Your apiKeyHelper script is failing · This usually means you need to re-authenticate with your provider · Run /status to see the script's error outputClaude Code ran the command in the apiKeyHelper setting of your settings.json and didn't get a key back. Without one, the request reaches the API with a placeholder credential and is rejected with 401. Running /login doesn't help: while the setting is present, the helper's output takes precedence over a saved login.
The official docs split the failure into three types. The Authentication panel in the terminal shows which one happened.
| Type | What the panel shows | Fix |
|---|---|---|
| The command exited with an error or timed out | Exit code and error output | Fix the script · sign in to your SSO or vault again if the session expired |
| The command printed nothing to stdout | The docs don't give the panel wording | Make the script print the key to stdout |
| The command printed something besides the key (login banner, log line) | returned output that cannot be used as an API key plus what's wrong (the output itself isn't repeated) | Print only the key |
[1] Run the configured command in your shell to reproduce the failure. Most of the time this alone shows where it breaks.
$ cat ~/.claude/settings.json$ ~/bin/get-gateway-key.sh; echo "exit=$?"The path above is an example. Use the value of your own apiKeyHelper. A non-zero exit code or empty output means the first or second type.
[2] Run /status inside Claude Code. If the apiKeyHelper row shows Failing with the last failure's exit code and error output, that also confirms apiKeyHelper is the active credential source. The row disappears after the next successful run.
[3] In non-interactive runs (claude -p and similar), look in stderr for lines starting with apiKeyHelper failed:. The docs say the specific reason follows that prefix.
$ claude -p "hello" 2>&1 | grep 'apiKeyHelper failed:'[4] Fix the script to match the cause. The docs give one rule: print only the key to stdout, as a single token of printable ASCII up to 16,384 characters, and exit with code 0. The docs' example reads a value from a vault and prints it as-is:
#!/bin/bashvault kv get -field=api_key secret/llm-gateway/claude-code{ "apiKeyHelper": "~/bin/get-gateway-key.sh"}If the script is new, make it executable:
$ chmod +x ~/bin/get-gateway-key.shIf your script prints progress text or debug output, send it to stderr instead of stdout. Per the docs, from v2.1.227 a banner or log line printed next to the key makes the helper fail. To check for invisible characters, run ~/bin/get-gateway-key.sh | od -c | head (works on macOS and Linux).
[5] If the cause is an expired session, fix the login side, not the script. When the command reports an expired session, sign in to your SSO or secrets vault again, then retry in Claude Code.
Retries and when the helper re-runs
The message doesn't appear on the first failure. Per the docs, Claude Code re-runs the script and retries the request up to two more times before showing it, so the failure surfaces within three attempts. Before v2.1.208, it spent the full retry budget resending the placeholder credential and then reported only a generic 401 authentication error.
| When the helper re-runs | Condition |
|---|---|
| The cache lifetime elapsed | Five minutes by default · change with CLAUDE_CODE_API_KEY_HELPER_TTL_MS (milliseconds) |
The API returns 401 or 403 | Directly or through an LLM gateway |
A gateway request returns 401 (not 403) | When CLAUDE_CODE_SKIP_VERTEX_AUTH or a similar skip variable is set · v2.1.318 or later |
| The cached JWT expired after the helper produced it | Checked before sending the request · v2.1.246 or later |
The last three rows apply only when the helper's output is the credential Claude Code sends and ANTHROPIC_AUTH_TOKEN isn't set.
Tell it apart from similar messages
| Message | Meaning | Post |
|---|---|---|
Your apiKeyHelper script is failing | The script didn't give a usable key | This post |
API Error: 401 OAuth token has expired | The saved login token expired | 401 OAuth token has expired |
Your organization's settings need an update. Ask your administrator to fix the API key helper configuration. | Problem with the key helper in managed settings | Ask your administrator |
| Any other error text | Claude Code error messages and fixes |
For gateway limit problems, see spend limit reached through a gateway.