Agent Programming
Use this sub-skill when the user is writing Python code around Browser Use agents: constructing an agent, designing the task prompt, running it in an async loop, inspecting history, adding callbacks, preloading initial actions, choosing legacy vs beta agent entrypoints, or debugging agent-loop behavior.
Route Here
- The user imports
Agent from browser_use or browser_use.beta.
- The task is about
Agent(...), await agent.run(...), agent.step(...), agent.take_step(...), callbacks, stop/pause hooks, history parsing, or prompt design.
- The request mentions
initial_actions, max_steps, max_failures, llm_timeout, step_timeout, flash_mode, enable_planning, message_compaction, save_conversation_path, or generate_gif.
- The user wants to compare legacy Python agent behavior with the beta Rust-backed agent.
- The user hit a workflow symptom such as empty history, repeated actions, planning not showing, initial navigation failing, final result missing, or
BetaAgentError.
Route Elsewhere
- Browser/profile/CDP/session launch settings, real Chrome profiles, downloads, proxies, screenshots, videos, HAR, domains, and low-level Actor APIs belong in
../browser-control/SKILL.md.
- Custom actions, default tools, file tools, action schemas,
ActionResult, sensitive data guardrails, and domain/file security belong in ../tools-and-actions/SKILL.md.
- LLM provider selection, API keys,
ChatBrowserUse, structured output, extraction models, fallback models, and cost accounting belong in ../llm-and-output/SKILL.md.
- Terminal
browser-use or bu CLI sessions belong in ../cli-and-sessions/SKILL.md.
- Cloud sandbox, MCP, skill services, telemetry, hosted production, and integration workflows belong in
../production-integrations/SKILL.md.
Entry Point Choice
Prefer the legacy Python agent when the user needs maximum open-source Python extensibility:
from browser_use import Agent, ChatBrowserUse
Prefer the beta agent when the user asks for the Rust-backed/core runtime path or is following the 0.13 beta quickstart:
from browser_use.beta import Agent, BrowserProfile, ChatBrowserUse
The public package exposes both paths. Existing Python code can keep from browser_use import Agent; the beta path is explicitly from browser_use.beta import Agent and requires the Browser Use terminal/core runtime to be available.
Safe Default Agent Pattern
Use an explicit async main(), load environment variables outside the skill content, and set a small step limit during debugging:
import asyncio
from browser_use import Agent, ChatBrowserUse
async def main():
agent = Agent(
task="Go to https://example.com and report the page title.",
llm=ChatBrowserUse(),
)
history = await agent.run(max_steps=20)
print(history.final_result() or "No final result")
asyncio.run(main())
For new Browser Use automation tasks, recommend ChatBrowserUse() as the default model because Browser Use maintains it for browser automation. Do not replace user-specified model names in existing code.
Prompt Design Checklist
- Make browser tasks explicit: start URL, goal, fields to extract, output format, login assumptions, and fallback path.
- Name default actions when deterministic behavior matters:
navigate, click, input, scroll, extract, send_keys, done.
- Add recovery instructions for flaky sites: try keyboard navigation, wait/refresh once, use search fallback, or stop with a clear reason.
- Keep credentials out of prompts; use approved sensitive-data or custom-tool patterns from sibling skills.
- For multi-step tasks, ask for intermediate validation and a final
done result rather than open-ended browsing.
See references/workflows.md for reusable recipes.
Core Constructor Knobs
Use references/agent-api.md for parameter details. The most common agent-programming knobs are:
task: natural-language browser objective; include a URL if directly_open_url=True should pre-navigate.
llm: model object; when omitted, Browser Use resolves a configured default or creates ChatBrowserUse().
initial_actions: ordered list of action dictionaries executed before model-driven steps.
max_failures, final_response_after_failure: controls failure stopping and final recovery call.
llm_timeout, step_timeout: separate model-call and per-step/action watchdogs.
max_actions_per_step: lets the model emit multiple actions in a single step.
flash_mode: faster output shape; strips thinking and planning fields and disables planning.
enable_planning, planning_replan_on_stall, planning_exploration_limit: plan/replan nudges for full-mode agents.
register_new_step_callback, register_done_callback, on_step_start, on_step_end: observe or interrupt run progress.
save_conversation_path, generate_gif, max_history_items, message_compaction: debugging and memory controls.
Initial Actions
Use initial_actions when setup is deterministic and should happen before the model reasons:
initial_actions = [
{"navigate": {"url": "https://example.com", "new_tab": False}},
]
agent = Agent(task="Summarize the landing page", llm=llm, initial_actions=initial_actions)
Notes:
- Legacy and beta agents convert action dictionaries through the current tool registry when possible.
- The beta agent preserves the original ordered payloads in task context and maps legacy aliases such as
click_element_by_index to current action names when possible.
- If
directly_open_url=True and the task contains a URL, Browser Use can add an initial navigate action automatically when no explicit initial_actions are supplied.
- Initial actions can time out or fail before any model step; inspect history and
history.errors().
Running and Observing
- Use
history = await agent.run(max_steps=...) for normal execution.
- Use
on_step_start(agent) and on_step_end(agent) when you need per-step hooks around the run loop.
- Use constructor callbacks for external systems:
register_new_step_callback(state, model_output, step_number) can be sync or async.
register_done_callback(history) can be sync or async.
register_should_stop_callback() and register_external_agent_status_raise_error_callback() can stop or fail externally.
- For beta follow-up tasks, call
await agent.follow_up("...", max_steps=...) only after a successful run() created an active Rust session.
Reading History
Agent.run() returns AgentHistoryList. Use its helper methods instead of parsing private fields:
history = await agent.run(max_steps=30)
print(history.final_result())
print(history.is_done(), history.is_successful())
print(history.urls())
print(history.action_names())
print(history.errors())
Useful helpers include final_result(), is_done(), is_successful(), has_errors(), urls(), screenshot_paths(), screenshots(), action_names(), model_actions(), model_outputs(), model_thoughts(), action_results(), action_history(), extracted_content(), number_of_steps(), total_duration_seconds(), and save_to_file(...).
For Pydantic structured output, prefer the sibling model/output guidance. At the agent-programming level, remember that history.structured_output only works when the history retains the output model schema; otherwise use history.get_structured_output(MyModel).
Planning and Flash Mode
- Full-mode output can include thinking, previous-goal evaluation, memory, next goal, current plan item, and plan updates.
enable_planning=False makes plan rendering and plan updates no-ops.
flash_mode=True removes thinking and planning fields from the output schema and forces enable_planning=False.
- In the current implementation, a Browser Use provider model automatically enables flash mode; use a non-flash-compatible configuration if the user specifically needs plan fields.
- Replan nudges are soft context messages; they do not block repeated actions.
Beta/Core Routing
The beta agent is a Python wrapper around a Rust Browser Use terminal SDK server. Use it when the user explicitly asks for beta/core behavior or imports from browser_use.beta.
from browser_use.beta import Agent, ChatBrowserUse
agent = Agent(task="Open https://example.com and report the title", llm=ChatBrowserUse())
history = await agent.run(max_steps=20)
Beta-specific notes:
find_browser_use_terminal_binary() locates the required terminal binary.
- Missing terminal/core runtime raises
BetaAgentError with instructions to install Browser Use Terminal/core or set BROWSER_USE_TERMINAL_BINARY.
run(max_steps=100) delegates the main loop to the SDK server after Python-side setup and initial actions.
take_step(...) can execute initial actions and then run one SDK step.
follow_up(...) requires an active Rust session from a prior run().
Validation Commands
Use this portable check when validating agent code or reproducing issues:
python - <<'PY'
from browser_use import Agent, ChatBrowserUse
from browser_use.beta import Agent as BetaAgent
print(Agent, ChatBrowserUse, BetaAgent)
PY
For normal user projects, prefer import/signature smoke checks and small scripts with max_steps capped. Run source-checkout test suites only when the user is explicitly maintaining Browser Use itself.
Troubleshooting First Steps
See references/troubleshooting.md for symptoms and fixes. Start with:
- Confirm
browser-use imports and Chromium/Browser Use terminal dependencies are installed for the chosen agent path.
- Confirm the relevant provider API key is present without printing it.
- Reduce to a minimal
Agent(task="Open https://example.com...", max_steps=5) script.
- Lower complexity: remove custom tools, structured output, proxies, and persistent profiles until the run loop works.
- Inspect
history.errors(), history.action_names(), and saved conversation logs.
- If initial actions fail, set
step_timeout, disable automatic URL pre-navigation with directly_open_url=False, or make the first navigation explicit.
1---2name: agent-programming3description: "Write and debug Browser Use Python agent workflows with legacy4 browser_use.Agent and Rust-backed browser_use.beta.Agent, including prompts,5 run loops, callbacks, initial_actions, planning, timeouts, flash mode,6 history, and API routing."7disable-model-invocation: true8metadata:9 source: https://skillmd.com/skills/vectorspacelab/agent-programming10 disco-role: operating11license: MIT12---1314# Agent Programming1516Use this sub-skill when the user is writing Python code around Browser Use agents: constructing an agent, designing the task prompt, running it in an async loop, inspecting history, adding callbacks, preloading initial actions, choosing legacy vs beta agent entrypoints, or debugging agent-loop behavior.1718## Route Here1920- The user imports `Agent` from `browser_use` or `browser_use.beta`.21- The task is about `Agent(...)`, `await agent.run(...)`, `agent.step(...)`, `agent.take_step(...)`, callbacks, stop/pause hooks, history parsing, or prompt design.22- The request mentions `initial_actions`, `max_steps`, `max_failures`, `llm_timeout`, `step_timeout`, `flash_mode`, `enable_planning`, `message_compaction`, `save_conversation_path`, or `generate_gif`.23- The user wants to compare legacy Python agent behavior with the beta Rust-backed agent.24- The user hit a workflow symptom such as empty history, repeated actions, planning not showing, initial navigation failing, final result missing, or `BetaAgentError`.2526## Route Elsewhere2728- Browser/profile/CDP/session launch settings, real Chrome profiles, downloads, proxies, screenshots, videos, HAR, domains, and low-level Actor APIs belong in `../browser-control/SKILL.md`.29- Custom actions, default tools, file tools, action schemas, `ActionResult`, sensitive data guardrails, and domain/file security belong in `../tools-and-actions/SKILL.md`.30- LLM provider selection, API keys, `ChatBrowserUse`, structured output, extraction models, fallback models, and cost accounting belong in `../llm-and-output/SKILL.md`.31- Terminal `browser-use` or `bu` CLI sessions belong in `../cli-and-sessions/SKILL.md`.32- Cloud sandbox, MCP, skill services, telemetry, hosted production, and integration workflows belong in `../production-integrations/SKILL.md`.3334## Entry Point Choice3536Prefer the legacy Python agent when the user needs maximum open-source Python extensibility:3738```python39from browser_use import Agent, ChatBrowserUse40```4142Prefer the beta agent when the user asks for the Rust-backed/core runtime path or is following the 0.13 beta quickstart:4344```python45from browser_use.beta import Agent, BrowserProfile, ChatBrowserUse46```4748The public package exposes both paths. Existing Python code can keep `from browser_use import Agent`; the beta path is explicitly `from browser_use.beta import Agent` and requires the Browser Use terminal/core runtime to be available.4950## Safe Default Agent Pattern5152Use an explicit async `main()`, load environment variables outside the skill content, and set a small step limit during debugging:5354```python55import asyncio56from browser_use import Agent, ChatBrowserUse5758async def main():59 agent = Agent(60 task="Go to https://example.com and report the page title.",61 llm=ChatBrowserUse(),62 )63 history = await agent.run(max_steps=20)64 print(history.final_result() or "No final result")6566asyncio.run(main())67```6869For new Browser Use automation tasks, recommend `ChatBrowserUse()` as the default model because Browser Use maintains it for browser automation. Do not replace user-specified model names in existing code.7071## Prompt Design Checklist7273- Make browser tasks explicit: start URL, goal, fields to extract, output format, login assumptions, and fallback path.74- Name default actions when deterministic behavior matters: `navigate`, `click`, `input`, `scroll`, `extract`, `send_keys`, `done`.75- Add recovery instructions for flaky sites: try keyboard navigation, wait/refresh once, use search fallback, or stop with a clear reason.76- Keep credentials out of prompts; use approved sensitive-data or custom-tool patterns from sibling skills.77- For multi-step tasks, ask for intermediate validation and a final `done` result rather than open-ended browsing.7879See `references/workflows.md` for reusable recipes.8081## Core Constructor Knobs8283Use `references/agent-api.md` for parameter details. The most common agent-programming knobs are:8485- `task`: natural-language browser objective; include a URL if `directly_open_url=True` should pre-navigate.86- `llm`: model object; when omitted, Browser Use resolves a configured default or creates `ChatBrowserUse()`.87- `initial_actions`: ordered list of action dictionaries executed before model-driven steps.88- `max_failures`, `final_response_after_failure`: controls failure stopping and final recovery call.89- `llm_timeout`, `step_timeout`: separate model-call and per-step/action watchdogs.90- `max_actions_per_step`: lets the model emit multiple actions in a single step.91- `flash_mode`: faster output shape; strips thinking and planning fields and disables planning.92- `enable_planning`, `planning_replan_on_stall`, `planning_exploration_limit`: plan/replan nudges for full-mode agents.93- `register_new_step_callback`, `register_done_callback`, `on_step_start`, `on_step_end`: observe or interrupt run progress.94- `save_conversation_path`, `generate_gif`, `max_history_items`, `message_compaction`: debugging and memory controls.9596## Initial Actions9798Use `initial_actions` when setup is deterministic and should happen before the model reasons:99100```python101initial_actions = [102 {"navigate": {"url": "https://example.com", "new_tab": False}},103]104agent = Agent(task="Summarize the landing page", llm=llm, initial_actions=initial_actions)105```106107Notes:108109- Legacy and beta agents convert action dictionaries through the current tool registry when possible.110- The beta agent preserves the original ordered payloads in task context and maps legacy aliases such as `click_element_by_index` to current action names when possible.111- If `directly_open_url=True` and the task contains a URL, Browser Use can add an initial `navigate` action automatically when no explicit `initial_actions` are supplied.112- Initial actions can time out or fail before any model step; inspect history and `history.errors()`.113114## Running and Observing115116- Use `history = await agent.run(max_steps=...)` for normal execution.117- Use `on_step_start(agent)` and `on_step_end(agent)` when you need per-step hooks around the run loop.118- Use constructor callbacks for external systems:119 - `register_new_step_callback(state, model_output, step_number)` can be sync or async.120 - `register_done_callback(history)` can be sync or async.121 - `register_should_stop_callback()` and `register_external_agent_status_raise_error_callback()` can stop or fail externally.122- For beta follow-up tasks, call `await agent.follow_up("...", max_steps=...)` only after a successful `run()` created an active Rust session.123124## Reading History125126`Agent.run()` returns `AgentHistoryList`. Use its helper methods instead of parsing private fields:127128```python129history = await agent.run(max_steps=30)130print(history.final_result())131print(history.is_done(), history.is_successful())132print(history.urls())133print(history.action_names())134print(history.errors())135```136137Useful helpers include `final_result()`, `is_done()`, `is_successful()`, `has_errors()`, `urls()`, `screenshot_paths()`, `screenshots()`, `action_names()`, `model_actions()`, `model_outputs()`, `model_thoughts()`, `action_results()`, `action_history()`, `extracted_content()`, `number_of_steps()`, `total_duration_seconds()`, and `save_to_file(...)`.138139For Pydantic structured output, prefer the sibling model/output guidance. At the agent-programming level, remember that `history.structured_output` only works when the history retains the output model schema; otherwise use `history.get_structured_output(MyModel)`.140141## Planning and Flash Mode142143- Full-mode output can include thinking, previous-goal evaluation, memory, next goal, current plan item, and plan updates.144- `enable_planning=False` makes plan rendering and plan updates no-ops.145- `flash_mode=True` removes thinking and planning fields from the output schema and forces `enable_planning=False`.146- In the current implementation, a Browser Use provider model automatically enables flash mode; use a non-flash-compatible configuration if the user specifically needs plan fields.147- Replan nudges are soft context messages; they do not block repeated actions.148149## Beta/Core Routing150151The beta agent is a Python wrapper around a Rust Browser Use terminal SDK server. Use it when the user explicitly asks for beta/core behavior or imports from `browser_use.beta`.152153```python154from browser_use.beta import Agent, ChatBrowserUse155156agent = Agent(task="Open https://example.com and report the title", llm=ChatBrowserUse())157history = await agent.run(max_steps=20)158```159160Beta-specific notes:161162- `find_browser_use_terminal_binary()` locates the required terminal binary.163- Missing terminal/core runtime raises `BetaAgentError` with instructions to install Browser Use Terminal/core or set `BROWSER_USE_TERMINAL_BINARY`.164- `run(max_steps=100)` delegates the main loop to the SDK server after Python-side setup and initial actions.165- `take_step(...)` can execute initial actions and then run one SDK step.166- `follow_up(...)` requires an active Rust session from a prior `run()`.167168## Validation Commands169170Use this portable check when validating agent code or reproducing issues:171172```bash173python - <<'PY'174from browser_use import Agent, ChatBrowserUse175from browser_use.beta import Agent as BetaAgent176print(Agent, ChatBrowserUse, BetaAgent)177PY178```179180For normal user projects, prefer import/signature smoke checks and small scripts with `max_steps` capped. Run source-checkout test suites only when the user is explicitly maintaining Browser Use itself.181182## Troubleshooting First Steps183184See `references/troubleshooting.md` for symptoms and fixes. Start with:185186- Confirm `browser-use` imports and Chromium/Browser Use terminal dependencies are installed for the chosen agent path.187- Confirm the relevant provider API key is present without printing it.188- Reduce to a minimal `Agent(task="Open https://example.com...", max_steps=5)` script.189- Lower complexity: remove custom tools, structured output, proxies, and persistent profiles until the run loop works.190- Inspect `history.errors()`, `history.action_names()`, and saved conversation logs.191- If initial actions fail, set `step_timeout`, disable automatic URL pre-navigation with `directly_open_url=False`, or make the first navigation explicit.