LLM Inference Engine: Practical Optimization of First-Token Latency
This article explains what P90 first-token latency measures and why it matters more than raw generation speed in interactive coding tools. It breaks down where the waiting time comes from, focusing on prefill and prefix caching, and lists reusable optimization techniques such as chunked prefill, batch tuning, priority scheduling, and observability. It also outlines an engineering rollout approach and shows how these methods can reduce startup wait from over ten seconds to sub-second levels.
1. Why the First Token Matters
In real-world use of an AI coding assistant, the most noticeable experience metric is not the overall generation speed. It is the wait between pressing Enter and seeing the first character appear. Fixing a bug often takes multiple rounds of interaction: pasting source code, pasting an error message, trying a fix, failing again, and adding logs. Each round requires waiting again, so the cost of waiting is multiplied.
In a benchmark on “agents that can build agents,” a research team asked a coding tool to implement its own inference engine under a constrained network environment, given hardware, and a fixed time budget. The evaluation replayed real coding sessions. The results showed that, on the same hardware and with the same model, the self-built engine had a P90 first-token latency of about 0.48 seconds, while a common mature industry solution was around 12 seconds. Once output began, the median generation speed of both was close, at more than 120 tokens per second.
This shows that the gap is not mainly about how fast tokens are generated. It is about where the time before the first word is spent.
2. What P90 First-Token Latency Measures
P90 first-token latency is calculated as follows:
- Record the time from when each request is sent to when the model outputs its first token.
- Sort all request wait times in ascending order.
- Take the value at the 90th percentile as P90.
P90 is chosen instead of the average because interactive scenarios are extremely sensitive to the slow tail. Only when 90% of requests start responding quickly does the user experience feel stable. A small number of extremely slow requests can noticeably hurt that experience.
3. Where the Wait Comes From: Prefill and Prefix Caching
In real coding sessions, multiple rounds of context are sent to the server together. In the test, there were 16 concurrent requests, with a median input of about 89,000 tokens per request. The model must first “read” the input before it can reply. This step is called prefill.
If the beginning of a new request is highly consistent with the previous round, the engine can reuse previously computed intermediate results and only process the new content. This is prefix caching. Its core benefit is turning repeated long-context computation into a one-time cost, so later rounds only do incremental work.
Illustrative pseudocode:
if prefix_cache.hit(request):kv = prefix_cache.load()new_part = request.tokens[len(kv):]kv = prefill(new_part, base=kv)else:kv = prefill(request.tokens)prefix_cache.store(request.tokens, kv)first_token = decode(kv)
4. Reusable Optimization Techniques
- Improve prefix cache hit rate: keep the system prompt, tool descriptions, and fixed templates first, and put changing content later, to avoid “polluting” the prefix.
- Chunked prefill: split very long inputs into chunks to avoid scheduling blockage caused by a single oversized chunk.
- Batch strategy tuning: trade off between throughput and first-token latency. Interactive scenarios should limit the single-batch wait window.
- Memory and cache layout optimization: manage intermediate result caches properly to reduce swapping in and out.
- Priority scheduling: prioritize short requests that are “waiting for user input,” and defer long tasks.
- Observability: use logging services and monitoring alerts to separately track queue time, prefill time, first-token time, and generation speed, in order to locate bottlenecks.
5. Engineering Rollout Recommendations
- Establish a baseline first: fix the hardware, model, concurrency, and dataset, and measure P50/P90 first-token latency and generation speed separately.
- Then validate one item at a time: change only one variable each time, for example adjusting only the batch wait window, and observe the change in P90.
- Finally, lock in the strategy: write the prefix layout conventions, cache eviction policy, and scheduling priorities into the inference service configuration, and combine them with a container platform and message queue for elasticity and traffic shaping.
6. Conclusion
The essence of first-token latency is the governance of repeated computation and queue waiting. Through prefix cache reuse, chunked prefill, and batch strategy and scheduling optimization, engineering can compress the startup wait in interactive coding scenarios from over ten seconds to sub-second levels, while keeping generation speed basically unchanged. For developers, this means a more continuous train of thought and fewer context switches. For enterprises, it means a significantly more stable interactive experience on existing hardware.
