Reducing Time to First Token from 12 Seconds to 0.5 Seconds

    This article examines how a reasoning engine built for AI-assisted coding reduced Time to First Token (TTFT) P90 from roughly 12 seconds to 0.478 seconds, while keeping throughput nearly identical to a mature industry solution. It explains why P90 matters for interactive workloads, how prefill and prefix caching affect TTFT, and lists the engineering techniques involved: session-level cache key design, chunked prefill, state swapping, continuous batching, paged memory management, and scheduling priorities. It closes with practical guidance for deploying such inference services in the cloud.

    1. The Overlooked Wait Before the First Word

    In real AI-assisted programming workflows, a single fix rarely happens in one exchange. A user pastes in source code and an error message, the tool responds with suggestions, the user applies them and reruns the code, the terminal throws a similar error, the new logs get appended to the conversation, and the request is submitted again.

    Every submission is followed by a blank stretch: the cursor blinks and the response is slow to appear.

    In engineering terms, this blank stretch has a concrete metric: Time To First Token (TTFT), the interval between sending a request and the model emitting its first token. TTFT determines how responsive an interaction feels.

    A widely cited public benchmark compared two conditions: the same model, 16 concurrent requests, and a replay of real coding sessions on four high-end accelerators. Under these conditions, a mature industry solution had a TTFT P90 of about 12 seconds, while a reasoning engine built by an AI coding tool within a limited timeframe had a P90 of just 0.478 seconds.

    It is worth noting that once generation began, throughput was nearly identical: median output was 124.2 tokens per second for one and 125.0 tokens per second for the other. The gap, in other words, was not in how fast each system could talk, but in how long it took to start talking.

    2. What P90 Means and Why It Matters

    To calculate P90, sort the TTFT of all requests in ascending order and take the value at the 90th percentile. This means 90% of requests begin responding within that time.

    Compared with the average, P90 is more sensitive to the long tail and therefore closer to the worst-case experience. The pain points of interactive workloads usually live in that tail: most requests are fast, but a few stall for ten seconds or more, and users conclude that “this tool is slow.”

    3. Where the 11.5 Seconds Go: Prefill and Prefix Caching

    When a single request enters the engine, the model must first “read” the input before it can respond. This step is called prefill.

    In coding sessions, source code, error messages, and patches from earlier rounds are all sent along with the request. The median input size is 89,000 tokens. If every round were recomputed from scratch, both compute cost and latency would grow linearly.

    This is where prefix caching becomes valuable: if the beginning of the current request matches the previous round, the engine can reuse the intermediate results it has already computed and process only the new segments.

    These precomputed results typically consist of two pieces of state: one that preserves earlier context so it can be revisited, and another that behaves like an account balance, accumulating as reading progresses. What truly determines long-tail behavior is the capacity, eviction, and hit strategy of these two states.

    4. Engineering Checklist

    1. Session-level cache key design: Use a hash of the system prompt plus the history prefix as the key, ensuring stable cache hits as new rounds are appended.
    2. Chunked prefill: Split very long inputs into chunks and interleave them with decoding, so a single request does not monopolize the device.
    3. State swapping: When GPU memory is insufficient, offload cold session states to host memory or a remote cache while keeping hot sessions resident.
    4. Continuous batching: Insert new requests at any time rather than waiting for a full batch to align.
    5. Paged memory management: Reduce fragmentation and increase the number of concurrent resident requests.
    6. Scheduling priority: Prioritize interactive requests over offline batch jobs to compress the P90 long tail.

    Example (pseudocode):

    1. key = hash(system_prompt + history_prefix)
    2. state = cache.get(key)
    3. if state is None:
    4. state = prefill(chunked(new_tokens))
    5. cache.put(key, state, evict_policy="lru")
    6. first_token = decode_step(state)

    5. Deployment Recommendations for the Cloud

    Enterprises building their own systems can combine general-purpose capabilities: a container platform to host engine instances, object storage for session snapshots, a message queue to smooth traffic spikes, and logging, monitoring, and alerting services to track P90 and cache hit rates.

    In building this type of inference service, Baidu AI Cloud typically uses three metrics as an acceptance baseline: cache hit rate, TTFT P90, and throughput.

    6. Conclusion

    Time to First Token is not a matter of chance; it is the engineering outcome of prefill and state reuse. When cache keys, chunking, state offloading, and scheduling are implemented carefully, the leap from 12 seconds to 0.5 seconds is no accident.