Engineering High-Speed LLM Inference: Practical Optimization Techniques for TTFT and Throughput
This article explains why high output speed and strong model capability are not mutually exclusive in LLM inference, and how to optimize them separately. It breaks end-to-end latency into TTFT and effective output rate, lists the metrics to collect before tuning, and covers key service-layer techniques including continuous batching, chunked prefill, KV cache management, prefix reuse, speculative decoding, quantization, and streaming delivery. It also outlines a layered production architecture, a rigorous comparison method, and common pitfalls to avoid.
1. The Problem: Why “Fast” and “Strong” Are Long Seen as a Trade-off
For a long time, inference serving has carried a default assumption: models tuned for high output speed are lightweight, smaller-parameter versions, while larger, more capable flagship models are assumed to be “slow.” The root of this trade-off is not mysterious—autoregressive decoding runs token by token in sequence, and each step re-runs attention over all previously generated content. The larger the parameter count and the longer the context, the higher the per-step cost.
From an engineering perspective, however, the “slowness” users perceive is not an inevitable cost of model capability. The end-to-end latency of a request can be split into two parts:
end-to-end latency ≈ TTFT (time to first token) + output tokens / effective output rate + network and queueing overhead
TTFT is determined by the prefill phase and correlates strongly with input length and compute parallelism. The effective output rate is determined by the decode phase and correlates strongly with memory bandwidth, batching strategy, and cache hit rate. Flagship capability lives in the weights and training strategy, while nearly all of the factors above belong to service-layer optimization. This is precisely why “flagship capability + high throughput” can hold at the same time in engineering.
2. Two Core Metrics Must Be Measured Separately
Much of the confusion in performance discussions comes from conflating two metrics.
- TTFT (Time To First Token): the time from sending a request to receiving the first token. It determines “how long until something responds after a click” and matters most for chat and completion interactions.
- Output rate (tok/s): the number of tokens generated per unit of time. It determines whether the token stream feels smooth. Human reading speed is roughly a few tokens per second, so once the rate reaches hundreds of tokens per second, output is faster than reading and rendering, and the user’s perception shifts from “waiting” to “instant.”
In engineering, set SLOs for the two as independent targets—for example, sub-second first token and a steady-state output rate of hundreds of tokens/s—and load-test them separately. Looking only at average total latency hides first-token stalls in long-input scenarios.
3. Locating Bottlenecks: Measure First, Then Optimize
The first step in optimization is knowing where the time goes. Collect at least the following metrics:
- Queueing latency: the time a request waits in the scheduling queue, often the largest hidden cost that gets overlooked.
- Prefill time vs. input length curve: to determine whether extra-long contexts are driving up TTFT.
- Per-step decode time vs. concurrent batch size curve: to see whether memory bandwidth is saturated.
- Cache hit rate: whether requests with identical prefixes actually reuse computed results.
- Chunk intervals in the output stream: whether the network or gateway is re-buffering streaming responses into whole packets.
A simplified illustrative flow:
# Pseudocode: latency breakdown instrumentation for a single requestt0 = now()slot = scheduler.acquire() # queueingt1 = now()first = engine.prefill(prompt) # first tokent2 = now()for tok in engine.decode(first): # token-by-token outputstream.send(tok)t3 = now()metrics.record(queue=t1-t0, ttft=t2-t1, decode=t3-t2)
4. Key Service-Layer Optimizations
4.1 Continuous Batching and Chunked Prefill
Traditional static batching requires all requests in a batch to start and finish together, so short requests must wait for long ones. Continuous batching dynamically swaps requests in and out at token or iteration granularity, keeping the GPU busy at all times. Combined with chunked prefill, which splits a long input’s prefill into small chunks interleaved with decode, it can significantly flatten the long tail of TTFT.
4.2 KV Cache Management and Prefix Reuse
The main cost of the decode phase is repeatedly reading the KV cache, so the cache’s memory layout and lifecycle management directly determine the concurrency ceiling. Paged cache management reduces fragmentation, and prefix sharing lets system prompts, fixed templates, and the same code repository context be computed only once. For coding scenarios, hit rates are often high and the gains are clear.
4.3 Speculative Decoding and Parallel Sampling
Use a small draft model or a lightweight prediction head to guess several consecutive tokens, then have the large model verify them all at once; if accepted, it advances multiple steps directly. Because verification is parallel, a decent hit rate can yield several-fold speedup, and the output distribution stays consistent with the large model, with no loss of capability.
4.4 Quantization and Operator Optimization
Weight quantization reduces memory reads, which directly benefits the bandwidth-bound decode phase. Operator fusion for hotspots such as attention and normalization, along with efficient attention kernels, reduces launch overhead and intermediate reads and writes.
4.5 Streaming Transport and Front-End Rendering
If any one of the three is missing—the server sending chunks incrementally, the gateway disabling response buffering, and the client rendering incrementally—the backend’s high rate gets held back. The real-time creation experience of “editing while talking and watching while editing” in demos is essentially the combined result of low TTFT + high output rate + an end-to-end streaming pipeline.
5. Validating Results: How to Run a Credible Comparison
The original piece presents the gap through parallel side-by-side runs of the same task. This approach is worth borrowing, but it should be done rigorously:
- Fix the model version, prompt, decoding parameters, and input length, changing only the service configuration;
- Record TTFT, steady-state tok/s, total duration, and failure rate at the same time;
- Distinguish cold start from warm start, and report the cache hit rate;
- Use aggregate throughput (total tok/s under concurrent multi-role/multi-session load) to measure capacity planning, and single-request rate to measure interaction experience; the two cannot substitute for each other.
A typical side-by-side result looks like this: for the same page-generation task, the high-speed configuration finishes and renders within a minute, while the standard configuration takes several times longer. The gap does not come from a “smarter” model, but from more tokens produced per unit of time.
6. Recommended Architecture for Production
To build similar capability in your own system, a layered design can be used as a reference:
- Access layer: authentication, rate limiting, and separate queues by input length and priority to prevent long requests from blocking short ones.
- Scheduling layer: continuous batching + priority preemption + chunked prefill.
- Inference layer: paged KV cache, prefix reuse, speculative decoding, and quantized kernels.
- Transport layer: streaming protocol, incremental rendering, and resume-after-disconnect.
- Observability layer: call-chain instrumentation, metrics dashboards and alerts, with capacity evaluated by aggregate throughput rather than request count.
When deploying on the cloud, these capabilities are typically carried by general-purpose components: container platforms handle elastic scaling and rolling releases, object storage holds models and artifacts, message queues smooth out request spikes and enable asynchrony, and logging plus monitoring and alerting handle full-link diagnosis.
7. Common Pitfalls
- Load-testing only short inputs: real long contexts can push TTFT up by an order of magnitude.
- Ignoring queueing: once concurrency rises, queueing latency often accounts for more than half of total latency.
- Judging experience by averages: interaction smoothness depends on the long tail, so look at P95/P99.
- Sacrificing capability for speed: with correct implementations, speculative decoding and quantization do not change the output distribution, so there is no need to choose between “fast” and “strong.”
8. Conclusion
High throughput and low latency are not the credit of any single point technology, but a systems engineering effort across scheduling, caching, parallel decoding, and the streaming pipeline. Define TTFT and output rate separately, optimize them separately, and accept them independently—a flagship-class model can then run at extremely high speed while preserving capability, taking “speak and it happens” interactions such as real-time coding and real-time visual creation from demo to production.
