FastAPI background tasks
After the response, in order
Declare tasks: BackgroundTasks on a handler and call tasks.add_task(audit, text). FastAPI builds the response first and then runs the tasks in the order you added them. A count the handler reads from the log is therefore the count before its own task runs, and the exercises check exactly that.
When a task never runs
Tasks belong to the response the handler returns. If the handler raises HTTPException after adding a task, FastAPI sends an error response without it, so the task is dropped. Use async def task functions here: FastAPI sends plain functions to a thread pool, and this browser runtime has none. The background tasks docs cover the same API. Work that must survive a crash belongs in a durable queue, which the unit’s interview tasks ask you to explain.