How to beginner · 4 min read

How to use Browser Use with LangChain

Quick answer
Use the browser-use package with LangChain by creating a Agent instance and passing a LangChain ChatOpenAI model as the LLM. Then call agent.run() asynchronously to automate browser tasks with AI-driven instructions.

PREREQUISITES

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

Setup

Install the required packages browser-use and langchain_openai via pip. Ensure you have your OpenAI API key set in the environment variable OPENAI_API_KEY. Also, install Playwright and the Chromium browser for browser automation.

bash
pip install browser-use langchain_openai openai
playwright install chromium
output
Collecting browser-use
Collecting langchain_openai
Collecting openai
Installing collected packages: ...
Successfully installed browser-use langchain_openai openai
[Playwright] Chromium browser installed successfully

Step by step

This example shows how to create a Agent with a LangChain ChatOpenAI model, then run a simple browsing task asynchronously.

python
import os
import asyncio
from browser_use import Agent
from langchain_openai import ChatOpenAI

async def main():
    # Initialize LangChain ChatOpenAI with OpenAI API key and model
    llm = ChatOpenAI(model="gpt-4o", temperature=0, openai_api_key=os.environ["OPENAI_API_KEY"])

    # Create Browser Use agent with the LangChain LLM
    agent = Agent(task="Go to google.com and search for 'AI news'", llm=llm)

    # Run the agent asynchronously
    result = await agent.run()

    print("Agent output:", result)

if __name__ == "__main__":
    asyncio.run(main())
output
Agent output: Here is a summary of the latest AI news from Google search results: ...

Common variations

  • Use different LangChain models like gpt-4o-mini or adjust temperature for creativity.
  • Run the agent synchronously by wrapping asyncio.run(agent.run()) in a sync function.
  • Stream output by integrating LangChain streaming features if supported.
python
import os
from browser_use import Agent
from langchain_openai import ChatOpenAI

# Synchronous wrapper example
def run_agent_sync():
    import asyncio
    llm = ChatOpenAI(model="gpt-4o-mini", temperature=0.7, openai_api_key=os.environ["OPENAI_API_KEY"])
    agent = Agent(task="Search Bing for 'latest AI breakthroughs'", llm=llm)
    result = asyncio.run(agent.run())
    print("Sync agent output:", result)

if __name__ == "__main__":
    run_agent_sync()
output
Sync agent output: The latest AI breakthroughs include advancements in large language models, multimodal AI, and reinforcement learning techniques...

Troubleshooting

  • If you see playwright not installed errors, run playwright install chromium to install the browser.
  • Ensure your OPENAI_API_KEY environment variable is set correctly to avoid authentication errors.
  • For network issues, verify your internet connection and proxy settings if applicable.

Key Takeaways

  • Use browser-use with LangChain by passing a ChatOpenAI instance as the LLM.
  • Run browser automation tasks asynchronously with agent.run() for best results.
  • Install Playwright and Chromium to enable browser control.
  • Adjust LangChain model parameters to customize AI behavior in browsing tasks.
Verified 2026-04 · gpt-4o, gpt-4o-mini
Verify ↗