Reducing Inference Engine TTFT from 12 Seconds to 0.5 Seconds

    This article examines how an AI-built inference engine reduced P90 time-to-first-token (TTFT) from about 12 seconds to under 0.5 seconds in a benchmark using identical models, hardware, and concurrent requests. It breaks down where first-token latency comes from, explains techniques such as prefix caching, chunked prefill, and continuous batching, and offers a practical tuning checklist for developers working with interactive AI and cloud deployments.

    1. The Problem: Cutting Idle Waiting by More Than 20x

    In a recent preprint benchmark evaluating agent capabilities, researchers gave an AI coding tool basic server code, four high-performance accelerators, and a 12-hour time limit. The task was to build an inference engine from scratch in an offline environment: accept requests, schedule GPUs, load models, and stream results back.

    The evaluation went beyond code review. It used replay stress tests based on real coding sessions: the same model, the same hardware, and the same batch of 16 concurrent requests were sent to both the self-built engine and a mature industry solution.

    The metric was P90 time-to-first-token (TTFT): sort all requests by the wait time from dispatch to the first generated token, then take the 90th percentile. The difference was substantial. The mature solution measured about 12.029 seconds, while the self-built engine measured about 0.478 seconds. Once generation began, however, median throughput was nearly identical (124.2 vs. 125.0 tokens/s). The gap was not about how fast tokens were generated, but how long each request waited before the first word appeared.

    The difference in user experience is immediate. Twelve seconds is enough time to switch back to your editor and scan a few lines of error output. Half a second means the first token is already in the chat box right after you press Enter.

    2. Where First-Token Latency Comes From

    First-token latency typically consists of four stages:

    1. Queue wait: the time from when a request enters the scheduler to when it is assigned to a specific replica.
    2. Prefill: a single forward pass over the input prompt to generate the KV cache. The compute cost scales roughly with the number of input tokens.
    3. Weights and cache readiness: whether model weights are in GPU memory, whether the KV cache hits and can be reused, and whether swapping in or out is required.
    4. Sampling and network return: first-token sampling, establishing the streaming channel, and first-packet network overhead.

    Real coding sessions have a distinct pattern: multiple turns, long context, and highly repetitive prefixes. You may have pasted source code, error messages, and several rounds of proposed fixes, and in the current turn you only add one line like “move the login button a bit to the right.” If the entire history is prefilled again on every turn, prefill costs accumulate linearly, and P90 latency gets pulled up by long-tail requests.

    3. The Key: Eliminating Repeated Computation

    The self-built engine was able to reduce long-tail latency to sub-second levels not because it used faster operators, but because it computed less. Reusable general techniques include:

    • Prefix cache reuse: reuse the KV cache for identical prefixes and perform incremental prefill only for the small number of newly appended tokens.
    • Chunked prefill: split long prompts into chunks and interleave them with decode requests in scheduling, avoiding head-of-line blocking caused by a long request monopolizing the device.
    • Continuous batching: dynamically batch requests at the request level to shorten queue time.
    • GPU memory and cache management: control cache eviction policies and reserve space to reduce swapping jitter.
    • First-token streaming priority: return the first token as quickly as possible, then smoothly stream the remaining output.

    Illustrative pseudocode:

    1. # Pseudocode: illustrates scheduling and cache reuse only
    2. def handle(request):
    3. prefix_key = hash(request.stable_prefix) # session history + code context
    4. cache = kv_cache.get(prefix_key)
    5. if cache is None:
    6. cache = prefill_chunked(request.stable_prefix, chunk=512)
    7. kv_cache.put(prefix_key, cache, evict_policy="lru")
    8. # Perform incremental prefill only for newly added content
    9. kv = prefill_incremental(request.new_tokens, base=cache)
    10. return stream_first_token(kv)

    4. An Actionable Tuning Checklist

    • Metrics first: monitor P50/P90/P99 TTFT and median throughput together so averages do not hide long-tail behavior.
    • Test close to real workloads: use multi-turn, long-context, concurrent replay sessions rather than single-turn short requests.
    • Stable prefix design: place unchanged system prompts and code context at the front and variable content at the end to improve cache hit rates.
    • Context slimming: carry only necessary files and snippets to avoid unnecessary prefill.
    • Capacity and elasticity: use container platforms for replica elasticity, and use monitoring alerts to track queue time and cache hit rates.
    • Object storage and logging services: store session context and stress-test reports to support regression comparisons.

    5. Conclusion

    First-token latency is the “first impression” of interactive AI. When generation throughput converges, the optimization focus should shift from “compute faster” to “compute less and queue less.” For developers, adding P90 TTFT to your metrics and making prefix reuse and scheduling strategies solid can compress long idle waits to the point where they are barely noticeable. In cloud deployments, using common capabilities such as container platforms, monitoring alerts, and object storage for elasticity and observability can make optimization continuously verifiable and regression-testable.