Reduce prompt latency and TTFT by trimming input, using token‑efficient APIs, and pre‑warming model instances.
Step‑by‑step
1. Trim the context – keep only the last N tokens that affect the answer (e.g., N=1024 for GPT‑4o). Remove static boilerplate with a reusable system prompt stored server‑side.
2. Use structured prompts – JSON‑encoded instructions let the model skip natural‑language parsing. Example schema:
{\"task\": \"summarize\", \"max_len\": 150, \"language\": \"en\"}3. Select the low‑latency endpoint – openai.ChatCompletion.create with model="gpt-4o-mini" and stream=true reduces TTFT by ~30 ms vs. non‑streaming.
4. Enable token‑caching – vLLM’s --kv-cache-dtype fp16 and --max-model-len 8192 keep KV cache across requests when the same system prompt is reused.
5. Pre‑warm containers – launch one warm replica per model and keep it alive with a health‑check ping every 30 s.
6. Batch dynamically – set batch_max_tokens=2048 and batch_timeout_ms=5 in TGI; this groups tiny requests without adding noticeable delay.
Quick comparison
| Setting | Latency (ms) | TTFT (ms) |
|--------|--------------|----------|
| Non‑streaming, no cache | 180 | 120 |
| Streaming, KV cache | 130 | 70 |
| Streaming + dynamic batch | 115 | 55 |
Python snippet (OpenAI SDK)
import openai, time
def chat(messages):
start = time.time()
resp = openai.ChatCompletion.create(
model="gpt-4o-mini",
messages=messages,
stream=True,
temperature=0,
max_tokens=200,
logit_bias={},
)
for chunk in resp:
print(chunk.choices[0].delta.get("content", ""), end="")
print("\nTTFT:", (time.time() - start) * 1000, "ms")Gotcha: If the batch‑size threshold is too high, a single request may wait for others, inflating TTFT during traffic spikes.