Reducing Time to First Token from 12 Seconds to Half a Second
This article examines why Time To First Token (TTFT) matters in real coding sessions, using a preprint evaluation that compared a mature open-source inference framework (about 12 seconds P90 TTFT) with a custom agent-built implementation (about 0.48 seconds) under identical hardware, model, and concurrency. It explains P90 TTFT and decode throughput, where prefill time goes, and three general techniques: prefix caching, chunked prefill with scheduling, and a reproducible two-metric evaluation loop. The takeaway is practical guidance for developers and teams that want to treat waiting time as a service-level metric.
1. It Started with Waiting on a Bug Fix
Most developers know this loop: paste an error log into the chat, get a suggested fix, rerun the tests, see the same red line in the terminal, paste the new log back, and wait for the next debugging direction. Even a simple login failure can take three or four rounds to pin down.
What actually feels slow in this loop is usually not the model generating an answer. It is the blank stretch between pressing Enter and seeing the first character on screen. The industry typically measures this with Time To First Token (TTFT). A recent preprint on agent capability evaluation, submitted by researchers from universities and compute vendors, put this metric front and center: in a constrained development environment, an agent was asked to build an inference service from scratch and was validated with replayed real coding sessions. The P90 TTFT of a mature open-source inference framework was about 12 seconds, while the agent’s own implementation was about 0.48 seconds. Hardware, model, and concurrency were identical.
This is not a claim that custom implementations always beat mature frameworks. It is a valuable engineering sample: under specific workload characteristics, targeted optimization around prefill and caching can yield very significant gains.
2. Defining the Metric: What P90 TTFT Means
Sort all requests by the time from dispatch to the model emitting its first token, then take the 90th percentile. That is P90 TTFT. It answers one question: within how long can 90% of requests start responding?
Why P90 instead of the average? The average is dragged down by a large number of short requests and hides the long tail. The pain in real coding sessions lives precisely in that tail: the longer the context and the longer the queue, the longer the user stares at a blank screen. P90, P95, and P99 used together are what reflect the experience of the worst-off batch of users.
A companion metric is generation throughput: the number of tokens produced per second after the model starts responding (median). In the sample above, the two approaches measured 124.2 and 125.0 tokens per second, essentially level. This comparison matters: optimizing TTFT did not sacrifice decode speed, which indicates the gains came from the stage before the model opens its mouth, not from robbing one stage to pay another.
3. Where the First 11.5 Seconds Go
Once a request enters the service, it roughly goes through: receive and authenticate, queue, prefill, then sample and emit the first token. TTFT is basically made up of the first four steps.
Prefill is the heavy part. The model must read the entire input and compute the intermediate representation at every position (the KV cache) before it can generate the first token. In the replayed coding sessions from the paper, the first few rounds of source code, error messages, and attempted fixes are all sent to the model together; at 16-way concurrency, the median single input reached about 89,000 tokens. Prefilling nearly 90,000 tokens is a scenario of combined compute and bandwidth pressure.
So the first optimization principle is: if something does not need to be recomputed, do not recompute it.
4. Prefix Caching: Reuse the Previous Round’s Computation
Multi-round coding sessions have one very strong regularity: the beginning of each round is almost identical (system prompt, conversation history, already-pasted source code), with only a new sentence appended at the end, such as “move the login button a bit to the right”.
Prefix Caching exploits exactly this: if a new request’s prefix matches a previously computed result, reuse the computed KV cache directly and run prefill only on the newly appended tokens. With a high hit rate, the tokens that actually need computing this round may be only a few hundred, and TTFT naturally drops from seconds to hundreds of milliseconds.
Three things matter in practice:
- Prefix matching must be exact. The token sequence must match position by position. Any change anywhere (even a timestamp or random ID inserted into the system prompt) invalidates the cache.
- Cache capacity and eviction policy. The KV cache occupies GPU memory, so it needs block-based management with eviction (such as least recently used), balancing hit rate against memory usage.
- Prompt structure should be “stable prefix, volatile suffix”. Putting volatile content last is the lowest-cost change for improving the hit rate.
An illustrative way to organize a request:
[stable prefix] system instructions + project conventions + relevant source snippets + prior turns[volatile suffix] this round's new one-line request + latest logs
5. Chunked Prefill and Scheduling: Don’t Let Long Requests Block the Queue
Even on a cache miss, two general techniques can significantly improve the long tail:
- Chunked prefill: split one very long prefill into smaller chunks and interleave them with decode requests, so a single long request does not monopolize the device and force everything behind it to queue.
- Continuous batching and priority scheduling: let newly arrived requests produce their first token first while long requests yield compute; also set queue limits and timeouts to prevent cascading failure.
Combined with sensible tensor/pipeline parallel partitioning and capacity planning for GPU memory and batch size, P90 can be held down. These are general industry practices, independent of any specific framework.
6. How to Verify Optimization with a Reproducible Method
Run the following closed loop in order, and avoid “tuning by feel”:
- Fix the variables: same hardware, same model weights, same concurrency (such as 16-way), same batch of replayed real sessions.
- Collect both metrics: P50/P90/P99 of TTFT, plus median throughput during decode.
- Run ablation experiments: toggle prefix caching and chunked prefill on and off separately, and quantify each contribution.
- Check the hit rate: measure prefix cache hit rate and GPU memory usage to confirm where the gains come from.
- Stress the long tail: construct very long contexts and burst concurrency, and watch whether P99 runs out of control.
A validation skeleton in pseudocode:
for cfg in configs: # different combinations of optimization switchesmetrics = replay(workload, cfg, concurrency=16)report(cfg, ttft_p50=metrics.p50, ttft_p90=metrics.p90,tps_median=metrics.decode_tps, cache_hit=metrics.hit_rate)
7. The Enterprise View: Treat Waiting Time as a Service Metric
For teams using AI coding tools or running their own inference services, TTFT should be included in service level objectives and monitored alongside availability and generation throughput. Container platforms can provide elastic scaling; logging and monitoring/alerting systems can set thresholds on P90/P99; object storage can hold replay datasets for regression testing. If you use general-purpose GPU instances or managed inference services in the cloud, “cache hit rate, queue time, batch size” should likewise be core observability items.
8. Conclusion
The gap from 12 seconds to 0.48 seconds is not fundamentally about a smarter model. It is about eliminating repeated computation, slicing long requests, and reordering the queue. The three most practical actions for developers are: keep the prompt prefix stable to hit the cache, enable chunked prefill to suppress the long tail, and use replayed real sessions for a reproducible two-metric evaluation. Do these three things, and the first character the user sees after pressing Enter will arrive much faster.
