How I Cut Time to First Token From 12 Seconds to Under Half a Second
This article examines why time to first token (TTFT) can dominate the latency of interactive coding sessions, using a benchmark that compared a mature inference engine with a self-built one at P90 TTFT. It explains how prefill and context growth create the bottleneck, then breaks down three mechanisms that reduced the delay: prefix caching, tiered state management, and scheduling with batching. A reusable checklist for measuring, optimizing, and load-testing TTFT is included.
1. The Overlooked 11 Seconds Before the First Word
You ask an AI coding tool to fix a login error. You apply the suggested change, rerun the tests, and the same red line appears in the terminal. You paste the new logs into the chat, press Enter, and wait.
That wait is one of the most underestimated costs in interactive development. A recent preprint on agent capability evaluation, submitted by researchers from universities and compute institutions, set up an interesting task: given base server code, four high-performance accelerators, and 12 hours, with no internet access allowed, an AI coding tool had to build an inference engine from scratch that could receive requests, schedule the GPUs, run the model, and return generated output.
After the engines were written, the researchers went beyond code review and ran replay tests with real coding sessions. Each engine ran the same model on four accelerators and handled the same stream of 16 concurrent sessions.
The metric was P90 time to first token (TTFT): sort all requests by the wait time from dispatch to the model emitting its first token, then take the 90th percentile. It means 90% of requests start receiving a response within that time.
The contrast was stark: the mature industry solution came in at about 12.029 seconds, while the self-built engine hit 0.478 seconds. Once generation began, median throughput was nearly identical (124.2 vs. 125.0 tokens/s). In other words, the entire gap happened before the first word.
2. Locating the Bottleneck: Prefill and Context Growth
To understand those 11.5 seconds, first understand the lifecycle of a request:
- The request arrives carrying the full conversation history.
- Prefill: the model reads all input in parallel and produces the first token.
- Decode: tokens are generated one by one, constrained by memory bandwidth.
In coding sessions, step 2 is the disaster zone. The context is packed with source code, error messages, and previous attempts, with a median input of 89,000 tokens. Sixteen concurrent requests mean these long inputs hit the same set of GPUs at once, prefill compute is quickly saturated, queueing time stacks up, and P90 is pushed higher.
Schematically, a request body looks roughly like this:
{"messages": [{"role": "system", "content": "You are a coding assistant"},{"role": "user", "content": "<source code ... about 30k tokens>"},{"role": "assistant", "content": "Suggested change A"},{"role": "user", "content": "<new logs ... about 20k tokens>"},{"role": "user", "content": "Move the login button a bit to the right"}],"stream": true}
The genuinely new content may be a single sentence, but a naive implementation recomputes all 89,000 tokens.
3. Three Mechanisms That Saved 11 Seconds
1. Prefix Caching: Compute Only the Increment
If the current request’s prefix matches the previous one, the engine reuses the already-computed intermediate results and only processes the new tail. With a high hit rate, prefill scale drops from “full” to “incremental,” which is the main reason latency plummets.
Engineering this requires cache key design, prefix matching, eviction policies, and concurrency isolation. The logic looks roughly like this:
def prefill(request, cache):prefix, new_part = split_common_prefix(request, cache)state = cache.get(prefix) or compute(prefix)state = extend(state, new_part) # compute only the incrementcache.put(prefix + new_part, state)return first_token(state)
2. Tiered State: Information Pages and Balance Pages
These models typically keep two kinds of state: one stores prior information for lookback, and one acts like an account balance, recording “how far we’ve read and how much capacity remains.” The self-built engine optimized layout and transfer paths separately for the read/write characteristics of each, reducing memory round trips.
3. Scheduling and Batching: Keeping Queues Under Control
- Chunked prefill, so a long request cannot monopolize compute.
- Separate pools or interleaved orchestration for prefill and decode, preventing them from blocking each other.
- Admission and prioritization based on remaining capacity, protecting P90 rather than the average.
4. A Reusable Practice Checklist
- Unify measurement: use P90/P99 TTFT as the primary metric, supplemented by throughput and memory usage.
- Prioritize reuse: keep the system prompt stable and templatize context to improve prefix hit rates.
- Slim down context: carry only necessary logs and snippets, and summarize or compress history.
- Make load tests realistic: use multi-way concurrency and long-input replay, not single requests with short text.
- Scale elastically: use common cloud capabilities such as container platforms, object storage, message queues, logging services, and monitoring and alerting for scaling and full-chain observability.
5. Conclusion
Time to first token is not magic; it is the combined result of prefill compute, cache hit rate, and scheduling strategy. When the time before the first word is squeezed down to half a second, the rhythm of human-AI collaboration truly shifts from “submit and wait” to “conversation.”
