Hierarchical Memory and Cache Scheduling for Multi-Agent Inference

    This article explains why multi-agent inference strains KV cache memory and how a tiered memory design with task-aware lifecycle scheduling can keep scarce accelerator memory focused on tasks that are actively computing. It also covers how to estimate cache size, when cached prefixes can be safely reused across instances, how to handle sharing and invalidation, and how to evaluate and roll out these optimizations in stages.

    1. Why Multi-Agent Inference Requires a New Approach to Memory Management

    Multi-agent applications typically include several stages: task planning, information retrieval, tool calling, and result verification. Unlike single-turn question answering, these stages may run in parallel, and some may wait for a long time for external systems to return results. They share part of the task context but maintain their own reasoning contexts. As a result, they produce many state data items with different lifetimes and access frequencies.

    In attention-based autoregressive models, the key-value (KV) cache stores the keys and values computed for tokens that have already been processed. When the model generates a new token, it uses these cached entries in the attention computation, which avoids recomputing the earlier tokens.

    The cache reduces repeated computation, but it also consumes a significant amount of storage capacity. When long contexts, high concurrency, and many branching tasks run at the same time, the cache can compete with model weights and intermediate computation results for accelerator memory.

    This raises a key question: which context states should limited high-speed memory keep first?

    Adding hardware capacity alone does not fully solve the utilization problem. If agents waiting for tool results occupy accelerator memory for a long time, while agents that are generating output queue because memory is short, the system can still suffer lower throughput and higher tail latency. A better direction is to connect cache management with the execution state of each agent.

    2. Estimate Cache Size First, Then Discuss Optimization

    Under a common attention structure, the theoretical payload of the KV cache for a single sequence can be approximated as:

    1. KV cache bytes ≈ 2 × number of layers × number of cached tokens × number of KV heads × head dimension × bytes per element

    The factor of 2 accounts for storing both keys and values. For models that use grouped-query or multi-query attention, use the actual number of KV heads, not the number of query heads.

    This formula is only a first-pass estimate. Real memory use also depends on:

    • the allocation granularity of cache blocks and internal fragmentation;
    • the number of concurrent sequences and their length distribution;
    • whether different sequences share a prefix;
    • the cache data type and its additional metadata;
    • how the inference engine implements batching and cache layout.

    Therefore, capacity planning should not rely only on “maximum context length × maximum concurrency,” and it should not look only at average context length. A more practical approach is to collect the distribution of context lengths, active task counts, and waiting times in typical workloads, then add a safety margin based on peak scenarios.

    It is important to note that the KV cache mainly reuses intermediate states that the model forms after processing a specific context. It is not a general knowledge base that can be applied directly to arbitrary prompts.

    3. The Core of Tiered Memory Is Matching Data Location to Access Patterns

    A common design divides cache storage into three logical tiers: hot, warm, and cold. These tiers describe access characteristics and management policies. They do not require every system to use the same mix of physical devices.

    1. Hot tier: serving decoding tasks that are running

    The hot tier usually sits in the high-bandwidth memory (HBM) of the accelerator. It holds the cache entries that are about to take part in attention computation.

    Tasks that generate tokens continuously are the most sensitive to cache access latency and bandwidth. If their data is repeatedly moved back from slower media, the speed of token generation can drop directly. Hot-tier management must therefore protect the working set of active tasks, rather than simply trying to store as much cache as possible.

    The system also needs to reserve space for new request admission, batch changes, and temporary computation. If accelerator memory is completely full, running tasks may lack the headroom they need to make progress.

    2. Warm tier: holding short waits and contexts that may resume soon

    The warm tier can use host memory or expansion memory with the appropriate access capability. It suits task state that is temporarily out of computation but is expected to resume soon.

    For example, an agent has started an external retrieval and must wait for the result. During the wait, its historical cache can be moved to the warm tier when conditions allow. This frees accelerator memory for other tasks.

    Migration is not free, however. Data movement consumes transfer bandwidth and can compete with normal inference. For tasks with very short waits, moving data out and then straight back may cost more than it saves.

    Warm-tier policy should therefore combine wait-time estimates, cache size, and the cost of moving data back. It should not decide based only on whether a task is waiting.

    3. Cold tier: keeping low-frequency state that still has value

    The cold tier typically uses larger, lower-cost storage, such as local solid-state drives. It suits context for tasks that are suspended for a long time or resume infrequently.

    Expansion memory and block storage have different access semantics. The former may offer memory-style access, while the latter usually requires explicit read and write operations. The two differ in latency, bandwidth, fault handling, and data movement paths, so they should not be treated as interchangeable media.

    The cold tier also should not become an unbounded cache repository. For state with a low chance of resumption and a limited recomputation cost, reclaiming it directly may be more economical than keeping it.

    4. Lifecycle Scheduling: From “Recently Used” to “Task-Aware”

    Traditional cache eviction policies often rely on last access time or access frequency. Multi-agent tasks, however, carry additional scheduling signals.

    A task that has just entered a long external wait may have been accessed recently, but its cache may not be worth keeping in accelerator memory. Conversely, a task that has received its tool result and is about to continue generating may need to be moved back first, even if it was inactive for a long time.

    Task states can be grouped as follows:

    • Running: performing context processing or token-by-token generation;
    • Waiting: waiting for a tool, user input, or the result of another task;
    • Resumable: the dependencies are satisfied, and the task is about to re-enter the execution queue;
    • Completed: reclaimed by default, with only the parts that have clear reuse value retained;
    • Invalid: no longer allowed to be reused because the model, configuration, or permissions have changed.

    On top of these states, the scheduler weighs task priority, estimated resumption time, cache size, historical reuse, and transfer queue pressure. It uses these factors to decide whether a cache should be kept, migrated, or evicted.

    A practical principle is:

    The benefit of keeping a cache should exceed its capacity cost, its data movement overhead, and the opportunity cost it imposes on other tasks.

    This is a decision framework, not a performance guarantee for any fixed algorithm. A real system can start with explainable rules and then adjust them based on runtime data. This avoids introducing complex prediction models that are hard to verify at the outset.

    The system should also apply hysteresis, such as a minimum residency time or a migration cooldown window. This prevents state from bouncing repeatedly between tiers.

    5. Cross-Instance Reuse: Sharing Data Is Not Enough, Reuse Must Be Verified

    In multi-instance deployments, different requests may repeatedly process the same system instructions, document prefixes, or task background. If each instance computes and stores these prefixes independently, the system repeats both computation and storage.

    Shared prefix caching reduces this waste, but reuse must meet strict conditions. At minimum, the system should verify the following:

    1. The model and related configuration match.