Area 08 · Backend · Build this

Production FastAPI backend for an AI product

The backend every later project plugs into: JWT auth, Postgres, Redis caching, a background worker that ingests documents, rate limiting and an SSE endpoint that streams LLM answers. Build 01–10 in Phase 0 and finish 11–20 in Phase 5.

Phase 0 + 5 · Weeks 1–2, 18–20

What you will have at the end

  • A FastAPI service with OpenAPI docs
  • Login, per-user data and rate limits
  • Document upload → background ingestion → status
  • A streaming /chat endpoint with structured logs and metrics

Step by step

  1. SkeletonFastAPI app, settings class, Docker Compose with Postgres and Redis, /health and /ready.
  2. Schemausers, documents, chunks, chats, messages with indexes on foreign keys and (chat_id, created_at).
  3. AuthRegister and login issuing short-lived JWTs; a current_user dependency on every protected route.
  4. Documents APIPresigned upload, complete endpoint, cursor-paginated list, per-user ownership checks, idempotency keys on create.
  5. WorkerIngestion job: download, parse, chunk, embed, insert in one transaction; status updates; retries with backoff.
  6. Streaming chatPOST /chat validates input, retrieves chunks, streams the answer via SSE with status and done events, records usage after.
  7. ProtectionRedis sliding-window rate limits per user and IP; cache for repeated embeddings.
  8. ObservabilityStructured JSON logs with request IDs, Prometheus metrics for latency, tokens and cost, Sentry for errors.
DONE WHEN
  • A new user can sign up, upload a PDF, wait for "indexed" and chat with it
  • Retrying an upload never creates duplicates
  • Every log line for a request shares one request ID

Stretch goals

  • Google login with OAuth
  • WebSocket endpoint with cancel
  • Row-level security per user