ॐ श्री गणेशाय नमः
    Jasmin.
    Back to Blog
    AI EngineeringIntermediate

    KV Cache and Speculative Decoding: Why AI Responses Feel Slow

    Learn how KV caching and speculative decoding change inference latency and how to benchmark both on your own traffic.

    3 min read586 words

    An internal assistant takes five seconds to show its first word and then finishes quickly. A second assistant starts immediately and trickles out the rest. Both can have the same total response time, yet the users experience different bottlenecks. KV caching and speculative decoding address parts of that timing problem.

    What the KV cache saves

    A transformer computes attention keys and values for tokens. During autoregressive decoding, it can reuse keys and values from earlier tokens when producing the next one. Keeping this state in a KV cache avoids recomputing those representations at every decoding step. It also uses memory, which can constrain the number of requests a server handles at once. PagedAttention paper

    Consider a service where every request begins with the same long operating instructions. A serving engine with compatible prefix reuse may be able to reuse cached state for matching prefixes across requests. Within-request KV caching still helps when every prompt is unique. Measure cold and warm paths independently so a warm benchmark reflects the reuse your production traffic can actually deliver.

    The KV cache is serving state. If an assistant must remember a customer's preference next week, that is an application storage and retrieval decision with a separate lifecycle.

    What speculative decoding changes

    A cheaper draft process proposes several tokens. The target model then evaluates those proposals together. When enough of them can be accepted, the system advances the response with fewer expensive target-model steps. Exact speculative algorithms include a correction procedure that preserves the target output distribution. Original speculative decoding paper

    Routine responses with predictable wording might give the draft process a strong signal. A highly specialised response can produce more rejected proposals, reducing the benefit. The draft itself also uses resources. A configuration that helps one stream may compete with other requests when the service is under load.

    Diagnose the delay before tuning it

    What users observeWhat to measure
    Long wait before text appearsTime to first token, queue time and input length
    Slow streaming after the first tokenTime between output tokens and draft acceptance
    Delays during peak trafficp95 latency, throughput and cache memory pressure
    Slowdowns on long conversationsPrompt-length distribution and active sequence memory

    The first word can arrive late because the queue is busy, because the prompt is long, or because a retrieval step happens before inference. Check timestamps for each stage before changing the decoding configuration. For the slow stream, compare output lengths and decoding settings as well as cache behaviour. A request that generates twice as much text may need trimming before it needs additional hardware.

    Run a benchmark that survives a busy day

    Replay representative inputs at typical and peak concurrency. Run short requests and long requests separately before mixing them at realistic rates. Log model and engine versions, batch settings, prompt lengths, output lengths and the timing metrics above. For speculative decoding, compare the same target with and without the draft path and keep generation settings aligned. Evaluate output quality when changing serving variants, especially if distribution preservation is uncertain.

    Make the deployment decision against a real requirement, such as p95 time to first token for chat or completed documents per minute for a batch workflow. A speed gain on an isolated request can disappear as memory fills or the queue grows. Include the required load in your acceptance criteria.

    For capacity planning, see the self-hosted AI cost checklist. If you are sizing a larger deployment, the technology consulting page covers the architecture decisions around it.

    Share: Twitter LinkedIn

    Written by

    Jasmin Shukla
    Jasmin ShuklaAuthor

    Jasmin Shukla is a production AI builder and educator who teaches AI engineering by shipping real business systems — agents, RAG, MCP, and automation.

    AgentsRAGMCPFastAPIn8nPythonLaravelNext.js

    Need production-ready AI agents built?

    I consult on the problem first, then build the agents, RAG, MCP, or automation. If your team needs to understand the system, I will teach them.