Time to First Token: From 12 Seconds to 0.5 Seconds — The Key Logic Behind Inference Engine Acceleration
This article explains why time to first token (TTFT) can differ so dramatically between inference engines, using a real case in which a self-built engine cut P90 TTFT from about 12 seconds to 0.5 seconds on the same hardware and model. It covers the P90 TTFT metric, why coding sessions are especially sensitive to it, how the prefill stage dominates TTFT, and how prefix caching reuses the KV cache and accumulated auxiliary state to skip redundant computation. It closes with a practical checklist for reducing TTFT through prefix caching, context management, scheduling, and instance affinity.
1. Starting with “waiting for the AI to say the first word”
You ask an AI to help fix a login error. You make the changes it suggests, rerun the tests, and the terminal throws up the same red line again. You paste the new logs into the chat box, press Enter, and stare at the cursor — waiting for the AI to say its first word.
Fixing a bug often means going back and forth like this several times. What really tests your patience isn’t just the mistakes — it’s the blank waiting that comes after every submission. A recent agent capability benchmark gave a striking number: researchers had a large language model agent, working in a network-restricted environment with only basic server code and a few GPUs, build a complete inference engine on its own within roughly a dozen hours. When they replayed real coding sessions to test it, a mature open-source inference engine showed a time-to-first-token wait of about 12 seconds, while the self-written engine came in at around 0.5 seconds. Same hardware, same model, a gap of more than twentyfold.
This isn’t magic. To understand where the difference comes from, you first need to see what the model is actually busy doing after a request arrives.
2. P90 Time to First Token: Why this metric
Two common metrics for measuring inference services:
- Time to First Token (TTFT): The wait time from when a request is sent to when the model emits its first token.
- Decode Throughput: Once the model starts outputting, how many tokens it can generate per second.
The focus of this article is the P90 value of TTFT. P90 means you sort the wait times of a batch of requests from smallest to largest and take the value at the 90th percentile — meaning 90% of requests can “speak” within that time.
Why P90 rather than the average? Because under concurrency, individual requests can be queued or preempted, forming a long and nasty “tail.” Average latency dilutes these extreme values and hides the genuinely bad experience. P90 (and even P99) better reflects the lag that the vast majority of users will actually feel. The 12-second vs. 0.5-second gap above is exactly a P90 comparison.
3. Why coding scenarios are especially sensitive to TTFT
In everyday chat, the input is often just a few hundred to a few thousand tokens. But real coding sessions have some distinctive features that make TTFT the bottleneck:
- Very long context: Source code, error messages, and multiple proposed fixes pasted in earlier rounds all get sent to the server again with each new request.
- High redundancy: The current round often adds just one sentence, like “move the login button a bit to the right,” but what the server receives is “the entire history plus this one sentence.”
- High concurrency: Multiple developers submit at the same time, and requests queue up and compete for compute.
In a replay with 16 concurrent paths and a median single-request input of about 89,000 tokens, this feature was amplified: if the engine recomputes the “90% duplicated history,” TTFT naturally gets dragged out. Whoever can eliminate this duplicated overhead wins on “opening speed.”
4. When a request arrives: the Prefill stage
Large language model inference has two stages:
- Prefill: The model “reads” the entire input in one pass, computing internal information such as attention keys and values for each token. This step is compute-intensive and highly parallelizable, and it must be fully completed before the first token can be generated. TTFT is therefore mainly determined by prefill.
- Decode: Generating subsequent tokens one at a time, constrained by memory bandwidth, which determines generation speed (TPS).
The key point: the longer the input, the heavier the prefill, and the longer the TTFT. An 89,000-token input that has to be computed from scratch every time makes a wait of over ten seconds entirely plausible.
5. Prefix caching: skipping the “90% recomputation”
The core idea of the optimization can be summed up in one sentence: if the beginning of the current request is the same as the previous round, there is no need to recompute it.
This is Prefix Caching. After the model reads a piece of preceding text, it leaves behind a “computation record.” As long as the new request shares the same prefix, the engine can directly reuse the previous result and only process the newly added tail. Expressed in pseudocode, it looks roughly like this:
def serve(request, cache):prefix, suffix = split_common_prefix(request, cache.last)if prefix in cache:# Hit: reuse the already-computed state, skip most of the computationstate = cache[prefix]else:# Miss: prefill from scratchstate = prefill(prefix)cache[prefix] = state# Only the newly added suffix needs real computationstate = prefill_suffix(state, suffix)return first_token(state)
In a multi-round scenario of “change one sentence, run once,” the history of the previous rounds is almost exactly the stable common prefix. After a cache hit, what really needs to be computed is often just the last small piece of new content — this is the main source of compressing from 12 seconds to half a second.
6. What are the “two records” being reused
The “computation record” reused by prefix caching contains at least two kinds of state that must stay consistent in most models:
- Attention Key-Value Cache (KV Cache): The Key/Value tensors produced when the model reads each token, used so that later attention computation can “look back” at preceding text. This is the main body of prefix reuse and also the largest consumer of memory.
- Auxiliary state that accumulates as position advances: Some models maintain a running quantity related to the number of tokens already read, similar to a “ledger balance” (for example, progress related to positional encoding, cumulative quantities related to normalization, and so on). It grows as more tokens are read, and when reusing preceding text you must connect this progress as well, otherwise the computation baseline for the new content will be misaligned.
In engineering terms, to make the cache “hit and stay correct,” several conditions usually need to be met: the request prefix must be byte-for-byte identical (even one different space counts as a different prefix), a memory and cache management strategy that supports prefix sharing must be used, and routing must send consecutive requests of the same session to the same instance that can hit the cache. If any one of these is missing, the hit rate drops significantly.
7. Why the gap can reach twentyfold
Stringing the above together, the chasm between 12 seconds and 0.5 seconds becomes clear:
- If the mature engine does not enable prefix caching or does not hit it properly, it recomputes all 89,000 tokens every round. The prefill overhead is huge, pushing TTFT into the teens.
- The self-written engine does prefix reuse for this scenario, processing only the newly added tail each round. The prefill workload drops by one to two orders of magnitude, and TTFT plummets accordingly.
Worth emphasizing: once you enter the decode stage, the two are nearly on par in generation speed (both in the range of over 120 tokens per second). This is precisely a counter-proof that the gap isn’t in “generation” but in “before speaking” — not in how much compute there is, but in whether the repeated computation is saved.
8. An actionable TTFT optimization checklist
Drawing on general engineering practice, reducing time to first token can start from these directions:
- Enable and tune prefix caching: The most direct gains come in multi-turn conversations, long system prompts, fixed knowledge prefixes, and similar scenarios. Try to put stable content up front to maximize the common prefix length.
- Control context size: Summarize, trim, or use retrieval-based recall on very long histories, sending only genuinely relevant content to prefill and reducing the computation at the source.
- Reasonable batching and scheduling: Trade off between throughput and TTFT; avoid large batches “dragging” short requests into the long tail. Priority handling or chunked prefill can smooth the tail.
- Instance affinity and session routing: Let consecutive requests of the same session land on an instance that can hit the cache, preventing the cache from being repeatedly invalidated.
- Use P90/P99 as the observation metric: Drive optimization with tail metrics rather than looking only at averages, and combine with monitoring alerts to catch regressions promptly.
9. Conclusion
The 12 seconds of “waiting for the AI to say its first word” may look like just an experience problem, but its essence is the inference engine’s waste of repeated computation under long context, multi-turn, and high-concurrency conditions. Mechanisms like prefix caching can bring order-of-magnitude improvements not because of faster, more powerful hardware, but because of “no longer repeating work that has already been done.” For teams building inference services or intelligent coding platforms, the direction really worth investing in is treating the P90 of TTFT as a first-class metric and working on cache hits, context governance, and scheduling strategy at the same time — so that the moment the user finishes pressing Enter, they see the first word appear in the chat box.
