Interactive LLM systems laboratory

Every generated token spends memory, compute, and time.

LLM inference economics emerges from a coupled system: weights must fit, prompts must be prefetched, KV state persists, and decode produces one token per sequence at a time. Change the workload and watch the bottleneck move.

Prefill parallelizes across prompt tokensDecode is memory-bandwidth sensitiveBatching improves utilizationKV cache grows with active contextQuantization reduces parameter bytesPrefill parallelizes across prompt tokensDecode is memory-bandwidth sensitiveBatching improves utilizationKV cache grows with active contextQuantization reduces parameter bytes

Route a workload through prefill, cache, and decode

The moving blocks are requests. Cyan motion represents parallel prompt processing, yellow storage is KV state, and pink pulses represent autoregressive output tokens.

prefillKV cachedecode
18
1282048
16256
116
50300
$0.50$3.00
0%20%
20%65%
Weight memory14.0 GB
Active KV memory4.0 GB
First-token latency0.00 s
Decode throughput0 tok/s
Estimated request cost$0.000
Transparent teaching estimate. It omits communication, kernels, memory bandwidth, scheduler overhead, speculative decoding, tensor parallelism, activation memory, hardware-specific efficiency, and provider margin. Use measured serving traces for capacity planning.

Serving cost is a queueing problem wrapped around a memory-and-compute problem.

Parameter residency

Weights usually dominate static memory. Quantization reduces bytes per parameter, but real formats add scales, zero points, padding, and runtime buffers. The simulator shows the ideal payload so the assumption remains visible.

weight GB = parameters x bits / 8 / 10^9
queueprefillKV cachedecode loopstream

KV state

Each active sequence stores keys and values for past tokens at every layer. GQA and MQA reduce KV heads, shrinking cache memory without changing weight count.

2 x layers x hidden x bytes x tokens

Prefill

Processes prompt tokens in parallel. Longer prompts increase time to first token, although prefix reuse can skip repeated work when cache identity and permissions are safe.

Decode

Generates one next token for each active sequence per iteration. Small batches underutilize hardware; huge batches increase queueing and per-request latency.

Utilization

Hourly hardware cost continues during idle gaps. Higher utilization spreads fixed time across more useful tokens, but saturation creates waiting and tail-latency risk.

Read the metrics as coupled constraints

Memory fit

Add weight payload, active KV cache, activations, runtime workspaces, and fragmentation. If the workload does not fit, throughput is irrelevant until parallelism or memory is changed.

Capacity boundary

Time to first token

Dominated by queueing and prompt prefill. Interactive agents often value low TTFT because users perceive silence before streaming more strongly than small differences later.

Responsiveness

Decode throughput

Total generated tokens per second across the batch. It is not the same as per-user speed: aggregate throughput can rise while each request waits longer.

Fleet efficiency

Request cost

Estimated occupied accelerator time divided across concurrent work. Actual price includes redundancy, networking, orchestration, storage, engineering, and margin.

Economic output
Optimization sequence

Move the bottleneck deliberately.

Serving optimization is rarely one trick. Every change modifies quality, memory, latency, throughput, operational complexity, or all five.

Fit the model safely

Choose precision and parallelism with accuracy tests, not memory targets alone. Leave headroom for cache, temporary buffers, allocator fragmentation, and bursts.

  • Measure quality on task-specific evaluations.
  • Profile real sequence-length distributions.
  • Reserve failure and failover capacity.

Control context growth

Retrieve only relevant evidence, summarize durable history, and cap low-value context. Prefix caching helps repeated prompts, but cache keys must include model, adapter, policy, and tenant boundaries.

Batch continuously

Continuous batching admits new requests as others finish, reducing padding and idle slots. Scheduler policy decides the tradeoff between throughput and latency fairness.

Measure the tails

Average latency hides queue spikes. Track TTFT and inter-token latency percentiles by prompt length, output length, batch occupancy, model version, and hardware pool.

Common questions

Why is decode often memory-bandwidth sensitive?

Each decode step may read a large fraction of model weights to produce relatively few tokens. Arithmetic units can wait on data movement, especially at small batch sizes.

Does INT4 make inference exactly four times cheaper than FP16?

No. Ideal weight bytes are four times smaller, but kernels, dequantization, KV precision, activation memory, bandwidth, batching, quality constraints, and hardware support determine realized gains.

Why does batching increase throughput?

More sequences share each weight read and expose more parallel work. The benefit eventually saturates, while queueing and memory consumption continue to grow.

Can prompt caching leak data?

Yes, if cache identity ignores tenant, permissions, policy, model configuration, or sensitive prompt differences. Reuse must preserve the same isolation rules as ordinary inference.

Treat every token as a systems decision.

Explore SuperPowers workflows that turn explicit goals into inspectable agent work, with reusable computer state and complete website generation.

Explore SuperPowersComputer-use cacheWebsite-building agent