Self-Evolving Agent with Reasoning Memory
Implementation of the ReasoningBank framework from the research paper "ReasoningBank: Scaling Agent Self-Evolving with Reasoning Memory" (Google Cloud AI Research + UIUC, September 2025).
ReasoningBank is a memory framework that enables AI agents to learn from both successful and failed experiences, extracting generalizable reasoning strategies that improve performance over time.
- Closed-Loop Learning: Complete cycle of Retrieve → Act → Judge → Extract → Consolidate
- Dual-Prompt Extraction: Learn from both successes (strategies) and failures (preventative lessons)
- Embedding-Based Retrieval: Semantic similarity search using OpenAI or Google embeddings
- Test-Time Scaling (MaTTS): Parallel and sequential scaling for improved performance
- ReAct Format: Reasoning + Acting loop for transparent agent execution
- Persistent Memory: JSON-based storage with import/export capabilities
| Component | Status | Notes |
|---|---|---|
| Core Agent | ✅ Complete | Full closed-loop implementation |
| Gap 21 (Streaming) | ✅ Validated | Test-time constraint enforcement |
| Gap 22 (Memory Growth) | Not Validated | |
| Gap 24 (Dual Learning) | ✅ Validated | Learn from successes AND failures |
| MaTTS Parallel | ✅ Complete | k-trajectory sampling |
| MaTTS Sequential | ✅ Complete | Progressive refinement |
┌─────────────────────────────────────────────────────────────────┐
│ ReasoningBank Agent │
│ │
│ Query ──► 1.RETRIEVE ──► 2.ACT ──► 3.JUDGE │
│ ▲ │ │
│ │ ▼ │
│ 5.CONSOLIDATE ◄── 4.EXTRACT ◄─┘ │
│ │ │
│ ▼ │
│ Memory Bank (JSON) │
└─────────────────────────────────────────────────────────────────┘
Closed-Loop Learning Cycle:
1. RETRIEVE: Find relevant past experiences using embeddings
2. ACT: Execute task with memory-augmented ReAct prompts
3. JUDGE: Determine success/failure (binary classification)
4. EXTRACT: Mine strategies (success) or lessons (failure)
5. CONSOLIDATE: Add to persistent memory bank
PARALLEL (Breadth): SEQUENTIAL (Depth):
Query Query
╱ │ ╲ │
A1 A2 A3 A1 ──► M1
╲ │ ╱ │
Best Result A2 ──► M1+M2
│
A3 ──► M1+M2+M3
📖 ARCHITECTURE.md - Comprehensive architecture documentation
- Python 3.9+ (recommended: 3.10 or 3.11)
- At least one LLM API key (Anthropic, Google, or OpenAI)
- 4GB+ RAM, 5GB+ disk space
# Clone the repository
git clone https://github.com/[YOUR-USERNAME]/ReasoningBank.git
cd ReasoningBank
# Create virtual environment
python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install package with dependencies
pip install -e . # Basic installation
# OR
pip install -e ".[test]" # Include testing tools
# OR
pip install -e ".[dev]" # Include testing + development tools
# Create environment configuration
cp .env.example .env
# Edit .env and add your API key(s) - see Configuration sectionCreate a .env file with your API keys:
# Minimum requirement: ONE of these API keys
ANTHROPIC_API_KEY=sk-ant-api03-your-key-here
# OR
GOOGLE_API_KEY=AIza-your-key-here
# OR
OPENAI_API_KEY=sk-your-key-here
# Optional: Configure provider (defaults to anthropic)
LLM_PROVIDER=anthropic # or google, openai
LLM_MODEL=claude-3-5-sonnet-20241022
# Optional: Memory storage location
MEMORY_BANK_PATH=./data/memory_bank.jsonPaper Replication (matches original research):
from reasoningbank import get_config_for_paper_replication
config = get_config_for_paper_replication() # Uses GeminiClaude Optimized (recommended):
from reasoningbank import get_config_for_claude
config = get_config_for_claude() # Uses Claude-3.5Custom Configuration:
from reasoningbank import ReasoningBankConfig
config = ReasoningBankConfig(
llm_provider="anthropic",
llm_model="claude-3-5-sonnet-20241022",
llm_api_key="your-key",
agent_temperature=0.7,
judge_temperature=0.0,
extractor_temperature=1.0,
extract_from_failures=True #Learn from failures too
)import os
from reasoningbank import ReasoningBankAgent, get_config_for_claude
# Set API key (or use .env file)
os.environ["ANTHROPIC_API_KEY"] = "your-key-here"
# Create agent
config = get_config_for_claude()
agent = ReasoningBankAgent(config)
# Execute task with learning
result = agent.run("What is 25 * 4 + 15?")
print(f"Success: {result.success}")
print(f"Output: {result.model_output}")
print(f"Memories extracted: {len(result.memory_items)}")
# Check memory bank
stats = agent.get_statistics()
print(f"Total memories: {stats['total_entries']}")
print(f"Success rate: {stats['success_rate']:.1%}")from reasoningbank import run_matts_parallel, get_config_for_matts_parallel
config = get_config_for_matts_parallel(k=3)
result = run_matts_parallel(
query="Design an efficient algorithm to find the median of two sorted arrays.",
config=config,
k=3 # Sample 3 trajectories in parallel
)
print(f"Best trajectory: {result.best_trajectory.success}")
print(f"Aggregated memories: {len(result.aggregated_memories)}")from reasoningbank import run_matts_sequential, get_config_for_matts_sequential
config = get_config_for_matts_sequential(k=3)
result = run_matts_sequential(
query="Write a function to reverse a linked list in-place.",
config=config,
k=3 # 3 refinement iterations
)
print(f"Refinements: {len(result.all_trajectories)}")
print(f"Final success: {result.best_trajectory.success}")# Run all tests (uses mocked LLM responses, no API costs)
python tests/run_all_tests.py
# Run specific test categories
python -m pytest tests/unit/ # Unit tests only
python -m pytest tests/integration/ # Integration tests
python -m pytest tests/e2e/ # End-to-end tests
# Run with coverage report
python -m pytest --cov=reasoningbank --cov-report=html
# Open coverage report
open htmlcov/index.html # Mac/Linux
# OR
start htmlcov/index.html # WindowsThese tests validate key paper claims:
# Gap 21: Streaming Constraint (no future information leakage)
python -m pytest tests/e2e/test_streaming_constraint.py -v
# Gap 22: Memory Growth (WARNING: costs ~$0.60 for quick test)
python -m pytest tests/stress/test_memory_growth_long_term.py::TestMemoryGrowthLongTerm::test_memory_bank_grows_linearly_quick
# Gap 24: Success+Failure Learning (core innovation)
python -m pytest tests/ablation/test_success_and_failure_extraction.py -vSome tests make real API calls and cost money:
- Gap 22 quick test: ~$0.60
- Gap 22 full test: ~$6-8
- Gap 22 stress test: ~$20-25
Always run quick validation first!
ReasoningBank/
├── reasoningbank/ # Main package
│ ├── __init__.py # Package exports
│ ├── agent.py # Core ReasoningBankAgent
│ ├── config.py # Configuration management
│ ├── models.py # Data models
│ ├── judge.py # Success/failure evaluation
│ ├── extractor.py # Memory extraction (dual-prompt)
│ ├── retriever.py # Embedding-based retrieval
│ ├── retriever_optimized.py # Performance optimizations
│ ├── consolidator.py # Memory bank management
│ └── matts/ # Test-time scaling
│ ├── parallel.py # Parallel sampling (breadth)
│ └── sequential.py # Sequential refinement (depth)
│
├── tests/ # Comprehensive test suite
│ ├── unit/ # Component tests
│ ├── integration/ # Component interaction tests
│ ├── e2e/ # End-to-end validation
│ ├── ablation/ # Core innovation tests
│ ├── stress/ # Performance and scale tests
│ └── fixtures/ # Test data and mocks
│
├── examples/ # Usage examples
│ ├── basic_usage.py # Simple agent usage
│ ├── matts_parallel_example.py # Parallel scaling demo
│ └── matts_sequential_example.py # Sequential refinement demo
│
├── data/ # Runtime data (git-ignored)
│ ├── memory_bank.json # Persistent memory storage
│ └── embeddings.json # Cached embeddings
│
└── docs/ # Documentation
└── archive/ # Development docs
Import Error: No module named 'reasoningbank'
# Solution: Install in development mode
pip install -e .API Key Not Found
# Solution: Create .env file
cp .env.example .env
# Edit .env and add your API keyRate Limit Errors
# Solution: Use optimized retriever with retry logic
from reasoningbank.retriever_optimized import OptimizedMemoryRetrieverMemory Growth Concerns
- The system uses "simple addition" (no deduplication) by design
- This is intentional per the paper's approach
- For production with >1000 memories, consider vector database
- Enable Embedding Cache: Reduces API calls by 100x
- Use Batch Operations: Process multiple queries together
- Monitor Memory Size: Check stats regularly
- Use MaTTS Wisely: Parallel for exploration, Sequential for refinement
📖 Core Documentation:
- ARCHITECTURE.md - System architecture and flow diagrams
- INSTALL.md - Detailed installation guide
- README_TESTING.md - Comprehensive testing guide
- REQUIREMENTS_ANALYSIS.md - Requirements deep-dive
📊 Implementation Reports:
- IMPLEMENTATION_PROGRESS.md - Test results
# Agent
agent = ReasoningBankAgent(config, environment=None)
result = agent.run(query, max_steps=30, enable_memory_injection=True)
# Memory Operations
success = judge_trajectory_success(query, trajectory, final_state, output)
memories = extract_memories(query, trajectory, final_state, output, success)
relevant = retrieve_memories(query, memory_bank, k=1)
# MaTTS
result = run_matts_parallel(query, config, k=3)
result = run_matts_sequential(query, config, k=3)- WebArena: 17% relative improvement (22.1% → 25.8%)
- Mind2Web: 15% relative improvement (31.5% → 36.1%)
- SWE-Bench: 43% relative improvement (7.0% → 10.0%)
- Parallel (k=3): Up to 30% additional improvement
- Sequential (k=3): Up to 50% improvement on complex tasks
- Memory Retrieval: <5s for 1000+ memories
- Embedding Cache: 100x reduction in API calls
- Memory Growth: Linear (by design, no deduplication)
Contributions welcome! Please ensure:
- All tests pass:
python tests/run_all_tests.py - Coverage maintained:
python -m pytest --cov=reasoningbank - Code formatted:
black reasoningbank/ - Type hints included
- Docstrings for public APIs
MIT License - See LICENSE file
@article{reasoningbank2025,
title={ReasoningBank: Scaling Agent Self-Evolving with Reasoning Memory},
author={Google Cloud AI Research and UIUC},
year={2025},
month={September}
}Based on the research paper by Google Cloud AI Research and UIUC. This implementation faithfully reproduces the paper's methodology while providing a production-ready codebase.