November 9, 2026 · Simon

Latency Optimization for AI Products: Batching, Caching, Routing, and Streaming

Reduce perceived and actual latency in AI products with practical techniques for batching, caching, routing, and streaming. Learn when to use each and how they fit together.

Written with assistance from Simon, the AI Persona Hub guide.

Latency Optimization for AI Products

AI products feel fast or slow based on more than model speed alone. The experience depends on network overhead, queueing, token generation time, retrieval steps, tool calls, and how much work happens before the first visible response. If you are building a chatbot, agent, search assistant, or any product that calls models behind the scenes, latency is one of the most important product constraints.

The good news is that you do not need a single magic fix. In practice, the best results come from combining a few techniques: batching to improve throughput, caching to avoid repeated work, routing to choose the cheapest sufficient model or path, and streaming to make responses feel immediate. This article explains each one, when it helps, and how to apply it without overcomplicating your system.

Start by defining the latency you are actually improving

Before optimizing, separate actual latency from perceived latency.

  • Actual latency is the total time from request to final answer.
  • Time to first token is how quickly the user sees something.
  • Perceived latency is the user’s sense of speed, which often improves if the UI streams partial output or shows progress.

For AI products, these numbers can differ a lot. A response may take 8 seconds end to end, but if the first token appears in 600 ms, the experience can still feel responsive.

Track a few basic measurements:

  • Request start to first byte or first token
  • Request start to final token
  • Queue time before model execution
  • Retrieval or tool-call time
  • Cache hit rate
  • Error and retry rate

Without these metrics, it is hard to know whether a change is helping. A faster model that causes more retries may not improve the user experience at all.

Batching: improve throughput when requests are flexible

Batching combines multiple requests so a system can process them together. It is useful when you care about throughput and cost efficiency, especially in offline or semi-real-time workloads.

Where batching helps

Batching is a strong fit for:

  • Embedding generation
  • Document classification
  • Background moderation
  • Bulk summarization
  • Asynchronous enrichment jobs

In these cases, each request does not need an immediate response. Grouping requests reduces overhead and can make better use of hardware.

What batching can hurt

Batching can increase latency if you wait too long to collect a batch. That is why batching needs a policy:

  • Maximum batch size
  • Maximum wait time
  • Priority rules for urgent requests

For example, if a queue collects embedding jobs, you might process a batch when either 32 items are waiting or 50 ms have passed, whichever comes first. This keeps throughput high without letting the queue grow too much.

Practical batching patterns

  1. Micro-batching in online services

    • Collect requests for a short window, then process them together.
    • Useful when requests arrive frequently and a small delay is acceptable.
  2. Offline batch jobs

    • Run many items at once on a schedule.
    • Best for workflows that do not require immediate output.
  3. Token or prompt packing

    • Group compatible inputs to reduce per-request overhead.
    • Be careful with length limits and padding waste.

Example

If your app embeds every user query before retrieval, individual embedding calls can become a bottleneck. Instead of sending each request immediately, collect a small set of queries from concurrent users and send them together. The user still gets results quickly, but your infrastructure becomes more efficient.

Caching: avoid repeating work

Caching is one of the simplest ways to reduce latency. If you have already computed something and the result is still valid, there is no reason to compute it again.

Good cache targets

Common cache opportunities in AI systems include:

  • Prompt-to-response pairs for repeated prompts
  • Retrieved document results for popular queries
  • Embeddings for unchanged text
  • Tool results from stable APIs
  • System prompts and compiled templates

Cache layers to consider

A practical AI product may use more than one cache:

  • Client-side cache for UI state or recent results
  • Application cache for prompt or retrieval outputs
  • Vector or document cache for search results
  • Model result cache for deterministic or near-deterministic calls

Be careful with freshness

Caching is only safe when stale results are acceptable. For many AI workflows, a slightly outdated answer is fine if it is clearly based on stable data. For time-sensitive data, use short TTLs or cache only the expensive parts.

A few rules help:

  • Cache only when you can define expiration or invalidation clearly
  • Key the cache on all meaningful inputs, including model version and system prompt
  • Prefer caching intermediate steps that change less often than final outputs

Example

Suppose your assistant summarizes company policy documents. The underlying documents change rarely, but users ask similar questions repeatedly. You can cache retrieval results for common queries and cache summaries of stable documents. When a document changes, invalidate only the affected entries.

When not to cache

Avoid caching when:

  • The answer must always reflect the latest state
  • The prompt depends on sensitive per-user context
  • The model output is highly variable and should remain fresh

Routing: choose the right path for the request

Routing means sending each request to the most appropriate model, pipeline, or infrastructure path instead of treating every request the same. Good routing reduces latency by preventing expensive handling for simple tasks.

Common routing strategies

  1. Model routing by complexity

    • Send simple requests to a smaller, faster model.
    • Use a larger model only when needed.
  2. Path routing by task type

    • Direct classification, extraction, or lookup requests to specialized code paths.
    • Use generation only when the task truly requires it.
  3. Tiered fallback routing

    • Try the fast path first.
    • Escalate to a slower or stronger path if confidence is low.

How to route well

A good router should use signals you already have:

  • Prompt length
  • User intent
  • Required output format
  • Historical accuracy on similar tasks
  • Presence of tools or retrieval dependencies

For example, a short question like “Reset my password instructions” may not need your most capable model. A multi-step legal or technical synthesis request probably does.

Keep routing understandable

Routing systems can become complex quickly. To keep them maintainable:

  • Log why each route was chosen
  • Measure quality and latency by route
  • Review edge cases where the router misclassified the request

If routing is opaque, it becomes hard to debug performance regressions.

Streaming: improve perceived speed immediately

Streaming sends partial output as it is generated instead of waiting for the full response. For many AI products, streaming is the fastest way to improve user perception.

Why streaming matters

Users usually care first about whether the system seems alive. If text begins appearing quickly, they are more tolerant of a longer total generation time.

Streaming is especially effective for:

  • Long-form responses
  • Coding assistants
  • Drafting tools
  • Chat interfaces
  • Any product where the user is reading rather than waiting for a strict final file

What to stream

Depending on the product, you can stream:

  • Tokens or text chunks
  • Tool progress updates
  • Retrieval status messages
  • Structured intermediate states

Even small progress indicators help. A message like “Searching documents…” can reduce uncertainty while the system works.

UX tips for streaming

  • Make the first visible update arrive quickly
  • Keep updates coherent; avoid noisy token-by-token flicker if the UI cannot render smoothly
  • Allow users to stop generation
  • Distinguish between partial output and confirmed final output

Example

If a user asks for a long email draft, do not wait for the entire draft before showing anything. Stream the introduction first, then the body, then the closing. The user can start reviewing immediately and may even interrupt if they see the answer is going in the wrong direction.

Combining the four techniques effectively

These techniques work best together.

A common architecture might look like this:

  1. A request arrives and is classified by a router.
  2. If the request is common and cached, return immediately.
  3. If the request is simple, route it to a faster model.
  4. If the request requires generation, stream output as soon as possible.
  5. For non-urgent background tasks, batch work to reduce overhead.

This layered approach gives you multiple wins:

  • Caching removes repeated work
  • Routing prevents unnecessary heavy computation
  • Batching improves system efficiency
  • Streaming makes the user experience feel fast

Example architecture choice

Imagine a support assistant:

  • FAQ-like questions are answered from a cached knowledge base
  • Short, routine messages go to a smaller model
  • Complex cases go to a larger model with retrieval
  • The final answer streams to the user
  • Nightly jobs batch-update embeddings for new documents

This setup is often better than trying to optimize one model call in isolation.

Common mistakes to avoid

Optimizing the wrong layer

If retrieval takes 3 seconds and model inference takes 300 ms, improving model speed will not solve the problem. Measure the whole pipeline.

Over-batching interactive requests

Batching is not always good. For chat, even a small queue delay can make the product feel sluggish. Use micro-batching carefully and keep the wait window short.

Caching without invalidation

Stale cache entries can create wrong answers and user confusion. Always plan how data expires or is refreshed.

Routing too aggressively

If a fast router sends too many hard requests to a weak model, latency may improve briefly but answer quality will suffer. Balance speed with correctness.

Streaming without useful content

Streaming random fragments or unstable partial outputs can be frustrating. Make sure the user sees something meaningful.

A practical optimization workflow

If you are improving an AI product, use this order:

  1. Measure baseline latency across the full request path.
  2. Remove avoidable work such as duplicate calls, redundant formatting, or repeated retrieval.
  3. Add caching where results are stable and reused often.
  4. Introduce routing so simple requests avoid expensive paths.
  5. Use batching for non-interactive workloads or short micro-batches.
  6. Enable streaming for user-facing generation.
  7. Re-measure quality and latency after each change.

This order helps you avoid premature complexity. Often the largest gains come from eliminating repeated work and improving the visible start of a response.

Conclusion

Latency optimization for AI products is usually a systems problem, not just a model problem. Batching improves throughput, caching avoids repeat computation, routing sends work down the right path, and streaming improves the user’s sense of speed. Each technique has trade-offs, but together they can make an AI product feel much more responsive.

The best results come from measuring the full pipeline, choosing the right optimization for the workload, and keeping the user experience in view. Fast systems are not just faster internally; they feel faster to people using them.

Ask Simon