Inference tickets should account for both persistent KV cache and transient prefill workspace.
Dense full-attention KV cache
For standard layers, calculate bytes as:
elements per token per layer = 2 * kvHeads * headDimlayer elements = tokens * elements per token per layerlayer bytes = layer elements * bytesPerElementtotal KV bytes = layer bytes * numAttentionLayers
Note: bytesPerElement is 2 for FP16/BF16, 1 for INT8, and 0.5 for INT4.
Hybrid / MoE models with SSM
For models like Qwen3-Next that alternate full-attention layers with linear/SSM layers, sum the full-attention KV cache (using the math above) with the SSM cache sizes for the linear layers.
Prefill attention workspace (transient)
Prefill allocates large temporary buffers. To estimate the peak transient workspace, sum the bytes for the following tensors (multiplied by bytesPerElement):
Q = B * H * L * DK = B * Hkv * L * DV = B * Hkv * L * DScores = B * H * L * LOutput = B * H * L * D- (Optional) Gating tensor:
B * L * (H * D)
Where: B=Batch, H=Heads, L=Prefill Chunk Size, D=Head Dim, Hkv=KV Heads.
Practical Implementation Pattern
- Single Ticket Pattern: Most callers create one ticket for the entire
generate() call. Budget this ticket for the peak usage: weights + KV cache + prefill workspace. - Separate Reservation Pattern: If you have already created a separate reservation ticket for weights, the inference ticket should only cover
KV cache + prefill workspace.