01 /
A notebook hides time
In a notebook, waiting is free. You run a cell, the model thinks for eight seconds, a good answer appears, and the prototype is a success. Nobody in that room is bored, and nobody leaves.
Put the same code behind a button and those eight seconds become the product. People stare at a spinner, wonder whether it is broken, tap again, and send a second request that costs you twice. The answer can be excellent and the feature can still fail, because the experience of waiting was never designed. It was inherited from the notebook.
The good news is that most of that time can be removed or hidden, and Python, the language the prototype was written in, is perfectly capable of doing it.
02 /
Measure the right numbers
Average latency is the wrong number. A feature that takes 1.5 seconds on average can still take 9 seconds for one request in twenty, and those are the requests people remember. Track percentiles: the median (p50) for the typical experience, and p95 or p99 for the tail.
For AI features, split the time into stages. How long until the first word appears on screen? How long until the answer is complete? How much of the time is retrieval, how much is the model, how much is your own code? Time to first token shapes how fast a feature feels; time to the last token shapes how much it costs.
03 /
Overlap, then stream
Prototype code tends to be strictly sequential: load the user’s profile, then search the documents, then call the model, then wait for the whole answer, then show it. Two of those steps usually do not depend on each other, and the last step does not need to wait at all.
The marker is when the first word reaches the screen. Illustrative timings: profile 0.3 s, document search 0.5 s, first token 0.7 s after the prompt is sent, full answer 3.7 s.
With asyncio, independent calls run together with one line, and an async generator hands each token to the user as soon as it exists:
import asyncio
async def answer(question: str):
# independent lookups run together, not one after the other
profile, docs = await asyncio.gather(
load_profile(question),
search_docs(question),
)
prompt = build_prompt(question, profile, docs)
async for token in model.stream(prompt):
yield token # the first words reach the screen immediately
load_profile, search_docs and model.stream stand in for your own clients. Most model SDKs offer a streaming call with this shape.
04 /
Cache what repeats
The fastest request is the one you do not make. Pure, repeated computations, such as embedding the same document or formatting the same template, can be cached. For ordinary synchronous functions, functools.lru_cache does this in one line. Be careful with async code: decorating a coroutine function with lru_cache stores the coroutine object, which can only be awaited once, not its result. Async calls need a cache that stores finished values, in memory or in something like Redis.
Model providers offer a second kind of cache. When many requests share a long, stable prefix, such as system instructions, tool definitions or a reference document, prompt caching lets the provider reuse that work, which cuts both latency and cost. It rewards a simple discipline: put the stable parts of the prompt first and the changing parts last.
05 /
Know where Python is slow, and where it isn’t
It is tempting to blame the language. Usually the evidence points elsewhere. An AI request spends most of its life waiting on the network, and while one coroutine waits, asyncio serves the others. The heavy numerical work in NumPy and PyTorch runs in compiled code. For this kind of workload, Python is rarely the bottleneck.
CPU-bound Python is different. The global interpreter lock lets only one thread run Python bytecode at a time, so threads do not speed up pure-Python computation; processes do. That is starting to change: Python 3.13 introduced an experimental free-threaded build, and Python 3.14 made it officially supported. Either way, the rule is the same. Profile before you rewrite, with cProfile or py-spy, and fix the part the profile shows you, not the part you suspected.
06 /
Design the wait
Some waiting cannot be removed, so it has to be designed. Show what is happening in plain words: searching your documents, writing the answer. Show partial results as they arrive. Let people cancel, and make sure cancelling actually stops the work on the server. Disable the button after the first tap instead of sending duplicate requests.
Finally, give each feature a latency budget, for example a first word within one second at p95, and treat it like any other requirement in the eval suite. A budget turns “it feels slow” into a number the team can defend, and it keeps the notebook’s eight seconds out of the product for good.