Cutting Time to First Token (TTFT) from 12 Seconds to Under Half a Second
When you use AI to assist with programming, every turn re-sends an almost-unchanged prefix, forcing the inference server to recompute it before it can produce the first token. This article explains how that repeated work drives Time to First Token (TTFT) as high as about 12 seconds under concurrency, and how prefix caching brings P90 TTFT down to roughly 0.48 seconds on the same hardware. It defines TTFT and the P90 percentile, walks through the prefill and decode phases, shows how to reuse the KV cache for shared prefixes, and covers production trade-offs such as cache eviction, chunked prefill, and continuous batching. It also explains why a faster first token should not come at the cost of decode throughput, and offers a reproducible method for benchmarking your own inference service.
The Frustrating Wait in Multi-Turn Debugging
When you use AI to assist with programming, the most typical pattern is “change one spot, run once, paste the logs, ask what to do next.” Each turn of the conversation re-sends all the earlier context—the system prompt, the source code you pasted, the previous error—inside the request. The problem is that when you have only changed one line of code, the inference service often has to “read” that large, nearly identical prefix all over again before it can produce the first token.
In real programming sessions, the context easily runs into thousands or even tens of thousands of tokens. If the prefix is computed from scratch on every turn, first-token latency gets dragged down by this “re-reading.” And programming is inherently concurrent: many people are asking questions and making changes at the same time, requests pile up in a queue, and tail latency (the slowest portion of requests) gets even worse.
One research team ran a controlled comparison: two inference services on the same hardware, running the same model, replaying the same batch of 16-concurrent real programming sessions. The P90 time to first token of a mature general-purpose solution was around 12 seconds, while a self-built engine optimized for this scenario brought that number down to about 0.48 seconds. The gap is not in compute—it is in “whether you need to re-read the prefix that hasn’t changed.”
First, Get the Metric Right: What Is P90 Time to First Token
Time to First Token (TTFT) is the time from when a request is sent to when the model generates the first token. It determines “how long until the user sees a response,” and is the direct source of the feel of an interactive application.
P90 is a percentile metric that is closer to the actual experience: sort all requests’ TTFT from small to large, and the value at the 90th-percentile position means that 90% of requests can start responding within this time, while only the slowest 10% take longer. Why look at P90 instead of the average? Because in concurrent scenarios, the few requests dragged down by long prefixes, queuing, and swapping memory in and out will pull the tail out long. User experience is decided by “most of the time,” while reputation is often ruined by “the few most laggy moments,” and P90 is exactly the balance point between the two.
Where Those 12 Seconds Go: The Cost of Recomputing the Prefix
An autoregressive large model generates in two phases: prefill and decode.
- Prefill: computes all input tokens forward in a single pass, producing the KV cache for each position. The compute grows with input length and is very expensive with long context.
- Decode: generates tokens one by one, each one depending on the existing KV cache.
The key point is that the result of prefill is reusable. If the prefix of this request (system prompt + conversation history) is exactly the same as the previous turn, then this prefix’s KV cache can theoretically be reused as-is, and you only need to prefill the small number of newly added tokens (that one changed line + the newly pasted log + the new question).
That roughly 12 seconds is basically “wasted” on repeatedly prefilling the unchanged prefix. On top of that, under high concurrency, the long-prefix prefill of multiple requests piles up and competes for compute, decode is forced to queue, and tail latency is pushed up further. The gap between half a second and twelve seconds mostly hides here.
Getting the Time Back: Prefix Caching
The core idea in one sentence: compute the same prefix only once, let subsequent requests hit the cache directly, and only prefill the incremental part.
In practice, you usually need a cache structure that can locate and reuse KV by token sequence (a common approach is to maintain a prefix tree / radix tree, using the token sequence as the path, with the corresponding KV blocks attached to the nodes). After a hit, the service skips computing the shared prefix and starts prefilling from the first uncached token.
Simplified illustration:
def serve(request, kv_cache):tokens = tokenize(request.messages)# 1. Match the longest common prefix in the cachematched = kv_cache.match_longest_prefix(tokens)# 2. Prefill only the incremental part that missed the cacheincremental = tokens[matched:]kv = kv_cache.get_kv(matched) if matched else Noneif incremental:new_kv = prefill(incremental, prefix_kv=kv)kv_cache.insert(tokens, merge_kv(kv, new_kv))# 3. Continue decoding from the incremental resultreturn decode(kv)
In multi-turn programming scenarios, the system prompt and most of the history barely change, so the hit rate is very high, and the prefill volume may shrink to just tens or hundreds of incremental tokens. First-token latency naturally drops from “recomputing tens of thousands of tokens” to “computing a few dozen extra tokens,” and this is the main source of the drop from over ten seconds down to sub-second.
Fast First Token Isn’t Fast Generation: Keep the Two Phases Separate
A common misreading is to equate “fast first token” with “good overall experience.” You need to distinguish two metrics:
- TTFT: how long you wait before it starts (determined by prefill and queuing);
- TPOT (Time Per Output Token) / generation throughput: how fast it emits tokens after it starts (determined by decode).
In the comparison above, once both engines started generating, the decode speeds were nearly identical (by median, about 124–125 tokens per second each). This shows exactly that the optimization acts only on the “prefill and scheduling” segment, without sacrificing decode quality. To judge whether an optimization is healthy, you should check whether it lowers TTFT on its own without slowing down decode throughput—if the first token is faster but token emission is noticeably slower, that is often robbing one part of the experience to patch another.
What Engineering in Production Can’t Avoid
Prefix caching is not a free lunch; production requires trade-offs:
- GPU memory and cache capacity: The KV cache takes up a lot of GPU memory, and more cache entries crowd out the concurrency you can serve, so you need a reasonable eviction strategy (LRU/LFU, etc.) and chunked management.
- Hit rate and consistency: Only a completely identical prefix can hit. Injecting timestamps or random IDs into the system prompt breaks the cache; put volatile content as late as possible.
- Scheduling strategy: Long-prefix prefill blocks other requests. A common approach is chunked prefill, which slices large prefills and interleaves them with decode to lower tail latency; going further, you can isolate the resources for prefill and decode.
- Concurrency and batching: Continuous batching lets new requests join a batch that is already being decoded at any moment, and combined with prefix caching, it can hold down both TTFT and TPOT at a concurrency level like 16.
Illustration: putting volatile content last to improve the hit rate
# Unfavorable: timestamp up front, different every turn, prefix can't be reused[system prompt, current time 12:03:41, ...history...]# Favorable: static prefix first, volatile content last[system prompt (static), ...history...], current time: 12:03:41
How to Evaluate Your Own Inference Service
If you are building or choosing an inference service, you can use a reproducible method to run a comparison:
- Fix the variables: same hardware, same model and quantization precision, same input data, changing only one factor at a time;
- Use real-session replay rather than synthetic short prompts—the long prefix of a real programming context is the source of the pressure;
- Set a clear concurrency (e.g., 16), and collect TTFT at P50/P90/P99 together with decode throughput;
- Report the two groups of metrics—first-token latency and generation speed—separately, to avoid “only reporting the good-looking one”;
- Watch process metrics such as cache hit rate, GPU memory usage, and queuing time, to pinpoint whether the bottleneck is in prefill, memory, or scheduling.
Summary
Optimizing time to first token is essentially an exercise in engineering “don’t do the same work twice.” In multi-turn programming sessions, what really slows down the first token is usually not a shortage of compute, but repeatedly prefilling a prefix that has barely changed. By reusing KV through prefix caching, chunked prefill, and reasonable batching and scheduling, you can compress P90 first-token latency from the order of ten-plus seconds to sub-second on the same hardware, while decode throughput is barely affected. For teams building interactive AI applications, this methodology is universal and does not depend on any specific product or vendor—it is more like a transferable inference-service design paradigm.
