Batching & Serving

Layer 3 · Code

Batching & Serving

Implement a toy continuous-batching scheduler and see how a paged KV cache allocator works.

12 min read110 XP

Incoming request queue

Scheduler (admit/evict per step)

Active decode batch

Paged KV cache allocator

The pieces you're about to sketch in code.

Real serving engines (vLLM, TensorRT-LLM, TGI) are large C++/CUDA systems, but the scheduling logic is simple enough to prototype in pure Python. Below is a minimal continuous-batching loop that admits new requests, runs one decode step for everyone active, and evicts finished ones — the same shape as the real thing, minus the GPU kernels.