AI Inference Engine Breakthrough: How First-Token Latency Fell from 12 Seconds to 0.5 Seconds
A self-built large language model inference engine reduced P90 time to first token from about 12 seconds to 0.478 seconds in a realistic 16-session coding workload. The improvement came not from faster token generation, but from efficient prefix caching and KV cache reuse during the prefill stage. This article explains the performance bottleneck and outlines practical optimization strategies for enterprise-scale LLM inference.
The Waiting Problem in AI-Assisted Coding
AI-assisted coding has become a standard part of many software development workflows. Consider a common scenario: you ask an AI assistant to fix a login error, apply its suggested code changes, and run the test again—only to see the same error appear in the terminal. You paste the latest logs into the chat, press Enter, and wait for the assistant’s next debugging suggestion.
Fixing a complex bug often requires several rounds of this interaction. How long do you have to wait before the first character of the AI’s response appears? If the delay is 12 seconds, you may switch back to your editor and spend that time tracing the error through the code. If the delay is only half a second, the response begins appearing almost immediately after you press Enter. That smoother experience can significantly improve developer productivity.
A recent preprint, Singularity Benchmark: Can Agents Build Agents?, reported a notable result. In a test based on real programming sessions, an advanced language model autonomously built an inference engine that reduced time to first token—the period users spend waiting for the response to begin—from approximately 12 seconds to 0.48 seconds.
The result not only demonstrated an agent’s ability to build a complex system, but also exposed an often-overlooked bottleneck in large language model inference services.
The Singularity Test: Can an Agent Build an Agent?
The research team explored a challenging question: can an agent autonomously build another agent?
For the experiment, researchers provided a large pretrained model with basic server code, multiple high-end AI accelerators, and a 12-hour time limit. To keep the test focused, the development environment imposed strict restrictions on access to online resources.
Using an AI programming tool, the model had to build a complete LLM inference engine from scratch. The engine needed to accept highly concurrent user requests, schedule the underlying hardware resources to run the model, and return generated responses accurately to users.
After the engine was completed, the researchers did not stop at code review. They deployed it in a realistic programming scenario and subjected it to a demanding performance test.
The evaluation replayed 16 concurrent coding sessions. Under the same hardware conditions and with the same model weights, the researchers compared the agent-built inference engine with a mature, widely used open-source inference framework.
Why P90 Time to First Token Matters
P90 time to first token is a critical user experience metric for LLM inference services. It is calculated by sorting all request-to-first-response delays and taking the value at the 90th percentile.
In practical terms, a P90 of 0.5 seconds means that 90% of requests begin producing a response within half a second. This kind of responsiveness is important for maintaining a developer’s flow during interactive work.
The test results were striking:
- Mature open-source inference framework: approximately 12.029 seconds P90 time to first token
- Agent-built inference engine: 0.478 seconds P90 time to first token
Once generation began, however, the two engines performed almost identically. Based on the median, the self-built engine generated 124.2 tokens per second, compared with 125.0 tokens per second for the existing framework.
If their generation speeds were nearly the same, how did the self-built engine eliminate roughly 11.5 seconds of waiting before generation began? The answer lies in request preprocessing.
How the Engine Saved 11.5 Seconds
The process of handling user input and generating a response is generally divided into two stages: prefill and decoding.
1. The Computationally Expensive Prefill Stage
Imagine using an AI assistant to modify a webpage. You have already pasted a large amount of source code and error information and tried several proposed solutions. In the latest request, you only add: “Move the login button slightly to the right.”
In the replayed coding sessions, the previous conversation history was sent to the server as context with every new request. Under the 16-way concurrent workload, the median input length per request reached 89,000 tokens.
Before the model can formulate a response, it must process this entire input. This computation is known as prefill. Because it involves large matrix operations across a long input sequence, prefill is typically compute-intensive and can take a significant amount of time.
2. The Key Optimization: Prefix Caching
If the beginning of a new request is identical to the context processed in the previous request, the inference engine does not need to calculate it again. It can reuse the previously computed intermediate results and process only the newly added content. This technique is known as prefix caching.
The cached results are the model’s KV Cache, which stores key-value pairs generated while processing the input. In a multi-turn conversation, the KV Cache for the historical context remains unchanged. Recomputing the KV Cache for 89,000 tokens on every request would waste a substantial amount of computing capacity.
The self-built engine reduced latency to roughly half a second by implementing prefix caching highly efficiently. When the user sent “Move the login button slightly to the right,” the engine recognized that the first 89,000 tokens had already been processed. It retrieved the corresponding KV Cache from GPU memory, performed prefill only for the newly added tokens, and then moved directly into the decoding stage.
Engineering Practices for Enterprise LLM Inference
The test results point to several practical directions for optimizing large-scale LLM inference services. Enterprise applications can reduce first-token latency through the following approaches.
1. Optimize KV Cache Storage and Scheduling
When deploying an LLM on a container platform or GPU cloud server, use efficient memory management strategies to maximize KV Cache reuse. Eviction policies such as LRU, or least recently used, can help keep frequently accessed multi-turn conversation caches in GPU memory and avoid repeated computation.
2. Use Continuous Batching
Traditional static batching waits until enough requests have accumulated—or until a timeout occurs—before processing them together. This introduces queueing delay.
Continuous batching can dynamically insert new requests while the model is generating existing responses. New requests can then begin prefill without waiting for the current batch to finish, significantly reducing P90 latency.
3. Apply Cloud-Native Monitoring for Dynamic Tuning
Use cloud logging, monitoring, and alerting capabilities to track fluctuations in time to first token. When a surge in concurrency causes the cache hit rate to fall, monitoring systems can trigger elastic scaling and add inference replicas. This helps maintain low-latency responses under heavy workloads.
Conclusion
The reduction from 12 seconds to 0.5 seconds was more than a demonstration of an agent’s ability to write complex code. It was an engineering achievement based on a deeper understanding of LLM inference internals.
As AI applications become more common, teams that make better use of hardware and optimize prefill and prefix-caching strategies will gain an advantage in user experience. The future of AI inference services will continue moving toward near-zero-latency interaction.
