AI-Written Inference Engine: Lessons from Cutting P90 Time to First Token from 12 Seconds to 0.5

    This article examines a notable engineering case in which an AI coding assistant led the development of a custom LLM inference engine tailored to a specific workload. Under identical hardware, model, and replayed real-world coding sessions at 16-way concurrency, the mature open-source engine showed a P90 time to first token (TTFT) of about 12 seconds, while the new engine achieved roughly 0.48 seconds—a gap of more than 20x. The article explains what P90 TTFT means, breaks down where the latency comes from, discusses why replaying real sessions is a more credible validation method than synthetic benchmarks, and draws broader lessons about the feasibility of AI-written system software and the trade-offs between general-purpose and workload-specific engines.

    Why This Case Deserves Attention

    Large model inference services typically rely on mature, community-built inference engines to accept user requests, schedule GPUs to run models, and stream back generated results. This pipeline is so standardized that few teams would consider “writing their own.” A recent practice stands out: an AI coding assistant took the lead in developing a purpose-built inference engine for a specific scenario, validated against real workloads. On identical hardware with the identical model, replaying the same batch of real coding sessions at 16-way concurrency, a mature open-source engine delivered a P90 time to first token of about 12 seconds, while the new engine delivered roughly 0.48 seconds—a gap of more than 20x.

    The value of this result is not that “the new engine beat the old one.” It reveals two things. First, general-purpose inference engines leave enormous optimization headroom under specific workload patterns. Second, AI-assisted development has matured to the point where it can handle system-level software—provided the validation method is realistic and rigorous enough.

    What P90 Time to First Token Actually Means

    Time to first token (TTFT) is the waiting time between a client sending a request and the model emitting its first token. For interactive scenarios—such as AI coding assistants and live chat—this metric almost directly determines the experience: if nothing appears on screen for a long time after a user submits a question, the system simply feels frozen.

    The latency of any single request is subject to randomness, so engineering practice relies on percentile metrics. P90 means: sort all requests’ first-token waiting times from smallest to largest over a period, and take the value at the 90th percentile. In other words, 90% of requests begin responding within that time. P90 reflects the experience of “the unluckiest users” far better than the average—the average may look great, but if a small fraction of requests are severely delayed, P90 exposes the problem.

    In this case, a 12-second P90 means nearly one in ten requests waited more than 12 seconds before the first character appeared. In a coding context, the user has long since switched back to the editor to read code; no matter how fast the AI’s reply eventually arrives, it has lost its value.

    Where the Latency Comes From: Dissecting the 12s vs. 0.5s Gap

    Why does the same hardware and the same model produce latency an order of magnitude apart? From the perspective of a typical inference service architecture, time to first token is roughly the sum of the following components:

    1. Queueing delay. After entering the service, a request waits in a queue for scheduling. When concurrent requests share the same pool of GPUs, the scheduling policy determines whether requests are processed one by one serially or merged into a batch for parallel processing. If scheduling is inefficient, a newly arrived request may be stuck behind a previous batch of long requests: until the earlier request’s decode phase finishes, later requests simply wait. A 12-second-scale delay is very likely caused by queueing and batching granularity—long-output generation monopolizes compute resources, and new requests’ first tokens get dragged down with it.

    2. Prefill latency. Before generating the first token, the model must run a forward pass over the entire input prompt. The longer the prompt, the longer prefill takes. If the engine does not chunk the computation, prioritize it properly, or reasonably interleave prefill with the ongoing decode phase, long inputs naturally lead to slow first tokens.

    3. Resource and memory management overhead. KV cache allocation, memory fragmentation, and the efficiency of page-table-style management all affect whether a request can be admitted and run “immediately.” With coarse memory management, the engine may leave requests idling in the queue due to insufficient free memory.

    4. Abstraction overhead of the framework itself. To support a wide variety of model architectures and features, general-purpose engines accumulate layers of generic logic, conditional branches, and extension points along their code paths. This overhead may only be milliseconds per request in isolation, but it gets amplified once queueing effects stack up.

    An engine designed specifically for a workload like “replayed coding sessions” can make targeted trade-offs: for example, more aggressive request-level chunked prefill mixed with decode scheduling, so slices of a new request’s prefill can be interleaved into the batch currently generating; finer-grained memory and KV cache management so requests run as soon as they are admitted; and cutting unused general-purpose features to shorten the critical path. A P90 of 0.48 seconds is essentially the result of flattening each of these stages one by one.

    Validation Method: Why Replaying Real Sessions Beats Reading Code

    This case also highlights a methodological detail worth emphasizing: after the engine was written, validation did not consist of running a few synthetic benchmarks. Instead, it replayed real coding sessions—replaying the same batch of requests at 16-way concurrency on 4 high-end data center GPUs.

    The advantages of this approach:

    1. Realistic workload. Synthetic tests tend to use uniform input lengths and regular arrival patterns, which mask the characteristics of real scenarios: mixed long and short requests, bursty concurrency, and so on. In real sessions, some requests are tens of tokens of completion, others are refactoring tasks of thousands of tokens. This mixed load is exactly the best tool for exposing scheduling flaws.

    2. Fair comparison. Both engines run the same model, same hardware, same batch of requests, and same concurrency level. The only variable is the engine itself, so the latency difference can be attributed to engine design.

    3. Metrics aligned with the business. Choosing P90 TTFT rather than throughput shows the validation target was interactive experience, which matches the real demands of scenarios like coding assistants.

    The takeaway: when evaluating any inference service optimization, first define a workload and metrics that reflect the business, and only then talk about numbers.

    Broader Implications: A Viable Path for AI-Written System Software

    This case also answers a more macro-level question: can an AI coding assistant write system-level software like an inference engine? Judging from the process, a viable path looks roughly like this:

    1. Define clear boundaries. Do not chase the full feature set of a general-purpose engine. Trim requirements to the target workload pattern (such as a coding-session service for a fixed model), keeping the problem size within what AI can reliably complete.

    2. Focus AI on the hard core problems. The modules that determine performance—the scheduler, memory management, batch organization—are iteratively implemented by the AI under explicit constraints, while peripheral concerns like configuration, logging, and monitoring reuse mature components as much as possible.

    3. Drive with end-to-end metrics. Rather than auditing the correctness of every line of code, use metrics like P90 under real workloads as the acceptance criteria and let results speak. This “black-box acceptance plus metrics-driven” approach sidesteps the impracticality of manually reviewing massive amounts of system code line by line, and it forces implementation quality.

    Final Thoughts

    The gap between 12 seconds and 0.48 seconds looks like a score between two engines, but it is really a divide between two engineering philosophies: a general-purpose solution strives to cover everything, while a purpose-built one dares to cut everything; validation relies either on synthetic stress tests or on real workload replay. For teams building large model services, perhaps the biggest lesson from this case is this: first precisely characterize your workload and experience metrics, then decide whether to tune a general-purpose engine or build one tailored to your scenario. And as AI coding tools grow ever more capable, the cost of customization is falling fast.