How to Intermediate · 3 min read

Browser Use with LangGraph

Quick answer
Use the browser_use package to create an asynchronous Agent that performs browser automation tasks, and combine it with LangGraph by invoking the agent within a graph node function. This enables stateful, graph-based AI workflows that include web browsing capabilities.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install openai>=1.0
  • pip install browser-use langgraph
  • Playwright with Chromium installed (run: playwright install chromium)

Setup

Install the required packages and set your OpenAI API key in the environment. Also, install Playwright's Chromium browser for browser_use to function.

  • Install Python packages:
bash
pip install openai browser-use langgraph
playwright install chromium
output
Requirement already satisfied: openai ...
Requirement already satisfied: browser-use ...
Requirement already satisfied: langgraph ...
[Playwright] Downloading Chromium...
Chromium downloaded successfully.

Step by step

This example shows how to create a browser_use.Agent to perform a Google search, then integrate it into a LangGraph node for stateful invocation.

python
import os
import asyncio
from browser_use import Agent as BrowserAgent
from langgraph.graph import StateGraph, END
from typing import TypedDict

# Set your OpenAI API key in environment variable OPENAI_API_KEY

class State(TypedDict):
    messages: list

async def browser_node(state: State) -> State:
    agent = BrowserAgent(
        task="Go to google.com and search for 'AI news'",
        llm=None  # Uses default OpenAI client with env var key
    )
    result = await agent.run()
    new_messages = state["messages"] + [result]
    return {"messages": new_messages}

# Create LangGraph
graph = StateGraph(State)
graph.add_node("browser_node", browser_node)
graph.set_entry_point("browser_node")
graph.add_edge("browser_node", END)

# Compile graph to callable app
app = graph.compile()

# Run the graph
async def main():
    output = await app.invoke({"messages": []})
    print("Browser Use output:", output["messages"])

if __name__ == "__main__":
    asyncio.run(main())
output
Browser Use output: ['Browsing result: ... snippet of AI news search results ...']

Common variations

You can customize the BrowserAgent with different tasks or use other LLMs by passing a llm parameter. LangGraph supports synchronous or asynchronous nodes; here we use async for browser automation. You can also chain multiple nodes for complex workflows.

Troubleshooting

  • If you get errors about missing browsers, run playwright install chromium.
  • If OPENAI_API_KEY is not set, the BrowserAgent will fail to authenticate.
  • Ensure Python 3.8+ is used to support async/await syntax.

Key Takeaways

  • Use browser_use.Agent for asynchronous browser automation tasks in Python.
  • Integrate browser_use with LangGraph by invoking the agent inside graph node functions.
  • Always set OPENAI_API_KEY in your environment for authentication.
  • Install Playwright browsers with playwright install chromium to avoid runtime errors.
  • LangGraph enables stateful, graph-based AI workflows combining browsing and other AI capabilities.
Verified 2026-04 · gpt-4o, gpt-4o-mini
Verify ↗