Fix HTTP 401 Unauthorized / Invalid API Key (5 Hidden Causes)
Still getting 401 with a fresh API key? Check trailing whitespace, Bearer vs x-api-key header mismatch, stale env vars, and key spend limits.
Root Causes
Common Triggers
- Sending a Brioi API key to api.openai.com because base_url was not updated
- Mixing Authorization: Bearer with Anthropic’s x-api-key header on the wrong endpoint
- Hidden whitespace, line breaks, or stray quotes in .env files
- Per-key spend cap reached or key disabled in console
Long-Term Stability Path
Brioi lets teams issue isolated per-project keys and audit status and quota consumption in real time.
Step-by-Step Fix
- Step 1 · Confirm base_url matches your key providerAlways pair Brioi keys with https://vip.brioi.com/v1 (or https://vip.brioi.com for native Anthropic).
- Step 2 · Trim env strings & inspect active shellStrip accidental whitespace and confirm no global OPENAI_API_KEY overrides your .env.
- Step 3 · Check per-key quota statusVerify the key is enabled and below its configured spend limit in vip.brioi.com.
Working Reference Snippet
curl -i https://vip.brioi.com/v1/models \
-H "Authorization: Bearer ${BRIOI_API_KEY}"FAQ
Are failed 401 requests billed?
Never—requests rejected at authentication incur zero charges.
More Troubleshooting Guides
Diagnose whether your OpenAI HTTP 429 is caused by exhausted credits or RPM/TPM throttling, with backoff code and multi-route failover options.
Resolve Anthropic 403 region blocks and proxy-triggered account suspensions when running Claude API or Claude Code.
Troubleshoot terminal proxy mismatches, TLS timeouts, and ECONNRESET drops in Claude Code CLI with a clean custom gateway setup.
Why Claude prompt caching returns 0 cached tokens—check prefix instability, 5-minute TTL expiry, protocol conversion, and route quality.

