Latency, throughput, and cost.
Understand where response time and spending come from, and choose optimisations without losing quality.
Start with the meaning.
Inference is the work a model does when it produces an output. For an application, total response time also includes request handling, retrieval, ranking, model queuing, generation, validation, and rendering. A fast model cannot compensate for a slow search index or an unnecessary chain of calls. Measure the whole path.
Latency is the time one user waits. Throughput is how much work a system handles over time. A system can have low average latency and still be slow at busy times. Percentiles such as the 95th percentile show the experience of slower requests. Cost may depend on input tokens, output tokens, tool calls, infrastructure, and human review.
How the idea works.
Start optimisation by measuring stages and distributions. A long prompt may dominate cost; repeated retrieval may dominate latency; a judge call may double the number of model requests. Possible changes include shorter context, a smaller model for easy cases, caching safe repeated results, parallel independent searches, or streaming partial output to the interface. Each change can affect answer quality and must be re-evaluated.
Caching is only safe when the key includes everything that affects the answer: task version, document version, permissions, and relevant user context. A cached answer to one user's private question must never be served to another user. For changing facts, use an expiry or invalidation rule. Sometimes caching search results is safer than caching final prose.
Streaming improves perceived responsiveness by showing output as it arrives, but it does not reduce the time until the final checked answer exists. If the application must validate an entire JSON object or citation set, it should not present an early fragment as authoritative. A progress indicator may be more honest for such tasks.
Average latency can hide a poor experience. If most requests finish in one second but one in twenty takes ten seconds, a busy product will have many frustrated users. Percentiles describe that tail. Trace individual stages for slow requests: perhaps a search service queued, a model generated an unusually long answer, or a retry doubled the work. Optimise the stage that actually dominates, then check that the change did not reduce answer quality.
A cheap call can be expensive if it rarely solves the user's task. Compare cost per successful outcome: include retries, retrieval, judge calls, human review, and abandonment. A smaller model that needs three attempts may cost more than a stronger model that succeeds once. Caching and routing can help, but each adds correctness conditions. For private or changing data, a stale cached answer can cost far more than the tokens it saved.
Follow the code.
stages_ms = {"retrieval": 120, "ranking": 80, "generation": 1450, "validation": 30}
total_ms = sum(stages_ms.values())
share = {name: round(time / total_ms, 2) for name, time in stages_ms.items()}
print(total_ms, share)The example totals four stage timings and calculates each stage's share. Generation dominates this particular request, so shortening retrieval by ten milliseconds would have little effect. Another request might have a very different profile. Timings should be measured across many requests, including slow cases, rather than inferred from one example.
The code omits network and interface time for clarity. A production trace would include them and link the stages with a request identifier.
What the result does not say.
A lower bill is valuable only if the system still meets its task contract. Evaluate cost per successful outcome, not simply cost per call.
Set budgets for agents and retries. A rare loop can create a large bill even if average requests are cheap.
One clear idea.
Optimise the measured end-to-end path while protecting the quality users rely on.
The AI cookbook
Complete, explained local workflows sit alongside this reading course.