---
title: "Continuous batching from first principles"
description: "Continuous batching falls out naturally once you optimize LLM serving for throughput, and the post derives it step by step from attention, causal masking, KV caching and chunked prefill."
url: "https://swe.quest/articles/continuous-batching-from-first-principles-b2a5a3d9c00e56be"
canonical: "https://huggingface.co/blog/continuous_batching"
source: "Hugging Face"
published: "2025-11-25"
difficulty: "medium"
kind: "Deep dive"
reading_minutes: 17
tags: ["inference","llms","performance"]
---

# Continuous batching from first principles

A post on [Hugging Face](https://swe.quest/sources/hugging-face.md), published 2025-11-25, 17 min read, Deep dive. Original: [https://huggingface.co/blog/continuous\_batching](https://huggingface.co/blog/continuous_batching)

## Summary

Continuous batching falls out naturally once you optimize LLM serving for throughput, and the post derives it step by step from attention, causal masking, KV caching and chunked prefill. The KV cache cuts the cost of generating each new token from O(n²) to O(n) at O(n) memory, about 16 KB per token per layer for Llama-2-7B in float16. Chunked prefill splits long prompts across forward passes. Padded batched generation wastes compute when prompts finish at different times, and dynamic batching needs heavy padding because newly inserted prompts must prefill while others decode, which motivates continuous batching's mixing of prefill and decode tokens in one forward pass.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Inference & serving](https://swe.quest/tags/inference.md) (AI & ML)
- [LLMs](https://swe.quest/tags/llms.md) (AI & ML)
- [Performance engineering](https://swe.quest/tags/performance.md) (Reliability & operations)

## Prerequisites

- transformer attention
- matrix multiplication
- LLM token generation ([easy posts on LLMs](https://swe.quest/tags/llms.md?difficulty=easy))

## Keywords

continuous batching, KV cache, chunked prefill, Llama-2-7B, attention mask

## Related posts

- [Prefill and Decode for Concurrent Requests - Optimizing LLM Performance](https://swe.quest/articles/prefill-and-decode-for-concurrent-requests-optimizing-llm-performance-66a6db57e54903ca.md): Hugging Face · 2025-04-16 · Medium · 10 min read · Inference & serving, LLMs, Performance engineering, Hardware
  LLM token generation splits into a compute-bound prefill phase, which processes all prompt tokens in parallel to produce the first token, and a memory-bandwidth-bound decode phase, which produces later tokens one at a time.
- [Cache-aware prefill–decode disaggregation (CPD) for up to 40% faster long-context LLM serving](https://swe.quest/articles/cache-aware-prefill-decode-disaggregation-cpd-for-up-to-40-faster-long-context-7f727a0ea889f194.md): Together AI · 2026-03-04 · Hard · 9 min read · Inference & serving, Caching, LLMs, Performance engineering, Scheduling
  Cache-aware prefill–decode disaggregation (CPD) extends standard PD serving with a dedicated pre-prefill tier for low-reuse (cold) long prompts, a router that estimates prefix-cache reuse per request, and a three-level KV cache hierarchy (GPU memory, host DRAM, RDMA-connected distributed cache).
- [Unlocking Longer Generation with Key-Value Cache Quantization](https://swe.quest/articles/unlocking-longer-generation-with-key-value-cache-quantization-c7f36c464d98f5fa.md): Hugging Face · 2024-05-16 · Medium · 11 min read · Inference & serving, LLMs, Python
  Hugging Face Transformers adds KV cache quantization, inspired by the KIVI paper, that stores keys and values per-token in int2/int4/int8 via quanto or HQQ backends.
- [Making open models work for long-horizon agentic workloads](https://swe.quest/articles/making-open-models-work-for-long-horizon-agentic-workloads-904e5a2645303fb2.md): Together AI · 2026-11-16 · Medium · 12 min read · Inference & serving, AI agents, Caching, LLMs, Load balancing
  Agentic coding workloads resend the whole growing transcript each turn while emitting short tool calls, so they are prefill-dominated and depend on prefix caching, which only reuses work up to the first changed token.
- [KV Cache from scratch in nanoVLM](https://swe.quest/articles/kv-cache-from-scratch-in-nanovlm-f1d92b90631a1366.md): Hugging Face · 2025-06-04 · Medium · 9 min read · Inference & serving, LLMs, Performance engineering, Python
  KV caching avoids recomputing keys and values for earlier tokens during autoregressive generation, and implementing it from scratch in the pure-PyTorch nanoVLM codebase gave a 38% speedup in generation.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
