How to Optimize LLM Inference Speed? A Practitioner's Guide (2026)
Back in 2023, I was sitting in a client meeting at a fintech company — let's call them FinFlow. They'd spent six months fine-tuning a 70B parameter model for trade signal extraction. Inference latency? 8 seconds per query. For a system that needed to respond in under 200 milliseconds. That's not a speed problem. That's a business-killer.
I've since spent three years building data pipelines and production AI systems at SIVARO. We've optimized inference for everything from 1B parameter edge models to 400B cluster monsters. Here's what actually works — and what doesn't.
Optimizing LLM inference speed isn't about one magic lever. It's a stack problem spanning model architecture, hardware, caching, batching, decoding strategy, and even your prompt design. This guide walks through each layer with concrete techniques, code, and hard-won trade-offs.
Quantization: The Obvious Win (But Watch the Tails)
Most people start here. They should. In 2025, we ran a benchmark: FP16 Mistral 7B vs. INT4 on an A100. Throughput jumped 3.2x. Latency dropped from 45ms to 18ms per token.
But here's the trap: quantization doesn't degrade uniformly. We saw a production system where INT4 caused a 0.7% drop in F1 score — acceptable. But on the 99.9th percentile tail of complex queries, error rate tripled. For AI art worth collecting platforms generating detailed prompts, that killed image quality.
Do this: Use AWQ or GPTQ for weights. Keep activations in FP16 unless you've profiled your specific task. We've settled on a hybrid: quantize all linear layers except the final LM head and attention projections. Custom kernel? Use vLLM's FP8 support — it's mature as of mid-2026.
python
# Pseudo-config for hybrid quantization in vLLM
from vllm import LLM
model = LLM(
model="meta-llama/Meta-Llama-3-70B",
quantization="awq", # weight-only INT4
kv_cache_dtype="fp8", # compress KV cache
max_model_len=8192,
gpu_memory_utilization=0.95,
enforce_eager=False # use CUDA graphs
)
Speculative Decoding: Why Is Speculative Decoding Faster?
I get this question every week. The short answer: it trades compute for parallelism.
Standard autoregressive generation is serial — one token at a time. Speculative decoding runs a tiny "draft" model (e.g., a 100M param) to propose K tokens in parallel, then the big model verifies them all at once. If the draft is right (70-90% of the time), you've just generated K tokens in the time of one forward pass.
In a 2024 experiment on CodeLlama 34B with a 117M draft, we saw 2.8x tokens-per-second improvement on code generation tasks. For natural language, it's closer to 1.8x. Why the difference? Code is more predictable — the draft model has higher acceptance rates.
But speculative decoding doesn't help when memory bandwidth is the bottleneck (e.g., very small batch sizes or long prompts). We only enable it for batch sizes >= 8. Below that, the draft overhead kills you.
python
# Speculative decoding config with vLLM (v0.8+)
from vllm import LLM
llm = LLM(
model="meta-llama/Llama-3.1-70B",
speculative_model="meta-llama/Llama-3.1-8B",
num_speculative_tokens=5, # try 5-7, tune per model
speculative_draft_threshold=0.8, # acceptance prob
block_size=16
)
One more thing: draft model must share the same tokenizer. If you mix tokenizers, you get a 15% performance penalty from detokenization overhead. Ask me how I learned that.
KV Cache Management: The Hidden Tax
Most engineers obsess over FLOPs. The real bottleneck is memory bandwidth to the KV cache.
Each token generates a key and value vector. At 32K context, a 70B model's KV cache is ~20GB in FP16. That's 40% of an A100's high-bandwidth memory gone before you even start generation.
Solutions we've tested:
- PagedAttention (vLLM's core innovation): Reduces fragmentation. We saw 40% higher throughput on shared-prompt workloads (chatbots, copilots).
- KV cache quantization to FP8: Near-zero quality loss. We use it in production. Reduces cache memory by 50%.
- Prefix caching: If the same initial prompt appears repeatedly (e.g., system prompt + few-shot examples), cache the KV state once. We've seen 5-10x latency reduction on multi-turn conversations.
On that last point: at FinFlow, we implemented prefix caching for their stock screening prompts. First query took 3 seconds. Subsequent queries with the same prefix? 400ms.
Batching Strategies: Static vs. Dynamic
Two schools. Both wrong if taken to extremes.
Static batching: Wait until N requests arrive, then process together. Simple, high throughput, but terrible tail latency. We measured p99 under static batching at 12 seconds during a flash crowd.
Dynamic batching / continuous batching: vLLM's approach — add incoming requests to active batches mid-generation. Much better latency. At SIVARO, we run continuous batching with a max 256 concurrent requests per GPU. Mean token latency stays under 20ms.
But dynamic batching adds complexity. Memory management, padding, scheduling. The vLLM team solved most of this by early 2026, but if you're rolling your own, prepare for pain.
Our rule of thumb: For real-time apps (chatbots, copilots), use continuous batching with a target batch size of 32-64. For offline batch processing (data labeling, summarization), static batching with max throughput is fine.
Model Architecture: Not All Models Are Created Equal
Everyone wants the biggest model. Often they shouldn't.
We tested three families for a legal document summarization task:
| Model | Latency (ms/token) | Cost/token | ROUGE-L |
|---|---|---|---|
| GPT-4 128K (API) | 45 | $0.06 | 0.72 |
| Llama-3.1-70B (self-hosted) | 22 | $0.001 | 0.71 |
| Mistral-7B (fine-tuned) | 8 | $0.0002 | 0.68 |
The 7B learned enough domain specifics through fine-tuning to nearly match the 70B — at 1/150th the cost. Most people think RAG vs fine-tuning vs prompt engineering is about accuracy. It's also about inference speed. Fine-tuning a smaller model for your domain can be the fastest path.
Read the full breakdown: RAG vs fine-tuning vs. prompt engineering. The decision framework from winder.ai says: if your task is narrow and stable, fine-tune a small model. If you need to update knowledge weekly, use RAG. If you need zero overhead, prompt engineer.
For speed, fine-tuning wins because you can go small. At SIVARO, we now default to 7B-13B for 90% of customer work. Only when we need reasoning (math, multi-step) do we scale up.
Prompt Engineering for Speed
Your prompt length directly affects inference latency. Every token in the input must be processed through attention — O(n²) for the prefill phase.
We saw a client whose prompt was 8,000 tokens. They were copying their entire knowledge base into each request. Prefill took 1.2 seconds. After we moved to a RAG approach where only relevant chunks were retrieved, average prompt length dropped to 1,200 tokens. Prefill latency: 180ms. That's a 6.7x improvement without touching the model.
Two hard rules we enforce:
- Keep system prompts under 500 tokens for real-time apps.
- Use instruction-tuned models that understand concise prompts. We benchmarked Llama-3.1-8B with a 200-token prompt vs. a 1000-token prompt — same answer quality, 3x speed difference.
Hardware: It Matters Less Than You Think
Here's a contrarian take: don't obsess over H100s vs. A100s. The gap is ~30% on inference, but the cost gap is 150%. For production inference, A100 80GB is still the value king in 2026.
The real hardware differentiator is memory bandwidth. H100 has 3.35 TB/s. A100 has 2.0 TB/s. But if your bottleneck is compute (small batch, short context), the difference narrows.
We've run production on A100s, H100s, and Grace Hopper. For workloads with context > 16K tokens, Grace Hopper's 500GB/s CPU-GPU coherence actually hurt because the KV cache overflowed into system memory. Stick with GPUs that have enough HBM for your model + cache.
One emerging option: inference on Mac Studio with M3 Ultra (192GB unified memory). We prototype on it. For small models (<7B), it's shockingly fast — 30 tokens/sec. But no CUDA graphs or flash attention, so you lose optimization headroom.
The Tooling Stack (2026 Edition)
Forget writing custom CUDA kernels. The ecosystem has matured. Our standard stack:
- Serving: vLLM (v0.10+) — handles continuous batching, PagedAttention, tensor parallelism, prefix caching.
- Quantization: AWQ for weights, FP8 for KV cache.
- Scheduling: Ray Serve for multi-model routing, autoscaling.
- Monitoring: Langfuse for prompt latency breakdowns, GPU memory fragmentation tracking.
If you're on the edge (phones, browsers), use LlamaCPP with GGUF quantized models. We've shipped a 2.7B parameter model running at 40 tokens/sec on an iPhone 15 Pro Max.
When Optimization Backfires
Not everything that improves benchmark speed works in production.
- Flash Attention 2: Great for long context, but adds 10-20% memory overhead on short batches. We disable it for contexts under 2K tokens.
- CUDA graphs: Reduce kernel launch overhead. But they fix the graph shape — if your batch size changes, you pay a huge recompilation penalty. We use them only for fixed-batch production endpoints.
- Tensor parallelism across GPUs: For models < 13B, the communication overhead of all-reduce often kills any gain. Use single GPU until you need memory.
FAQ
Q: Is speculative decoding always faster?
A: No. It hurts on small batch sizes (<4), very long prompts (>8K tokens), and when draft model accuracy is below 60%. Profile your workload.
Q: How to optimize llm inference speed for real-time chatbots?
A: Use a 7B model with INT4 quantization, prefix caching, and continuous batching. Target <200ms first token latency. Keep context under 4K tokens.
Q: Why is speculative decoding faster than normal decoding?
A: It parallelizes the drafting step — a small model proposes multiple tokens, then the big model verifies them in one forward pass. The probability of acceptance is high enough that you generate 2-3x more tokens per second.
Q: Should I use RAG or fine-tuning for speed?
A: Fine-tuning a small model on your domain data is usually fastest. RAG adds retrieval latency (20-100ms per search). RAG vs Fine-Tuning in 2026: A Decision Framework gives a good rule of thumb: if you can precompute knowledge, fine-tune; if knowledge changes monthly, RAG.
Q: What's the cheapest way to serve a 70B model?
A: Two A100 80GB GPUs with tensor parallelism, FP8 KV cache, and AWQ quantization. About $2.50/hour on Lambda Labs spot. Throughput ~20 req/sec for 1K context.
Q: How does prompt engineering affect inference speed?
A: Every token in the prompt costs quadratic attention during prefill. We've seen 10x latency differences between a verbose prompt (3K tokens) and a concise one (300 tokens). The RAG vs. Fine-Tuning vs. Prompt Engineering paper quantifies: prompt engineering can reduce prefill time by 85% with zero accuracy loss.
Q: What about AI art worth collecting — how do those models optimize inference?
A: Image generators (Stable Diffusion, Flux) use different techniques: UNet distillation, step reduction (from 50 to 4 steps with LCM), and FP16 inference. For the text prompts, they often use a separate small LLM to expand user prompts into detailed descriptions — that runs under 10ms.
Q: What's the single biggest mistake companies make?
A: Not measuring the right thing. They look at tokens-per-second on synthetic benchmarks. Real user experience depends on time-to-first-token (TTFT), which is dominated by prompt length and KV cache prefill. We once halved TTFT by removing three lines of boilerplate from the system prompt.
Conclusion
I've been building inference systems for six years. The landscape shifts every six months. But the fundamentals stay: understand where your bottleneck is — compute, memory bandwidth, I/O, or generation pattern — then apply the right lever.
Quantize weights. Manage your KV cache. Use speculative decoding if batch size is decent. Keep prompts short. Fine-tune a small model instead of prompting a big one. And always measure in production, not on your laptop.
How to optimize LLM inference speed? Start by killing your biggest latency term first. Usually it's the prompt length. Then the model size. Then the decoding strategy.
One last story: in early 2025, a startup asked us to optimize their legal document analysis pipeline. They were running GPT-4 on every query. Total cost: $80K/month, latency 12 seconds. We swapped to a fine-tuned Mistral-7B with RAG for citations. Latency: 400ms. Cost: $400/month. Accuracy: within 2% of GPT-4 on their test set.
That's not optimization. That's a rewrite of the entire business model.
Nishaant Dixit — Founder of SIVARO. Building data infrastructure and production AI systems since 2018. Built systems processing 200K events/sec.