Time to First Token (TTFT): Cutting Inference Latency from 12 Seconds to Under 1 Second

    This article explains why Time To First Token (TTFT), measured at the P90 level, often matters more than raw generation speed in interactive coding workflows. Drawing on a public agent benchmark, it shows how a self-built inference engine reduced P90 TTFT from about 12.03 seconds to 0.478 seconds while keeping median generation speed nearly unchanged. The core techniques covered are prefix caching and accurate cache accounting, followed by a practical optimization checklist: stable prefixes, context management, scheduling priorities for prefill versus decode, cache observability, elastic capacity, and tiered storage. The article also notes how container platforms, logging, and monitoring capabilities from Baidu AI Cloud can support this optimization path.

    1. The Overlooked Experience Metric: Time to First Token

    When developers work with AI coding tools, the most common loop is “paste logs, wait for a reply, edit code again.” What really shapes perceived efficiency is often not generation speed, but the blank interval between pressing Enter and seeing the first character on screen.

    The industry usually quantifies this with P90 Time To First Token (TTFT): sort the wait times of a batch of requests and take the 90th percentile. P90 is chosen over the average because the average is dragged down by a few extremely fast requests, hiding the long-tail experience; P90 captures “how long most users wait in the worst case.”

    In a public benchmark targeting agents, researchers gave a coding agent basic server code, 4 high-end accelerator cards, and 12 hours, asking it to build an inference engine under a restricted network environment, then validated it with a 16-way concurrent replay of real coding sessions. The results were representative:

    Metric Mature open-source solution Agent-built engine
    P90 TTFT ~12.03 s ~0.478 s
    Generation speed (median) ~125.0 token/s ~124.2 token/s

    Key takeaway: once both engines start “speaking,” their token output speed is nearly identical. The entire gap is concentrated “before they open their mouths.”

    2. Why the First Token Takes 10+ Seconds: Prefill and Context Growth

    Model generation has two phases. The first is prefill, which runs all input tokens through the network in parallel to produce the first token; the second is decode, which generates tokens autoregressively one by one. Prefill is compute-intensive, while decode is memory-access-intensive, so the two require completely different optimizations.

    The trouble with multi-turn coding sessions is that context only grows. In the test, the median single input reached 89,000 tokens, because source code, errors, and patches from earlier turns are resent with each request. Even if the user only says “move the button a bit to the right” this turn, the server still faces nearly 90,000 input tokens. With 16 sessions running concurrently, these prefill tasks compete for the same compute, queueing time is rapidly amplified, and long-tail latency spikes to the ten-second range.

    3. Saving 11 Seconds: Prefix Caching and Cache Accounting

    If the beginning of the current request is identical to the previous turn, the engine does not need to recompute. Instead, it reuses prior results and only processes the newly added tail. This is prefix caching. It works only when the request prefix is byte-for-byte identical, so the concatenation order of the system prompt, conversation history, and file contents must remain stable.

    Such models typically maintain two kinds of “computed results”: one retains prior context for later lookup, and another acts like a ledger balance, recording how far has been read and which positions can be reused. The engineering difficulty is not “whether a cache exists,” but “whether the ledger is accurate”: capacity eviction, concurrency isolation, and hit-rate accounting—any error turns reuse back into recomputation.

    Illustrative prefix-matching logic:

    1. def match_prefix(cached, incoming):
    2. n = 0
    3. for a, b in zip(cached, incoming):
    4. if a != b:
    5. break
    6. n += 1
    7. return n # length of the reusable prefix

    4. A Reusable Optimization Checklist

    1. Stable prefix: Put system instructions and tool descriptions first, and place changing content later; avoid timestamps and random IDs polluting the prefix.
    2. Context governance: Summarize and compress resolved turns, keeping only conclusions and key code snippets.
    3. Scheduling strategy: Separate scheduling priorities for prefill and decode so long inputs do not block short requests.
    4. Cache observability: Monitor hit rate, eviction rate, and cache usage; establish a baseline with logging services and monitoring alerts.
    5. Elastic capacity: Use container platforms and autoscaling to handle concurrency peaks and push long-tail latency back into a controllable range.
    6. Tiered storage: Offload reusable computed results to high-performance storage or object storage, balancing capacity and bandwidth.

    5. Conclusion

    Time to First Token is not a single-point parameter, but a result jointly determined by cache hit rate, context length, and scheduling strategy. Get these three right, and a ten-second wait can be compressed to sub-second while generation quality stays intact. For teams looking to build their own inference services or improve the AI coding experience, general-purpose capabilities from Baidu AI Cloud—such as container platforms, logging services, and monitoring alerts—can serve as the infrastructure to put this optimization path into practice.