Browserbase Tools
Navigate quotes.toscrape.com and extract paginated quotes and authors with BrowserbaseTools.
"""
Browserbase Tools
=============================
Demonstrates browserbase tools.
"""
from agno.agent import Agent
from agno.tools.browserbase import BrowserbaseTools
# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------
# Browserbase Configuration
# -------------------------------
# These environment variables are required for the BrowserbaseTools to function properly.
# You can set them in your .env file or export them directly in your terminal.
# BROWSERBASE_API_KEY: Your API key from Browserbase dashboard
# - Required for authentication
# - Format: Starts with "bb_live_" or "bb_test_" followed by a unique string
# BROWSERBASE_PROJECT_ID: The project ID from your Browserbase dashboard
# - Required to identify which project to use for browser sessions
# - Format: UUID string (8-4-4-4-12 format)
# BROWSERBASE_BASE_URL: The Browserbase API endpoint
# - Optional: Defaults to https://api.browserbase.com if not specified
# - Only change this if you're using a custom API endpoint or proxy
# ==================== Usage ====================
# BrowserbaseTools automatically uses the correct implementation based on context:
# - Sync tools when using agent.run() or agent.print_response()
# - Async tools when using agent.arun() or agent.aprint_response()
agent = Agent(
name="Web Automation Assistant",
tools=[BrowserbaseTools()],
instructions=[
"You are a web automation assistant that can help with:",
"1. Capturing screenshots of websites",
"2. Extracting content from web pages",
"3. Monitoring website changes",
"4. Taking visual snapshots of responsive layouts",
"5. Automated web testing and verification",
],
markdown=True,
)
# ==================== Sync Usage ====================
# Use this for regular scripts and synchronous execution
# Content Extraction and SS
# agent.print_response("""
# Go to https://news.ycombinator.com and extract:
# 1. The page title
# 2. Take a screenshot of the top stories section
# """)
# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------
if __name__ == "__main__":
agent.print_response("""
Visit https://quotes.toscrape.com and:
1. Extract the first 5 quotes and their authors
2. Navigate to page 2
3. Extract the first 5 quotes from page 2
""")
# ==================== Async Usage ====================
# Use this for FastAPI, async frameworks, or when using agent.arun()
# The same agent instance works for both sync and async - just use arun/aprint_response!
# import asyncio
#
#
# async def main():
# # Same agent, just use async methods - it will automatically use async tools
# await agent.aprint_response("""
# Visit https://quotes.toscrape.com and:
# 1. Extract the first 5 quotes and their authors
# 2. Navigate to page 2
# 3. Extract the first 5 quotes from page 2
# """)
#
#
# if __name__ == "__main__":
# asyncio.run(main())Close the browser connection
Keep a reference to the toolkit (browser_tools = BrowserbaseTools(), then tools=[browser_tools]) and put the agent call in try/finally, calling browser_tools.close_session() in the finally block. For an async run, await browser_tools.aclose_session() instead. This disconnects local Playwright resources and clears the toolkit state; it does not send an explicit Browserbase session-release request. Remote lifetime still follows the provider's session policy.
Export the variables in the setup below. This script does not load .env itself. Playwright connects to the hosted browser over CDP; it does not launch a local browser.
Run the Example
Set up your virtual environment
uv venv --python 3.12
source .venv/bin/activateInstall dependencies
uv pip install -U agno browserbase openai playwrightExport environment variables
export BROWSERBASE_API_KEY="your_browserbase_api_key_here"
export BROWSERBASE_PROJECT_ID="your_browserbase_project_id_here"
export OPENAI_API_KEY="your_openai_api_key_here"Run the example
Save the code above as browserbase_tools.py, then run:
python browserbase_tools.pyFull source: cookbook/91_tools/browserbase_tools.py