Single logo
  • Courses
  • Blog
  • About
Join free on SkoolJoin free on Skool

Learning path · 15 steps

AI System Design: Reliable, Fast and Affordable LLM Apps
  1. 1
    Fallback Models for an LLM App: A Chain That Checks Region and Fit, Flags Degraded Answers and Gets Rehearsed
  2. 2
    Back Pressure for LLM Traffic: Bound the Queue, Give Chat Its Own Lane, Count Tokens, Cancel When the Caller Leaves
  3. 3
    Load Balancing LLM Traffic: Count the Work, Weigh the Prefix Cache, Check Real Health, Drain Before Deploys
  4. 4
    A Message Queue for Document Ingestion: At Least Once, Leases, an Idempotent Consumer and a Dead Letter Queue
  5. 5
    Caching for AI Apps: Four Layers, What Goes in Each Key, and How a Delete Travels
  6. 6
    Semantic Caching: Why a Similarity Threshold Can't Stop Confident Wrong Answers
  7. 7
    LLM Model Routing: Rules, a Classifier or a Cascade, Judged on Cost per Win and Every Topic
  8. 8
    Observability and Tracing for LLM Apps: A Span per Stage, Redaction First, Bad Traces Become Tests
  9. 9
    Cutting p95 and Cost in Half for an LLM App: Read the Bill, Match the Lever, Guard Success
  10. 10
    Guardrails for an LLM Agent: Check What Comes In, What It Wants to Do, and What Goes Out
  11. 11
    Embedding Migration With Zero Downtime: Two Indexes, a Dual-Write Backfill, a Shadow Check and a Flag
  12. 12
    Design a Multi-Tenant RAG Assistant, Part 1: Requirements, Rough Numbers and a Contract
  13. 13
    Design a Multi-Tenant RAG Assistant, Part 2: Tenant Isolation, a Scoped Cache and Walking an Outage
  14. 14
    A 500 MB PDF Takes 50 s to Answer, Get It to 2 s: Measure First, Then Promise Per Path
  15. 15
    A 500 MB PDF From 50 s to 2 s, the Fast Path: Queue, Status, Streaming and an Acceptance Test That Fails
  1. Blog
  2. /
  3. AI Engineering
  4. /
  5. AI System Design

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%.

Chaand Sheikh

·27 November 2026AI System Design

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

  1. Read the bill. Break latency and cost down by stage, because you can't cut what you haven't measured.
  2. Pick the cut that fits each stage: a queue, a cache, a router, a token budget or streaming.
  3. 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%
Where one slow request spent its time retrieval 44 first word 499 streaming the answer 3000 ms (84%) final check 30 0 3572 ms Simulated request t058
A month spent tuning search would have cut the 44 ms line. The big line is the long answer.

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 billLever
Waiting in lineA queue with limits, so a spike waits its turn instead of piling onto the model
The same question again and againA cache (Caching for AI Apps)
Easy questions on a big modelRouting (Model Routing)
Long answersA token budget, a ceiling on what one answer can spend
A blank screenStreaming. 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
Cost by variant, with the success rate beside it baseline cache router v1 router v2 v2 + cache 92.7, 100%, 0.46/task 60.6, 100% 35.1, 95.5% success, 0.18/task 42.0, 100%, 0.21/task 29.3, 100%, 0.15/task Simulated, 200 requests, illustrative cost units
The cheap router drops success to 95.5%. The success rate is a guard, not just part of a formula.

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.
LevelWhat the answer has (a judgement guide, not a measurement)
WeakNames a tool and promises a half
GoodBreaks the time down by stage and matches a lever to each line
StrongAlso guards the success rate, counts cost per successful task, and says what didn't move

Recap

IdeaWhat to remember
Read the billStage by stage; a spike shows up as waiting in line
Match the leverQueue, cache, routing, token budget, streaming, one per line
Guard successCost per successful task, and the success rate never drops
Report honestlySimulated: 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?

Tried the exercise? Share your answer or ask a question.

The StudyEasy AI Community is free to join, with the lesson files, notebooks and code.

Join free on Skool

Keep going in the learning path AI System Design: Reliable, Fast and Affordable LLM Apps · step 9 of 15

Previous
Observability and Tracing for LLM Apps: A Span per Stage, Redaction First, Bad Traces Become Tests
Next post
Guardrails for an LLM Agent: Check What Comes In, What It Wants to Do, and What Goes Out

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.

On this page

  • The answer in three moves
  • The bad afternoon
  • Read the household bill
  • One request's bill
  • One lever for each line
  • The trap: a cheap router
  • Did we cut both in half?
  • The follow-ups
  • Recap
  • Try it yourself
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

Learn AI engineering, share what you build, and connect with other learners.

  • Free resources: lesson files, notebooks and code

  • Ask questions and join engineering discussions

  • Meet real people learning and building with AI

Join free on Skool

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)

Single logo

Empowering Education, One Course at a Time

About
About usContact usFAQs
Legal
Terms of ServicePrivacy Policy

© 2026 StudyEasy Organization. All rights reserved.