Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

Prompt improvement and set max actions per step

magmueller81e33566a40a
diff --git a/browser_use/agent/message_manager/service.py b/browser_use/agent/message_manager/service.py
index 0263066e3..e1605ffee 100644
--- a/browser_use/agent/message_manager/service.py
+++ b/browser_use/agent/message_manager/service.py
@@ -35,6 +35,7 @@ class MessageManager:
image_tokens: int = 800,
include_attributes: list[str] = [],
max_error_length: int = 400,
+ max_actions_per_step: int = 10,
):
self.llm = llm
self.system_prompt_class = system_prompt_class
@@ -46,9 +47,13 @@ class MessageManager:
self.IMG_TOKENS = image_tokens
self.include_attributes = include_attributes
self.max_error_length = max_error_length
+
system_message = self.system_prompt_class(
- self.action_descriptions, current_date=datetime.now()
+ self.action_descriptions,
+ current_date=datetime.now(),
+ max_actions_per_step=max_actions_per_step,
).get_system_message()
+
self._add_message_with_tokens(system_message)
self.system_prompt = system_message
task_message = HumanMessage(content=f'Your task is: {task}')
diff --git a/browser_use/agent/prompts.py b/browser_use/agent/prompts.py
index 952373220..a0cc2d115 100644
--- a/browser_use/agent/prompts.py
+++ b/browser_use/agent/prompts.py
@@ -8,20 +8,23 @@ from browser_use.browser.views import BrowserState
class SystemPrompt:
- def __init__(self, action_description: str, current_date: datetime):
+ def __init__(
+ self, action_description: str, current_date: datetime, max_actions_per_step: int = 10
+ ):
self.default_action_description = action_description
self.current_date = current_date
+ self.max_actions_per_step = max_actions_per_step
def important_rules(self) -> str:
"""
Returns the important rules for the agent.
"""
- return """
+ text = """
1. RESPONSE FORMAT: You must ALWAYS respond with valid JSON in this exact format:
{
"current_state": {
- "evaluation_previous_goal": "Success|Failed|Unknown - Analyze the current state and image and check if the previous goal is achieved",
- "memory": "Brief description of what has been done-What you need to remember until the end of the task",
+ "evaluation_previous_goal": "Success|Failed|Unknown - Analyze the current website and image to check if the previous goal is achieved and why/why not",
+ "memory": "Description of what has been done and what you need to remember until the end of the task",
"next_goal": "What needs to be done with the next actions"
},
"action": [
@@ -43,10 +46,12 @@ class SystemPrompt:
{"click_element": {"index": 3}}
]
- Navigation and extraction: [
+ {"open_new_tab": {}},
{"go_to_url": {"url": "https://example.com"}},
{"extract_page_content": {}}
]
+
3. ELEMENT INTERACTION:
- Only use indexes that exist in the provided element list
- Each element has a unique index number (e.g., "33[:]<button>")
@@ -60,7 +65,7 @@ class SystemPrompt:
5. TASK COMPLETION:
- Use the done action as the last action as soon as the task is complete
- Don't hallucinate actions
- - If the user asks for specific information - make sure to include everything in the done function. This is what the user will see.
+ - If the task requires specific information - make sure to include everything in the done function. This is what the user will see.
6. VISUAL CONTEXT:
- When an image is provided, use it to understand the page layout
@@ -68,18 +73,22 @@ class SystemPrompt:
- Each bounding box and its label have the same color
- Most often the label is inside the bounding box, on the top right
- Visual context helps verify element locations and relationships
+ - sometimes labels overlap, so use the context to verify the correct element
+
+7. Form filling:
+ - If you fill a form and the your action sequence is interrupted, most often a list with suggestions poped up and you need to first select the right element from the suggestion list in the new state before filling the next field, otherwise the input text might not be taken correctly.
-7. ACTION SEQUENCING:
+8. ACTION SEQUENCING:
- Actions are executed in the order they appear in the list
- Each action should logically follow from the previous one
- - If the page gets new elements between actions, the sequence is interrupted and you get the new state.
+ - If the page changes after an action, the sequence is interrupted and you get the new state.
- If content only disappears the sequence continues.
- Only provide the action sequence until you think the page will change.
- Try to be efficient, e.g. fill forms at once, or chain actions where nothing changes on the page like saving, extracting, checkboxes...
- - use maximum 10 actions per sequence
-8. Form filling:
- - If you fill a form and the sequence is interrupted, most often a list with suggestions poped up and you need to first select the right element before filling the next field, otherwise the input text might not be taken correctly.
+ - only use multiple actions if it makes sense.
"""
+ text += f' - use maximum {self.max_actions_per_step} actions per sequence'
+ return text
def input_format(self) -> str:
return """
@@ -95,7 +104,7 @@ INPUT STRUCTURE:
Example:
33[:]<button>Submit Form</button>
_[:] Non-interactive text
- 45[:]<input>Email field</input> # Indented to show hierarchy
+
Notes:
- Only elements with numeric indexes are interactive
@@ -122,7 +131,7 @@ Current date and time: {time_str}
{self.important_rules()}
-Remember: Your responses must be valid JSON matching the specified format. Each action in the sequence must be valid and logically connected."""
+Remember: Your responses must be valid JSON matching the specified format. Each action in the sequence must be valid."""
return SystemMessage(content=AGENT_PROMPT)
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.py
index c4b4b9cb0..ff970fdb1 100644
--- a/browser_use/agent/service.py
+++ b/browser_use/agent/service.py
@@ -68,6 +68,7 @@ class Agent:
validate_output: bool = False,
include_attributes: list[str] = [],
max_error_length: int = 400,
+ max_actions_per_step: int = 10,
):
self.agent_id = str(uuid.uuid4()) # unique identifier for the agent
@@ -80,6 +81,7 @@ class Agent:
self.max_error_length = max_error_length
# Controller setup
self.controller = controller
+ self.max_actions_per_step = max_actions_per_step
# Browser setup
self.injected_browser = browser is not None
@@ -116,6 +118,7 @@ class Agent:
max_input_tokens=self.max_input_tokens,
include_attributes=self.include_attributes,
max_error_length=self.max_error_length,
+ max_actions_per_step=self.max_actions_per_step,
)
# Tracking variables
@@ -243,7 +246,8 @@ class Agent:
response: dict[str, Any] = await structured_llm.ainvoke(input_messages) # type: ignore
parsed: AgentOutput = response['parsed']
-
+ # cut the number of actions to max_actions_per_step
+ parsed.action = parsed.action[: self.max_actions_per_step]
self._log_response(parsed)
self.n_steps += 1
@@ -258,9 +262,9 @@ class Agent:
else:
emoji = '🤷'
- logger.info(f'{emoji} Evaluation: {response.current_state.evaluation_previous_goal}')
+ logger.info(f'{emoji} Eval: {response.current_state.evaluation_previous_goal}')
logger.info(f'🧠 Memory: {response.current_state.memory}')
- logger.info(f'🎯 Next Goal: {response.current_state.next_goal}')
+ logger.info(f'🎯 Next goal: {response.current_state.next_goal}')
for i, action in enumerate(response.action):
logger.info(
f'🛠️ Action {i + 1}/{len(response.action)}: {action.model_dump_json(exclude_unset=True)}'