Skip to main content
OpenBrowser 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, OpenBrowser 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 lets you see what the main task is, agent.add_new_task(...) lets you queue up a new one
  • agent.tools give 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.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 items
  • agent.settings contains all the configuration options passed to the Agent(...) at init time
  • agent.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.history gives access to historical data from the agent’s execution:
    • agent.history.model_thoughts(): Reasoning from the model.
    • agent.history.model_outputs(): Raw outputs from the 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 gives direct access to the BrowserSession and CDP interface
    • agent.browser_session.agent_focus: Get the current CDP session 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 things 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.