Back to patterns

State and Memory Control

Collaboration & CoordinationOCLS LAYER

Separate short-term state from long-term memory to prevent leakage and confusion.


Context

Agents handle both session-scope state (current step, temporary variables) and long-term memory (learned patterns, user preferences, prior outcomes). Without separation, ephemeral state outlives the session or important memory is lost with it.

Problem

With no boundary between state and memory, stale session state leaks into new sessions and triggers wrong decisions. The reverse failure — discarding learning at session end — causes the system to repeat the same mistakes. In multi-agent settings, conflicts over shared state must also be managed.

Forces

  • Remembering everything raises retrieval cost and noise; remembering selectively risks dropping critical signal.
  • Centralized state gives consistency but becomes a bottleneck; distributed state introduces synchronization problems.
  • Memory expiration is easy to manage but may discard memory that turns out to be needed longer than expected.

Solution

Draw a hard line between session-scope short-term state and system-scope long-term memory. Short-term state is cleared automatically at session end; long-term promotion is governed by explicit criteria (repeated pattern, quality contribution, etc.). In multi-agent settings, define read/write permissions on shared memory per agent. Long-running agents need a compaction mechanism. The compaction pattern from Anthropic's [Effective Context Engineering for AI Agents] summarizes the conversation when the context window fills, initializing a new window while preserving architectural decisions, open bugs, and implementation details, and discarding duplicate tool output. The companion structured note-taking pattern — where the agent writes structured notes outside the context and reads them back — keeps state accurate across thousands of steps. A long context window (hundreds of thousands to a million tokens) being available does not mean every piece of information should be loaded into one window — "fit it all in a big window" versus "narrow it with context routing" is a trade-off across cost, latency, and attention budget, chosen per task. The intermediate reasoning trace of an extended-thinking model is session-scope short-term state, not long-term memory — never promote the thinking process itself to long-term memory.

Judgment question

What stays in the session, and what gets promoted to system memory?

Application scenario

Illustrative scenario — figures and company names in this page are hypothetical for explaining the pattern, not measured data.

Customer A asks about a shipping delay, then about changing payment method in the same session. Short-term state (current conversation, last response) is valid only inside the session. The fact that "this customer has experienced three consecutive shipping delays and previously received a 10% coupon" is promoted to long-term memory, managed by the Knowledge Agent. The Response Agent can read long-term memory but not write it; it can only request promotion. The QA Agent validates the promotion criteria (≥3 occurrences, quality contribution) before the write lands.

How it breaks

Without separation, the previous session's shipping-delay context bleeds into the new session's payment-change response — an inappropriate "sorry your shipment is delayed" opens a reply about billing. The opposite failure — wiping everything at session end — leaves the system unable to consult prior resolutions when the same customer returns with the same problem, so it proposes the same answer again.

Implementation pattern bridge

  • Session Store / Vector Memory

Mostly an infrastructure-design concern. Key implementation work: separating session store (short term) from vector DB / knowledge base (long term) and controlling read/write authority.

Academic References

  • The Rise of Agentic AI: Architectures, Taxonomies, and Evaluation Metrics — Future Internet (MDPI)

Apply this pattern

Use the review skill in your repository, then compare the implementation decisions in the related articles. Check each example against your own permissions, data, and failure modes.

Related implementation notes

  • Token Strategy — Efficiency, Balance, Aggressive, and StaffingSaving tokens and burning them are both strategies. Run the same loop in three modes — efficiency, balance, aggressive — and allocate mode and people to the business situation and evolution stage. This piece organizes token usage as a technical choice.
  • The Data Flywheel — Making the Loop Improve, Not Just PersistMoney runs the loop and tokens power the compute, but what makes the next turn better than the last is the data the operation accumulates. Turn the flywheel with three assets — traces, evals, learning signals — and keep data as an asset outside the model to preserve sovereignty.
  • Model Sovereignty and Human Agency — Two Axes on the Four LayersModels get swapped and compute gets cheap. So what you control is not the model but the learning loop you build on top of it, and what aims that loop is human direction. This piece translates both axes into design using the vocabulary of reopt architecture's four layers, eight patterns, and OCLS loop.

Related patterns

  • Context RoutingDesign information flow so each participant receives exactly what it needs.
  • Evaluation and GuardrailsDistinguish acceptable judgment from dangerous judgment via evaluation criteria and safety rules.