KV Cache Management When GPU Compute Is Scarce: Keep, Offload, or Recompute

    When large language model inference hits compute limits, how you manage the KV Cache directly determines how many concurrent sessions your GPUs can serve. This article breaks down the memory cost of KV Cache, compares three strategies for idle sessions—keeping them in VRAM, offloading them to host memory or SSD, and dropping them for later recomputation—and offers practical guidance on capacity planning, scheduling, and monitoring to maximize throughput under constrained resources.

    1. The Core Problem: Why Compute Capacity Determines Concurrency Limits

    The concurrency of an LLM chat service is largely determined by GPU resources. The token-by-token generation phase (decode) is memory-bandwidth bound: for every token generated, the GPU must read the model weights and the session’s KV Cache. During this phase, compute units are rarely fully utilized—actual throughput depends mainly on how fast memory access completes.

    The logic for service operators is straightforward: the more tokens produced per second, the faster each request finishes on a given card, freeing resources for the next request. This increases the number of sessions a server can maintain simultaneously. Peak concurrency can be roughly estimated as:

    Peak concurrency ≈ Available VRAM ÷ Average VRAM usage per session

    This formula highlights two key points. First, both VRAM capacity and per-session usage determine the concurrency ceiling—neither alone is sufficient. Second, reducing per-session VRAM usage or speeding up request turnaround lets the same hardware serve more users.

    2. The Cost Model of KV Cache

    The KV Cache stores the Key and Value vectors of past tokens at every attention layer, avoiding repeated computation over the full history each time a new token is generated. Its VRAM footprint can be estimated as:

    KV Cache bytes per token = 2 × number of layers × number of KV heads × head dimension × bytes per element

    The factor of 2 accounts for storing both Key and Value. Below is a set of hypothetical parameters used to illustrate the calculation; they do not correspond to any specific model:

    • Layers: 32
    • KV heads: 8
    • Head dimension: 128
    • Precision: FP16, 2 bytes per element

    The result is 2 × 32 × 8 × 128 × 2 = 131,072 bytes, or about 128 KiB per token. For a session with a 4,096-token context, the KV Cache takes up roughly 512 MiB.

    This number has two implications. First, long-context sessions consume VRAM quickly, so the same GPU handles long conversations and short Q&A very differently in terms of concurrency. Second, a session’s KV Cache can be freed: it is essentially an intermediate computation result that can be restored by recomputation. This is the fundamental reason cache policies have room for trade-offs.

    3. Three Options for Idle Sessions and Their Trade-offs

    When a session has no new requests for a while, the service can choose one of three approaches.

    1. Keep it in VRAM

    This offers the lowest latency: when the user returns, generation can resume immediately. The cost is that VRAM stays occupied, which squeezes other requests at high concurrency.

    2. Offload to host memory or local SSD

    This frees VRAM; the cache is loaded back when the user resumes. Restore time depends on read bandwidth and the KV Cache size. This suits sessions with moderate idle durations, but requires extra I/O resources and data management logic.

    3. Drop it and recompute when needed

    This uses no cache space. On recovery, the full history is re-fed into the model for prefill. Prefill can process many tokens in parallel with high compute utilization, but its duration scales with history length—the longer the history, the higher the recomputation cost.

    The core differences can be summarized as follows:

    Strategy VRAM usage Restore latency Extra overhead
    Keep in VRAM High Lowest Almost none
    Offload to SSD Low Depends on I/O bandwidth Storage and scheduling logic
    Drop and recompute None Depends on history length Extra computation

    Choosing a strategy comes down to comparing two recovery paths: the time to load the KV Cache versus the time to recompute the history. The following pseudocode illustrates this decision logic:

    1. def choose_policy(session, ssd_bandwidth, prefill_throughput, idle_threshold=60.0):
    2. # session.kv_bytes: KV Cache size of the session in bytes
    3. # session.history_tokens: number of history tokens
    4. # session.idle_seconds: seconds since the last interaction
    5. # ssd_bandwidth: available read bandwidth in bytes/second
    6. # prefill_throughput: prefill speed during recomputation in tokens/second
    7. if session.idle_seconds < idle_threshold:
    8. return "keep_in_gpu"
    9. load_time = session.kv_bytes / ssd_bandwidth
    10. recompute_time = session.history_tokens / prefill_throughput
    11. if load_time <= recompute_time:
    12. return "offload_to_ssd"
    13. return "drop_and_recompute"

    Three points stand out in this logic. First, check idle duration first: sessions likely to continue soon should stay in VRAM. Second, compare the two recovery paths quantitatively rather than relying on intuition. Third, parameters such as thresholds, bandwidth, and prefill throughput must be obtained through load testing—you cannot simply reuse numbers from elsewhere.

    4. A Capacity Estimation Example

    Assume a GPU with 80 GiB of VRAM, where model weights take 14 GiB and 10% is reserved as runtime headroom, with an average session context of 2,048 tokens. Using the earlier assumptions, a single session’s KV Cache is about 128 KiB × 2,048 = 256 MiB.

    VRAM available for KV Cache: 80 × 0.9 − 14 = 58 GiB

    Theoretical concurrency: 58 × 1024 ÷ 256 ≈ 232 sessions

    This is only a theoretical ceiling. In practice, you must also account for fragmentation, scheduling overhead, and headroom for traffic spikes. If the average context grows to 8,192 tokens, theoretical concurrency on the same card drops to about 58 sessions. Clearly, context length is one of the most sensitive factors in capacity planning.

    5. Engineering Best Practices

    1. Manage session state in tiers

    Classify sessions by idle duration and value: active sessions stay in VRAM, short-idle sessions are offloaded to local storage, and long-idle sessions are dropped with only their text history retained. This preserves the experience for active users without letting cold sessions hold resources indefinitely.

    2. Reuse shared prefixes

    Many requests share the same system prompt or fixed documents. Reusing the KV Cache for common prefixes reduces redundant computation and VRAM usage. When implementing this, make sure to isolate data between users so cached content is never read across sessions.

    3. Use continuous batching

    Traditional static batching waits for an entire batch to finish before scheduling the next one. Continuous batching allows requests to join or leave mid-generation, significantly improving GPU utilization. This is the scheduling approach adopted by most mainstream inference frameworks today.

    4. Control context length

    Longer conversations mean more history tokens and higher VRAM and recomputation costs. Set a reasonable context limit and summarize or compress earlier turns, keeping only key information. This requires balancing information completeness against cost.

    5. Establish monitoring metrics

    Continuously track the following metrics: time to first token, per-token generation latency, VRAM utilization, cache hit rate, recomputation ratio, and restore time. Only with data can you judge whether the current policy is sound and whether the bottleneck lies in VRAM, bandwidth, or compute.

    6. Rate Limiting and Capacity Planning

    When compute runs short, simply pausing the service is not the only option. More common measures include queuing new requests, allocating quotas by user tier, compressing overly long contexts, and capping the maximum generation length during peak hours. These spread resource pressure across multiple points without interrupting service.

    For capacity planning, proceed as follows:

    1. Estimate average and peak concurrency from historical traffic.
    2. Run load tests with realistic context length distributions—not just short prompts.
    3. Reserve headroom for traffic spikes and failover.
    4. Only when capacity stays near its limit long-term should you decide whether to scale up, optimize the model serving stack, or adjust product strategy.

    In public cloud environments, elastic GPU instances, container orchestration, and autoscaling can support capacity expansion, but decisions should still factor in actual resource quotas and budget constraints rather than relying on automation alone.

    7. Conclusion

    What fundamentally determines an LLM service’s concurrency is whether VRAM and bandwidth can sustain enough concurrent sessions. The KV Cache is both the guarantee of performance and the main source of cost. Among the three strategies—keep, offload, and drop-and-recompute—the right choice depends on idle duration, recovery time, and how latency-sensitive the business is, validated through continuous metric monitoring and load testing.