Latency, Cost, and Capacity
Measure what users experience
End-to-end latency includes request validation, retrieval, queueing, prefill, decoding, tool calls, and output validation. Streaming can reduce perceived wait but does not necessarily reduce total computation or final-answer latency.
Time to first token measures when output begins. Time to complete measures when the result is usable. For a JSON extraction task, the complete validated object may matter more than an early brace.
A hypothetical cost calculation
Let input cost be 0.20 units per million tokens and output cost be 0.80 units per million. A request with 2,000 input tokens and 500 output tokens costs 2000/1e6×0.20 + 500/1e6×0.80 = 0.0008 units. At 100,000 requests that is 80 units before retries, embeddings, search, hosting, and other costs. These are invented rates for arithmetic, not a provider quote.
requests = 100_000
input_tokens, output_tokens = 2000, 500
per_request = input_tokens / 1_000_000 * 0.20
per_request += output_tokens / 1_000_000 * 0.80
print(round(per_request, 6)) # 0.0008
print(round(requests * per_request, 2)) # 80.0Run in the Python compiler. Add a 10% retry rate and recalculate: under identical request sizes, model-call cost becomes 88 units.
Where optimization helps
Reduce irrelevant context, cap output appropriately, cache stable authorized results, batch compatible work, and choose models that meet the task's required quality. Semantic caching can return wrong or stale answers to superficially similar queries; partition by tenant, model, policy version, and relevant user state, then evaluate hit correctness.
Model routing can use a small model for easy cases and a stronger model for difficult ones. A router is another component to measure. Escalation adds latency and can double cost for routed requests.
Capacity and tail latency
At a steady 5 requests per second and a mean request duration of 4 seconds, expected in-flight work is roughly 20 requests under stable conditions. Bursts, tool calls, and variable output lengths can require more capacity. Track p50, p95, and p99 latency rather than relying on the average alone.
Checkpoint with answers
Does a faster first token mean a faster completed answer? Not necessarily.
Why include failed calls in cost monitoring? They consume resources and can trigger retries even when the user gets no useful output.
Exercise: A shared cache keys only on question text. What is missing? Answer: authorization scope, evidence versions, relevant user state, and configuration needed to prevent incorrect cross-user or stale reuse.