Week 4 • Day 1Week 4

Day 1:How the Internet, HTTP & REST APIs Work (From Zero to LLM Wire Protocol)

Battle-tested daily lesson note • 100% runnable code, interview Q&A, test suite & interactive quiz

Week 4 — Day 1: How the Internet, HTTP & REST APIs Work (From Zero to LLM Wire Protocol)

Mentor's Note (10+ YOE Staff AI Engineer POV): When you call await client.chat.completions.create(model="gemini-2.5-flash", messages=[...]) in Python, what is actually travelling across the internet wire? An HTTP POST request carrying a JSON body and an Authorization: Bearer <API_KEY> header!

Understanding HTTP from first principles allows you to:

  1. Debug any LLM provider (OpenAI, Google Gemini, Anthropic, Groq, vLLM, Ollama) even before an official Python SDK exists!
  2. Know immediately whether an error should be retried (429 Too Many Requests, 502 Bad Gateway, 503 Service Unavailable) or failed immediately without wasting retries (400 Bad Request, 401 Unauthorized, 422 Unprocessable Entity).

Part 1: Anatomy of an LLM HTTP Request & Response

text
[Your Python App (Client)] [Google AI Studio / OpenAI Server] │ │ │ 1. HTTP POST /v1/chat/completions │ │ Headers: │ │ Content-Type: application/json │ │ Authorization: Bearer AQ.A***986g │ │ Body: │ │ {"model": "gemini-2.5-flash", "temperature": 0.0, ...} │ ├───────────────────────────────────────────────────────────────>│ │ │ (GPU runs Transformer forward pass) │ 2. HTTP/1.1 200 OK │ │ Headers: │ │ x-ratelimit-remaining-requests: 1499 │ │ Body: │ │ {"choices": [{"message": {"content": "{...}"}}], │ │ "usage": {"prompt_tokens": 180, "completion_tokens": 42}}│ │<───────────────────────────────────────────────────────────────┤

Part 2: The HTTP Status Codes Every AI Engineer Must Memorize

Status CodeCategoryMeaning in AI EngineeringShould Your Code Retry?
200 OK2xx SuccessLLM generated completion successfully.No (Succeeded!)
400 Bad Request4xx Client ErrorMalformed JSON request, invalid parameter (temperature=5.0), or context length exceeded (context_length_exceeded).NO! (Retrying the exact same bad request will fail 100% of the time—trim context or fix payload first!).
401 Unauthorized4xx Client ErrorMissing, expired, or invalid LLM_API_KEY.NO! (Alert on-call engineer immediately).
403 Forbidden4xx Client ErrorValid key, but your organization lacks permission for that model or region.NO!
422 Unprocessable Entity4xx Client ErrorFastAPI / Pydantic validation failed on input schema.NO!
429 Too Many Requests4xx Rate LimitYou exceeded Requests Per Minute (RPM) or Tokens Per Minute (TPM).YES! (Wait for Retry-After header or Exponential Backoff + Jitter!).
500 / 502 / 503 / 5045xx Server ErrorProvider GPU cluster overloaded or gateway timeout.YES! (Retry with Exponential Backoff or route to fallback model!).

Part 3: Hands-On Lab — LLM HTTP Wire Inspector & Status Classifier (http_protocol_inspector.py)

Check out src/http_protocol_inspector.py (in Tab 2: Codes — click Run in Browser to execute) and run the script and tests:

powershell
python week04_apis_http_llms_and_capstone_1/day01_http_rest_apis_headers_status_codes/src/http_protocol_inspector.py python -m unittest discover -s week04_apis_http_llms_and_capstone_1/day01_http_rest_apis_headers_status_codes/tests -v