Customize agent behavior with lifecycle 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 all the actions for the current step, before it starts the next step |
async
callable function that accepts the agent
instance as its only parameter.
Agent
instance. Here are some useful data points you can access:
agent.task
lets you see what the main task is, agent.add_new_task(...)
lets you queue up a new oneagent.controller
give access to the Controller()
object and Registry()
containing the available actions
agent.controller.registry.execute_action('click_element_by_index', {'index': 123}, browser_session=agent.browser_session)
agent.context
lets you access any user-provided context object passed in to Agent(context=...)
agent.sensitive_data
contains the sensitive data dict, which can be updated in-place to add/remove/modify itemsagent.settings
contains all the configuration options passed to the Agent(...)
at init timeagent.llm
gives direct access to the main LLM object (e.g. ChatOpenAI
)agent.state
gives access to lots of internal state, including agent thoughts, outputs, actions, etc.
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_session
gives direct access to the BrowserSession()
and playwright objects
agent.browser_session.get_current_page()
: Get the current playwright Page
object the agent is focused onagent.browser_session.browser_context
: Get the current playwright BrowserContext
objectagent.browser_session.browser_context.pages
: Get all the tabs currently open in the contextagent.browser_session.get_page_html()
: Current page HTMLagent.browser_session.take_screenshot()
: Screenshot of the current pageapi.py
- The FastAPI server componentclient.py
- The Browser-Use agent with recording hookpython api.py
python client.py
recordings
directory. Here’s how you can use this data: