User Agents

Web browsers and everyday web tech, explained

Agent Browser with Python SDK & CLI Workflows

· 4 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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

  1. Install the Agent Browser SDK
    Open your terminal and run:
    pip install agent-browser-sdk
    This installs the Python package and the agent-browser CLI tool.
  2. 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, use set AGENT_BROWSER_API_KEY=your_key_here in Command Prompt or $env:AGENT_BROWSER_API_KEY="your_key_here" in PowerShell.
  3. Create a Python script
    Make a file named workflow.py with 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)
      
  4. 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.
  5. 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 --json to get structured output.
  6. Handle multi-step workflows in Python
    For more complex tasks, chain actions using the step method:
    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)
      
  7. Pass parameters via CLI
    Use --param to inject variables:
    agent-browser run "Search for {query} on example.com" --param query="python"
  8. 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.output and result.status to handle errors gracefully.

Tips

  • Start with a simple task to verify your API key works before building complex workflows.
  • Use --verbose on 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 .env file and load it with python-dotenv for 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. Run python -m agent_browser.cli instead, or reinstall with pip install --force-reinstall.
  • Unexpected output format
    Use --json on the CLI or set response_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.