Reducing Inference Engine Time to First Token from 12 Seconds to 0.5 Seconds

    This article breaks down how an inference engine cut P90 Time to First Token (TTFT) from about 12 seconds to roughly 0.48 seconds while keeping generation speed unchanged. It explains what TTFT and the P90 percentile measure, where the pre-generation delay comes from, and three practical optimizations: prefix caching and context reuse, smarter scheduling and batching, and moving one-time startup costs out of the request path. A reusable tuning checklist is included for teams optimizing interactive AI coding workloads.

    Reducing Inference Engine Time to First Token from 12 Seconds to 0.5 Seconds

    Introduction: What Users Perceive as “Slow” Happens Before the First Word

    In AI-assisted coding, a user’s first impression of responsiveness is determined almost entirely by time to first token. An often-overlooked fact is that once models actually start generating, token throughput is similar across solutions. What really separates the experience is the blank gap between sending a request and the model emitting its first word.

    A benchmark for evaluating agent capabilities offers a concrete example. In a task that asked an agent to build an agent on its own, the system under test was given a basic server codebase, four high-end accelerators, and a 12-hour window, with internet access disabled in the development environment. It had to write an inference engine capable of receiving requests, scheduling GPUs, running the model, and returning results. During evaluation, two engines were compared on identical hardware, with the same model and the same concurrent request replay. The metric was P90 time to first token: sort all request wait times in ascending order and take the value at the 90th percentile, meaning 90% of requests begin responding within that time.

    The result: the mature industry solution had a P90 time to first token of about 12 seconds, while the in-house engine came in at about 0.48 seconds. Once generation began, median generation speed was 125.0 tokens per second versus 124.2 tokens per second, essentially identical. The entire 11.5-second gap occurred before the first word.

    1. Defining the Metric: What P90 Time to First Token Measures

    Time to first token (TTFT) is the interval from when the client sends a request to when it receives the first renderable token. P90 means sorting a batch of TTFT values in ascending order and taking the 90th percentile. Compared with an average, P90 exposes long-tail problems more clearly: a small number of requests may be delayed by queuing or repeated computation, the average can still look good, but users have already switched away.

    Choosing P90 over the mean is fundamentally a measure of predictable experience. Interactive coding sessions are continuous and multi-turn. Users switch back and forth between errors, source code, and proposed fixes, and any single long-tail stall breaks their flow.

    2. Where the 11.5 Seconds Go: Four Sources of Time to First Token

    1. Queue wait: the request has arrived but the scheduler has not assigned resources, influenced by concurrency limits and batching strategy.
    2. Context loading and preprocessing: reading session history, concatenating the system prompt, and running tokenization and template rendering.
    3. Prefill: a single forward pass over all input tokens to produce the first token. Its cost scales roughly linearly with input length and grows significantly with long contexts.
    4. Cold start and resource preparation: one-time overhead such as GPU memory allocation, weight loading, kernel compilation, and graph capture.

    With long contexts and high-concurrency replay, mature solutions tend to accumulate long tails in queuing and repeated prefill, which is the main cause of the 12-second range.

    3. Key Optimization 1: One New Sentence Should Not Trigger a Full History Re-Read

    The original scenario is highly representative: the user has already pasted source code and error messages and tried a few versions of a fix. This round adds only one sentence, such as “move the login button a bit to the right.” If the engine re-prefills the entire history, cost grows linearly with each turn.

    The common engineering solution is prefix caching and context reuse: cache the intermediate state corresponding to the stable prefix (system prompt, confirmed file contents, tool definitions), then compute only the new segment incrementally for subsequent requests. The logic looks like this:

    1. input = stable prefix (system prompt + confirmed source code + tool definitions) + incremental segment (this turn's sentence)
    2. if prefix fingerprint matches cache:
    3. reuse cached state, run prefill only on the incremental segment
    4. else:
    5. run full prefill and write to cache

    Combined with a session-level cache eviction policy (managed by recency of use and fingerprint hashing), time to first token for multi-turn short-increment requests drops from “recompute the whole thing” to “compute only the increment.”

    4. Key Optimization 2: Scheduling and Batching, So Requests Do Not Sit in the Queue

    Under high-concurrency replay, P90 long tails often come from scheduling. Practical, general approaches include:

    • Chunked prefill: split long inputs into chunks and interleave them with decoding steps, preventing long requests from monopolizing the device and blocking short ones.
    • Priority and fair queues: prioritize interactive short requests and deprioritize batch-style long requests to control the long tail.
    • Continuous batching: dequeue requests as soon as they finish and insert new ones immediately, improving device utilization.
    • Tiered GPU memory and cache management: keep hot prefixes resident in GPU memory and push cold data down to reduce repeated loading.

    These techniques do not change the model itself, yet they reduce the two “invisible overheads” of queuing and repeated computation.

    5. Key Optimization 3: Move One-Time Costs Out of the Request Path

    Cold start, weight loading, kernel compilation, and graph capture are one-time costs and should not count toward every request’s TTFT. The general strategy is service warm-up, capturing execution graphs in advance, keeping a resident cache pool, and completing resource pre-allocation during release, so user requests land on instances that are already “hot.”

    6. A Reusable Tuning Checklist

    1. Measure before optimizing: separately track queue, preprocessing, prefill, and generation time to locate the source of the long tail.
    2. Replace averages with P90 and percentile dashboards, and watch the worst 1% of sessions.
    3. Keep stable prefixes as far forward as possible and cache them; keep incremental segments short.
    4. Separate interactive traffic from offline traffic in different pools so they do not compete for resources.
    5. Move warm-up and graph capture earlier to shorten cold starts.
    6. Replay real sessions for regression testing rather than relying only on synthetic load tests.

    Conclusion

    Optimizing time to first token is fundamentally about removing repeated computation and unnecessary waiting from the critical path. Once an engine learns to reuse context, schedule fairly, and warm up in advance, the gap from 12 seconds to 0.5 seconds is no mystery. What users perceive is simply that after pressing Enter, the chat box starts moving immediately.