Skip to content

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

Move user request before agent history

MagMueller90a052371d91
diff --git a/browser_use/agent/prompts.py b/browser_use/agent/prompts.py
index 3a0fb2be5..6840051dd 100644
--- a/browser_use/agent/prompts.py
+++ b/browser_use/agent/prompts.py
@@ -327,9 +327,6 @@ Available tabs:
_todo_contents = '[empty todo.md, fill it when applicable]'
agent_state = f"""
-<user_request>
-{self.task}
-</user_request>
<file_system>
{self.file_system.describe() if self.file_system else 'No file system available'}
</file_system>
@@ -348,6 +345,9 @@ Available tabs:
agent_state += f'<available_file_paths>{available_file_paths_text}\nUse with absolute paths</available_file_paths>\n'
return agent_state
+ def _get_user_request_description(self) -> str:
+ return f'<user_request>\n{self.task}\n</user_request>\n\n'
+
def _get_step_meta_description(self) -> str:
# Per-step varying metadata (step counter, wall-clock date). Kept out of <agent_state> so it
# lives at the tail of the user message — anything before this block can in principle be
@@ -401,7 +401,8 @@ Available tabs:
# Build complete state description
state_description = (
- '<agent_history>\n'
+ self._get_user_request_description()
+ + '<agent_history>\n'
+ (self.agent_history_description.strip('\n') if self.agent_history_description else '')
+ '\n</agent_history>\n\n'
)
diff --git a/browser_use/agent/system_prompts/system_prompt.md b/browser_use/agent/system_prompts/system_prompt.md
index 82cb2ca32..56c67d878 100644
--- a/browser_use/agent/system_prompts/system_prompt.md
+++ b/browser_use/agent/system_prompts/system_prompt.md
@@ -14,12 +14,19 @@ You excel at following tasks:
</language_settings>
<input>
At every step, your input will consist of:
-1. <agent_history>: A chronological event stream including your previous actions and their results.
-2. <agent_state>: Current <user_request>, summary of <file_system>, <todo_contents>, and <step_info>.
-3. <browser_state>: Current URL, open tabs, interactive elements indexed for actions, and visible page content.
-4. <browser_vision>: Screenshot of the browser with bounding boxes around interactive elements. If you used screenshot before, this will contain a screenshot.
-5. <read_state> This will be displayed only if your previous action was extract or read_file. This data is only shown in the current step.
+1. <user_request>: Your ultimate objective.
+2. <agent_history>: A chronological event stream including your previous actions and their results.
+3. <agent_state>: Summary of <file_system>, <todo_contents>, and other current agent context.
+4. <browser_state>: Current URL, open tabs, interactive elements indexed for actions, and visible page content.
+5. <browser_vision>: Screenshot of the browser with bounding boxes around interactive elements. If you used screenshot before, this will contain a screenshot.
+6. <read_state> This will be displayed only if your previous action was extract or read_file. This data is only shown in the current step.
</input>
+<user_request>
+USER REQUEST: This is your ultimate objective and always remains visible.
+- This has the highest priority. Make the user happy.
+- If the user request is very specific - then carefully follow each step and dont skip or hallucinate steps.
+- If the task is open ended you can plan yourself how to get it done.
+</user_request>
<agent_history>
Agent history will be given as a list of step information as follows:
<step_{{step_number}}>:
@@ -30,12 +37,6 @@ Action Results: Your actions and their results
</step_{{step_number}}>
and system messages wrapped in <sys> tag.
</agent_history>
-<user_request>
-USER REQUEST: This is your ultimate objective and always remains visible.
-- This has the highest priority. Make the user happy.
-- If the user request is very specific - then carefully follow each step and dont skip or hallucinate steps.
-- If the task is open ended you can plan yourself how to get it done.
-</user_request>
<browser_state>
1. Browser State will be given as:
Current URL: URL of the page you are currently viewing.
diff --git a/browser_use/agent/system_prompts/system_prompt_anthropic_flash.md b/browser_use/agent/system_prompts/system_prompt_anthropic_flash.md
index 05a34b8e7..01c0d8fa9 100644
--- a/browser_use/agent/system_prompts/system_prompt_anthropic_flash.md
+++ b/browser_use/agent/system_prompts/system_prompt_anthropic_flash.md
@@ -101,12 +101,19 @@ BEFORE calling `done` with `success=true`, you MUST perform this verification:
</task_completion_rules>
<input>
At every step, your input will consist of:
-1. <agent_history>: A chronological event stream including your previous actions and their results.
-2. <agent_state>: Current <user_request>, summary of <file_system>, <todo_contents>, and <step_info>.
-3. <browser_state>: Current URL, open tabs, interactive elements indexed for actions, and visible page content.
-4. <browser_vision>: Screenshot of the browser with bounding boxes around interactive elements. This is your GROUND TRUTH.
-5. <read_state> This will be displayed only if your previous action was extract or read_file. This data is only shown in the current step.
+1. <user_request>: Your ultimate objective.
+2. <agent_history>: A chronological event stream including your previous actions and their results.
+3. <agent_state>: Summary of <file_system>, <todo_contents>, and other current agent context.
+4. <browser_state>: Current URL, open tabs, interactive elements indexed for actions, and visible page content.
+5. <browser_vision>: Screenshot of the browser with bounding boxes around interactive elements. This is your GROUND TRUTH.
+6. <read_state> This will be displayed only if your previous action was extract or read_file. This data is only shown in the current step.
</input>
+<user_request>
+USER REQUEST: This is your ultimate objective and always remains visible.
+- This has the highest priority. Make the user happy.
+- If the user request is very specific - then carefully follow each step and dont skip or hallucinate steps.
+- If the task is open ended you can plan yourself how to get it done.
+</user_request>
<agent_history>
Agent history will be given as a list of step information as follows:
<step_{{step_number}}>:
diff --git a/browser_use/agent/system_prompts/system_prompt_no_thinking.md b/browser_use/agent/system_prompts/system_prompt_no_thinking.md
index f61b4168a..e89a985bc 100644
--- a/browser_use/agent/system_prompts/system_prompt_no_thinking.md
+++ b/browser_use/agent/system_prompts/system_prompt_no_thinking.md
@@ -14,12 +14,19 @@ You excel at following tasks:
</language_settings>
<input>
At every step, your input will consist of:
-1. <agent_history>: A chronological event stream including your previous actions and their results.
-2. <agent_state>: Current <user_request>, summary of <file_system>, <todo_contents>, and <step_info>.
-3. <browser_state>: Current URL, open tabs, interactive elements indexed for actions, and visible page content.
-4. <browser_vision>: Screenshot of the browser with bounding boxes around interactive elements. If you used screenshot before, this will contain a screenshot.
-5. <read_state> This will be displayed only if your previous action was extract or read_file. This data is only shown in the current step.
+1. <user_request>: Your ultimate objective.
+2. <agent_history>: A chronological event stream including your previous actions and their results.
+3. <agent_state>: Summary of <file_system>, <todo_contents>, and other current agent context.
+4. <browser_state>: Current URL, open tabs, interactive elements indexed for actions, and visible page content.
+5. <browser_vision>: Screenshot of the browser with bounding boxes around interactive elements. If you used screenshot before, this will contain a screenshot.
+6. <read_state> This will be displayed only if your previous action was extract or read_file. This data is only shown in the current step.
</input>
+<user_request>
+USER REQUEST: This is your ultimate objective and always remains visible.
+- This has the highest priority. Make the user happy.
+- If the user request is very specific - then carefully follow each step and dont skip or hallucinate steps.
+- If the task is open ended you can plan yourself how to get it done.
+</user_request>
<agent_history>
Agent history will be given as a list of step information as follows:
<step_{{step_number}}>:
@@ -30,12 +37,6 @@ Action Results: Your actions and their results
</step_{{step_number}}>
and system messages wrapped in <sys> tag.
</agent_history>
-<user_request>
-USER REQUEST: This is your ultimate objective and always remains visible.
-- This has the highest priority. Make the user happy.
-- If the user request is very specific - then carefully follow each step and dont skip or hallucinate steps.
-- If the task is open ended you can plan yourself how to get it done.
-</user_request>
<browser_state>
1. Browser State will be given as:
Current URL: URL of the page you are currently viewing.
diff --git a/tests/ci/test_prompt_step_meta_suffix.py b/tests/ci/test_prompt_step_meta_suffix.py
index 30ce6842a..91e415b03 100644
--- a/tests/ci/test_prompt_step_meta_suffix.py
+++ b/tests/ci/test_prompt_step_meta_suffix.py
@@ -1,6 +1,7 @@
"""Lock per-step varying metadata (step counter, wall-clock date) at the tail of the user message.
The agent's user message looks roughly:
+ <user_request>...</user_request>
<agent_history>...</agent_history>
<agent_state>...</agent_state>
<browser_state>...</browser_state>
@@ -60,6 +61,8 @@ def test_step_info_lives_at_suffix(tmp_path):
assert isinstance(content, str)
assert '<step_info>' in content
+ assert content.index('<user_request>') < content.index('<agent_history>')
+ assert content.index('</agent_history>') < content.index('<agent_state>')
# Suffix: step_info must come after agent_state and browser_state.
assert content.index('<agent_state>') < content.index('<step_info>')
assert content.index('<browser_state>') < content.index('<step_info>')
@@ -68,6 +71,16 @@ def test_step_info_lives_at_suffix(tmp_path):
state_start = content.index('<agent_state>')
state_end = content.index('</agent_state>')
assert '<step_info>' not in content[state_start:state_end], 'per-step metadata leaked into <agent_state> prefix region'
+ assert '<user_request>' not in content[state_start:state_end], 'user request leaked back into <agent_state>'
+
+
+def test_agent_history_end_marker_is_present_once(tmp_path):
+ content = _make_prompt(tmp_path).get_user_message(use_vision=False).content
+ assert isinstance(content, str)
+
+ assert content.count('</agent_history>') == 1, (
+ 'super important: LLM gateway cache splitting expects exactly one </agent_history> marker'
+ )
def test_prefix_up_to_step_info_is_stable_across_steps(tmp_path):