Minimize prompt latency and TTFT by trimming token count, selecting a lightweight instruction‑tuned model, and enabling streaming inference with hardware‑accelerated kernels.
Step‑by‑step checklist
1. Model selection – Prefer the smallest instruction‑tuned variant that meets quality. Example: Llama-3.1-8B-Instruct (≈8 B parameters, 4 k context) vs Llama-3.1-70B-Instruct. Target latency < 30 ms per request for 8 B; 70 B typically > 80 ms.
2. Static system prompt caching – Pre‑tokenize the system prompt once and store the token IDs. Use a hash map keyed by sha256(prompt) to retrieve the cached tensor.
3. Structured prompts – Replace verbose natural‑language instructions with JSON schemas and function calls. Example schema:
{
"type": "object",
"properties": {
"action": {"type": "string"},
"params": {"type": "object"}
},
"required": ["action"]
}4. Streaming inference – Launch vLLM with chunked pre‑fill:
vllm serve Llama-3.1-8B-Instruct \
--max-model-len 4096 \
--enable-chunked-prefill \
--tokenizer-mode auto \
--port 8000Request body:
import requests, json
payload = {"prompt": user_prompt, "max_new_tokens": 1, "stream": True}
resp = requests.post("http://localhost:8000/generate", json=payload, stream=True)
for line in resp.iter_lines():
print(json.loads(line)["token"])5. Quantization – Load a 4‑bit GPTQ checkpoint with bitsandbytes:
from transformers import AutoModelForCausalLM, AutoTokenizer
model = AutoModelForCausalLM.from_pretrained(
"meta-llama/Meta-Llama-3.1-8B-Instruct",
load_in_4bit=True,
device_map="auto"
)6. Context window trimming – Keep only the most recent N tokens where N = floor(target_latency_ms / 0.5). For a 20 ms target, retain ≈40 tokens.
Quick comparison
| Approach | Latency impact | Quality trade‑off |
|-------------------------|----------------|-------------------|
| Smaller model (8 B) | -30 ms | Minor ↓ (depends on task) |
| 4‑bit GPTQ quantization | -15 ms | Negligible if calibrated |
| Structured JSON prompts| -10 ms | Improves consistency |
| Chunked pre‑fill | -20 ms | None |
Use the checklist to verify each optimization before deploying to production.