PROGRAM ZERO 18-month live AI, LLM & full-stack programme · ₹17,999 for all 18 months · Starts 9 January 2027
Explore Program Zero →
Careers Ninza — business and startup leadership training
JOIN ZERO NINZA KIDS
JOIN ZERO NINZA KIDS
See all programs → Free live masterclass → Guides & articles → Newsletter →
Careers Ninza
AI & AUTOMATION 9 min read · Updated 13 October 2026

How LLM inference works: KV cache, batching and latency explained

Why the first word takes longer than the rest, what the KV cache stores, how batching and paged attention make serving cheaper, and how speculative decoding speeds up generation.

CN
Careers Ninza AI faculty
Careers Ninza · Kolkata, India
Learn this live in Program Zero

LLM inference happens in two phases: prefill, where the model processes the whole prompt at once, and decode, where it generates one token at a time. The KV cache stores intermediate attention results so each new token does not recompute the whole prompt, and serving systems batch many users’ requests together to keep GPUs busy and costs down.

Inference is where most of the money in AI is spent, because it happens every time anyone uses a model. Understanding it explains why responses stream word by word, why long prompts are slower and costlier, and how providers serve millions of users on finite hardware.

Why does the first token take longer?

During prefill, the model reads every token of your prompt and computes attention across all of them. That work happens before the first word appears, so long prompts mean a longer wait, measured as time to first token. After that, decoding generates one token per step, which is why answers stream out steadily.

PhaseWhat happensBottleneck
PrefillProcess the whole prompt in parallelComputation; grows with prompt length
DecodeGenerate one token per stepMemory bandwidth; grows with output length

What is the KV cache?

In a transformer, each token produces keys and values used by attention. Without caching, generating each new token would recompute keys and values for every earlier token. The KV cache stores them, so each step only computes the newest token. It makes generation dramatically faster, but the cache grows with context length and the number of concurrent users, and quickly becomes the main consumer of GPU memory.

How does batching reduce cost?

A GPU generating tokens for one user is mostly waiting on memory. Serving many users together uses the same work far more efficiently. Continuous batching goes further: instead of waiting for a whole batch to finish, new requests join and finished ones leave at every step, keeping the GPU busy.

What is paged attention?

KV caches for different requests have different, unpredictable lengths, which wastes memory if space is reserved up front. PagedAttention, introduced with the vLLM serving system, manages the cache in small blocks, like pages in an operating system, reducing waste so more requests fit on the same GPU.

What is speculative decoding?

Speculative decoding uses a small, fast draft model to propose several tokens ahead, then the large model checks them in a single step and accepts the ones it agrees with. When the draft is often right, generation speeds up without changing the large model’s outputs.

Which levers reduce latency and cost?

—Shorter prompts: less prefill work and a smaller KV cache.
—Shorter outputs: fewer decode steps; ask for concise answers.
—Smaller or quantised models: faster steps and more room for the cache; see LLM quantisation explained.
—Prompt caching: reuse the processed form of repeated instructions or documents.
—Batching and efficient servers: higher throughput per GPU.
—Streaming: show tokens as they arrive, so users wait less for the first words.

Latency and throughput pull against each other. Large batches serve more users per GPU but can slow each individual response. Production systems tune this balance for their product: a chat app prioritises fast first tokens, a nightly batch job prioritises total throughput.

Why does this matter if you only use APIs?

Because the same mechanics show up in your bill and your user experience. Long system prompts and huge retrieved contexts raise cost and time to first token; verbose outputs raise decode time; many sequential agent steps multiply everything. Engineers who understand inference design faster, cheaper applications. Our guide to why companies want engineers who understand models gives real examples.

How do you estimate LLM serving cost?

Estimate tokens per request, including system prompt, retrieved context and output, then multiply by requests per day. For hosted APIs, apply the provider’s per-token prices. For self-hosting, estimate tokens per second your hardware can sustain at your target latency, and how many GPUs that requires at peak load. The answer often shows that trimming prompts and outputs saves more than any infrastructure change.

Which metrics should you track for a live LLM service?

—Time to first token and total response time, especially the slowest requests.
—Tokens per second per user and per GPU.
—Cost per request and per active user.
—Error and timeout rates.
—Quality scores from your evaluation set and user feedback.

How does inference relate to model architecture?

Architecture choices affect serving: grouped-query attention shrinks the KV cache, mixture-of-experts models reduce compute per token but need memory for every expert, and long-context techniques change memory needs. See mixture of experts explained for one example.

Where can you learn LLM serving?

Program Zero covers KV-cache management, quantisation, speculative decoding, continuous batching, production serving systems and latency, throughput and cost-per-token trade-offs in its advanced LLM engineering phase, and model serving with APIs in its cloud and MLOps phase.

Program Zero runs live online, so you can learn this from anywhere in India: metros such as Chennai, Hyderabad, Bengaluru and Pune, growing cities such as Coimbatore, Kochi, Indore and Nagpur, and smaller towns such as Ranchi, Patna, Bhubaneswar and Raipur. Every learner joins the same evening classes, at the same fee.

Want to learn this live, with mentors?

Program Zero teaches how LLMs are trained and served, from attention and the KV cache to batching, quantisation and production serving, over 18 months of live classes. ₹17,999 for all 18 months; the batch starts 9 January 2027.

PROGRAM ZERO · 18-MONTH LIVE PROGRAMME
Program Zero: AI, LLM & Full-Stack Developer Programme

Learn AI, large language models and full-stack development live with mentors: from your first line of code to building and deploying your own language model.

₹17,999 for all 18 monthsStarts 9 January 2027Mon–Fri, 9:00–10:30 PM ISTEvery class recorded

Frequently asked questions

How does LLM inference work?+

Inference has two phases. In prefill, the model processes the whole prompt at once. In decode, it generates one token at a time, reusing stored attention results from the KV cache so each new token does not recompute the entire prompt.

What is the KV cache in LLMs?+

The KV cache stores the keys and values computed by attention for earlier tokens, so each new token only needs computation for itself. It speeds up generation greatly but grows with context length and the number of users, using a lot of GPU memory.

Why is the first token slower in LLM responses?+

Before generating anything, the model must process the entire prompt in the prefill phase. Longer prompts mean more work, so time to first token rises with prompt length, after which tokens stream at a steadier pace.

What is continuous batching?+

Continuous batching lets a serving system add new requests and remove finished ones at every generation step instead of waiting for a whole batch to complete, keeping the GPU busy and improving throughput.

What is PagedAttention?+

PagedAttention manages the KV cache in small fixed-size blocks, similar to memory pages in an operating system, reducing wasted memory from unpredictable sequence lengths so more requests fit on the same GPU. It was introduced with the vLLM serving system.

What is speculative decoding?+

Speculative decoding uses a small draft model to propose several tokens, then the large model verifies them in one step and keeps the ones it agrees with. It speeds up generation without changing the large model's outputs.

How can I reduce LLM latency and cost?+

Use shorter prompts and outputs, smaller or quantised models where quality allows, prompt caching for repeated content, efficient batching and streaming. Fewer sequential agent steps also cut both latency and cost.

Where can I learn LLM inference and serving in India?+

Program Zero by Careers Ninza covers KV-cache management, quantisation, speculative decoding, continuous batching and production serving within an 18-month live programme for Rs 17,999.

Related reading

AI & AUTOMATION LLM Course: What You Should Learn to Build Large Language Models Most LLM courses teach you to call an API. Building and adapting language models needs more: tokenisers, transformers, training, fine-tuning, alignment, evaluation and serving. Here is the full syllabus, in order. 10 min read AI & AUTOMATION Agentic AI Course: The Skills You Need to Build AI Agents in 2027 Building reliable AI agents takes more than chaining prompts. The skills an agentic AI course should teach, from tool use and memory to evaluation, security and deployment, and how to judge a course. 10 min read AI & AUTOMATION What Is Tokenization in LLMs? BPE, WordPiece and SentencePiece Explained Before a language model reads a word, a tokenizer chops text into pieces. How BPE, WordPiece and SentencePiece work, why tokens decide cost and context, and why Indian languages are affected most. 9 min read

We teach this, live

Every article here comes from something we teach. Sit in on a free masterclass and judge the mentors yourself.