Cutting Inference Engine Time-to-First-Token from 12 Seconds to 0.5 Seconds

    This article breaks down how an AI coding agent reduced P90 time-to-first-token (TTFT) in a self-built inference engine from about 12 seconds to under 0.5 seconds. Based on a preprint benchmark that replayed real 16-concurrent coding sessions, it explains the root causes of prefill latency, then details three engineering optimizations—prefix caching, chunked prefill, and request scheduling with GPU memory management—along with reusable lessons for monitoring and tuning interactive AI workloads.

    Cutting Inference Engine Time-to-First-Token from 12 Seconds to 0.5 Seconds

    1. A Real Pain Point: Waiting for AI to Type the First Word

    You ask an AI coding tool to fix a login error. You apply the suggested change, rerun the tests, and the terminal throws the same red line again. You paste the new logs into the chat, hit Enter, and start waiting.

    How long? In a real coding session, this wait can stretch to more than ten seconds. By then, you have probably switched back to the editor, scrolled through a few lines around the error, and lost your train of thought. When the AI finally starts responding, you have to reload the context all over again.

    A recent preprint, The Singularity Benchmark: Can Agents Build Agents?, submitted by researchers from universities and a chip vendor, offers a data point every backend engineer should notice: on a task that asked an agent to build its own inference engine, one AI coding tool cut P90 time-to-first-token from about 12 seconds to under 0.5 seconds.

    This did not come from faster GPUs or a larger model. It came from engineering optimizations inside the inference engine itself. This article breaks down where those eleven and a half seconds went.

    2. How the Experiment Was Designed: Let the Agent Build the Engine

    The setup was deliberately tough. Researchers gave the agent a basic server codebase, 4 high-end GPUs, and 12 hours, with network access restricted so it could not look things up online. Using an AI coding tool, the agent had to write a complete inference engine that accepts requests, schedules the GPUs to run the model, and returns generated output.

    Once the engines were written, the researchers went beyond reading code. They ran replay tests with real coding sessions: each engine ran the same model on 4 GPUs and replayed the same batch of 16 concurrent real sessions.

    The core metric was P90 time-to-first-token. Sort the wait time from request dispatch to the model emitting its first token across all requests, then take the 90th percentile. It means 90% of requests start responding within that time. Compared with the average, P90 exposes long-tail stalls better, and the long tail is exactly what kills interactive experience.

    The results:

    • Mature solution: P90 time-to-first-token of about 12.029 seconds
    • Agent-built engine: P90 time-to-first-token of about 0.478 seconds

    Once responses began, the two engines generated at nearly the same speed, with medians of 125.0 and 124.2 tokens per second.

    The conclusion is clear: the gap is not in “how fast it talks,” but in “how long it waits before opening its mouth.”

    3. Root Cause Analysis: Where the 11.5 Seconds Go

    To understand the optimization, first understand what a request goes through inside the engine.

    1. Prefill: The Model Must Read the Context First

    Large model generation has two phases: prefill processes the input, and decode generates tokens one by one. Time-to-first-token is mainly determined by prefill.

    In a coding session, the previous chat turns are sent to the server together. In the test with 16 concurrent requests, the median single input reached 89,000 tokens. The model has to “read” all 89,000 tokens before it can start responding. This is the main source of the 12 seconds.

    2. Repeated Computation: Rereading the Same Context Every Turn

    The key observation is that the beginning of a new request overlaps heavily with the previous turn. You only add one line like “move the login button a bit to the right,” but if the engine does not reuse prior work, it recomputes all 89,000 tokens again.

    This is the problem prefix caching solves. The “computed result” is the intermediate computation record (KV Cache) left when the model reads the context. If the new request’s prefix matches history, the engine can reuse it directly and only process the new content.

    3. Scheduling and GPU Memory: Resource Contention Under Concurrency

    16 concurrent requests mean GPU memory must hold multiple caches at once, and compute units must switch among requests. Poor scheduling leads to request queuing, cache eviction, and memory fragmentation, all of which push up P90.

    4. Breaking Down the Three Optimizations

    Optimization 1: Prefix Caching, Turning Repeated Reading into a Lookup

    Prefix caching is content-addressed cache reuse. Key implementation points:

    • Cache key design: Use a hash of the token sequence prefix as the key and build an index block by block;
    • Block-level management: Store and reuse in fixed-length token blocks to reduce fragmentation;
    • Eviction policy: When GPU memory is tight, evict by policies such as least recently used;
    • Hit validation: Compare token by token to avoid incorrect reuse when prefixes look similar but differ in content.

    Illustrative logic:

    1. def prefill(request, cache):
    2. prefix_tokens = request.tokens
    3. matched = cache.match_prefix(prefix_tokens) # returns the longest matched prefix
    4. if matched:
    5. kv = matched.kv_cache
    6. remaining = prefix_tokens[matched.length:]
    7. else:
    8. kv, remaining = None, prefix_tokens
    9. new_kv = model.prefill(remaining, base_kv=kv)
    10. cache.insert(prefix_tokens, new_kv)
    11. return new_kv

    In coding sessions, where the context stays fixed and new content is appended at the end, the prefix cache hit rate is very high. This is the first contributor to the sharp latency drop.

    Optimization 2: Chunked Prefill, Preventing Long Requests from Monopolizing Compute

    Even on a cache miss, 89,000 tokens should not be computed all at once. Chunked prefill splits long inputs into small chunks and schedules them mixed with decode requests:

    • Prevents a single long prefill from blocking all decodes, improving concurrent tail latency;
    • Improves compute unit utilization;
    • Works with continuous batching to assemble batches dynamically.

    Optimization 3: Request Scheduling and GPU Memory Management

    • Priority and fair scheduling: Prioritize interactive requests so they are not starved by batch jobs;
    • Continuous batching: Swap out a request as soon as it finishes, without waiting for the whole batch;
    • GPU memory pooling: Preallocate a cache pool to reduce fragmentation and allocation overhead;
    • P90-oriented observability: Use percentile latency rather than the mean as the tuning target.

    5. Reusable Engineering Lessons

    1. Time-to-first-token is a core experience metric for interactive AI and should be included in monitoring and alerting, observed by P90/P99.
    2. Prioritize prefix caching for long-context scenarios, where it offers the best return on investment.
    3. Pay attention to scheduling and GPU memory management under concurrency to avoid the long tail.
    4. Benchmark with real session replays, which are closer to production than synthetic load tests.
    5. Once generation speed is adequate, shift the optimization focus to “before it opens its mouth.”

    For cloud deployment, you can run the engine on a container platform, store session and cache snapshots in object storage, and use logging and monitoring services for full-chain percentile observability, forming a closed loop.

    6. Conclusion

    From 12 seconds to 0.5 seconds, the essence is systematically eliminating the waste of “rereading long context” through caching, chunking, and scheduling. For developers, the takeaway is: to optimize interactive experience, measure time-to-first-token first, then focus on prefill and prefix reuse.