HTTP and Status Codes
Correct status codes for success, client errors, rate limits, and upstream failures.
In plain words
A status code is a short number at the start of a reply that says whether the request worked, whether you made a mistake or whether the server had a problem.
Think of it like…
Traffic signals for machines. Green (2xx) means proceed, a red "fix your request" sign (4xx) means don't try again unchanged and an amber "road works ahead, try later" sign (5xx, 429 with Retry-After) tells the driver to wait and retry.
Where you’ve seen it
A chat API maps outcomes like this: a valid message returns 200; a missing or expired token returns 401; a valid token without access to that conversation returns 403; an unknown conversation returns 404; malformed JSON returns 400; JSON that parses but fails validation (temperature of 7) returns 422 with field details; a 2 MB prompt over the 1 MB limit returns 413; a repeated idempotency key with a different body returns 409; the caller exceeding their own quota returns 429 with Retry-After: 12; the model provider being throttled for everyone returns 503 with Retry-After: 5; a provider call exceeding the 20-second budget returns 504; an unexpected exception returns 500. SDKs retry 429, 503 and 504 (honouring Retry-After and adding jitter) and never retry 400, 401, 403, 404 or 422. For availability, the SLO counts only 5xx: of 100,000 daily requests, 2,000 are 4xx and 150 are 5xx, so availability is 1 − 150 / 100,000 = 99.85%; counting the 4xx as failures would wrongly report 97.85% and send the team chasing client mistakes. Streaming adds a trap: once a 200 and the first event have been sent the status cannot change, so a later failure is delivered as an error event and the stream closes, and the dashboards record it from that event, not from the HTTP status. Every error body repeats the request_id that also appears in the X-Request-Id header and in logs.
Words you’ll hear
- 4xx vs 5xx
- Client-side faults versus server or upstream faults.
- Retry-After
- A header telling the client how long to wait before retrying.
- 422 Unprocessable Content
- The request was well-formed but failed validation.
- Retryable
- A failure that may succeed if repeated, such as 429, 503 and 504.
Visual diagram
Theory & deep dive
Choosing the right code
Success: 200 for a body, 201 for a created resource (with a Location header), 202 for accepted-but-not-finished work and 204 for success with no body. Client errors: 400 for unparseable or structurally wrong requests, 401 when authentication is missing or invalid, 403 when the caller is known but not allowed, 404 for unknown resources (also used to avoid revealing that a forbidden resource exists), 409 for conflicts such as a stale version or an idempotency-key reuse with a different body, 413 for oversize payloads, 415 for an unsupported content type, 422 for validation failures that carry field details and 429 for rate limiting. Server errors: 500 for unexpected faults, 502 when an upstream returns something invalid, 503 for temporary unavailability or overload (with Retry-After) and 504 when an upstream times out.
AI-specific mappings need a policy. A provider 429 is *your* capacity problem, not the caller's, so return 503 with a Retry-After and your own error code (provider_overloaded) rather than passing a confusing 429 through. A provider 400 that says the context is too long is a caller-facing 422 (or 400) with a precise code and the limit. A safety refusal is a successful call with a refusal payload or a 200 with a finish_reason, not an error. Timeouts you enforce yourself are 504. Keep a table of these mappings in the API documentation so SDK authors and clients can rely on them.
Provider returns 429 because your account is throttled → your API returns 503, code provider_overloaded, Retry-After: 5. Provider returns 400 context_length_exceeded → your API returns 422, code context_too_long, with max_tokens: 128000. Provider takes 25 s against a 20 s budget → 504, code upstream_timeout.
Retries, headers and measuring availability
Retry policy follows the code: retry 429, 503 and 504 (and connection resets) for idempotent requests or requests with idempotency keys, using exponential backoff with jitter and honouring Retry-After when present; never retry 400, 401, 403, 404, 409 or 422 unchanged, because repeating them cannot help. 500 is ambiguous: retry a bounded number of times for safe methods and treat it as a bug to fix. Useful headers: Retry-After (seconds or a date), X-Request-Id for correlation, Content-Type: application/json, Cache-Control and ETag for cacheable GETs, and rate-limit headers (limit, remaining, reset) so clients can pace themselves before hitting a 429.
Measurement: define availability from 5xx and timeouts, not from 4xx, which reflect caller mistakes. With 100,000 requests, 2,000 4xx and 150 5xx the availability is 99.85%; counting 4xx would show 97.85% and mislead. Still watch 4xx rates, since a spike in 401 or 422 after a release signals a broken SDK or contract change. For streaming responses the HTTP status is fixed when the headers are sent, so a failure mid-stream must be emitted as an in-band error event followed by a close, and your metrics should classify the request as failed based on that event. Return the same request_id in headers and bodies so a user report can be traced in seconds.
100,000 requests: 97,850 2xx, 2,000 4xx, 150 5xx. Availability by 5xx = (100,000 − 150) / 100,000 = 99.85%. Counting 4xx as errors: (100,000 − 2,150) / 100,000 = 97.85%. Only the first reflects the service's reliability.
How it works, step by step
Step 1 of 41. Write the mapping table
List every outcome (validation, auth, quota, provider failure, timeout, refusal) with its status, code and retryability.
- Publish
- in the docs
When to use it & trade-offs
What it is
HTTP responses begin with a status code that tells the caller what happened and what to do next. 2xx means success (200 OK, 201 Created, 202 Accepted, 204 No Content), 4xx means the client's request is at fault (400, 401, 403, 404, 409, 413, 422, 429) and 5xx means the server or an upstream failed (500, 502, 503, 504). Headers carry the rest of the contract: Content-Type, Authorization, Retry-After, ETag, Cache-Control and a correlation header such as X-Request-Id. For an AI backend the codes matter because the right one decides whether a client retries, whether an alert fires and whether the failure counts against your availability objective.
Why it matters: Clients, SDKs, gateways and monitors all branch on status codes. A 500 for a bad prompt makes mobile apps retry forever, a 200 carrying an error message hides failures from your dashboards and a 400 for a rate limit stops well-behaved clients from backing off. AI services add awkward cases: provider throttling, context-length errors, safety refusals, timeouts and streams that fail after the headers are already sent, and each needs a deliberate mapping.
When to reach for it
- A documented mapping from outcomes to codes
- Retry-After on 429 and 503 responses
- Request IDs in headers, bodies and logs
When to avoid or be careful
- Returning 200 with an error flag in the JSON
- Passing provider status codes straight through
- Counting client 4xx errors against availability
Similar-looking codes
Strengths: Clients can tell parse errors from field errors.
Watch out: Some APIs use 400 for both; document the choice.
Strengths: Tells clients whether to log in or request access.
Watch out: 404 is sometimes better for hiding resources.
Strengths: Separates caller behaviour from capacity problems.
Watch out: Both use Retry-After; keep error codes distinct.
Interview signals & numbers to know
When this topic comes up
- “Which status code would you return here?”
- 401 versus 403
- 429 versus 503 for provider throttling
- What to retry and what not to
- Errors in streaming responses
Benchmarks & rules of thumb
- 99.85%
- availability with 150 5xx in 100,000 requests
- 97.85%
- the misleading figure if 4xx are counted
- 429 · 503 · 504
- the usual retryable statuses
Common mistakes & follow-ups
Common interview mistakes
- Returning 500 for validation problems.
- Retrying 4xx errors that cannot succeed.
- Ignoring Retry-After and hammering a throttled service.
Likely interviewer follow-ups
- How do you report a failure that happens mid-stream?
- Why not pass the provider's 429 through?
- Which codes should an SDK retry?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply HTTP and Status Codes with generative AI interview Q&A and hands-on practice.