A FastAPI service for long-running agents: runs, resumable streams, cancellation
Notebook· 12.04· 3–4 h· Lesson 103 of 129
The notebook opens in your lab, in a new tab. If your server is stopped, it starts first (a few seconds).
By the end you can
- Design an HTTP API for agents around background runs: start (202), follow, cancel, resume, instead of one long request.
- Load settings with
pydantic-settingsso a bad value fails at startup and secrets never print. - Stream run events over Server-Sent Events with FastAPI's native SSE, and resume after a disconnect with
Last-Event-ID. - Enforce who can do what: API keys, per-user ownership (404, not 403), and a manager-only approval step.
- Make side effects safe: idempotency keys, one active run per thread (409), back-pressure and budgets (429).
- Load test the service with 20 concurrent users and read the result as SLOs.
Before you start
Lessons 12.01–12.03 and Lesson 08.03 (interrupts and resume).
