Long-running agents accumulate memory that eventually poisons their own decisions. A financial document reviewer that runs for six hours will retrieve outdated risk assessments, conflicting guidance from earlier in the session, or stale user preferences. Conventional retrieval optimizes for semantic similarity, not downstream utility. You get the most relevant memory entry by cosine distance, which might be the exact wrong context for the current task.
MeClear treats memory management as a risk-attribution problem. Instead of pruning by recency or similarity score, it uses cooperative game theory to measure which memory entries degrade task performance, then selectively suppresses them from the active context without permanent deletion.
The Failure Mode
External memory systems extend agent context across sessions. A portfolio management agent might store:
- User risk tolerance statements from last week.
- Sector allocation rules from yesterday.
- A contradictory instruction from two hours ago.
When the agent retrieves all three for a new trade decision, the conflicting evidence creates a context collision. The LLM sees incompatible guidance and either hallucinates a middle ground or defaults to the most recent (but possibly incorrect) instruction.
Standard retrieval pipelines rank by embedding similarity. They do not measure whether a memory entry will help or hurt the next inference step. You can add recency weighting or manual tagging, but neither solves the core problem: some memories are semantically relevant yet functionally toxic.
Cooperative Shapley Attribution
MeClear borrows Shapley values from cooperative game theory. Instead of asking "how similar is this memory to the query?" it asks "how much does this memory contribute to task success when combined with other retrieved entries?"
The process:
- Leave-One-Out screening: Remove each memory entry individually and measure task performance. If removing memory M improves accuracy, M is a candidate for clearance.
- Sampled Shapley attribution: Evaluate memory subsets (coalitions) to distribute utility across interacting entries. A memory that looks harmless in isolation might poison the context when combined with another entry.
- Nested filtration: Rank memories by attributed risk, then apply a minimal clearance strategy. Remove only enough entries to restore task performance above a threshold.
Shapley attribution resolves redundant conflict masking. If two memories contradict each other, removing just one might not fix the problem. The cooperative framework detects when both must be suppressed together.
Architecture
MeClear sits between the retrieval layer and the agent executor. It does not replace your vector store or semantic search. It adds a post-retrieval filter that evaluates downstream utility before injecting memories into the prompt.
┌─────────────────┐
│ User Query │
└────────┬────────┘
│
▼
┌─────────────────┐
│ Semantic Search │ ← Standard embedding retrieval
│ (Vector Store) │
└────────┬────────┘
│
▼
┌─────────────────┐
│ MeClear Filter │ ← Shapley attribution + clearance
│ │
│ 1. LOO screen │
│ 2. Shapley rank │
│ 3. Minimal prune│
└────────┬────────┘
│
▼
┌─────────────────┐
│ Agent Executor │ ← Receives filtered context
└─────────────────┘
The memory bank remains intact. MeClear suppresses entries from the active context for the current task, then releases them back into the pool. This allows the agent to recover from temporary conflicts without losing long-term knowledge.
Triggering Clearance
MeClear runs clearance on-demand, not continuously. Three common triggers:
| Trigger | When to Use | Trade-off |
|---|---|---|
| Token budget exhaustion | Context window fills with retrieved memories | Reactive; waits until the problem is critical |
| Performance degradation | Task accuracy drops below threshold across N recent inferences | Requires instrumentation to measure task success in real time |
| Periodic pruning | Every M queries or T minutes | Proactive but may clear memories unnecessarily |
Financial agents benefit from performance-based triggers. If a portfolio rebalancing agent starts making trades that violate stated risk limits, clearance runs immediately. The system measures task recovery: does removing the flagged memories restore compliance?
Measuring Downstream Utility
The core challenge is evaluating memory utility without re-running the entire agent workflow for every candidate eviction. MeClear uses sampled Shapley values to approximate contribution.
For a memory set M = {m1, m2, ..., mn} and task T:
- Sample random subsets (coalitions) of memories.
- Run the task with each coalition and measure success (binary: pass/fail or continuous: accuracy score).
- Compute marginal contribution of each memory across coalitions.
- Rank memories by negative contribution (high negative = high risk).
The sampling budget controls cost. Evaluating all possible coalitions is exponential. MeClear defaults to 100 samples per clearance cycle, which provides stable rankings for memory pools up to 50 entries.
Minimal Clearance Strategy
Once memories are ranked by risk, MeClear applies a greedy removal strategy:
- Remove the highest-risk memory.
- Re-run the task on the cleared context.
- If task performance recovers, stop. Otherwise, remove the next highest-risk memory and repeat.
This nested filtration avoids over-pruning. If removing two memories restores task success, the third-ranked memory stays in context even if it has a negative Shapley value.
The recovery threshold is configurable. For high-stakes financial tasks, you might require 95% accuracy before stopping clearance. For exploratory document review, 80% might suffice.
Code Sketch: Shapley Sampling
import random
from typing import List, Callable
def shapley_attribution(
memories: List[str],
task_fn: Callable[[List[str]], float],
n_samples: int = 100
) -> dict[str, float]:
"""
Approximate Shapley values for memory entries.
task_fn takes a list of memory strings and returns a success score (0.0 to 1.0).
"""
contributions = {m: [] for m in memories}
for _ in range(n_samples):
# Sample a random coalition size
k = random.randint(1, len(memories))
coalition = random.sample(memories, k)
# Measure task performance with and without each memory
for m in memories:
if m in coalition:
without = [x for x in coalition if x != m]
marginal = task_fn(coalition) - task_fn(without)
else:
with_m = coalition + [m]
marginal = task_fn(with_m) - task_fn(coalition)
contributions[m].append(marginal)
# Average marginal contributions
return {m: sum(vals) / len(vals) for m, vals in contributions.items()}
def minimal_clearance(
memories: List[str],
task_fn: Callable[[List[str]], float],
threshold: float = 0.85
) -> List[str]:
"""
Remove memories until task performance exceeds threshold.
Returns the cleared memory list.
"""
shapley = shapley_attribution(memories, task_fn)
ranked = sorted(shapley.items(), key=lambda x: x[1]) # Lowest (most negative) first
cleared = memories.copy()
for memory, score in ranked:
if task_fn(cleared) >= threshold:
break
cleared.remove(memory)
return cleared
This is a simplified implementation. Production systems add early stopping, parallel coalition evaluation, and caching for repeated task runs.
Experimental Results
The paper evaluates MeClear on ten long-dialogue memory pools. Each pool contains 50 to 200 memory entries accumulated over multi-hour agent sessions. Tasks include document Q&A, preference-based recommendation, and multi-step planning.
Key metrics:
- Target recall: 85.9% (percentage of correct task completions after clearance).
- Task recovery rate: 82.3% (percentage of failed tasks that recover after clearance).
- Improvement over LOO baseline: +25.5 percentage points.
Leave-One-Out alone fails when conflicts require removing multiple memories. Shapley attribution detects these interactions and clears both entries together.
Observability Hooks
To run MeClear in production, instrument:
- Memory retrieval logs: Track which entries are retrieved for each task.
- Task success metrics: Binary pass/fail or continuous accuracy scores.
- Clearance events: Log which memories were suppressed, their Shapley scores, and whether task recovery succeeded.
- Coalition sampling cost: Count task evaluations per clearance cycle to monitor inference budget.
Financial agents should also log the business impact of cleared memories. If MeClear suppresses a user preference that was actually correct, you need a feedback loop to restore it and adjust the attribution model.
Failure Modes
High sampling cost: Evaluating 100 coalitions means running the task 100 times. For agents with expensive tool calls (database queries, API requests), this becomes prohibitive. Mitigation: cache task results for identical memory coalitions or reduce sample count at the cost of attribution accuracy.
Incorrect task success measurement: If your task metric is noisy or delayed, Shapley values will be unstable. A portfolio agent that measures success by end-of-day P&L cannot run clearance in real time. You need a proxy metric (e.g., compliance with risk limits) that updates immediately.
Permanent memory loss: MeClear suppresses memories for the current task but does not delete them. If you need to permanently remove toxic entries, add a secondary review step that flags memories with consistently negative Shapley values across multiple tasks.
Context window overflow during clearance: If the memory pool is large, even the clearance process might exceed token limits. Mitigation: pre-filter by recency or semantic similarity before running Shapley attribution, or partition the memory bank into task-specific namespaces.
When to Use MeClear
Good fit:
- Long-running agents (hours to days) that accumulate conflicting or outdated context.
- Financial document review, portfolio management, or compliance monitoring where stale guidance creates regulatory risk.
- Multi-session agents that preserve user preferences across interactions.
- Tasks where semantic similarity does not predict downstream utility.
Poor fit:
- Short-lived agents (minutes) where memory conflicts are rare.
- Agents with small, curated memory banks that rarely conflict.
- Real-time systems where the cost of coalition sampling exceeds the cost of occasional bad retrievals.
- Tasks where you can afford to re-run the entire workflow from scratch instead of managing memory state.
Technical Verdict
MeClear solves a real production problem: agents that poison their own context with semantically relevant but functionally toxic memories. The cooperative game-theoretic approach is elegant and the experimental results are strong. The cost is non-trivial (100+ task evaluations per clearance cycle), so you need cheap task metrics or a high tolerance for inference latency.
For financial agents that run for hours and make high-stakes decisions, the trade-off is worth it. A portfolio rebalancing agent that avoids a single bad trade due to stale risk guidance pays for the clearance overhead many times over. For exploratory document review or low-stakes chatbots, stick with recency-weighted retrieval and save the Shapley computation for when you actually see performance degradation.
The framework is modular. You can plug MeClear into any agent architecture that uses external memory, as long as you can measure task success in a reasonable time window. The key insight is treating memory management as a risk problem, not a similarity problem.
Top comments (1)
The failure mode section is the part I'd read twice before adopting this. If your task_fn is a proxy metric rather than the real outcome, the Shapley values you compute are only as reliable as that proxy. I ran into this with a document pipeline where the proxy was field validation passing, but the real failure was subtler: valid-but-wrong extractions that only showed up in downstream processing an hour later. The proxy passed, so MeClear kept the conflicting memories, and the errors compounded quietly.