Code beginner · 3 min read

How to use Browser Use with Python

Direct answer
Use the browser_use package by creating an Agent with a task and a LangChain OpenAI chat model, then run it asynchronously with await agent.run() to automate browser tasks.

Setup

Install
bash
pip install browser-use langchain-openai
Env vars
OPENAI_API_KEY
Imports
python
from browser_use import Agent
from langchain_openai import ChatOpenAI
import asyncio
import os

Examples

inGo to google.com and search for 'AI news'
outPerformed search on google.com for 'AI news' and returned summarized results.
inOpen wikipedia.org and find the summary of 'Python programming language'
outNavigated to wikipedia.org and extracted the summary of 'Python programming language'.
inVisit example.com and take a screenshot
outVisited example.com and saved a screenshot of the homepage.

Integration steps

  1. Install the browser-use and langchain-openai packages and set the OPENAI_API_KEY environment variable.
  2. Import Agent from browser_use and ChatOpenAI from langchain_openai.
  3. Initialize the ChatOpenAI model with your OpenAI API key and desired model name.
  4. Create an Agent instance with the task description and the ChatOpenAI model.
  5. Run the agent asynchronously using asyncio.run() and capture the output.
  6. Print or process the agent's response which contains the browser automation result.

Full code

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

async def main():
    # Initialize the ChatOpenAI model with your API key
    llm = ChatOpenAI(model="gpt-4o", temperature=0.3)

    # Define the browser automation task
    task = "Go to google.com and search for 'AI news'"

    # Create the Agent with the task and LLM
    agent = Agent(task=task, llm=llm)

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

    # Print the result
    print("Agent output:", result)

if __name__ == "__main__":
    asyncio.run(main())
output
Agent output: Successfully searched 'AI news' on google.com and summarized the top results.

API trace

Request
json
{"task": "Go to google.com and search for 'AI news'", "llm": {"model": "gpt-4o", "temperature": 0.3}}
Response
json
{"result": "Successfully searched 'AI news' on google.com and summarized the top results."}
Extractresponse['result'] or the returned string from await agent.run()

Variants

Streaming output version

Use when you want to display partial results in real-time for long-running browser automation tasks.

python
from browser_use import Agent
from langchain_openai import ChatOpenAI
import asyncio

async def main():
    llm = ChatOpenAI(model="gpt-4o", temperature=0.3, streaming=True)
    task = "Go to google.com and search for 'AI news'"
    agent = Agent(task=task, llm=llm)

    async for chunk in agent.stream():
        print(chunk, end='', flush=True)

if __name__ == "__main__":
    asyncio.run(main())
Synchronous blocking version

Use when you want a simple synchronous script without async/await syntax.

python
from browser_use import Agent
from langchain_openai import ChatOpenAI
import asyncio

llm = ChatOpenAI(model="gpt-4o", temperature=0.3)
task = "Open wikipedia.org and find the summary of 'Python programming language'"
agent = Agent(task=task, llm=llm)

# Run synchronously by wrapping async call
result = asyncio.run(agent.run())
print("Agent output:", result)
Alternative model with Claude

Use when you prefer Anthropic Claude models instead of OpenAI for browser automation.

python
import os
import asyncio
from anthropic import Anthropic
from browser_use import Agent

class ClaudeLLM:
    def __init__(self, api_key):
        self.client = Anthropic(api_key=api_key)

    async def __call__(self, prompt):
        response = self.client.messages.create(
            model="claude-3-5-sonnet-20241022",
            system="You are a helpful assistant.",
            messages=[{"role": "user", "content": prompt}],
            max_tokens=1024
        )
        return response.content

async def main():
    llm = ClaudeLLM(api_key=os.environ["ANTHROPIC_API_KEY"])
    task = "Go to google.com and search for 'AI news'"
    agent = Agent(task=task, llm=llm)
    result = await agent.run()
    print("Agent output:", result)

if __name__ == "__main__":
    asyncio.run(main())

Performance

Latency~2-5 seconds depending on task complexity and browser interactions
Cost~$0.002 to $0.01 per call depending on tokens used by the underlying LLM
Rate limitsDepends on the underlying LLM provider (e.g., OpenAI Tier 1: 500 RPM / 30K TPM)
  • Keep task descriptions concise to reduce token usage.
  • Reuse browser sessions if supported to avoid repeated setup costs.
  • Limit the number of browser steps requested in a single task.
ApproachLatencyCost/callBest for
Basic async Agent.run()~2-5s~$0.002-$0.01Simple browser automation tasks
Streaming outputStarts immediately, total ~2-5s~$0.002-$0.01Long tasks with progressive output
Synchronous blocking~2-5s~$0.002-$0.01Scripts without async support

Quick tip

Always run the Browser Use Agent asynchronously with await agent.run() to ensure proper browser automation execution.

Common mistake

Beginners often forget to use asyncio.run() or await when calling agent.run(), causing the script to hang or not execute.

Verified 2026-04 · gpt-4o, claude-3-5-sonnet-20241022
Verify ↗