Skip to content

Serving patterns: streaming, batching and caching

The infrastructure levers that determine how fast and how cheaply inference feels to users.

6 MIN READUPDATED 2026-11-18Inference & production
ORIGINAL DIAGRAM · 4 STEPS1Stream responses for an…2Keep stable prefixes (s…3Batch background worklo…4Queue non-urgent jobs a…

Original flow diagram from note steps — not a copied image

§ 01Key concepts
StreamingSending tokens as they are generated instead of waiting for the full answer.
BatchingGrouping requests to improve GPU/provider efficiency.
Prompt cachingReusing the computed representation of a repeated prefix.
§ 02When to use
  • Users wait on long answers.
  • Workloads have repeated system prompts or documents.
  • Traffic is bursty or high volume.
§ 03How it works
  1. 01Stream responses for anything longer than a short sentence.
  2. 02Keep stable prefixes (system prompt, policy) identical to enable caching.
  3. 03Batch background workloads; do not batch latency-sensitive requests aggressively.
  4. 04Queue non-urgent jobs and expose progress.
§ 04Use cases & scenarios

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.
§ 05Examples

Stream with a cacheable prefix

ts
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

yaml
stream: false
batch_window_ms: 50
max_batch_size: 16
queue: summaries
progress_updates: true

Batch non-urgent jobs only. Interactive chat should stream and never wait for a batch window.

§ 06Trade-offs

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

§ 07Implementation checklist
  • 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.
§ 08Configuration reference
KeyTypical valueNote
streamon for interactiveDisable for background jobs.
cache_prefixstable system promptAvoid timestamps in the prefix.
batch_windowshortBalance throughput vs latency.
§ 09Troubleshooting
SymptomCauseFix
Cache hit rate near zeroPrefix changes per requestMove dynamic content after the static prefix.
Streaming cuts offUpstream timeoutExtend timeouts and buffer partial output.
§ 10Frequently asked questions

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