Dev.to AI 🤖 Ai 👁 0 📖 4 min read

A map of API errors that do not mean what you think: 401, 402, 404, 429

Seven HTTP status codes cover almost every "my agent stopped working" report. Three of them are routinely misread, and one of those misreads sends people to edit the wrong file for an entire evening. This map is written

Seven HTTP status codes cover almost every "my agent stopped working" report. Three of them are routinely misread, and one of those misreads sends people to edit the wrong file for an entire evening.

This map is written from the gateway side. The examples below are the actual responses one endpoint returned when probed on 2026-12-01 - not paraphrases.

The four layers, and the status each one produces

Layer Typical status Distinguishing detail
Path 404 plain text body is 404 page not found, no JSON
HTTP method 404 plain text same body: a GET route hit with POST looks identical
Auth 401 with JSON {"code":"INVALID_API_KEY", ...}
Balance 402 request parsed, nothing to spend
Model name 404 with JSON route exists; the name is not available to that key
Request body 400 parsed route, rejected shape
Rate 429 retryable, but only this one is

1. Two different 404s, and why the distinction matters

GET  /v1/models            401  {"code":"INVALID_API_KEY","message":"Invalid API key"}
POST /v1/models            404  404 page not found
POST /v1/v1/chat/completions   404  404 page not found

The first line proves the route exists: an auth layer answered. The second and third return the router's plain-text 404, because the verb was wrong in one case and the path was wrong in the other.

That is the trap: a wrong method and a wrong path are indistinguishable from the response alone. I hit it myself - I probed a /v1/models endpoint with curl -d, which sends POST, concluded "this gateway has no model discovery", and nearly published that.

Rule: before concluding a route is missing, confirm the method. GET for discovery, POST for completions.

The other 404 carries JSON:

404 {"error":{"message":"model not available in your group"}}

Same code, different meaning: the route works, the model name does not exist for that key. Fix is a model name, not a URL.

2. 401 is the good news

401 with a machine-readable body means the route exists, the auth layer is in front of it, and it rejected you for exactly one reason: the key.

If you see 401 and your key is definitely right, check the group. Many gateways scope a key to a group, and a key created without one authenticates but can call nothing - some implementations return 401 there, others 403.

3. 402 is not a rate limit

402 Payment Required means the request is valid and the account cannot pay for it. It arrives after auth, which is why it feels like a sudden failure: yesterday it worked, today the balance is zero.

Two things make it appear out of nowhere:

  • a client that omits max_tokens inherits a very large default, and reasoning tokens are billed even when the visible content comes back empty;
  • retry loops re-bill every attempt, so one bad request can cost dozens.

If a 402 arrives after a long run of failures, look at the retry count before you look at the price.

4. 400 is a body-shape problem, and the message rarely names the field

Two shapes break agents against non-OpenAI backends constantly:

  • role: "developer" sent by newer SDKs, where the backend only knows role: "system";
  • max_completion_tokens sent where the backend only accepts max_tokens.

Neither error names the offending field, which is why this class of bug reads as "the endpoint is broken".

5. 429 is the only code worth retrying blindly

429 means "come back later" and is safe to retry with backoff. Everything above is deterministic: retrying a 400, 401, 402 or 404 produces the same answer and, for 402, a bigger bill.

That is the single most useful rule in this post: only 429 and 5xx are retryable. 4xx is not.

The ten-second preflight

Run these three before touching any client config:

BASE=https://api.lbase.com
KEY=sk-your-key

curl -s -o /dev/null -w "discovery:%{http_code}\n" -H "Authorization: Bearer $KEY" "$BASE/v1/models"

curl -s -o /dev/null -w "chat:%{http_code}\n" "$BASE/v1/chat/completions" \
  -H "Authorization: Bearer $KEY" -H "Content-Type: application/json" \
  -d '{"model":"deepseek-flash","messages":[{"role":"user","content":"hi"}],"max_tokens":8}'

curl -s -o /dev/null -w "messages:%{http_code}\n" "$BASE/v1/messages" \
  -H "x-api-key: $KEY" -H "anthropic-version: 2023-06-01" -H "Content-Type: application/json" \
  -d '{"model":"deepseek-flash","max_tokens":8,"messages":[{"role":"user","content":"hi"}]}'

Three numbers, three layers. discovery tests routing and auth; chat tests the OpenAI path end to end; messages tells you whether the Anthropic format is served at all. A client that needs Anthropic format but gets a plain-text 404 here will never work, no matter how the config is edited.

The per-agent field list (nine tools, same three values) is at https://novaapi.lbase.com/agents.html - model names belong to their owners; the endpoint above is an independent gateway.

📰 Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.