COUNTERMEM: World-Model Verified Counter-Factual Memory for Language Agents
Organizations: University of Illinois Urbana–Champaign · Department of Computer Science, Rutgers University
Abstract
Existing agent memory frameworks mainly create memory through an agent's interaction with the factual world, e.g., remembering feedback from actions taken to improve performance on future tasks. However, these frameworks seldom ask the "what if" question during memory construction: what if a different action had been taken, would the feedback have changed, and how could this feedback become useful memory? Obtaining such feedback directly in an active environment can be expensive and can alter the state needed for comparison. In this work, we introduce COUNTERMEM, a reinforcement-learning framework for constructing and using verified counterfactual memory across tasks. After a failed action, COUNTERMEM evaluates local alternatives from a copy or reset of the original state using executable world models, such as tests, proof checkers, and solvers. It stores improvements with the original and corrected actions, checked outcomes, and conditions for reuse. A learned memory-use policy selects a retrieved record or skips memory to balance task success and interaction cost, while the base LLM remains fixed. Both memory and policy are frozen during held-out evaluation. We evaluate COUNTERMEM on 12 benchmark settings across six domains. With gpt-oss-120b, COUNTERMEM improves both ReAct and Reflexion on all 12 benchmarks across six domains, averaging a gain of 12.6 percentage points over their unaugmented versions. In the four-domain comparison across two backbones, task-run tokens decrease by 7.7-42.0%, excluding offline selector-training costs. Further analyses show that removing verification or persistent storage weakens the gains, while applying verified corrections to unsuitable decisions can reverse them. Code will be released upon acceptance.
Figures & tables
| Base Agent | Augmentation | Math | Coding | Text-to-SQL | SAT | Tokens(M) |
| Backbone: gpt-oss-120b | ||||||
| ReAct | None | 29.8 | 48.7 | 26.8 | 50.7 | 6.0 |
| Best-of- (compute control) | 34.5 | 38.1 | 36.2 | 41.1 | 9.4 | |
| ExpeL ( Zhao et al., 2024 ) | 27.4 | 56.5 | 17.2 | 49.0 | 5.4 | |
| Generative Agents ( Park et al., 2023 ) | 22.6 | 37.1 | 17.6 | 41.6 | 6.2 | |
| Voyager ( Wang et al., 2024 ) | 21.9 | 60.6 | 26.3 | 51.4 | 5.2 | |
Appendix figures & tables10 assets
Supplementary material from the paper’s appendix.
Appendix
| Field | Content and purpose |
| : situation | A numeric threshold predicate may omit its boundary value. |
| : inferior action | The original query uses age > 18 . |
| : improved action | Replace that predicate with age >= 18 . |
| : evidence | The original and edited queries’ result-check outcomes, from the same database state. |
| : conditions | The question requests “at least” the threshold; the column and comparison types are compatible. |
| : reuse statistic | Initially zero; later summarizes observed helpful, neutral, or harmful reuse during construction. |
| Setting | Value |
| Training decision steps | 2,000 |
| Hidden layers | |
| Activation | ReLU |
| Optimizer | Adam, |
| Learning rate | |
| Learning starts | 100 decisions |
| Domain | Benchmark | Evaluator | Score | Decision unit |
| Math | miniF2F ( Zheng et al., 2022 ) | Lean kernel | pass rate | tactic / proof step |
| Math | ProofNet ( Azerbayev et al., 2023 ) | Lean kernel | pass rate | proof continuation |
| Math | AMBER ( Yang et al., 2026 ) | Lean-based checker | pass rate | tactic / lemma choice |
| Coding | SWE-bench Verified ( Jimenez et al., 2024 ; Chowdhury et al., 2024 ) | repository tests | resolved rate | edit / patch |
| Coding | BugsInPy ( Widyasari et al., 2020 ) | regression tests | repair rate | localized edit |
| Formal | DafnyBench ( Loughridge et al., 2025 ) | Dafny verifier | verification | invariant / annotation |
| Condition | What changes | Example of the reuse question |
| Same environment, new task | A different request within the source environment. | Does the boundary correction help a new query over the same schema? |
| New environment, new task | The execution setting as well as the request. | Can a predicate correction apply in a different database with compatible types? |
| Same error, new surface | The form of the task or action, while the underlying mistake is similar. | Does the record match a renamed column or a differently worded inclusive request? |
| Look-alike, changed condition | Surface similarity remains, but a requirement for the correction fails. | Can the agent avoid changing to when the request requires a strict boundary? |
| Benchmark | Score gain | Call reduction | Failure reduction |
| miniF2F | 31.5 / 15.8 | 1.1 / 0.4 | 0.8 / 0.3 |
| ProofNet | 24.2 / 12.1 | 0.9 / 0.3 | 0.7 / 0.4 |
| AMBER | 27.4 / 9.9 | 2.4 / 0.9 | 1.6 / 0.6 |
| SWE-bench Verified | 21.4 / 8.4 | 3.6 / 1.2 | 1.1 / 0.5 |
| BugsInPy | 14.4 / 3.8 | 2.7 / 1.4 | 1.4 / 0.7 |
| DafnyBench | 11.2 / 5.2 | 0.6 / | 0.3 / 0.1 |
| Template | Used in | Required behavior |
| GENERATE_LOCAL ALTERNATIVES | Module I | Return local edits in the fixed edit grammar, one changed decision unit per candidate; do not generate a complete replacement submission. |
| DISTIL_RECORD | Module II | Distill the checked contrast into a reusable record. The condition must be checkable on a future task and must be able to be false on a look-alike task. |
| DRAFT_ACTION | Module III | Produce the action draft before any memory is shown. This is the memory-free draft used by the selector and reuse-credit logic. |
| REVISE_WITH_RECORD | Module III | Show exactly one selected record and revise the draft only when useful; the model may return the draft unchanged. |
| RECORD_RENDER / RECORD_RENDER_NO CONDITION | Condition ablation | Render the same record fields in both cases; the only intended difference is whether the applicability clause is shown. |
| BEST_OF_N_JUDGE | Best-of- control | Choose exactly one candidate and return CHOICE: <n> . Parse failures are logged and fall back to candidate 1; the verifier does not choose the candidate. |