Time to First Token (TTFT): From 12 Seconds to Sub-Second Latency in Inference Engines

    This article explains why time to first token (TTFT) often has a larger impact on user experience than overall generation speed. Drawing on a benchmark replay of real coding sessions, it compares a mature inference engine with an AI-agent-written engine, shows how TTFT differed from about 12 seconds at P90 to sub-second response time, and breaks down the key optimizations behind the improvement: prefill bottlenecks, prefix caching, continuous batching, chunked prefill, and practical engineering recommendations for developers and enterprises.

    1. The Overlooked Wait: Why TTFT Shapes Experience

    In real AI-assisted coding scenarios, interactions are rarely a single question-and-answer exchange. They usually become a series of back-and-forth steps: you paste an error, get a suggestion, modify the code, and paste new logs. Every time you press Enter, you are waiting for the system to “speak.” What affects the perceived experience most is often not how fast the final output can be generated, but how quickly the model produces its first word.

    The time from sending a request to receiving the first token from the model is known as time to first token (TTFT). It is different from overall generation speed. A benchmark replay test illustrates this clearly: under the same hardware and model, two inference engines replayed the same batch of real programming sessions. One mature engine had a P90 TTFT of about 12 seconds, meaning that 90% of requests could begin responding within that time. The other engine, autonomously written by an AI agent after being provided with the target environment, achieved sub-second TTFT. After 12 seconds, you may have already switched back to the editor and scrolled through several lines of the error message. With half a second, the first word of the reply appears as soon as you press Enter.

    2. Where the Latency Comes From: The Lifecycle of an Inference Request

    Before optimizing latency, you first need to understand where the time is spent. A large-model inference request typically has two stages:

    • Prefill: The model reads and processes all input tokens, building the intermediate states needed for subsequent attention calculations.
    • Decode: The model generates the reply token by token until completion.

    TTFT is almost entirely determined by the prefill stage. A common scenario in the test was “AI helps modify a webpage.” In earlier turns, source code, error messages, and several proposed solutions had already been pasted into the conversation. In the latest turn, the user only added something like “move the login button a bit to the right.” In other words, most of the new request’s input was identical to the previous turn. Under high concurrency, the median single-request input length reached about 89,000 tokens. Processing nearly 90,000 tokens was the main source of those “several seconds before the system starts speaking.”

    3. Prefill: The Most Underestimated Compute Bottleneck

    Prefill is a highly parallel matrix-computation process, and its time cost is roughly proportional to the input length. When the context grows to tens of thousands of tokens, even with sufficient GPU compute capacity, simply passing all those tokens through the neural network takes a considerable amount of time.

    More importantly, prefill is compute-bound, while decode is memory-bound, and the two have very different resource characteristics. If an engine simply queues requests of different lengths and performs large prefill operations in bulk, it can create situations where “short requests wait for long requests” and GPU memory is underutilized. As a result, P90 tail latency can become even worse.

    4. Prefix Caching: Avoiding Repeated Work

    Since a large portion of the input in multi-turn conversations is repeated, the most direct optimization is to avoid recomputing it. This is where prefix caching comes in.

    During prefill, the model computes a set of key-value tensors, or KV cache, for each token, and these are later used in attention calculations. If the beginning of a new request, the prefix, matches a previous request token for token, the engine can reuse the existing KV cache and process only the newly added part.

    1. Previous input: [system prompt + source code + error logs + Option A]
    2. Current input: [system prompt + source code + error logs + Option A] + [move the login button a bit to the right]
    3. ↑__________ Reusable __________↑ ↑__ only new tokens need processing __↑

    In this example, the prefix of nearly 90,000 tokens does not need to be recomputed. Only the few dozen or hundred newly added tokens at the end actually need prefill. The prefill workload may therefore drop by one to two orders of magnitude, and TTFT can collapse from “over ten seconds” to around “half a second.”

    For prefix caching to work effectively, several engineering conditions must be met:

    • The prefix must match exactly, token by token.
    • Efficient cache management and eviction policies are needed to keep as many hot prefixes as possible within limited GPU memory.
    • Request routing should preferably send sessions with the same prefix to the same instance to avoid lowering the cache hit rate.

    5. Scheduling and Batching: Keeping Memory and Compute Busy

    Caching alone is not enough. Under high concurrency, scheduling is equally important. Traditional static batching requires a group of requests to arrive together and proceed in lockstep, which means long requests can slow down the entire batch.

    A better approach is continuous batching. The scheduler dynamically adjusts the batch at each decode step. When a sequence finishes generation, it immediately exits, and the freed slot is quickly filled by a queued request. This keeps the GPU continuously busy.

    Combining this with sensible orchestration of prefill and decode—such as preventing a single extremely long prefill from monopolizing the GPU and blocking other requests that are already decoding—can achieve a better balance between throughput and latency.

    These scheduling strategies are often the result of long-term refinement in mature frameworks. An engine written over a short period may lack them. This also explains why the two engines in the test had almost the same steady-state generation speed, both around 120 tokens per second. The gap was almost entirely located before the first token.

    6. Chunked Prefill and Other Acceleration Techniques

    When the input is especially long, chunked prefill can be used. Instead of performing one massive prefill operation, the engine splits it into several smaller chunks and interleaves them with the decode steps of other requests.

    This avoids letting one long request “hog” the GPU for an extended period and delaying other users’ token output. It also helps balance compute-heavy and memory-heavy workloads across the pipeline, further reducing tail latency.

    Other common techniques aimed at faster first-token response include KV cache quantization, kernel-level optimization for attention computation, and request-level priority scheduling. Fundamentally, they all answer the same question: how to minimize repeated computation and improve resource scheduling so that the user-visible wait is as short as possible.

    7. Engineering Practice: Recommendations for Developers and Enterprises

    To apply these principles in real systems, the following optimization path can be followed:

    1. Measure first, then optimize: Focus on P90/P99 TTFT rather than only average generation speed. Tail experience is often where user complaints concentrate.
    2. Make full use of prefix caching: Keep system prompts stable, place large unchanged context at the beginning, and avoid inserting dynamic content early in a conversation that could break prefix matching.
    3. Use continuous batching: Allow short and long requests to run together without holding each other back.
    4. Use chunked prefill for very long contexts: Balance TTFT and overall throughput.
    5. Improve routing: Group sessions with similar prefixes onto the same instance to maximize cache hit rates.

    The significance of this test goes beyond a single impressive number. It shows that optimizations such as prefill cost control, prefix reuse, and dynamic scheduling are not limited to what senior engineers can manually build. After being given a clear objective and a defined environment, an AI agent can also construct a highly competitive system on key metrics. For enterprises and developers, this lowers the barrier to building high-performance inference services. It also highlights a broader point: what truly shapes the experience of AI applications is often the engineering detail before the first token appears.