Agent Browser with Python SDK & CLI Workflows
In this article
This guide shows you how to drive the Agent Browser from Python using its hosted SDK and from the command line, so you can automate browsing tasks, extract data, and run multi-step workflows without a manual browser session. You'll learn the core setup, a basic Python script, a CLI invocation, and how to handle common failures.
What You'll Need
- Python 3.9 or later installed on your machine.
- An API key from the Agent Browser platform (sign up at the provider's dashboard).
- Access to a terminal (Command Prompt, PowerShell, or bash).
- Basic familiarity with Python and JSON.
Step-by-Step
- Install the Agent Browser SDK
Open your terminal and run:
pip install agent-browser-sdk
This installs the Python package and theagent-browserCLI tool. - Set your API key
Export it as an environment variable to keep it out of your code:
export AGENT_BROWSER_API_KEY="your_key_here"
On Windows, useset AGENT_BROWSER_API_KEY=your_key_herein Command Prompt or$env:AGENT_BROWSER_API_KEY="your_key_here"in PowerShell. - Create a Python script
Make a file namedworkflow.pywith the following minimal example:
from agent_browser import AgentBrowser client = AgentBrowser() result = client.run( task="Go to example.com and extract the main heading text", timeout=30 ) print(result.output) - Run the Python script
Execute it with:
python workflow.py
The SDK sends the task to the hosted Agent Browser, which performs the browsing and returns the result. - Use the CLI for one-off tasks
Instead of writing a script, you can run:
agent-browser run "Go to example.com and extract the main heading"
The CLI prints the result to stdout. Add--jsonto get structured output. - Handle multi-step workflows in Python
For more complex tasks, chain actions using thestepmethod:
client = AgentBrowser() client.step("Go to example.com") client.step("Click the 'More information' link") client.step("Extract the page title") result = client.execute() print(result.output) - Pass parameters via CLI
Use--paramto inject variables:
agent-browser run "Search for {query} on example.com" --param query="python" - Save output to a file
Redirect the CLI output:
agent-browser run "Extract all links from example.com" --json > links.json
Common Mistakes
- Hardcoding the API key in your script – it gets exposed if you share code. Always use environment variables.
- Forgetting to install the SDK – you'll get a
ModuleNotFoundError. - Using a timeout that's too short – complex pages may need more than 10 seconds. Start with 30–60 seconds.
- Assuming the browser is local – the SDK is hosted; you don't need a browser on your machine.
- Ignoring the result schema – always check
result.outputandresult.statusto handle errors gracefully.
Tips
- Start with a simple task to verify your API key works before building complex workflows.
- Use
--verboseon the CLI to see detailed logs, which help debug failures. - For long-running tasks, set a generous timeout and consider adding a retry loop in your code.
- Store your API key in a
.envfile and load it withpython-dotenvfor cleaner project setup. - When extracting data, ask for structured JSON output (e.g., "return a JSON list of product names and prices") to make parsing easier.
Troubleshooting
- Error:
ModuleNotFoundError
Reinstall the SDK:pip install --upgrade agent-browser-sdk. Check that you're using the correct Python environment (e.g., a virtualenv). - Error:
Authentication failed
Verify your API key is set correctly and hasn't expired. Re-export the environment variable and try again. - Task times out
Increase the timeout parameter (e.g.,timeout=120). If it still times out, break the task into smaller steps. - CLI command not found
After installing, the CLI may not be on your PATH. Runpython -m agent_browser.cliinstead, or reinstall withpip install --force-reinstall. - Unexpected output format
Use--jsonon the CLI or setresponse_format="json"in the Python SDK to get structured data. - Rate limiting errors
Wait a few seconds between requests. If you're making many calls, implement exponential backoff in your code.
Frequently Asked Questions
Do I need to install a browser locally?
No. The Agent Browser runs in the cloud. You only need the SDK/CLI to send tasks and receive results.
Can I use the CLI in a CI/CD pipeline?
Yes. The CLI works in any shell environment. Set the API key as a CI secret and call the command normally.
How do I get the extracted data as a file?
Use the CLI with --json and redirect to a file, or in Python write result.output to a file using standard file I/O.
What if a step fails in a multi-step workflow?
Check result.status – it will indicate failure. You can implement retry logic or use the on_error parameter if available in your SDK version.
Is there a free tier?
Check the provider's pricing page. Most hosted services offer a limited free tier for testing, but this varies.
Can I run multiple tasks in parallel?
Yes, you can create multiple client instances or use async methods if supported by the SDK. Be mindful of rate limits.