Skip to main content
Browser-Use provides lifecycle hooks that allow you to execute custom code at specific points during the agent’s execution. Hook functions can be used to read and modify agent state while running, implement custom logic, change configuration, integrate the Agent with external applications.

Available Hooks

Currently, Browser-Use provides the following hooks:
Each hook should be an async callable function that accepts the agent instance as its only parameter.

Basic Example

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.task provides the main task, agent.add_new_task(...) allows you to queue up a new one
  • agent.tools gives access to the Tools() object and Registry() containing the available actions
    • agent.tools.registry.execute_action('click', {'index': 123}, browser_session=agent.browser_session)
  • agent.sensitive_data contains the sensitive data dict, which can be updated in-place to add/remove/modify items
  • agent.settings contains all the configuration options passed to the Agent(...) at init time
  • agent.llm provides direct access to the main LLM object (e.g. ChatOpenAI)
  • agent.state provides access to internal state, including agent thoughts, outputs, actions, and more
  • agent.history gives access to historical data from the agent’s execution:
    • agent.history.model_thoughts(): Reasoning from Browser Use’s model.
    • agent.history.model_outputs(): Raw outputs from the Browser Use’s model.
    • agent.history.model_actions(): Actions taken by the agent
    • agent.history.extracted_content(): Content extracted from web pages
    • agent.history.urls(): URLs visited by the agent
  • agent.browser_session provides direct access to the BrowserSession and CDP interface
    • agent.browser_session.agent_focus_target_id: Get the current target ID the agent is focused on
    • agent.browser_session.get_or_create_cdp_session(): Get the current CDP session for browser interaction
    • agent.browser_session.get_tabs(): Get all tabs currently open
    • agent.browser_session.get_current_page_url(): Get the URL of the current active tab
    • agent.browser_session.get_current_page_title(): Get the title of the current active tab

Tips for Using Hooks

  • Avoid blocking operations: Since hooks run in the same execution thread as the agent, keep them efficient and avoid blocking operations.
  • Use custom tools instead: Hooks are fairly advanced. Most use cases can be implemented with custom tools instead.
  • Increase step_timeout: If your hook is doing something that takes a long time, you can increase the step_timeout parameter in the Agent(...) constructor.