FastAPI: async is about waiting
Understand the difference between waiting for an API response and spending CPU time on a model prediction.
Identify what the endpoint does
An endpoint that waits for a network response has a different bottleneck from one running a long calculation. Async code can yield while awaiting compatible I/O. Marking a function async does not automatically make its work non-blocking or run CPU-heavy inference in parallel.
Match the library to the endpoint
Use async def when calling an awaitable library. A regular def path operation is run by FastAPI in an external thread pool. Calling an ordinary blocking helper directly inside async def does not automatically move that helper into the pool.
@app.get('/summary')
async def summary():
# fetch_summary must be an async I/O function.
result = await fetch_summary()
return {'summary': result}Give expensive work a separate plan
For a slow inference job, decide how users will receive the result. A queued job with a status endpoint can be a useful design when requests should not stay open. Measure response latency and concurrency under realistic load before deciding whether threads, worker processes, or a dedicated model service fit the workload.
Draw the request timeline
Break a document-summary request into validation, file lookup, text processing, model execution, and response formatting. Label each stage with the resource it consumes. Waiting on an external service is different from parsing a large local document. This sketch helps identify whether slow requests come from network delays, CPU work, queueing, or an overloaded dependency. Add timing around those stages before rewriting the endpoint. A single total-duration number cannot tell you which part of the request actually needs attention.
Define the asynchronous contract
An awaitable operation still needs a failure policy. Decide what happens when the upstream provider times out, returns an invalid response, or declines a request. Give the caller a stable error shape and avoid returning raw exception text. If the operation creates something expensive, consider how a retry could duplicate it. A job identifier can give the UI a way to resume checking the same request. These are application design decisions: the async keyword alone does not provide timeouts, idempotency, or a durable job queue.
Test competing requests
Use a small load experiment with simultaneous calls, including one deliberately slow dependency. Check whether otherwise quick endpoints remain responsive. Record timeout counts and tail latency as well as the average response time. Repeat with the model loaded and with realistic document sizes. If a queued workflow is appropriate, test the states the user sees: accepted, running, completed, and failed. Make sure a refresh can recover the job status. Concurrency should preserve useful behavior under contention, rather than only making a single demonstration request look fast.