Skip to content

Memory System

Browser-Use includes a powerful memory system that enables agents to maintain context across multiple browser actions and pages. This is crucial for complex multi-step tasks that require information to be remembered and used later in the workflow.

Why Memory Matters

When interacting with websites, many tasks require the agent to remember information from previous steps:

  • Comparing product details across multiple pages
  • Filling out forms with previously gathered information
  • Following multi-step processes that span several pages
  • Tracking progress in complex workflows
  • Building up knowledge incrementally during research tasks

Without memory, the agent would be limited to what's visible on the current page, making complex tasks impossible.

How Memory Works

Browser-Use's memory system functions as a structured storage mechanism that:

  1. Captures Information: Stores important data from web pages automatically or explicitly
  2. Organizes Knowledge: Structures information by topic, page, or custom categories
  3. Retrieves Context: Makes relevant information available when needed
  4. Maintains State: Keeps track of the agent's progress through multi-step tasks

Memory Architecture

The memory system consists of several components:

1. Short-Term Memory

Short-term memory maintains information for the duration of the current task. It automatically records:

  • Recent pages visited
  • Actions performed
  • Key information extracted
  • Intermediate results

This memory is ephemeral and is cleared when the agent finishes its task.

2. Long-Term Memory (Optional)

For more complex tasks or recurring agents, long-term memory can persist information across multiple sessions:

  • User preferences
  • Previously learned information
  • Historical interactions
  • Frequently accessed data

3. Memory Types

The memory system supports different types of storage:

  • Key-Value Store: Simple associations between keys and values
  • Page Memory: Information associated with specific URLs
  • Structured Data: Tables, lists, and other structured formats
  • Vector Store: Semantic storage for similarity-based retrieval

Using Memory in Your Agents

Basic Memory Usage

To enable memory for an agent, simply pass a Memory instance when creating the agent:

python
from browser_use import Agent, Memory

agent = Agent(
    task="Research apartment listings in San Francisco and compare features and prices of the top 5 options",
    llm=llm,
    memory=Memory()  # Enable default memory
)

result = await agent.run()

Explicitly Storing Information

While many things are remembered automatically, you can explicitly store important information in memory:

python
import asyncio
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from browser_use import Agent, Memory, BrowserAction

load_dotenv()

async def compare_products():
    llm = ChatOpenAI(model="gpt-4o", temperature=0)
    memory = Memory()  # Create memory instance
    
    # Create agent with memory
    agent = Agent(
        task="""Compare prices of iPhone 15 Pro across three different online retailers. 
            For each store, save the price, shipping cost, and estimated delivery time.""",
        llm=llm,
        memory=memory,
        actions=[BrowserAction.REMEMBER]  # Enable explicit memory actions
    )
    
    result = await agent.run()
    print(result)
    
    # Access memory contents after the task
    stored_data = memory.get_all()
    print("\nMemory contents:")
    for key, value in stored_data.items():
        print(f"{key}: {value}")

if __name__ == "__main__":
    asyncio.run(compare_products())

Memory Configuration

You can configure memory behavior to suit your needs:

python
from browser_use import Agent, Memory, MemoryConfig

# Create a custom memory configuration
memory_config = MemoryConfig(
    automatic_storage=True,       # Automatically store important information
    max_items=100,                # Maximum items to store
    page_content_storage=True,    # Store content from visited pages
    expiration=3600,              # Memory expiration time in seconds (1 hour)
    vector_storage=True           # Enable semantic search capability
)

# Create memory with custom configuration
memory = Memory(config=memory_config)

agent = Agent(
    task="Research the history of artificial intelligence and create a timeline of major developments",
    llm=llm,
    memory=memory
)

Using Memory from Previous Sessions

For tasks that span multiple sessions, you can persist and reload memory:

python
import pickle
from browser_use import Agent, Memory

# Save memory to a file
default_memory_path = "agent_memory.pkl"

async def save_memory_demo():
    # First session: Gather information
    memory = Memory()
    agent = Agent(
        task="Find and collect information about the top 3 national parks in the US",
        llm=llm,
        memory=memory
    )
    
    await agent.run()
    
    # Save memory to file
    with open(default_memory_path, "wb") as f:
        pickle.dump(memory, f)
    print("Memory saved to file")

async def load_memory_demo():
    # Load memory from file
    with open(default_memory_path, "rb") as f:
        loaded_memory = pickle.load(f)
    
    # Second session: Use the collected information
    agent = Agent(
        task="Using the information about national parks, create a 3-day itinerary for each park",
        llm=llm,
        memory=loaded_memory
    )
    
    result = await agent.run()
    print(result)

Advanced Memory Features

Memory Reflection

For complex tasks, the agent can reflect on its memory to extract insights or summarize information:

python
from browser_use import Agent, Memory, BrowserAction

agent = Agent(
    task="Research the impact of climate change on agriculture across different regions",
    llm=llm,
    memory=Memory(),
    actions=[BrowserAction.REFLECT]  # Enable memory reflection
)

result = await agent.run()

With reflection enabled, the agent periodically examines its memory to:

  • Identify patterns and connections
  • Summarize key findings
  • Plan additional research based on gaps
  • Organize information more effectively

Memory Visualization

You can visualize the contents of memory to understand what the agent knows:

python
from browser_use import Agent, Memory, memory_utils

memory = Memory()
agent = Agent(
    task="Research electric vehicle market trends",
    llm=llm,
    memory=memory
)

result = await agent.run()

# Generate a visualization of memory contents
memory_utils.visualize(memory, format="graph")  # Options: "graph", "table", "tree"

Best Practices

When to Use Memory

  • Enable memory for tasks that:

    • Span multiple pages or websites
    • Require comparing information
    • Have multi-step workflows
    • Need to track progress
  • Consider disabling memory for:

    • Simple, single-page tasks
    • Privacy-sensitive operations
    • Reducing computational overhead

Memory Optimization

  • Be selective: Configure memory to store only relevant information
  • Use expiration: Set appropriate expiration times for transient data
  • Segment memory: Use different memory instances for unrelated tasks
  • Clean up: Periodically clear memory that's no longer needed

Memory Limitations

  • Memory increases token usage and can impact LLM costs
  • Very large memory collections may slow down processing
  • Some browser states cannot be fully captured in memory
  • Memory persistence requires additional storage management

Next Steps

Now that you understand Browser-Use's memory system:

  • Explore Browser Actions that can interact with memory
  • Learn about Vision Support for remembering visual elements
  • See Complex Examples that leverage memory effectively