1

Get your API Key

Visit the Dashboard’s Overview tab:

Then copy and set the BROWSERBASE_API_KEY and BROWSERBASE_PROJECT_ID environment variables in your .env file.

2

Install and create a virtual environment with UV

UV is a modern package manager for Python.

uv venv
3

Install Browserbase and Browser Use

source .venv/bin/activate # follow the `uv venv` output
uv pip install browserbase browser-use langchain-anthropic python-dotenv
4

Create the Browser Session

We’ll create a browser session using Browserbase’s CDP connection with proper configuration for remote browser stability.

main.py
from dotenv import load_dotenv
import os
import asyncio
from browserbase import Browserbase
from browser_use import Agent
from browser_use.browser.session import BrowserSession
from browser_use.browser import BrowserProfile
from langchain_anthropic import ChatAnthropic

async def main():
    load_dotenv()
    
    # Create Browserbase session
    bb = Browserbase(api_key=os.environ["BROWSERBASE_API_KEY"])

    session = bb.sessions.create(project_id=os.environ["BROWSERBASE_PROJECT_ID"])
    debug_url = f"https://www.browserbase.com/sessions/{session.id}"
    print("Debug URL:", debug_url)
5

Configure Browser Profile

Set up the browser profile with appropriate timeouts and settings for remote browser usage:

main.py
    # Configure browser profile for Browserbase
    browser_profile = BrowserProfile(
        keep_alive=True,  # Don't close browser automatically
        wait_between_actions=2.0,  # Add delay between actions
        # Increase timeouts to handle remote browser latency
        default_timeout=45000,
        default_navigation_timeout=45000,
    )

    # Create browser session with Browserbase CDP URL
    browser_session = BrowserSession(
        cdp_url=session.connect_url,
        browser_profile=browser_profile,
        keep_alive=True,
        initialized=False,  # Let it initialize properly
    )
    
    # Pre-initialize the browser session
    try:
        await browser_session.start()
        print("Browser session initialized successfully")
    except Exception as e:
        print(f"Failed to initialize browser session: {e}")
        return
6

Create and Run the Agent

Set up the LLM and create the browser automation agent:

main.py
    # Set up LLM
    llm = ChatAnthropic(model="claude-3-5-sonnet-20240620", temperature=0.0)

    # Create agent with browser session
    agent = Agent(
        task="Go to https://www.macrumors.com/contact.php and fill in the form. Make sure to use the selectors and submit the form",
        llm=llm,
        browser_session=browser_session,  # Pass browser_session directly
        enable_memory=False,
        max_failures=5,
        retry_delay=5,
        max_actions_per_step=1,  # Reduce actions per step for stability
    )
    
    try:
        result = await agent.run(max_steps=20)
        print(result)
    finally:
        try:
            # Properly close the browser session
            if browser_session.initialized:
                await browser_session.stop()
        except Exception as e:
            print(f"Error closing browser session: {e}")

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

Run your script

Run your script:

uv run main.py

You should see your Browserbase session start in Browserbase. The debug URL will be printed to console for real-time session monitoring.

Important Environment Variables

Make sure you have these environment variables in your .env file:

  • BROWSERBASE_API_KEY
  • BROWSERBASE_PROJECT_ID
  • ANTHROPIC_API_KEY

Key Configuration Notes

  • Use BrowserSession directly instead of custom context classes
  • Set max_actions_per_step=1 for better stability with remote browsers
  • Use the debug URL to monitor your session in real-time
  • Increase timeouts to handle remote browser latency

Common Issues & Fixes

  • If actions timeout, increase default_timeout and default_navigation_timeout
  • If browser context closes unexpectedly, ensure keep_alive=True in both BrowserProfile and BrowserSession
  • For troubleshooting, check the debug URL to see what’s happening in the browser
  • Reach out to us at support@browserbase.com