# How Do You Diagnose a Slow LLM Pipeline?

> How to find where an LLM pipeline loses time, which numbers to measure and the usual fixes, explained simply for your AI interview.

**In short:** Measure before you change anything. Time every step from the user's click to the last word on screen, compare time to first token with total time, and find the step that holds most of the wait. Then fix that cause first.

AI and LLM interview question Q318, Inference, performance and cost. Intermediate, very common (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/how-to-diagnose-a-slow-llm-pipeline

## Say it in 30 seconds

When an AI pipeline is slow, I don't guess. I time every step, from the user's click to the last word on screen. I look at time to first token and total time, at the median and the 95th percentile, and at the token counts for each call. Then I find the slow step. Usually it's too many calls one after another, a long prompt with no cache, or an answer much longer than the user needs. I fix that step first, then measure again.

## Say it in 2 minutes

The first thing I do is measure where the user waits. A slow pipeline usually hides its delay in one place: a search that stalls, a model call that waits for another one to finish, or a check at the end that nobody timed. I start a clock when the user acts and stop it when the full answer is on screen. Then I split that time into steps: retrieval, each model call, each tool call, and the rendering in the browser.

Next I read two numbers for each step. Time to first token shows how long the user waits before anything appears. Total time shows how long they wait for the whole answer. I compare the median with the 95th percentile, because an average can look fine while one run in twenty takes far too long. I also record input and output tokens, since long prompts and long answers slow things down in different ways.

Then I look for the usual causes. Calls that wait for each other add their waits together, so I check whether some can run at the same time. A long prompt that changes on every request can delay the first token, so I check whether the stable part can be cached. An answer longer than the user needs costs time too, so I cut it down. And if a step is a lookup the code already knows how to do, I replace the model call with code.

I change one thing at a time, measure again, and keep the change only if the numbers move. If the model itself is the limit on our hardware, that's a question for the serving setup, and I would test it before buying anything.

## What is a slow LLM pipeline, in plain words?

An LLM pipeline is a chain of steps that ends in a model's answer. It might rewrite the user's question, search your documents, call the model, then check the reply before showing it. Every step takes time, and the pipeline feels slow when the user waits too long for the whole chain.

Diagnosing it means finding which step makes them wait. Think of a bank branch manager timing a customer's visit. Nobody cares that the teller is quick if the form goes to three desks first. The manager times the whole visit, then finds the desk where people stand.

Take a card-fee assistant at a mid-size bank. Its answers take about 8 to 10 seconds, and the team's first guess is that the model is too slow. That is only a guess. The timings will show the real cause.

- **Token:** A small piece of text, often part of a word, that the model reads and writes. Speed and cost both count tokens.
- **Median:** The middle run when you line up every run by time. Half are faster, half slower.
- **Time to first token (TTFT):** How long, from sending the model request until the first word of the reply arrives.
- **End-to-end latency:** How long the user waits, from their click to the full answer on screen.
- **95th percentile (p95):** The time that 95 out of 100 runs finish within. The slowest 1 in 20 take longer than this.

## Which numbers should you measure?

Start with the two timings for the whole request. OpenAI's latency guide says to measure where users wait before you change the model or the prompt, and it defines time to first token and end-to-end latency.

> "Measure where users wait before changing the model or prompt." (OpenAI, latency optimization)
>
> "Time to first token (TTFT) measures how long an API request takes to start returning content." (OpenAI, latency optimization)

- **Time to first token: When the first word appears.** Hit hard by a long prompt. Work done before the model call is not in this number, so time it separately.
- **Total time: When the answer is complete.** Adds every lookup, model call, tool call and retry before the last word.
- **Token counts: How much went in and out.** Input, cached input and output tokens, recorded next to each timing.

Then compare the median with the 95th percentile. The median is the typical run. The 95th percentile shows how bad the slow runs get, and OpenAI says to compare both on representative traffic.

The vLLM project's metrics page names the same measures. Its time per output token is the average gap between output tokens once the answer has started. It is worked out from the end-to-end time minus the first-token time, divided by the number of output tokens after the first.

## Where does the time go?

Find the slow step before you touch the prompt. The chart shows the path from the question to the fix, with the usual causes listed after the timed steps.

*Diagram, Finding the slow step: The timer starts when the user sends the question and runs through every step. The slowest step is matched to a likely cause, then one fix goes in and the pipeline is timed again. Nothing changes before the timings say where to look.*
1. The timer starts when the user sends the question and runs through every step.
2. The slowest step is matched to the usual cause that fits it.
3. One fix goes in, then the pipeline is timed again.

1. **Time the whole visit:** From the user's click to the last word on screen, including the browser's drawing of the reply.
2. **Time each step:** Retrieval, every model call, every tool call and the final check, each on its own clock.
3. **Read the slowest step:** Usually one step holds most of the wait. A median can hide a stall that only some runs hit.
4. **Check the token counts:** Long input mostly delays the first word. Long output adds time at the end.

Here is the idea in Python, with five made-up slow runs from that assistant. The answer step takes most of the wait, and one search stalled once.

```python
# Made-up timings in milliseconds for five slow answers from a bank's card-fee assistant.
import statistics

RUNS = [
    {"rewrite": 820, "search": 240, "answer": 5900, "check": 1050},
    {"rewrite": 790, "search": 260, "answer": 6400, "check": 990},
    {"rewrite": 840, "search": 230, "answer": 6100, "check": 1120},
    {"rewrite": 810, "search": 2900, "answer": 5800, "check": 1010},
    {"rewrite": 830, "search": 250, "answer": 6250, "check": 1080},
]
totals = [sum(run.values()) for run in RUNS]
print("Median total:", statistics.median(totals), "ms")
print("Slowest total:", max(totals), "ms")
for stage in RUNS[0]:
    share = sum(run[stage] for run in RUNS) / sum(totals) * 100
    print(f"{stage:<8} {share:5.1f}% of all waiting time")
median_search = statistics.median(run["search"] for run in RUNS)
print("Median search:", median_search, "ms, but one run took", max(run["search"] for run in RUNS), "ms")
```

Output:

```text
Median total: 8410 ms
Slowest total: 10520 ms
rewrite    9.4% of all waiting time
search     8.9% of all waiting time
answer    69.7% of all waiting time
check     12.0% of all waiting time
Median search: 250 ms, but one run took 2900 ms
```

The median search time is 250 ms, so a median-only dashboard looks healthy. The one 2900 ms run is what the user who waited felt, which is why the 95th percentile matters.

## What are the usual fixes, and what do they cost?

Once the slow step has a cause, pick the smallest fix that targets it. The chart maps each cause to its usual fix.

*Diagram, Each cause, its fix: Three common causes, each with a fix that targets it, and one check at the end. Measure the same requests before and after each fix, so the change is judged on the same work.*
1. Calls with no dependency on each other run side by side.
2. The stable part of the prompt is reused, so the first token comes sooner.
3. The model is asked for only the detail the user needs.
4. The side-by-side calls are timed again to confirm they helped.
5. The cached prompt is timed again on the same requests.
6. The shorter answer is timed again on the same requests.

1. **Run independent work at the same time:** OpenAI's guide says to run independent work concurrently. Two lookups that don't need each other can start together, and the answer waits only for the slower one.
2. **Cache the stable part of the prompt:** Prompt caching can reuse an eligible exact match at the start of the prompt, so instructions, examples and tool definitions go first and stay unchanged.
3. **Ask for less text:** OpenAI's guide says that on supported models in its Responses API (its request format), a setting called text.verbosity can be lowered for shorter answers. Users rarely need the long version.
4. **Let code answer what it already knows:** An order's status or a refund that already went through doesn't need a model at all. OpenAI's guide says to use application code when you already know the answer.
5. **Stream the reply:** Streaming lets the user start reading before generation finishes. The wait feels shorter, even though the full answer takes as long to finish.

Anthropic's caching also reuses a matching prefix, but you choose how the cache mark goes in. Automatic caching means one cache_control field on the request, and the system moves the breakpoint to the last cacheable block as conversations grow. Explicit caching puts cache_control at the end of the stable text, so later requests that start the same way can reuse it. Changing text, such as a timestamp or the new user message, goes after that mark. To check it worked, look at cache_read_input_tokens, which counts the tokens taken from the cache for that request.

Each fix has a cost. Caching needs the prompt to stay the same. Starting a lookup before you know it is needed can waste work on a wrong guess, so start only read-only lookups you can discard. A short answer can drop detail a reader needs. Measure each change on the same requests before you keep it.

### How would you find a slowdown that only happens at 9 am?

A coding agent that runs in the team's automatic build checks, called CI, gets slow every weekday at 9 am, when the whole team pushes code. The daily average hides it, so I split the timings by hour and compare the 95th percentile for each hour.

Next I check the model server as well as the app. vLLM's metrics include KV cache usage, which shows what fraction of the server's working memory for requests in progress is in use. Sometimes vLLM puts a request back in the waiting queue to make room for other requests, and its docs call that state PREEMPTED.

- **If the queue is long: Capacity or fewer jobs at once.** Test running fewer agent jobs at the peak, or more server capacity, and measure the cost first.
- **If one step stalls: Fix that step.** A search or a tool call that spikes at peak hours gets its own timeout and retry rules.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] Measure first: time each step, from the user's click to the last word.
- [ ] Know time to first token and total time, and why they differ.
- [ ] Compare the median with the 95th percentile.
- [ ] Name the usual causes: calls one after another, long uncached prompts, long answers.
- [ ] Fix one thing, time it again, and keep it only if the numbers move.
- [ ] Name the cost of each fix, such as the rule caching needs or work wasted on a wrong early lookup.

**Common traps**

- Trap: "Switch to a faster model first." Better: Measure first. A faster model won't help if the slow step is a search stall or three calls in a row.
- Trap: "The average response time looks fine." Better: Check the 95th percentile. The average can hide the slow runs that users remember.
- Trap: "Make the prompt shorter, always." Better: Only when input length is the cause. Cutting facts from a prompt can make answers wrong.

## Can you spot the slow step?

**Spot the right plan:** A team wants to know why one question takes 14 seconds. Which plan finds the slow step?

A. Log the average total time once a week and compare it with last month.
B. Time each step for every request, then compare the median and p95 per step.
C. Ask three users whether the bot feels slow, then guess the cause.
D. Switch to a bigger model and see whether the wait drops.

**Answer:** B. Per-step timing with the median and p95 shows where the wait sits, and whether it comes from a few bad runs. The other plans hide the step or change the thing you wanted to test.

**Quick check:** A pipeline's median is 2 seconds, its p95 is 14 seconds, and the search step stalls only at peak hours. What do you check first?

A. Whether the system prompt needs new wording
B. The queue and the search step's p95 at peak hours
C. The model's temperature setting
D. Whether the team should buy a faster model

**Answer:** B. A fine median with a spike at peak hours points at waiting, from a queue or a stalled step. Prompt wording, temperature and model choice don't explain a stall that depends on load.

## Can you answer these from memory?

**Flash cards**

- **What is time to first token?** How long, from sending the model request until the first word arrives. Shortened to TTFT.
- **Median vs p95?** The median is the typical run. The p95 is the time that 95 in 100 runs finish within.
- **First step of diagnosis?** Time the whole visit, from the user's click to the last word on screen.
- **Usual causes of slowness?** Calls one after another, long uncached prompts and answers longer than needed.
- **When can you skip a model call?** When code already knows the answer, such as an order's current status.
- **What does caching need?** The same exact text at the start of the prompt, so stable text goes first.

## Likely follow-up questions

- Q316 TTFT vs tokens/sec vs total latency? https://designervenkat.online/interview/ai/ttft-vs-tokens-per-second-vs-latency
- Q317 The latency vs throughput trade-off? (full answer coming)
- Q088 What is prompt caching, and how does it save money? https://designervenkat.online/interview/ai/what-is-prompt-caching
- Q260 What is tracing for LLM apps? https://designervenkat.online/interview/ai/what-is-tracing-for-llm-apps
- Q344 What is a p95/p99 latency budget? (full answer coming)
- Q340 How do reasoning tokens affect cost and latency? (full answer coming)

## Learn it in depth

- [Watching cost and speed](https://designervenkat.online/learn/ai-engineering/cost-latency-monitoring)
- [Seeing inside your AI app](https://designervenkat.online/learn/ai-engineering/llm-observability)
