Learning path: AI System Design: Reliable, Fast and Affordable LLM Apps · Step 9 of 15
Cutting p95 and Cost in Half for an LLM App: Read the Bill, Match the Lever, Guard Success
Asked to halve p95 and cost without hurting success, read the bill first. In a 200-request simulation, streaming the answer was 84% of the slowest request, a cheap router dropped success to 95.5%, and the careful router plus a cache cut cost to 31.6% while p95 fell only about 8%.
What you will learn
- Break one request's latency down by stage before choosing any fix
- See how a traffic spike turns into waiting in line, not slower stages
- Match one lever to each line of the bill: queue, cache, routing, token budget, streaming
- Judge each change by cost per successful task with the success rate as a guard, and report what didn't move
Before you start
- Latency Math: p50, p95, p99
- LLM Tokens, Context Windows and Cost
The interview question sounds simple: your support assistant is slow and costs too much. Cut p95 latency and cost in half, and the successful task rate can't drop. The strong answer doesn't start with a fix. It reads the bill first, stage by stage. Everything below comes from a small simulation, and cost is in illustrative units, not prices.
The answer in three moves
- Read the bill. Break latency and cost down by stage, because you can't cut what you haven't measured.
- Pick the cut that fits each stage: a queue, a cache, a router, a token budget or streaming.
- Guard the result. Judge every change by cost per successful task, and don't let the success rate drop.
Notice there's no promise of 50% in there, because you haven't looked yet.
The bad afternoon
Traffic spikes, the model's limits fill up, and the assistant feels stuck. In the simulation: 200 requests, 4 workers, and only the gap between arrivals changes.
== 4. p95 under load (4 workers) ==
calm gap 700 ms: mean 1764 p50 1501 p95 3207 p99 3557
busy gap 450 ms: mean 2557 p50 2482 p95 4219 p99 4631
over gap 380 ms: mean 7780 p50 7895 p95 12942 p99 13906
p95 is the time 95% of requests stay under (more in Latency Math: p50, p95, p99). With 700 ms between arrivals it's about 3.2 seconds; at 450 ms, about 4.2; at 380 ms, almost 13. No stage got slower. That extra time is all waiting in line, and from the outside nobody can tell which part to blame. Handling a spike like this is the job of back pressure.
Read the household bill
At the end of the month, before you cut anything, you read the bill line by line. Most of us go after the line that annoys us and switch off a few lights. The saving barely shows. The big line is usually somewhere else, and often we've never really looked at it. And there's a line you don't get to cut, like keeping the house warm in winter. A cold house isn't a saving. For us, that line is the successful task rate.
One request's bill
Here is the slowest request in the simulated baseline:
trace t058, 3572 ms
retrieval 44 ms 1%
model_ttft 499 ms 14%
model_stream 3000 ms 84%
guardrail 30 ms 1%
On this request the big line is the long answer, not the search. You only know that because you read the bill.
One lever for each line
| Line on the bill | Lever |
|---|---|
| Waiting in line | A queue with limits, so a spike waits its turn instead of piling onto the model |
| The same question again and again | A cache (Caching for AI Apps) |
| Easy questions on a big model | Routing (Model Routing) |
| Long answers | A token budget, a ceiling on what one answer can spend |
| A blank screen | Streaming. It doesn't shorten the total, it brings the first word sooner |
With streaming, be clear about which wait you're fixing.
The trap: a cheap router
Now pull some levers, with success right beside the cost. "Success" here means passing a simple rule, not a model evaluation.
== 5. Cost and latency, with a quality check ==
variant cost ttft95 tot95 hit ok
baseline 92.7 695 3206 0% 100%
cache 60.6 695 2954 41% 100%
router v1 35.1 600 3206 0% 96%
router v2 42.0 639 3206 0% 100%
v2+cache 29.3 639 2954 41% 100%
cost: illustrative units, not real prices
The cache hits 41.5% of requests and cost falls from 92.7 to 60.6, with success unchanged. Then the trap: send lots of questions to a small model (router v1) and, without the cache, cost drops to 35.1. A weak candidate stops here. But success has fallen to 95.5%.
Work it out per successful task. 35.1 over 191 successes is about 0.18. The careful router (v2) costs 42.0 with all 200 successful, about 0.21. Even per success, the bad router looks cheaper. That's why the success rate is a guard you don't cross, not just a term in a formula. So keep the careful router and add the cache: 29.3, all 200 successful, about 0.15 per task, against 0.46 where we started.
Did we cut both in half?
- Cost: yes. 31.6% of where we started.
- p95: no. From 3206 ms to 2954 ms, about 8% lower.
Why? The 11 slowest requests in the baseline are all hard questions on the large model, so routing leaves them alone, and only the cache helps a little. The line to go after next is long answers, with a token budget, and then replay the same spike to test it. That wasn't tried in the simulation, so there's no number for it. Say this plainly: you measured first, and you report what moved and what didn't.
The follow-ups
- Does streaming count as cutting p95? Not the total. It moves the first word, so report both numbers.
- A spike hits the model's rate limit. What comes first? A queue with limits, so requests wait instead of piling on, and retries that don't add to the pile (Retries and Backoff).
- How do you show a saving didn't hurt anyone? Replay the same traffic, and put cost per successful task and the success rate side by side.
| Level | What the answer has (a judgement guide, not a measurement) |
|---|---|
| Weak | Names a tool and promises a half |
| Good | Breaks the time down by stage and matches a lever to each line |
| Strong | Also guards the success rate, counts cost per successful task, and says what didn't move |
Recap
| Idea | What to remember |
|---|---|
| Read the bill | Stage by stage; a spike shows up as waiting in line |
| Match the lever | Queue, cache, routing, token budget, streaming, one per line |
| Guard success | Cost per successful task, and the success rate never drops |
| Report honestly | Simulated: cost to about a third, p95 only about 8% lower |
Try it yourself
Pick an AI tool you use and guess which line of its bill is the biggest: waiting, retrieval, the first word, or the long answer. Which lever would you try first, and what would you watch so success doesn't drop?
Related course on Udemy
Machine Learning Engineer: From First Model to ML Engineer
Learn machine learning in Python, then ship it: data analysis, regression, classification, tuning, deployment and MLOps.View on Udemy ↗: Machine Learning Engineer: From First Model to ML Engineer (paid course, opens in a new tab)Related posts
Fallback Models for an LLM App: A Chain That Checks Region and Fit, Flags Degraded Answers and Gets Rehearsed
When the main model goes down, the backup has to be allowed to take the data, able to read the whole request and tested before the bad day. A simulated four-model chain for an EU customer: a 6,000-token request lands on mid-eu, flagged degraded; a 10,000-token request is served by nobody, on purpose.
Back Pressure for LLM Traffic: Bound the Queue, Give Chat Its Own Lane, Count Tokens, Cancel When the Caller Leaves
A bulk upload buries the chat. In a small simulation, one shared queue grows to 199 waiting and the middle chat answer takes 80.2 s. A bound plus a chat lane brings it to 0.35 s while 178 bulk jobs are told to retry in 10 s, and cancelling abandoned calls saves 21,000 wasted tokens.
Load Balancing LLM Traffic: Count the Work, Weigh the Prefix Cache, Check Real Health, Drain Before Deploys
Six GPU replicas, two at 95% and four at 30%: round robin counts requests, and requests aren't equal. In a small simulation, least outstanding requests cuts p95 from 6.52 s to 3.05 s, prefix affinity lifts cache hits to 95% but pushes p95 to 11.45 s, a real health check takes errors from 48 to 2, and draining cuts 0 answers instead of 4.
A Message Queue for Document Ingestion: At Least Once, Leases, an Idempotent Consumer and a Dead Letter Queue
A queue doesn't lose a document, but it can hand one out twice. In a small simulation, a worker crash makes document two go from 3 to 6 chunks and the index holds 11 when the documents hold 8. An idempotent consumer brings it back to 8 with the same two deliveries, and a limit of three tries moves a poison file to a dead letter queue.