| Streaming | Sending tokens as they are generated instead of waiting for the full answer. |
| Batching | Grouping requests to improve GPU/provider efficiency. |
| Prompt caching | Reusing the computed representation of a repeated prefix. |
- Users wait on long answers.
- Workloads have repeated system prompts or documents.
- Traffic is bursty or high volume.
- 01Stream responses for anything longer than a short sentence.
- 02Keep stable prefixes (system prompt, policy) identical to enable caching.
- 03Batch background workloads; do not batch latency-sensitive requests aggressively.
- 04Queue non-urgent jobs and expose progress.
Customer chat with a fast first token
- Context
- Users wait for answers from a support assistant.
- Problem
- Total generation time is acceptable but the blank screen feels slow.
- Approach
- Stream tokens and keep the system prompt byte-identical for prefix caching.
- Outcome
- Fast perceived response and lower cost on repeated prefixes.
Nightly summarisation backlog
- Context
- A platform summarises thousands of documents overnight.
- Problem
- Sending them one by one wastes provider capacity.
- Approach
- Queue the jobs, batch them with a short window and expose progress updates.
- Outcome
- Higher throughput with no impact on interactive users.
Stream with a cacheable prefix
const SYSTEM = "You are a support assistant. Answer only from sources.";
const response = await client.chat({
messages: [{ role: "system", content: SYSTEM }, ...sessionMessages],
stream: true,
});Keep the system prompt byte-identical across requests; putting a timestamp in it drops the cache hit rate to zero.
Batch background summarisation
stream: false
batch_window_ms: 50
max_batch_size: 16
queue: summaries
progress_updates: trueBatch non-urgent jobs only. Interactive chat should stream and never wait for a batch window.
Streaming
Up: Better perceived latency
Down: Harder to handle errors mid-stream
Prompt caching
Up: Large cost/latency win
Down: Requires stable prefixes
Batching
Up: Higher throughput
Down: Can increase tail latency
| Option | Upside | Cost |
|---|---|---|
| Streaming | Better perceived latency | Harder to handle errors mid-stream |
| Prompt caching | Large cost/latency win | Requires stable prefixes |
| Batching | Higher throughput | Can increase tail latency |
- Put static instructions first in the prompt.
- Measure time to first token separately from total time.
- Handle partial output when a stream fails.
- Cache only content safe to reuse across users.
| Key | Typical value | Note |
|---|---|---|
| stream | on for interactive | Disable for background jobs. |
| cache_prefix | stable system prompt | Avoid timestamps in the prefix. |
| batch_window | short | Balance throughput vs latency. |
| Symptom | Cause | Fix |
|---|---|---|
| Cache hit rate near zero | Prefix changes per request | Move dynamic content after the static prefix. |
| Streaming cuts off | Upstream timeout | Extend timeouts and buffer partial output. |
Does streaming reduce total time?
Not usually, but it dramatically improves perceived responsiveness.
What is safe to cache?
Static instructions and shared reference material — never user-specific or confidential content.
More in Inference & production