Agent-Built Inference Engine Cuts Time to First Token from 12 Seconds to Half a Second
An agent-built inference engine reportedly reduced P90 time to first token from about 12 seconds to 0.48 seconds in a replay of concurrent coding sessions, while maintaining nearly identical generation speed. This article examines prefill, prefix caching, and workload-specific scheduling to explain where such latency savings come from and what they mean for LLM serving.
1. The Problem: Waiting for AI to Start Responding
A familiar AI-assisted debugging session goes like this: you paste source code and an error log into a chat, receive a suggested fix, apply it, and run the program again. The terminal prints another error—perhaps the same one. You paste the new log into the conversation, press Enter, and wait.
A difficult bug can require several rounds of this process. Each round includes the same pause: the interval between submitting a request and seeing the first token of the response.
A benchmark released by researchers from universities and a chip manufacturer, exploring whether agents can build agents, included a task directly relevant to this problem. The researchers provided starter server code, several high-end GPUs, and a 12-hour time limit. Without searching the web for documentation, a coding agent had to build a complete inference engine that could accept requests, schedule model execution on GPUs, and return generated responses to clients.
The evaluation focused on performance rather than code style. The agent-built engine and an established open-source inference engine replayed the same real-world coding sessions at 16-way concurrency, using the same model and hardware. The key metric was time to first token (TTFT).
The reported results were striking:
- Established engine: approximately 12 seconds at P90 TTFT.
- Agent-built engine: approximately 0.48 seconds at P90 TTFT.
- Median generation speed: roughly 124–125 tokens per second for both.
The difference was concentrated almost entirely in the time before generation began. Where did those roughly 11.5 seconds go?
2. Where Time to First Token Comes From
Understanding the optimization starts with the two main phases of LLM inference: prefill and decoding.
Prefill: Processing the Input Before Responding
Before generating a response, a language model must process the input it receives. This phase is called prefill. Longer inputs generally require more work, making prefill a major contributor to TTFT.
Agentic coding workloads amplify this problem. On each turn, the client typically sends the full context again: system instructions, previous messages, source code, error logs, and attempted fixes.
In the replayed sessions, the median input length per request at 16-way concurrency was 89,000 tokens.
A user might add only a short instruction such as “move the login button slightly to the right,” yet the server receives nearly 90,000 tokens of context. Without effective reuse, much of that context must be processed again. With 16 concurrent requests, prefill work and queueing can combine to produce substantial tail latency.
Decoding: Generating the Response
After prefill, the model generates its response token by token. This is the decoding phase.
Decoding primarily determines how quickly the response appears once generation is underway. That distinction helps explain why the two engines could have nearly identical generation speeds despite very different TTFT results: their effective performance during decoding was similar.
For this workload, the main optimization opportunity was before sustained decoding began, particularly in prefill and the scheduling around it.
3. The Core Optimization: Prefix Caching
How Prefix Caching Works
During prefill, the model computes intermediate attention states stored in a key-value cache, usually called the KV cache.
The useful observation is that successive requests in a conversation often share a long, identical beginning. System instructions remain the same, earlier messages remain the same, and new content is appended at the end.
If an engine retains the relevant KV cache, it can reuse the states for an exact matching prefix rather than recomputing them. Only the newly appended portion needs additional prefill work. This mechanism is called prefix caching.
Turn 1: [System instructions][Source code][Error log]-> Prefill the full input and cache the KV statesTurn 2: [System instructions][Source code][Error log][Fix discussion]-> Reuse the cached prefix and prefill only the new contentTurn 3: [System instructions][Source code][Error log][Fix discussion][New error]-> Reuse a longer cached prefix
For requests with a median input length of 89,000 tokens, a sufficiently high prefix-cache hit rate can reduce the amount of fresh prefill work to hundreds or thousands of tokens per turn.
That can reduce prefill computation by one or two orders of magnitude, helping move TTFT from seconds into the subsecond range.
Why a General-Purpose Engine Can Be Slower
Established open-source engines are not necessarily missing prefix caching. They must, however, balance many competing requirements:
- Support for a broad range of model architectures.
- Complex batching and request scheduling.
- Multiple attention backends.
- General-purpose GPU memory management.
Those choices make an engine useful across many workloads, but they do not guarantee optimal behavior for a particular request pattern.
A workload consisting of 16 concurrent, long-context sessions with small additions on each turn places unusual importance on cache retention, memory allocation granularity, and scheduling priorities.
The agent-built engine was tailored to that pattern. Its cache management, GPU memory reservation, and scheduling were organized around the expectation that successive requests would reuse long prefixes.
The broader lesson is not that an agent-built engine is universally faster than an established one. It is that workload-specific optimization can outperform a general-purpose implementation on the workload it targets.
4. Practical Lessons for LLM Serving
The engineering implications extend beyond the novelty of an agent writing an inference engine.
1. Measure Before Optimizing
Track TTFT at P50, P90, and P99, and identify how much latency comes from queueing, prefill, and network overhead.
An improvement in average latency can hide a poor tail-latency experience. Percentiles help reveal what users encounter during slower requests and keep optimization efforts focused on the actual bottleneck.
2. Prioritize Prefix Caching for Stable, Growing Contexts
Prefix caching is a major optimization opportunity when requests repeatedly append content to a stable history.
Multi-turn conversations, coding agents, and document question-answering workflows can exhibit this pattern. Evaluate both the cache hit rate and the capacity available to retain reusable prefixes.
3. Coordinate Context Handling with the Server
Resending the full conversation history is common in agent workflows. A server that reliably recognizes and reuses matching prefixes can avoid much of the redundant computation this request pattern would otherwise cause.
It does not eliminate the transmission of that repeated context, but it can substantially reduce the server-side cost of processing it.
4. Treat KV Cache Memory Management as a Core Design Problem
Cache reuse is not free. Cached states must remain resident in GPU memory, and that memory is limited.
Eviction policies, such as least recently used (LRU), and session-affinity scheduling therefore matter. If useful prefixes are repeatedly evicted or requests cannot access their cached states, the expected benefit can disappear.
5. Weigh Generality Against Specialization
A general-purpose engine is designed to handle diverse requirements. A specialized implementation can make different trade-offs when the workload is narrow and latency requirements are demanding.
In those circumstances, adapting cache management and scheduling to the request pattern may offer more headroom than tuning general-purpose settings alone.
5. Conclusion
The shift from approximately 12 seconds to half a second was not about making token generation dramatically faster. It was about avoiding repeated work before generation started.
The central lesson is that LLM serving bottlenecks often sit in the engineering around the model: how inputs are structured, how cached states are reused, and how requests are scheduled.
Understanding prefill and prefix caching provides a practical foundation for optimizing long-context inference. Whether an engine is built by an agent or refined by engineers, matching its design to the workload remains a powerful way to reduce latency.
