Lifecycle Hooks
Customize agent behavior with lifecycle hooks
Using Agent Lifecycle Hooks
Browser-Use provides lifecycle hooks that allow you to execute custom code at specific points during the agent’s execution. These hooks enable you to capture detailed information about the agent’s actions, modify behavior, or integrate with external systems.
Available Hooks
Currently, Browser-Use provides the following hooks:
Hook | Description | When it’s called |
---|---|---|
on_step_start | Executed at the beginning of each agent step | Before the agent processes the current state and decides on the next action |
on_step_end | Executed at the end of each agent step | After the agent has executed the action for the current step |
Using Hooks
Hooks are passed as parameters to the agent.run()
method. Each hook should be a callable function that accepts the agent instance as its parameter.
Basic Example
Complete Example: Agent Activity Recording System
This comprehensive example demonstrates a complete implementation for recording and saving Browser-Use agent activity, consisting of both server and client components.
Setup Instructions
To use this example, you’ll need to:
-
Set up the required dependencies:
-
Create two separate Python files:
api.py
- The FastAPI server componentclient.py
- The Browser-Use agent with recording hook
-
Run both components:
- Start the API server first:
python api.py
- Then run the client:
python client.py
- Start the API server first:
Server Component (api.py)
The server component handles receiving and storing the agent’s activity data:
Client Component (client.py)
The client component runs the Browser-Use agent with a recording hook:
Working with the Recorded Data
After running the agent, you’ll find the recorded data in the recordings
directory. Here’s how you can use this data:
- View recorded sessions: Each JSON file contains a snapshot of agent activity for one step
- Extract screenshots: You can modify the API to save screenshots separately
- Analyze agent behavior: Use the recorded data to study how the agent navigates websites
Extending the Example
You can extend this recording system in several ways:
- Save screenshots separately: Uncomment the screenshot saving code in the API
- Add a web dashboard: Create a simple web interface to view recorded sessions
- Add session IDs: Modify the API to group steps by agent session
- Add filtering: Implement filters to record only specific types of actions
Data Available in Hooks
When working with agent hooks, you have access to the entire agent instance. Here are some useful data points you can access:
agent.state.history.model_thoughts()
: Reasoning from Browser Use’s model.agent.state.history.model_outputs()
: Raw outputs from the Browsre Use’s model.agent.state.history.model_actions()
: Actions taken by the agentagent.state.history.extracted_content()
: Content extracted from web pagesagent.state.history.urls()
: URLs visited by the agentagent.browser_context.get_page_html()
: Current page HTMLagent.browser_context.take_screenshot()
: Screenshot of the current page
Tips for Using Hooks
- Avoid blocking operations: Since hooks run in the same execution thread as the agent, try to keep them efficient or use asynchronous patterns.
- Handle exceptions: Make sure your hook functions handle exceptions gracefully to prevent interrupting the agent’s main flow.
- Consider storage needs: When capturing full HTML and screenshots, be mindful of storage requirements.
Contribution by Carlos A. Planchón.
Was this page helpful?