Skip to content

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

move examples to system prompt

mertunsall3a6a3e5d7528
diff --git a/.gitignore b/.gitignore
index 8e65ffa4f..4acad76e6 100644
--- a/.gitignore
+++ b/.gitignore
@@ -37,6 +37,7 @@ browser_cookies.json
cookies.json
gcp-login.json
saved_trajectories/
+old_tests/
AgentHistory.json
AgentHistoryList.json
private_example.py
diff --git a/browser_use/agent/message_manager/service.py b/browser_use/agent/message_manager/service.py
index b48dbdb62..f042bac3e 100644
--- a/browser_use/agent/message_manager/service.py
+++ b/browser_use/agent/message_manager/service.py
@@ -108,6 +108,7 @@ class MessageManager:
sensitive_data: dict[str, str | dict[str, str]] | None = None,
max_history_items: int | None = None,
images_per_step: int = 1,
+ include_tool_call_examples: bool = False,
):
self.task = task
self.state = state
@@ -118,6 +119,7 @@ class MessageManager:
self.use_thinking = use_thinking
self.max_history_items = max_history_items
self.images_per_step = images_per_step
+ self.include_tool_call_examples = include_tool_call_examples
assert max_history_items is None or max_history_items > 5, 'max_history_items must be None or greater than 5'
@@ -163,60 +165,62 @@ class MessageManager:
"""Initialize the message history with system message, context, task, and other initial messages"""
self._add_message_with_type(self.system_prompt)
- placeholder_message = UserMessage(
- content='<example_1>\nHere is an example output of thinking and tool call. You can use it as a reference but do not copy it exactly.',
- cache=True,
- )
- # placeholder_message = HumanMessage(content='Example output:')
- self._add_message_with_type(placeholder_message)
+ # Only add tool call examples if enabled
+ if self.include_tool_call_examples:
+ placeholder_message = UserMessage(
+ content='<example_1>\nHere is an example output of thinking and tool call. You can use it as a reference but do not copy it exactly.',
+ cache=True,
+ )
+ # placeholder_message = HumanMessage(content='Example output:')
+ self._add_message_with_type(placeholder_message)
- example_content = dict()
+ example_content = dict()
- # Add thinking field only if use_thinking is True
- if self.use_thinking:
- example_content[
- 'thinking'
- ] = """I have successfully navigated to https://github.com/explore and can see the page has loaded with a list of featured repositories. The page contains interactive elements and I can identify specific repositories like bytedance/UI-TARS-desktop (index [4]) and ray-project/kuberay (index [5]). The user's request is to explore GitHub repositories and collect information about them such as descriptions, stars, or other metadata. So far, I haven't collected any information.
+ # Add thinking field only if use_thinking is True
+ if self.use_thinking:
+ example_content[
+ 'thinking'
+ ] = """I have successfully navigated to https://github.com/explore and can see the page has loaded with a list of featured repositories. The page contains interactive elements and I can identify specific repositories like bytedance/UI-TARS-desktop (index [4]) and ray-project/kuberay (index [5]). The user's request is to explore GitHub repositories and collect information about them such as descriptions, stars, or other metadata. So far, I haven't collected any information.
My navigation to the GitHub explore page was successful. The page loaded correctly and I can see the expected content.
I need to capture the key repositories I've identified so far into my memory and into a file.
Since this appears to be a multi-step task involving visiting multiple repositories and collecting their information, I need to create a structured plan in todo.md.
After writing todo.md, I can also initialize a github.md file to accumulate the information I've collected.
The file system actions do not change the browser state, so I can also click on the bytedance/UI-TARS-desktop (index [4]) to start collecting information."""
- # Create base example content
- example_content['evaluation_previous_goal'] = 'Navigated to GitHub explore page. Verdict: Success'
- example_content['memory'] = 'Found initial repositories such as bytedance/UI-TARS-desktop and ray-project/kuberay.'
- example_content['next_goal'] = (
- 'Create todo.md checklist to track progress, initialize github.md for collecting information, and click on bytedance/UI-TARS-desktop.'
- )
- example_content['action'] = [
- {
- 'write_file': {
- 'path': 'todo.md',
- 'content': '# Interesting Github Repositories in Explore Section\n\n## Tasks\n- [ ] Initialize a tracking file for GitHub repositories called github.md\n- [ ] Visit each Github repository and find their description\n- [ ] Visit bytedance/UI-TARS-desktop\n- [ ] Visit ray-project/kuberay\n- [ ] Check for additional Github repositories by scrolling down\n- [ ] Compile all results in the requested format\n- [ ] Validate that I have not missed anything in the page\n- [ ] Report final results to user',
- }
- },
- {
- 'write_file': {
- 'path': 'github.md',
- 'content': '# Github Repositories:\n',
- }
- },
- {
- 'click_element_by_index': {
- 'index': 4,
- }
- },
- ]
-
- example_tool_call_1 = AssistantMessage(content=json.dumps(example_content), cache=True)
- self._add_message_with_type(example_tool_call_1)
- self._add_message_with_type(
- UserMessage(
- content='Data written to todo.md.\nData written to github.md.\nClicked element with index 4.\n</example_1>',
- cache=True,
- ),
- )
+ # Create base example content
+ example_content['evaluation_previous_goal'] = 'Navigated to GitHub explore page. Verdict: Success'
+ example_content['memory'] = 'Found initial repositories such as bytedance/UI-TARS-desktop and ray-project/kuberay.'
+ example_content['next_goal'] = (
+ 'Create todo.md checklist to track progress, initialize github.md for collecting information, and click on bytedance/UI-TARS-desktop.'
+ )
+ example_content['action'] = [
+ {
+ 'write_file': {
+ 'path': 'todo.md',
+ 'content': '# Interesting Github Repositories in Explore Section\n\n## Tasks\n- [ ] Initialize a tracking file for GitHub repositories called github.md\n- [ ] Visit each Github repository and find their description\n- [ ] Visit bytedance/UI-TARS-desktop\n- [ ] Visit ray-project/kuberay\n- [ ] Check for additional Github repositories by scrolling down\n- [ ] Compile all results in the requested format\n- [ ] Validate that I have not missed anything in the page\n- [ ] Report final results to user',
+ }
+ },
+ {
+ 'write_file': {
+ 'path': 'github.md',
+ 'content': '# Github Repositories:\n',
+ }
+ },
+ {
+ 'click_element_by_index': {
+ 'index': 4,
+ }
+ },
+ ]
+
+ example_tool_call_1 = AssistantMessage(content=json.dumps(example_content), cache=True)
+ self._add_message_with_type(example_tool_call_1)
+ self._add_message_with_type(
+ UserMessage(
+ content='Data written to todo.md.\nData written to github.md.\nClicked element with index 4.\n</example_1>',
+ cache=True,
+ ),
+ )
def add_new_task(self, new_task: str) -> None:
self.task = new_task
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.py
index cc5afa68d..2199a90b8 100644
--- a/browser_use/agent/service.py
+++ b/browser_use/agent/service.py
@@ -171,6 +171,7 @@ class Agent(Generic[Context, AgentStructuredOutput]):
cloud_sync: CloudSync | None = None,
calculate_cost: bool = False,
display_files_in_done_text: bool = True,
+ include_tool_call_examples: bool = False,
**kwargs,
):
# Check for deprecated memory parameters
@@ -233,6 +234,7 @@ class Agent(Generic[Context, AgentStructuredOutput]):
is_planner_reasoning=is_planner_reasoning,
extend_planner_system_message=extend_planner_system_message,
calculate_cost=calculate_cost,
+ include_tool_call_examples=include_tool_call_examples,
)
# Token cost service
@@ -311,6 +313,7 @@ class Agent(Generic[Context, AgentStructuredOutput]):
sensitive_data=sensitive_data,
max_history_items=self.settings.max_history_items,
images_per_step=self.settings.images_per_step,
+ include_tool_call_examples=self.settings.include_tool_call_examples,
)
if isinstance(browser, BrowserSession):
diff --git a/browser_use/agent/system_prompt.md b/browser_use/agent/system_prompt.md
index a8119d7ac..ed3d4a3e7 100644
--- a/browser_use/agent/system_prompt.md
+++ b/browser_use/agent/system_prompt.md
@@ -155,6 +155,36 @@ Exhibit the following reasoning patterns to successfully achieve the <user_reque
- Always reason about the <user_request>. Make sure to carefully analyze the specific steps and information required. E.g. specific filters, specific form fields, specific information to search. Make sure to always compare the current trajactory with the user request and think carefully if thats how the user requested it.
</reasoning_rules>
+<examples>
+Here are examples of good output patterns. Use them as reference but never copy them directly.
+
+<todo_examples>
+ "write_file": {{
+ "content": "# ArXiv CS.AI Recent Papers Collection Task\n\n## Goal: Collect metadata for 20 most recent papers\n\n## Tasks:\n- [x] Navigate to https://arxiv.org/list/cs.AI/recent\n- [x] Initialize papers.md file for storing paper data\n- [x] Collect paper 1/20: The Automated LLM Speedrunning Benchmark\n- [x] Collect paper 2/20: AI Model Passport\n- [x] Collect paper 3/20: Embodied AI Agents\n- [x] Collect paper 4/20: Conceptual Topic Aggregation\n- [x] Collect paper 5/20: Artificial Intelligent Disobedience\n- [ ] Continue collecting remaining papers from current page\n- [ ] Navigate through subsequent pages if needed\n- [ ] Continue until 20 papers are collected\n- [ ] Verify all 20 papers have complete metadata\n- [ ] Final review and completion\n\n## Progress:\n- Papers collected: 5/20\n- Current page: First page (showing 1-50 of 134 entries)\n- Next: Scroll down to see more papers on current page",
+ "file_name": "todo.md",
+ }}
+</todo_examples>
+
+<evaluation_examples>
+- Positive Examples:
+"evaluation_previous_goal": "Successfully navigated to the product page and found the target information. Verdict: Success"
+"evaluation_previous_goal": "Clicked the login button and user authentication form appeared. Verdict: Success"
+- Negative Examples:
+"evaluation_previous_goal": "Failed to input text into the search bar as I cannot see it in the image. Verdict: Failure"
+"evaluation_previous_goal": "Clicked the submit button with index 15 but the form was not submitted successfully. Verdict: Failure"
+</evaluation_examples>
+
+<memory_examples>
+"memory": "Visited 2 of 5 target websites. Collected pricing data from Amazon ($39.99) and eBay ($42.00). Still need to check Walmart, Target, and Best Buy for the laptop comparison."
+"memory": "Found many pending reports that need to be analyzed in the main page. Successfully processed the first 2 reports on quarterly sales data and moving on to inventory analysis and customer feedback reports."
+</memory_examples>
+
+<next_goal_examples>
+"next_goal": "Click on the 'Add to Cart' button (index 23) to proceed with the purchase flow."
+"next_goal": "Scroll down to find more product listings and extract details from the next 5 items on the page."
+</next_goal_examples>
+</examples>
+
<output>
You must ALWAYS respond with a valid JSON in this exact format:
diff --git a/browser_use/agent/system_prompt_no_thinking.md b/browser_use/agent/system_prompt_no_thinking.md
index 792d0466e..aadd944f9 100644
--- a/browser_use/agent/system_prompt_no_thinking.md
+++ b/browser_use/agent/system_prompt_no_thinking.md
@@ -145,6 +145,36 @@ Be clear and concise in your decision-making:
- Before done, use read_file to verify file contents intended for user output.
</reasoning_rules>
+<examples>
+Here are examples of good output patterns. Use them as reference but never copy them directly.
+
+<todo_examples>
+ "write_file": {{
+ "content": "# ArXiv CS.AI Recent Papers Collection Task\n\n## Goal: Collect metadata for 20 most recent papers\n\n## Tasks:\n- [x] Navigate to https://arxiv.org/list/cs.AI/recent\n- [x] Initialize papers.md file for storing paper data\n- [x] Collect paper 1/20: The Automated LLM Speedrunning Benchmark\n- [x] Collect paper 2/20: AI Model Passport\n- [x] Collect paper 3/20: Embodied AI Agents\n- [x] Collect paper 4/20: Conceptual Topic Aggregation\n- [x] Collect paper 5/20: Artificial Intelligent Disobedience\n- [ ] Continue collecting remaining papers from current page\n- [ ] Navigate through subsequent pages if needed\n- [ ] Continue until 20 papers are collected\n- [ ] Verify all 20 papers have complete metadata\n- [ ] Final review and completion\n\n## Progress:\n- Papers collected: 5/20\n- Current page: First page (showing 1-50 of 134 entries)\n- Next: Scroll down to see more papers on current page",
+ "file_name": "todo.md",
+ }}
+</todo_examples>
+
+<evaluation_examples>
+- Positive Examples:
+"evaluation_previous_goal": "Successfully navigated to the product page and found the target information. Verdict: Success"
+"evaluation_previous_goal": "Clicked the login button and user authentication form appeared. Verdict: Success"
+- Negative Examples:
+"evaluation_previous_goal": "Failed to input text into the search bar as I cannot see it in the image. Verdict: Failure"
+"evaluation_previous_goal": "Clicked the submit button with index 15 but the form was not submitted successfully. Verdict: Failure"
+</evaluation_examples>
+
+<memory_examples>
+"memory": "Visited 2 of 5 target websites. Collected pricing data from Amazon ($39.99) and eBay ($42.00). Still need to check Walmart, Target, and Best Buy for the laptop comparison."
+"memory": "Found many pending reports that need to be analyzed in the main page. Successfully processed the first 2 reports on quarterly sales data and moving on to inventory analysis and customer feedback reports."
+</memory_examples>
+
+<next_goal_examples>
+"next_goal": "Click on the 'Add to Cart' button (index 23) to proceed with the purchase flow."
+"next_goal": "Scroll down to find more product listings and extract details from the next 5 items on the page."
+</next_goal_examples>
+</examples>
+
<output>
You must ALWAYS respond with a valid JSON in this exact format:
diff --git a/browser_use/agent/views.py b/browser_use/agent/views.py
index 31e9ef0c6..379dc7486 100644
--- a/browser_use/agent/views.py
+++ b/browser_use/agent/views.py
@@ -63,6 +63,7 @@ class AgentSettings(BaseModel):
is_planner_reasoning: bool = False # type: ignore
extend_planner_system_message: str | None = None
calculate_cost: bool = False
+ include_tool_call_examples: bool = False
class AgentState(BaseModel):