Memory Systems · 2025

MEM1: Learning to Synergize Memory and Reasoning for Efficient Long-Horizon Agents

Memory Systems MEM1 2025 · arXiv 2506.15841
Topic
Memory Systems
Venue
NUS · MIT · Yonsei)
Read
16 min
Source
arXiv:2506.15841

In one line

Instead of letting an agent's context balloon as it works through a long, multi-step task, MEM1 trains the model with RL to rewrite a single compact "internal state" each turn — folding new observations into it and throwing everything else away — so memory stays roughly constant no matter how long the task runs.

The breakdown

TL;DR

Long-horizon agents (deep-research bots, web shoppers, multi-hop QA) usually keep every past thought, action, and tool output glued to the prompt. The context grows without bound, inference gets slow and expensive, and the model’s reasoning actually degrades once the context runs past what it saw in training. MEM1’s fix is deceptively simple: at every turn the agent produces one internal state (<IS>) that merges its old memory with the latest observation, and then the system deletes the previous turn’s context entirely. The only thing carried forward is that consolidated state. The trick is that this consolidation behavior is learned end-to-end with reinforcement learning — nobody hand-writes a summarizer. The headline result: a 7B MEM1 model beats a 14B instruct model on a 16-objective multi-hop QA task while using ~3.7× less peak memory and running ~3.4× faster, and it generalizes to far longer task horizons than it was trained on.

Problem & Motivation

The pain in one sentence: every extra turn an agent takes makes the next turn slower, more expensive, and dumber, because the whole history rides along in the prompt.

Concretely, the standard ReAct-style loop appends <think>, action, and observation to the context at every step. Three things break as that context grows:

  1. Cost and memory scale with history. Transformer attention is O(N²) compute (O(N) with KV-caching) and O(N) memory in context length N. A 20-turn web task can carry tens of thousands of tokens of stale page dumps. On a serving stack like vLLM you have to reserve GPU memory for the worst-case context, so long agents waste hardware.
  2. Generalization dies past the training horizon. If you trained on tasks that fit in, say, 4k tokens of history, a task that runs to 20k tokens is out-of-distribution. The model has literally never reasoned over inputs that long and falls apart.
  3. Long context dilutes attention. Even when the relevant fact is still technically in the prompt, it’s buried under irrelevant retrieved text. This is the well-documented “lost in the middle” problem — more context can make reasoning worse.

Prior fixes don’t close the loop. Long-context modeling targets static documents, not interactive multi-turn loops. External memory modules (a separate summarizer or a vector DB) are bolted on and trained separately from the agent’s policy — so the agent never learns to memorize for its own downstream reasoning, and you now operate two models instead of one. Meanwhile the RL-trained tool-use agents (Search-R1, DeepResearcher) just let the prompt grow unbounded. The open question the paper poses: can a model learn memory consolidation as part of its reasoning, keeping only what it needs to solve the task?

What’s New (Core Contribution)

  1. Reasoning is the memory (constant-memory rollout). Before: memory was an external module or the whole appended history. Now: the agent’s chain-of-thought is the memory store. Each turn it writes a fresh <IS> that consolidates prior state + new info, and the previous turn’s tokens are pruned. The agent provably retains at most two <IS>, two <query>, and one <info> block at any moment — bounded memory, no architecture changes, no extra model.
  2. End-to-end RL with no explicit memory reward. Before: summarizers trained with their own supervised objective, disconnected from task success. Now: MEM1 is trained purely on task-success reward (exact match / environment reward). Memory efficiency emerges because the rollout physically forces consolidation — the agent literally cannot see old context, so it must learn to carry forward what matters. There is no “be concise” term in the reward.
  3. Masked-trajectory policy optimization. This is the genuinely tricky engineering contribution. Pruning context mid-rollout breaks the linear-trajectory assumption that PPO relies on. MEM1 reconstructs a single coherent trajectory and applies a custom 2D attention mask so each generated token only attends to the context that was actually present when it was generated. This keeps the PPO log-prob ratio, advantage, and KL terms mathematically valid under a context that mutates each turn.
  4. Compositional multi-objective task augmentation. Existing “multi-hop” benchmarks (HotpotQA, 2Wiki) really only need ~2 hops. The authors stitch N independent QA questions into one composite query, manufacturing arbitrarily long-horizon tasks from standard datasets — a cheap way to create the long tasks needed to train and stress-test memory.

How It Works (Technically)

The whole system is a loop with one unusual rule: the context is reset to (almost) empty every turn. Let me walk one task through it.

The tags. MEM1 structures everything with four XML-style tags:

  • <IS>internal state: the agent’s blended reasoning + memory. This is the thing that survives.
  • <query> — an action that hits the environment (a search, a click).
  • <info> — the environment’s response (search results, page HTML). Generated by the world, not the model.
  • <answer> — the final response that ends the task.

One trace, step by step. Say the composite task is: “Who directed the film that won Best Picture in 1994, and what is the capital of the country where that director was born?” — two interleaved multi-hop questions.

  • Turn 0. Prompt = task. Agent writes <IS_0>: “Two objectives. Q1: 1994 Best Picture → director. Q2: that director’s birth country → capital. Start with Q1.” Then <query_0>: “1994 Academy Award Best Picture winner”. The system runs the search and appends <info_0> = the results (plus a hint: [HINT: YOU HAVE 5 TURNS LEFT]).
  • Prune. Everything from turn 0 except the original task is now eligible for deletion. What carries to turn 1 is the task prompt, <IS_0>, <query_0>, <info_0>.
  • Turn 1. Agent reads <info_0> (it says Forrest Gump, directed by Robert Zemeckis) and writes <IS_1>: “Q1 director = Robert Zemeckis ✓. Q2 now: Zemeckis birth country → capital. Need his birthplace.” Crucially <IS_1> bakes the Zemeckis fact into itself — because <info_0> is about to be thrown away. Then <query_1>: “Robert Zemeckis birthplace”. The old turn-0 tags are pruned; only <IS_1> and the new query/info survive.
  • Turn 2 … Continues: find Zemeckis was born in Chicago, USA → capital Washington D.C., fold into <IS_2>, then emit <answer>: “Robert Zemeckis; Washington, D.C.”

At no point did the growing pile of search results stay in context. The agent’s running understanding lived entirely in successive <IS> blocks. If the agent forgets to write a fact into <IS>, it’s gone forever — that’s the pressure that teaches consolidation.

The RL part (demystified)

MEM1 is trained with PPO (Proximal Policy Optimization), the same algorithm behind most RLHF. In plain terms:

  • The policy πθ is the LLM — given the current context, it outputs a probability over the next token. “Acting” means sampling tokens to build <IS>, <query>, <answer>.
  • The reward is task success: exact-match for QA, the environment’s score for WebShop. One number at the end of the trajectory. There is no reward term for brevity or memory.
  • Advantage answers “was this token better or worse than expected?” A critic (value function) predicts the expected reward from a given state; advantage = actual outcome − prediction. Tokens that led to above-expectation reward get reinforced. The paper uses PPO specifically because it gives token-level advantages, which stabilizes training over these long, branching rollouts.
  • The PPO objective maximizes a clipped ratio ρ = πθ(a|s) / π_old(a|s) weighted by advantage, with a KL penalty keeping the new policy from drifting too far from the old one (prevents the model from collapsing into gibberish that games the reward).

Why the masking matters. PPO assumes one clean left-to-right trajectory where token k attended to tokens 1…k−1. But MEM1 deletes tokens between turns, so a naive reconstruction would let token k “see” context that wasn’t actually there when it was generated — making the log-prob ratio ρ wrong, and corrupting the advantage and KL estimates. The fix is a 2D attention mask: for each token, mask out every prior token that wasn’t in memory at that turn. So <IS_(t+2)> is computed attending only to {task, <IS_(t+1)>, <query_(t+1)>, <info_(t+1)>} — exactly what the agent really saw. A second info mask zeros out gradients on <info> tokens (the environment wrote those, not the policy — you don’t want to train the model to “predict” search results). Together these let standard PPO run correctly over a context that mutates every turn.

Architecture & data flow

flowchart TD
  Task[Task prompt: composite multi-objective query] --> IS0["Generate IS_t (consolidate prior memory + last info)"]
  IS0 --> ACT{Query or Answer?}
  ACT -- query --> ENV[World model: search / browse / WebShop]
  ENV --> INFO["info_t (+ HINT: turns left)"]
  INFO --> PRUNE["PRUNE: delete turn t-1 tags<br/>keep only task + latest IS/query/info"]
  PRUNE --> IS0
  ACT -- answer --> OUT[Final answer y]
  OUT --> REWARD[Reward: exact match / env reward]
  REWARD --> MASK["Stitch full trajectory<br/>apply 2D attention mask + info mask"]
  MASK --> PPO["PPO update: advantage, clipped ratio, KL penalty"]
  PPO -.updates policy.-> IS0

Peak context tokens vs. number of task objectives — a baseline agent that appends everything (linear growth) versus MEM1's near-flat consolidated memory. Schematic, shaped to match the paper's reported scaling. Drag the slider to add objectives.

The algorithm, simplified

# MEM1 rollout: constant-memory agent loop (follows Alg. 1)
# llm(ctx) -> str : the policy model, samples until it emits </query> or </answer>
# world(query) -> str : environment feedback (search results, page HTML)

def mem1_rollout(task, world, max_turns=6):
    context = task                       # the ONLY thing that always survives
    internal_state = ""                  # the agent's running memory == its reasoning
    for t in range(max_turns):
        turns_left = max_turns - t
        # 1) Consolidate: write a fresh IS that folds prior memory + last info.
        #    Anything not written into IS is about to be deleted forever.
        out = llm(context)               # emits <IS>...</IS> then <query> or <answer>
        internal_state = parse(out, "IS")

        if has_tag(out, "answer"):
            return parse(out, "answer")  # task done

        query = parse(out, "query")
        info  = world(query)             # external observation (NOT model-generated)
        info  = f"[HINT: {turns_left} TURNS LEFT] " + info

        # 2) PRUNE: rebuild context from scratch — old turn is gone.
        #    Only task + current IS + current query + current info remain.
        context = f"{task}\n<IS>{internal_state}</IS>\n<query>{query}</query>\n<info>{info}</info>"

    return parse(out, "answer")          # forced to answer when budget runs out

# Training: run many rollouts, score final answers (exact match / env reward),
# stitch each rollout into one trajectory, apply the 2D attention mask so every
# token only "sees" the context it really had, then do a PPO update.

The contribution is in two lines: context = f"{task}..." (the reset) and the masked PPO update. Everything else is a vanilla ReAct loop.

Built on Prior Work

Prior ideaWhat it gaveWhat this paper changes
ReAct (Yao et al.)Interleave reasoning + acting in one prompt loopKeeps the loop, but prunes context each turn instead of appending — reasoning doubles as memory
Search-R1 / DeepResearcherRL-trained tool-use agents with verifiable rewardsSame RL-with-verifiable-reward recipe, but adds bounded memory; borrows the info-masking trick from Search-R1
PPO / Reinforce++Token-level policy optimizationExtends PPO to mutating contexts via the 2D attention mask so advantages stay valid
External memory (A-MEM, summarizers, RAG memory)A separate module stores/retrieves past infoDrops the separate module entirely; memory is learned inside the policy, trained end-to-end
Multi-hop QA (HotpotQA, 2Wiki, NQ)Datasets needing a few reasoning hopsComposes them into N-objective tasks to manufacture true long-horizon benchmarks

Results & Evidence

Setup. All MEM1 variants are RL-fine-tuned from Qwen2.5-7B Base with PPO. Three domains: internal RAG QA over a Wikipedia corpus, open-domain web QA (zero-shot transfer, unseen at train time), and WebShop navigation. Trained only on a 2-objective QA composition, then tested on 3/4/6/8/16-objective tasks.

Headline numbers:

  • 16-objective QA: MEM1-7B surpasses Qwen2.5-14B-Instruct on accuracy while using ~27% of the peak tokens and ~29% of the inference time — the abstract’s “3.5× performance / 3.7× memory” framing. Memory stays nearly flat as objectives scale 2→16, while every baseline scales roughly linearly (and several collapse — accuracy near zero — at high objective counts).
  • WebShop: MEM1-WebShop hits 70.87 avg reward, edging out AgentLM-13B (70.80) at half the parameters, with 2.8× lower peak tokens, 1.9× lower dependency, 1.5× faster inference. Also beats GPT-4o on this task.
  • Single-objective Wiki RAG: MEM1 gets the best EM and the lowest peak tokens / dependency among 7B models — despite never being trained on single-objective tasks.
  • RL beats SFT decisively. A supervised model trained on GPT-4o trajectories with the same rollout underperforms the RL agent across the board — the consolidation skill needs RL, not imitation.

Emergent behaviors (qualitative). Trace analysis shows the agent maintaining separate memory slots per sub-question, switching focus when one objective stalls, self-verifying and correcting earlier mistakes, decomposing complex queries, and re-scoping failed searches — none of it explicitly programmed.

Caveats — read these before you sell it:

  • Verifiable reward required. MEM1 needs environments with clean, checkable rewards (exact match, env score). The authors flag this as the central limitation: open-ended tasks with ambiguous/noisy/delayed reward are out of scope and untested.
  • Narrow domains. Everything is QA + WebShop. No code agents, no long multi-turn dialogue, no tool ecosystems with dozens of tools.
  • Synthetic long-horizon. The 16-objective tasks are composed from independent questions — they’re not naturally interdependent long tasks, so “16 objectives” is an upper-bound stress test, not a typical workload.
  • Consolidation is lossy by design. If the agent fails to write a needed fact into <IS>, it’s unrecoverable. There’s no fallback retrieval over discarded context — a real risk on tasks where relevance only becomes clear later.
  • Training cost. This is full PPO fine-tuning of a 7B model with a critic, not a prompt you can drop in. Non-trivial GPU spend.

How You’d Use It

For an AI-services shop, MEM1 is most interesting as a cost-and-latency lever for production agents, not as a research toy.

  • Bounded-cost deep-research / RAG agents. The clearest fit. If you’re billing clients per task or eating inference cost on long research loops, constant memory means predictable, flat per-task cost regardless of how many sub-questions a task needs. That’s a real margin story you can put in a proposal.
  • Long web-automation agents (WebShop-like). Page dumps are the worst context bloat in browser agents. Consolidating each page into an <IS> before discarding the HTML is exactly the pattern that keeps these agents from hitting context limits at turn 15.
  • A capability you can offer: “fixed-memory agents” as a tier — same accuracy, dramatically lower serving cost, no context-window blowups. The 7B-beats-14B result is your pitch: smaller model, lower hosting bill, better long-task behavior.
  • Where it does NOT fit yet: open-ended creative/advisory tasks without a verifiable reward, or anything where you can’t afford lossy memory (legal/medical where a dropped detail is a liability). For those, keep full context or a real retrieval backstop.

The honest read: the idea (consolidate-and-prune) you can prototype with prompting today. The trained behavior that makes it reliable needs the RL pipeline, which is a build, not a buy.

Build Your Own (Minimal Recipe)

Tier 1 — prompted prototype (a day). Get 80% of the intuition without any training:

  1. Write a ReAct loop where each turn the model must output an <IS> block + one action.
  2. After each tool call, reconstruct the prompt as task + latest_IS + latest_query + latest_info — drop everything else. (Literally the context = ... line above.)
  3. Add the [HINT: N turns left] so the model knows when to answer.
  4. Run it on a multi-hop QA set and watch memory stay flat. You’ll see decent behavior with a strong instruct model — but also the failure mode (dropped facts), which is exactly why the paper needed RL.

Tier 2 — the real thing (a project). To get reliable consolidation you need RL:

  • RL framework: verl or OpenRLHF (both support PPO for LLMs and multi-turn rollouts); the MEM1 repo (github.com/MIT-MI/MEM1) builds on this lineage.
  • Base model: Qwen2.5-7B Base (RL-from-base beat instruct/SFT in their ablations).
  • Reward: exact-match verifier for QA, or your environment’s score. Keep it verifiable.
  • Data: compose existing QA pairs into N-objective tasks — a few lines of glue over HotpotQA/NQ.
  • The two hard parts:
    1. The masked trajectory. This is the crux. You must stitch each pruned rollout into one sequence and apply a 2D attention mask so each token attends only to its real context, plus an info-mask so gradients skip environment tokens. Get this wrong and PPO silently learns garbage. Budget most of your engineering here.
    2. Rollout/serving plumbing. Multi-turn generation with mid-rollout context surgery, batched across many trajectories, fast enough to train. This is where verl earns its keep.

How to Improve It

  1. Add a retrieval backstop for discarded context. MEM1’s lossiness is its biggest weakness. Pair the consolidated <IS> with a cheap vector store of pruned <info> blocks the agent can optionally re-query. You’d keep constant active memory but recover from premature forgetting — testable as accuracy gain on tasks where late relevance matters.
  2. Reward-model MEM1 for open-ended tasks. The authors name this as the open frontier. Swap verifiable reward for a learned reward model / LLM-judge so consolidation can be trained on dialogue, writing, or advisory tasks. The risk is reward hacking, but it’s the obvious next domain.
  3. Adaptive memory budget. Right now memory is hard-bounded (two <IS>, etc.). Let the agent grow its state for genuinely hard objectives and shrink for easy ones — train it to spend memory like a budget. Could close the gap with full-context models on the hardest tasks.
  4. GRPO instead of PPO. GRPO drops the critic (uses group-relative advantages), which is cheaper and increasingly standard. Re-deriving the masked-trajectory math under GRPO would cut training cost and is a clean ablation.
  5. Naturally-interdependent long tasks. Replace the composed-from-independent-questions benchmark with tasks where step 12 genuinely depends on step 2’s finding. That’s a harder, more honest test of whether consolidation actually preserves the right things.

Glossary

  • Long-horizon agent — an agent that takes many sequential turns (searches, clicks, reasoning steps) to finish one task.
  • Internal state (<IS>) — MEM1’s single consolidated memory+reasoning block, rewritten each turn; the only thing carried forward.
  • Context / prompt growth — the unbounded expansion of the input as past turns get appended; the problem MEM1 kills.
  • Constant memory — peak context stays roughly flat regardless of task length, because old turns are pruned.
  • ReAct — a prompting pattern that interleaves reasoning (“thought”) and acting (“tool call”) in one loop.
  • RL (reinforcement learning) — training a policy by reward instead of labeled examples; the model learns from outcomes of its own actions.
  • Policy (πθ) — here, the LLM itself: maps context to a distribution over next tokens.
  • Reward (verifiable) — a checkable success signal (exact match, environment score) used to train the agent; “verifiable” = computable without human judgment.
  • Advantage — how much better an action was than the critic’s expectation; drives which tokens get reinforced.
  • PPO (Proximal Policy Optimization) — an RL algorithm that updates the policy with a clipped probability ratio + KL penalty for stability; gives token-level advantages.
  • KL penalty — a term keeping the updated policy close to the old one, preventing reward-hacking collapse.
  • Critic / value function — a model predicting expected future reward from a state; used to compute advantage.
  • 2D attention mask — MEM1’s mask ensuring each token attends only to the context that existed when it was generated, so PPO stays valid under pruning.
  • Info mask — masks gradients on environment-generated tokens so the model isn’t trained to predict tool outputs.
  • Masked trajectory — the stitched-together full rollout (with masks applied) that lets a mutating-context loop be optimized like a normal linear one.
  • Multi-objective task — a composite query built by interleaving N independent questions, used to manufacture long-horizon training data.
  • Exact Match (EM) / F1 — QA accuracy metrics; EM = answer exactly right, F1 = token overlap.
  • Peak token usage — the largest context the agent ever holds during a task; MEM1’s efficiency headline.
  • WebShop — a benchmark environment where an agent browses a simulated shopping site to fulfill a request.
  • SFT (supervised fine-tuning) — training on expert/teacher trajectories; underperformed RL here.
  • KV-cache — cached attention keys/values that make autoregressive decoding O(N) instead of O(N²); still grows with context.