TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/browser-use/browser-use/llms.txt
Use this file to discover all available pages before exploring further.
CodeAgent provides a Jupyter notebook-like interface where the LLM writes Python code that gets executed in a persistent namespace with browser control functions available.
Overview
Unlike the standardAgent which uses predefined actions, CodeAgent gives the LLM the ability to write and execute Python code directly. This provides more flexibility for complex automation tasks that require custom logic, data processing, or multi-step workflows.
Basic Example
Constructor
CodeAgent
The task description for the agent to complete.
The language model to use. If not provided, defaults to
ChatBrowserUse().Note: CodeAgent currently only works with ChatBrowserUse.Browser session instance. If not provided, a new browser will be created automatically.
Custom tools registry. If not provided, uses default
CodeAgentTools() which includes browser control functions.Separate LLM model for page content extraction. Useful for using a faster/cheaper model for extraction.
File system instance for file operations. Defaults to
FileSystem(base_dir='./').List of file paths the agent can access.
Dictionary containing sensitive data that should be handled carefully.
Maximum number of execution steps before terminating.
Maximum consecutive errors before auto-termination.
Maximum number of times to run the validator agent to verify task completion.
Whether to include screenshots in LLM messages for visual context.
Whether to calculate and track token costs.
Enable the in-browser demo panel for live logging and visualization.
Methods
run
Execute the agent to complete the task.Optional override for maximum number of steps. Uses constructor value if not provided.
NotebookSession - A notebook session containing all executed code cells, outputs, and browser states.
close
Close the browser session.How CodeAgent Differs from Agent
Agent (Standard)
- Uses predefined actions (click, type, navigate, etc.)
- LLM selects actions from a fixed set
- Structured, predictable behavior
- Best for straightforward automation tasks
CodeAgent
- LLM writes custom Python code
- Full programming flexibility
- Access to Python libraries (json, csv, re, etc.)
- Persistent namespace across cells
- Best for complex logic, data processing, custom workflows
Available Functions in Namespace
The CodeAgent executes code in a namespace with browser control functions available:Browser Navigation
Element Interaction
Information Retrieval
JavaScript Execution
Task Completion
The
done() function must be called to mark the task as complete. Without it, the agent will continue until max_steps is reached.Code Execution Features
Persistent Namespace
Variables persist across code cells, just like Jupyter notebooks:Top-Level Await
You can useawait at the top level without wrapping in async functions:
Multiple Code Block Types
The LLM can generate multiple types of code blocks:Result Object
Therun() method returns a NotebookSession object:
NotebookSession Properties
cells- List of executed code cells with outputsexecution_count- Current execution count_complete_history- Complete execution history with metadata_usage_summary- Token usage and cost summary
Cell Properties
source- The Python code that was executedoutput- Captured stdout outputerror- Error message if execution failedstatus- Execution status:SUCCESS,ERROR, orRUNNINGexecution_count- Cell execution numberbrowser_state- Browser state text at time of executioncell_type- Cell type:CODEorMARKDOWN
Advanced Examples
Data Extraction with Processing
Form Filling with Validation
Multi-Page Workflow
Error Handling
Consecutive Error Limit
The agent tracks consecutive errors and terminates aftermax_failures (default: 8):
Validation
Enable task completion validation to ensure the agent actually completed the task:Best Practices
1. Clear Task Descriptions
Be specific about what you want:2. Use Sensitive Data Parameter
Keep credentials safe:3. Enable Vision for Visual Tasks
4. Set Appropriate Limits
5. Track Costs
Comparison with Standard Agent
| Feature | Agent | CodeAgent |
|---|---|---|
| Execution Model | Predefined actions | Custom Python code |
| Flexibility | Fixed action set | Full Python capabilities |
| Data Processing | Limited | Full Python libraries |
| Learning Curve | Easier to understand | Requires Python knowledge |
| Predictability | More predictable | Less predictable |
| Use Cases | Standard automation | Complex workflows |
| LLM Support | Multiple LLMs | ChatBrowserUse only |
Troubleshooting
Agent Doesn’t Call done()
Make sure your task is clear about when to finish:Variables Not Persisting
Variables should persist automatically. If they don’t, check for:- Syntax errors in code
- Scope issues with function definitions
Browser State Not Updating
The browser state is fetched before each LLM call. If you need to force a refresh:Token Limit Errors
If you hit token limits:- Reduce
max_steps - Use
page_extraction_llmwith a smaller model - Disable
use_visionif screenshots aren’t needed
Related Documentation
- Agent Basics - Standard agent with predefined actions
- Browser API - Browser configuration options
- Tools - Custom tool development
- Going to Production - Deploy agents at scale