Troubleshoot LLM API errors and rate limits

  • API debugging
  • HTTP status codes
  • Rate limits

You paste a fresh API key into Codex, Claude Code, or OpenCode, hit Enter, and get a wall of red text back. Before tweaking five config files at once, read the HTTP status code and error.message—almost every API failure falls into one of a few predictable buckets.

Isolate the issue with a raw curl request

Send the smallest possible curl -i request first so you can see the raw status code and headers:

curl -i https://api.example.com/v1/chat/completions \
  -H "Authorization: Bearer $AI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model":"your-model-id","messages":[{"role":"user","content":"ping"}]}'
  • If curl fails too: the problem is your API key, base URL, model ID, or the provider itself.
  • If curl works: the problem is your coding tool’s protocol, inherited environment variables, or extra parameters.

Common HTTP status codes

Code Meaning Usual culprit
400 Bad Request Misspelled model ID, unsupported parameter, or context limit exceeded
401 Unauthorized Bad/expired key, conflicting auth env vars, or terms not accepted yet
403 Forbidden IP/region block, account flag, or no access to that model
404 Not Found Wrong /v1 suffix or unsupported endpoint (e.g. Responses vs. Chat Completions)
429 Too Many Requests RPM/TPM rate limit hit or free quota exhausted
500–503 Server Error Upstream overload, timeout, or maintenance

401 Unauthorized

Check for stray whitespace or quotes when copying the key, and verify in the console that your account doesn’t require accepting model terms first. For Claude Code, a common trap is leaving both ANTHROPIC_API_KEY and ANTHROPIC_AUTH_TOKEN set in the same shell, which can conflict and trigger 401 errors. Run unset ANTHROPIC_API_KEY before launching.

404 Not Found

Almost always a /v1 path mismatch or protocol mismatch. Some clients append /chat/completions (so your base URL needs /v1), while others append /v1/chat/completions (so adding /v1 turns into /v1/v1/chat/completions). Claude Code appends /v1/messages automatically, so ANTHROPIC_BASE_URL shouldn’t end in /v1. Codex CLI hits /v1/responses by default, which returns 404 on gateways that only proxy /v1/chat/completions. Run curl -v to inspect the exact URL path being requested (redacting the auth header before sharing logs).

429 Too Many Requests

Read the error body to tell rate limits apart from empty balances:

  • Rate limit (Rate limit reached or Retry-After header): lower concurrency, add exponential backoff (2s → 4s → 8s), or trim large files from the prompt to stay under TPM caps.
  • Out of credit (insufficient_quota, credit balance is too low, daily limit reached): retrying won’t help—wait for the reset or switch to a backup provider.

400 Bad Request

Double-check the exact model slug in the provider’s docs or /v1/models response—different providers use different names and date suffixes for the same weights. If the message says context_length_exceeded, start a fresh session (/clear), compact history (/compact), or ignore build folders and lockfiles. If it complains about reasoning_effort, beta headers, or max_completion_tokens, disable unsupported client flags (for example, export CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS=1 in Claude Code).

403 Forbidden

Usually a regional IP block or a WAF rejecting default client headers (such as a bare curl User-Agent returning a Cloudflare HTML page instead of JSON). Check the provider’s supported regions and access docs.

Interrupted streams and broken tool calls

If a request starts streaming and dies mid-answer—or fails with invalid tool call arguments—look for two causes:

  1. Upstream timeout (504 or dropped SSE stream): overloaded free nodes often hit a 60-second gateway timeout during long reasoning or slow time-to-first-token (TTFT).
  2. Output truncation (finish_reason: length): the response hit max_tokens halfway through emitting a JSON tool call, breaking the JSON parser. Split large edits into smaller per-file steps.

Free tiers fluctuate—traffic spikes and unannounced maintenance happen. Keep one or two backup providers tested from the free AI offers directory so you can switch immediately instead of debugging mid-task.