Hypothetical scenario. Northwind adds a read-only support assistant. Dev chooses a model whose weights fit in memory. Priya brings several long conversations to the serving path and asks where their active sequence state and waiting allowance fit.
Dev: “The parameter count fits the accelerator.”
Maya: “Does that establish how many useful requests we can admit under the chosen latency contract?”
Enter this optional specialization only with the listed model, token and serving background. Describe prompt and output work before composing memory, batching and admission assumptions. Fitting weights is one input, not a complete capacity or answer-quality argument.
Background for this lesson
Your outcome is a serving design with explicit memory, batching and admission assumptions. This optional module follows the completed System Design core. Before continuing, read what an LLM does, tokens and representations, and the GenAI serving chapter. Token generation and key/value state are new prerequisites here, not hidden requirements for the earlier job platform.
Which requests must this serving design support?
A decoder-only language model processes the input sequence and generates output tokens under a selected stopping rule. Prefill processes prompt context; decode extends active sequences. Their resource behavior differs, so one requests-per-second figure without prompt and output distributions is inadequate for admission planning.
Describe input length, output allowance, active conversations, deadlines and cancellation. Separate time waiting for admission, time to first output and subsequent token delivery. A fast first token does not make a long stalled completion acceptable, and a streaming connection does not prove that useful generation has started.
The product contract should name the behavior when the service is full. A bounded wait, explicit rejection or narrower supported workload can be honest outcomes. Silently accepting unlimited work moves the resource limit into unpredictable queueing and client timeouts, where retry amplification can consume still more capacity.
Account for weights and active sequence state
For an illustrative transformer, weights are persistent model parameters while cached keys and values retain attention state for active tokens. Cached state depends on layers, key/value heads, head dimension, retained tokens and element representation. It is not determined by parameter count alone. Runtime allocations and fragmentation add further memory demands.
An implementation can allocate cache blocks and reuse common prefixes under defined compatibility rules. That changes allocation behavior, not the need to retain the right state for each request. Prefix reuse requires compatible model, tokenization and execution context. Cross-tenant reuse also needs an explicit isolation policy; a shared process is not permission to expose another reader’s text.
Quantization can reduce selected representations, but its memory and quality effects depend on the actual model and runtime. Do not apply the weight bit width blindly to cache state or assume theoretical packed bytes equal physical allocation. Preserve the distinction between a visible subtotal and a measured deployment envelope.
Admit token-shaped work into a serving group
Hypothetical scenario. A request contract bounds input tokens, generated tokens, active sequences and cancellation behavior. Admission reserves capacity for the selected shape rather than counting every HTTP request equally. A long prefill and a short decode step can contend for the same accelerator while creating different latency effects.
| Admission record | Fields | Owner |
|---|---|---|
| Request shape | input/output caps, active sequence | Serving contract |
| Memory reservation | weights, runtime, active KV allowance | Serving group |
| Attempt placement | replica group + required partitions | Scheduler |
| Cancellation | requested / execution stopped / released | Runtime + ledger |
Keep replicas distinct from partitions of one model. Independent serving groups can each admit work; tensor or pipeline partitions cooperate to execute one model and share that group's failure boundary. Summing their device memory does not automatically create independent failover capacity. Recalculate admission after losing a whole required group.
KV state grows with active token positions and remains live while a sequence uses it. Paged management reduces some allocation waste but does not eliminate token-state demand or model-specific layout. Cancellation must release the reservation and actual live state when execution stops; releasing accounting first can admit more work than memory permits. Measure prefill, decode, queue wait and accepted outcomes by request shape.
Work a deliberately small memory model
Hypothetical worked example. Select a seven-billion-parameter model represented by two bytes per parameter. Weight payload is 7,000,000,000 × 2 = 14,000,000,000 bytes, or fourteen decimal GB. This says nothing about a particular advertised model, and excludes runtime memory and metadata.
Choose thirty-two layers, eight key/value heads per layer, head dimension one hundred twenty-eight, and two bytes per key or value element. Keys and values together contribute a factor of two. Cache payload per retained token is 2 × 32 × 8 × 128 × 2 = 131,072 bytes. For a selected 4,096 retained tokens, one sequence needs 131,072 × 4,096 = 536,870,912 bytes, or 512 MiB of cache payload.
For sixteen such active sequences, payload is 16 × 536,870,912 = 8,589,934,592 bytes, or eight GiB. Weights plus this cache subtotal are 14,000,000,000 + 8,589,934,592 = 22,589,934,592 bytes. Do not infer that a device with that nominal capacity is sufficient: activations, workspaces, allocation behavior and other residents still need measured headroom.
Doubling selected retained tokens to 8,192 doubles this simplified cache subtotal to 17,179,869,184 bytes for sixteen sequences. It does not prove that latency merely doubles. The memory derivation identifies a binding constraint to investigate; throughput and tail behavior still require workload-specific measurements.
Batch without losing the latency contract
Batching shares execution work across requests. Continuous batching can change the active set as sequences finish, while admission limits how much state is retained. The scheduler must balance work efficiency against waiting and fairness. A long prompt can interfere with short interactive work even when the average batch throughput looks attractive.
Choose whether long-context and short interactive requests need separate classes or capacity. Bound queued tokens as well as queued requests: two queues with equal request counts can have very different memory and service demand. Cancellation should release schedulable resources through the runtime’s actual supported mechanism, rather than merely closing the client socket.
Replica scaling also has a delay. Loading weights, preparing execution state and routing traffic are not instantaneous. Keep a bounded overload response during expansion. Replicas carrying incompatible model versions should not be treated as interchangeable merely because they expose the same HTTP shape.
In the wild: cache layout is implementation-specific
vLLM documents its paged-attention cache layout. Inspect the selected implementation’s representation and block behavior before translating the simplified payload calculation into allocation limits. The documentation explains a mechanism; it does not benchmark Northwind’s workload or certify the selected concurrency.
Record the model revision, tokenizer, serving configuration and admission policy with each release. A serving change can alter both performance and answer behavior. Keep quality evidence separate from resource measurements so that a faster configuration does not pass solely by silently shortening answers or dropping difficult requests.
Long prompts fill memory after a successful launch
Hypothetical failure scenario. A smoke check uses short prompts and one active request. Production then admits many long conversations because the weights loaded successfully. Active cache allocations exhaust memory, workers restart and clients retry, creating more queued work. The original fit check answered the wrong question.
Apply input and output bounds before admission, cap active token state and enforce a queue budget. Track rejected and cancelled requests alongside completions. Restore service with a narrower declared envelope if necessary, then expand only after measuring representative prompt, output and concurrency slices.
Use the model boundary only when it serves the product
Self-managed serving is useful when control, deployment scope or workload economics justify its operational cost. A hosted endpoint can remove some infrastructure work while retaining request-budget, privacy and availability decisions. Neither choice supplies application correctness or authorization automatically.
Keep the first assistant read-only and bounded. The next page attaches permissioned evidence to generation; it does not treat larger context or more replicas as a substitute for source authority. A complete serving review states which workload was measured, which memory subtotal was derived and which overload behavior the reader will actually experience.
Challenge the proposed mechanism
The gateway times out while decode continues. May it immediately refund all KV memory and admit another request?
Reveal the design reasoning
No. Wait for the runtime release evidence or conservatively retain the reservation. Caller cancellation and actual sequence reclamation are separate transitions. Include abandoned work when evaluating the survivor envelope.
Decision and reconsideration: Admit by bounded token and memory shape. Separate replica capacity, model partitioning and actual cancellation release.
Check the design against changed conditions
The weight payload fits. Does that establish safe concurrency?
Reason through the consequence
No. Active key/value state and runtime allocations depend on the admitted token workload. Derive their subtotal and measure the actual envelope with headroom.
Sixteen sequences double retained tokens. What changes in the simplified cache budget?
Reason through the consequence
The chosen linear token-state calculation doubles cache payload from eight to sixteen GiB. That arithmetic does not predict a proportional change in latency.
The client closes its stream. Can the scheduler immediately assume the model stopped?
Reason through the consequence
Only after the runtime’s cancellation mechanism has actually released or retired the work. Socket closure and execution cancellation are different events.
Serving can bound resource use without establishing that an answer is permitted or supported. Page 62 follows eligible source evidence into a read-only document assistant, preserving the user’s authority boundary.
Northwind decision log
Admit bounded token workloads with visible overload behavior.
Separate derived payload from measured memory and latency.
Next, constrain generation to current evidence the reader may access.
Go deeper: Token state and serving deployment
- Book chapter: LLM Engineer’s Handbook: chapter: Inference Optimization — Paul Iusztin and Maxime Labonne. Use this resource to examine active sequence state and inference scheduling.
- Book chapter: LLM Engineer’s Handbook: chapter: Inference Pipeline Deployment — Paul Iusztin and Maxime Labonne. Use this resource to examine deployment criteria for a declared latency and throughput mix.
- Video lecture: Deep Dive into LLMs like ChatGPT — Andrej Karpathy. Use this resource to examine the token-generation process before mapping its resource boundaries.
- Primary document: Paged Attention — vLLM project. Use this resource to examine the cache-block representation used by the selected runtime.