LLM Inference Optimization: Cutting Time to First Token from 12 Seconds to 0.5 Seconds
This article explains why time to first token (TTFT) matters in AI coding assistants. It covers a benchmark study in which an inference engine written by an AI agent cut P90 TTFT from about 12 seconds to about 0.5 seconds on the same hardware, model, and workload, with generation speed nearly unchanged. The article walks through the experiment design, breaks down where latency comes from, including scheduling, prefill, KV cache reuse, and memory management, and lists practical lessons for teams that run LLM inference services.
Where Do Those Dozen Seconds Go Before AI Starts Responding?
Picture a routine debugging session. You ask an AI coding assistant to analyze a log file with a login error. It suggests a fix, you apply it, and you run the code again. The same red error appears in the terminal. You paste the new log back into the chat, press Enter, and wait while the cursor blinks.
Fixing one bug often takes several rounds like this. In each round, the part that wears down your patience is usually not the model writing out its answer. Once the output starts, it is typically fast. The real delay is the gap between sending your request and seeing the first character. In engineering, this gap has a specific metric: Time To First Token (TTFT).
A recent benchmark study on whether AI agents can build AI agents put this metric in the spotlight. An inference engine written autonomously by a coding agent reduced P90 TTFT from about 12 seconds to about 0.5 seconds. The hardware, model, and real-world workload were the same for both engines. That is a reduction of about 96%. The result matters beyond the fact that AI can write code. It shows how much room for optimization exists in the inference engine layer.
1. How the Experiment Was Designed
The measurement method determines how much the conclusions can be trusted, so it is worth looking at first.
The research team gave the agent a basic server codebase, several high-performance inference GPUs, and about 12 hours of development time. External web search was disabled. The agent therefore could not copy the implementation of an existing open-source engine. It had to design the system from scratch, using only the knowledge it already had.
The task was to build a complete inference service. The service had to accept user requests, schedule GPUs to run the model, and stream the generated response back to the client.
The evaluation also deserves attention. Instead of relying only on code review or unit tests, the researchers stress-tested both engines by replaying real coding sessions. Both engines ran the same model on the same number and type of GPUs. They processed the same set of 16 concurrent real coding session requests, and their results were compared.
The core metric was P90 TTFT. To calculate it, the researchers sorted the wait time of every request, from submission until the model produced its first token, from smallest to largest. The value at the 90th percentile is P90. In other words, 90% of requests began receiving a response within that time. Compared with the average, P90 better reflects the experience of long-tail requests. In debugging, the longest and most complex requests are the ones most likely to show extreme delays.
2. Results: Latency Dropped to 4% While Generation Speed Stayed Nearly the Same
The comparison results were clear:
- Time to first token (P90): about 12 seconds for a mature, mainstream open-source inference engine, and about 0.48 seconds for the agent-built engine.
- Generation throughput: the median output speed of both engines was nearly identical, at roughly 120 tokens per second or slightly more.
Taken together, these numbers are informative.
What does 12 seconds feel like? After you press Enter, you have probably switched back to your editor, scanned a few more lines of code, and started to wonder whether the request was sent at all. At 0.5 seconds, your finger may still be on the Enter key when the first character appears in the chat window. The experience shifts from feeling like batch processing to feeling like a conversation.
Equal generation speed shows that the agent-built engine did not gain its faster start by sacrificing later performance. The 11-plus seconds it saved all came from the steps before the request reaches model computation. That points to the part of inference engine engineering that is most often underestimated.
3. What Makes Up TTFT: Where the Time Goes
To understand where those 11 seconds were saved, it helps to break down what happens between the moment a request reaches the server and the moment the first token is returned.
1. Queuing and scheduling overhead
When a request arrives, it usually does not start computing right away. It waits in a scheduling queue to be grouped into a batch. The scheduler decides which requests go into the current batch, whether GPU memory has room for the new request’s KV cache, and whether any existing request should be preempted or moved. A conservative scheduling policy, such as waiting for the entire current batch to finish before admitting new requests, lets long requests block short ones. This head-of-line blocking pushes up P90 directly. Continuous batching, which is widely used in the industry, is a key way to reduce queuing delay. After each decoding step, new requests can join the batch, and completed requests leave immediately.
2. Prompt processing (prefill) overhead
Before the first token can be generated, the model must process the entire input context. Coding sessions often have very long contexts. Source code, conversation history, and error logs can easily add up to tens of thousands of tokens. Prefill is a compute-intensive stage, and its duration is roughly proportional to the context length. If the engine does not split long inputs into chunks (chunked prefill), one large request can monopolize compute resources and make the other requests in the batch wait. This is a common reason P90 rises.
3. KV cache and prefix reuse
This is the most important clue in the debugging scenario. Go back to the example at the start. You pasted the log five times in a row, but in each request, more than 90% of the content, including source code and conversation history, was exactly the same as the previous round. Only a few hundred new words were added at the end. If the engine recomputes the full context from the beginning every round, each round pays the full prefill cost. If the engine supports prefix caching, it can recognize the prefix shared with earlier requests and reuse the KV cache it already computed. The prefill workload then drops from tens of thousands of tokens to a few hundred new tokens, and TTFT falls by an order of magnitude.
For multi-turn coding sessions, which have long shared prefixes and small incremental changes, prefix reuse is probably the single optimization with the largest payoff. Most of the gap between 12 seconds and 0.5 seconds is likely due to this. The experiment design was effective for this reason too. Replaying real sessions exposed this workload pattern completely.
4. Memory management and fragmentation
Allocating, reclaiming, and defragmenting KV cache memory also affects latency. Managing the KV cache with paging ideas, such as paged attention, is now an industry consensus. However, the allocation granularity, fragmentation handling, and eviction priority in a specific implementation can magnify latency differences that users notice, especially with long contexts and high concurrency.
4. Practical Lessons for Inference Engineering
Setting aside the specific experiment, the result offers several general lessons for teams building or optimizing inference services.
First, measure before you optimize. TTFT, inter-token latency, and end-to-end latency are three separate metrics. The bottleneck can sit in any of them. Watching the long tail with P90 rather than the average is how you find the problems that actually affect user experience.
Second, understand your workload. Multi-turn chat, coding assistants, and document question answering share a common trait: very long shared prefixes. For these workloads, prefix caching and incremental prefill yield far more benefit than adding raw compute. Conversely, if your workload consists mainly of independent short requests, the optimization focus should shift to scheduling and batching efficiency.
Third, scheduling policy is performance you get without extra cost. Continuous batching, chunked prefill, and separate scheduling for long and short requests do not change the model or require more hardware. They are purely software decisions. Yet they often deliver a several-fold improvement in TTFT.
Fourth, treat the inference engine as a first-class component. Many teams treat their inference framework as a black box and assume its default performance is the ceiling. This experiment suggests that an engine tailored to a specific workload profile can outperform a general-purpose engine by an order of magnitude. For teams that run inference services on cloud container platforms with log services and monitoring and alerting systems, it is worth tracking TTFT as a core metric. Replaying real traffic in load tests can then guide the decision on whether to tune an existing framework or build the critical path in-house.
5. A Broader Implication: The Line Between Optimizer and Optimized Is Blurring
The most significant point is what the experiment says about who did the work. The engine was written not by a team of human engineers but by an AI agent. It was completed in 12 hours, without internet access, delivered in one pass, and validated against real workloads. This suggests that agents can handle system-level software engineering, not just business scripts.
When an agent optimizes the infrastructure that runs agents, a self-accelerating loop forms. Faster inference lets agents iterate faster, and stronger agents can write faster engines. In this experiment, the loop produced roughly a 25-fold improvement in TTFT.
For practitioners, the most useful response is not anxiety. Treat this result as a free architecture review. It clearly points to inefficiencies in the inference stack that have been overlooked for a long time. The next 12-second wait may be hiding in your own service, waiting to be cut to half a second.
