Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

Rerun example

Magnus Müllercfc85044c386
diff --git a/examples/features/rerun_history.py b/examples/features/rerun_history.py
new file mode 100644
index 000000000..dac51b4c1
--- /dev/null
+++ b/examples/features/rerun_history.py
@@ -0,0 +1,67 @@
+"""
+Example: Rerunning saved agent history
+
+This example shows how to:
+1. Run an agent and save its history (including initial URL navigation)
+2. Load and rerun the history with a new agent instance
+
+Useful for:
+- Debugging agent behavior
+- Testing changes with consistent scenarios
+- Replaying successful workflows
+
+Note: Initial actions (like opening URLs from tasks) are now automatically
+saved to history and will be replayed during rerun, so you don't need to
+worry about manually specifying URLs when rerunning.
+"""
+
+import asyncio
+from pathlib import Path
+
+from browser_use import Agent
+from browser_use.llm.openai.chat import ChatOpenAI
+
+
+async def main():
+ # Example task to demonstrate history saving and rerunning
+ task = 'Go to GitHub and find the browser-use repository'
+ history_file = Path('agent_history.json')
+ llm = ChatOpenAI(model='gpt-4.1-mini')
+
+ # Step 1: Run agent and save history
+ print('🚀 Running agent and saving history...')
+
+ agent = Agent(
+ task=task,
+ llm=llm,
+ )
+
+ # Run the agent
+ history = await agent.run(max_steps=5)
+
+ # Save the history for later rerun
+ agent.save_history(history_file)
+
+ print(f'✅ History saved to {history_file}')
+ print(f'📊 Completed {len(history.history)} steps')
+
+ # Step 2: Load and rerun the history
+ print('\n🔄 Loading and rerunning history...')
+
+ # Create new agent for rerunning (task can be empty since we're replaying)
+ rerun_agent = Agent(
+ task='',
+ llm=llm,
+ )
+
+ # Load and rerun the saved history
+ results = await rerun_agent.load_and_rerun(
+ history_file=history_file,
+ max_retries=3, # Retry failed actions up to 3 times
+ skip_failures=True, # Continue even if some actions fail
+ delay_between_actions=1.0, # Wait 1 second between actions
+ )
+
+
+if __name__ == '__main__':
+ asyncio.run(main())