Area 08 · Backend · Concept 02 of 20

HTTP and Status Codes

How requests and responses work, and what 200, 201, 400, 401, 404, 429 and 500 mean.

01–10 · The basePhase 0 + 5 · Weeks 1–2, 18–20≈ 45 min

The idea

Every HTTP request has a method, a path, headers (metadata like Content-Type and Authorization) and an optional body. Every response has a status code, headers and a body. The status code is the machine-readable summary of what happened.

Codes to know: 200 OK, 201 Created, 204 No Content; 400 Bad Request (invalid input), 401 Unauthorized (not logged in), 403 Forbidden (logged in, not allowed), 404 Not Found, 409 Conflict, 422 Unprocessable (validation failed), 429 Too Many Requests; 500 Internal Server Error, 502/503/504 for upstream and availability problems.

Clients, retries and monitoring depend on correct codes: a retry library retries 429 and 503, not 400.

Why it matters

You will read LLM provider errors and design your own all the time. Codes tell you whether to fix the request, wait, or retry.

Words to know

  • 4xxThe client did something wrong.
  • 5xxThe server or an upstream failed.
  • HeaderKey-value metadata on a request or response.

Try it without code

Watch the network

  1. Open your browser’s developer tools Network tab.
  2. Load any site and click a few requests.
  3. Find a 200, a 304 and, if you can, a 404.

Use it in code

Inspect status codes and headers from an LLM API error.

python
# pip install httpx
import httpx, os

r = httpx.post("https://api.anthropic.com/v1/messages",
               headers={"x-api-key": "wrong-key", "anthropic-version": "2023-06-01",
                        "content-type": "application/json"},
               json={"model": "claude-haiku-5-5", "max_tokens": 10,
                     "messages": [{"role": "user", "content": "hi"}]})
print(r.status_code)                 # 401: bad credentials - do not retry
print(r.headers.get("content-type"))
print(r.json()["error"])
PRACTICE · TICK IT AFTER THIS

Write a function that maps a status code to an action: fix request, re-authenticate, wait and retry, or alert.

Common mistakes

  • Using 500 for validation errors.
  • Retrying 400s.

Go deeper