Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
- Change reasoning rules in system prompt - Flatten AgentOutput to get rid of current_state - Add agent initialization in history. - Update the example tool call. - Add a current_state property to AgentOutput for compatibility.
mertunsall9ae6354b76be
diff --git a/browser_use/agent/message_manager/service.py b/browser_use/agent/message_manager/service.pyindex 3ce35e77b..5c619bec9 100644--- a/browser_use/agent/message_manager/service.py+++ b/browser_use/agent/message_manager/service.py@@ -209,7 +209,7 @@ class MessageManager:self.state = stateself.system_prompt = system_messageself.file_system = file_system- self.agent_history_description = ''+ self.agent_history_description = 'Agent initialized.\n'self.read_state_description = ''self.sensitive_data_description = ''# Only initialize messages if state is empty@@ -242,27 +242,16 @@ class MessageManager:{'name': 'AgentOutput','args': {- 'current_state': {- 'thinking': """-**Understanding the Current State and History:**-- 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.--**Evaluating the Previous Action:**-- My navigation to the GitHub explore page was successful. The page loaded correctly and I can see the expected content.--**Preparing what goes into my memory:**-- I need to capture the key repositories I've identified so far.--**Planning my Next Action:**-- 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. This will help me track which repositories I've visited, what information I've collected, and ensure I don't miss any important repositories. The todo.md should include all visible repositories and a systematic approach to processing them.-- {INSERT REASONING ON WRITING TODO.MD HERE}-- After writing todo.md, I can also create a github.md file to accumulate the information I've collected. Let me put only a title as I haven't collected any information yet.-- These writing actions do not change the browser state, so I can also click on the bytedance/UI-TARS-desktop (index [4]) to start collecting information.+ '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.""",- 'evaluation_previous_goal': 'Navigated to GitHub explore page. Verdict: Success',- 'memory': 'Found initial repositories such as bytedance/UI-TARS-desktop and ray-project/kuberay.',- 'next_goal': 'Create todo.md checklist to track progress and github.md file for collecting information and click on bytedance/UI-TARS-desktop.',- },+ 'evaluation_previous_goal': 'Navigated to GitHub explore page. Verdict: Success',+ 'memory': 'Found initial repositories such as bytedance/UI-TARS-desktop and ray-project/kuberay.',+ 'next_goal': 'Create todo.md checklist to track progress, initialize github.md for collecting information, and click on bytedance/UI-TARS-desktop.','action': [{'write_file': {@@ -287,8 +276,6 @@ class MessageManager:'path': 'github.md','content': """# Github Repositories:-- bytedance/UI-TARS-desktop-- ray-project/kuberay""",}},diff --git a/browser_use/agent/system_prompt.md b/browser_use/agent/system_prompt.mdindex 306d167d1..b05382561 100644--- a/browser_use/agent/system_prompt.md+++ b/browser_use/agent/system_prompt.md@@ -1,4 +1,4 @@-You are a tool-using AI agent designed operating in an iterative loop to automate browser tasks. Your ultimate goal is accomplishing the task provided in USER REQUEST.+You are a tool-using AI agent designed operating in an iterative loop to automate browser tasks. Your ultimate goal is accomplishing the task provided in <user_request>.<intro>You excel at following tasks:@@ -137,66 +137,33 @@ If you are allowed 1 action, ALWAYS output only 1 most reasonable action per ste</action_rules><reasoning_rules>-You must reason explicitly and systematically at every step in your `thinking` block. Your reasoning should reflect deep understanding of the USER REQUEST, the rules, the current state, the agent history so far, and what needs to be done next.--Exhibit the following reasoning patterns:--1. **Understand the Current State**-- Carefully read and reason hard about all available context: <agent_history>, <browser_state>, <read_state>, <file_system>, and <user_request>.-- Identify what was expected, what changed, and what is now visible or actionable.-- Reason about all relevant interactive elements in the <browser_state> or <browser_vision>.-- Reason about your <agent_history> and previous step to track your progress towards the task.--2. **Evaluate the Previous Action**-- Determine whether your last action achieved the intended result.-- Use the DOM structure, screenshots, and newly visible elements to assess success.-- Clearly state what worked, what failed, or what is unknown—and why.-- If something failed, retry, adapt, or choose an alternative approach.--3. **Track and Plan with `todo.md`**-- For complex or multi-step tasks, create or update `todo.md` as soon as you reach the relevant URL.-- Mark completed items using the file tool.-- Refer to `todo.md` to guide and track progress throughout the task.--4. **Write Intermediate Results to Files**-- Append extracted results to `results.md` as they are found.--5. **Prepare Your Tool Call**-- Do all your reasoning in `thinking`.-- `evaluation_previous_goal`, `memory`, and `next_goal` fields are how you leave traceable progress for the next step.-- Each should be short, informative, concrete and specific. This is the information that will be visible to the user and you later.-- Reason carefully about the most relevant next action.-- If you need to do something repetitive for the user - always count how far you are in the process in memory. (Not the step number, but e.g. 32/74 pages visited)--6. **Before Calling `done`**-- Verify completeness—e.g., if the user asked to collect all products, confirm all were found by checking there are no further pages or more further down the page. Verify that you found the right number of items.-- If you are unsure about the file content use first `read_file` to verify before displaying it to the user.+You must reason explicitly and systematically at every step in your `thinking` block.++Exhibit the following reasoning patterns to successfully achieve the <user_request>:+- Reason about <agent_history> to track progress and context toward <user_request>.+- Analyze the most recent "Next Goal" and "Action Result" in <agent_history> and clearly state what you previously tried to achieve.+- Analyze all relevant items in <agent_history>, <browser_state>, <read_state>, <file_system>, <read_state> and the screenshot to understand your state.+- Explicitly judge success/failure/uncertainty of the last action.+- If todo.md is empty and the task is multi-step, generate a stepwise plan in todo.md using file tools.+- Analyze `todo.md` to guide and track your progress.+- If any todo.md items are finished, mark them as complete in the file.+- Analyze the <read_state> where one-time information are displayed due to your previous action. Reason about whether you want to keep this information in memory and plan writing them into a file if applicable using the file tools.+- If you see information relevant to <user_request>, plan saving the information into a file.+- Decide what concise, actionable context should be stored in memory to inform future reasoning.+- When ready to finish, state you are preparing to call done and communicate completion/results to the user.+- Before done, use read_file to verify file contents intended for user output.</reasoning_rules>-<example_reasoning_traces>-Here is a non-exhaustive examples of snippets of reasoning traces:-- In the previous step, I successfully extracted structured data. I should save that into a file with in this step.-- I finished all the navigation and information gathering necessary. I should check results.md to ensure it has the desired data before completion.-- In the previous step, I read results.md file and I can verify that it has the desired data. Now, I should pass it to the user as attachment alongside a message mentioning data is in the attachment.-- I wrote the data successfully to results.md in the last step. I should first read results.md, verify the content, and proceed to call done.-</example_thinking_traces>-<output>You must ALWAYS respond with a valid JSON in this exact format:{{- "current_state": {{- "thinking": "A long and structured <think>-style reasoning block that applies the <reasoning_rules> provided above.",- "evaluation_previous_goal": "One-sentence analysis of your last action. Clearly state success, failure, or uncertain.",- "memory": "1-3 sentences of specific memory of this step and overall progress. You should put here everything that will help you track progress in future steps. Like counting pages visited, items found, etc.",- "next_goal": "State the next immediate goal and the action to achieve it, in one clear sentence."- }},+ "thinking": "A structured <think>-style reasoning block that applies the <reasoning_rules> provided above.",+ "evaluation_previous_goal": "One-sentence analysis of your last action. Clearly state success, failure, or uncertain.",+ "memory": "1-3 sentences of specific memory of this step and overall progress. You should put here everything that will help you track progress in future steps. Like counting pages visited, items found, etc.",+ "next_goal": "State the next immediate goals and actions to achieve it, in one clear sentence.""action":[{{"one_action_name": {{// action-specific parameter}}}}, // ... more actions in sequence]}}-IMPORTANT: Note that at the TOP LEVEL is:-1. "current_state" - contains thinking, evaluation, memory, and next_goal-2. "action" - contains the list of actions to execute-Action list should NEVER be empty.</output>diff --git a/browser_use/agent/views.py b/browser_use/agent/views.pyindex 0f5dfeeff..1f94edbaa 100644--- a/browser_use/agent/views.py+++ b/browser_use/agent/views.py@@ -152,39 +152,35 @@ class StepMetadata(BaseModel):class AgentBrain(BaseModel):- thinking: str | None = None+ thinking: strevaluation_previous_goal: strmemory: strnext_goal: strclass AgentOutput(BaseModel):- """- Output model for LLM, i.e. what we are expecting in LLM structured output in response to our prompt.- {- current_state: AgentBrain({- thinking: "I think I should click on the link at index 127, then open that new tab",- evaluation_previous_goal: "we did ok, team",- memory: "filled in xyz into page, still need to do xyz...",- next_goal: "click on the link at index 127, then open that new tab"- }),- "action": [- ActionModel({action_name: "click_element_by_index", action_params: {index: 127}}),- ActionModel({action_name: "switch_to_tab", action_params: {page_id: 3}}),- ... other multi-action steps ...- ],- }- """-model_config = ConfigDict(arbitrary_types_allowed=True)- current_state: AgentBrain+ thinking: str+ evaluation_previous_goal: str+ memory: str+ next_goal: straction: list[ActionModel] = Field(...,description='List of actions to execute',json_schema_extra={'min_items': 1}, # Ensure at least one action is provided)+ @property+ def current_state(self) -> AgentBrain:+ """For backward compatibility - returns an AgentBrain with the flattened properties"""+ return AgentBrain(+ thinking=self.thinking,+ evaluation_previous_goal=self.evaluation_previous_goal,+ memory=self.memory,+ next_goal=self.next_goal,+ )+@staticmethoddef type_with_custom_actions(custom_actions: type[ActionModel]) -> type[AgentOutput]:"""Extend actions with custom actions"""@@ -231,7 +227,10 @@ class AgentHistory(BaseModel):if self.model_output:action_dump = [action.model_dump(exclude_none=True) for action in self.model_output.action]model_output_dump = {- 'current_state': self.model_output.current_state.model_dump(),+ 'thinking': self.model_output.thinking,+ 'evaluation_previous_goal': self.model_output.evaluation_previous_goal,+ 'memory': self.model_output.memory,+ 'next_goal': self.model_output.next_goal,'action': action_dump, # This preserves the actual action data}