Skip to content

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

Refactor agent prompts for clarity and consistency

Magnus Müllerc3d0c800a214
diff --git a/browser_use/agent/message_manager/service.py b/browser_use/agent/message_manager/service.py
index e4455186a..d303f1b37 100644
--- a/browser_use/agent/message_manager/service.py
+++ b/browser_use/agent/message_manager/service.py
@@ -190,10 +190,10 @@ class MessageManager:
logger.debug(f'Added extracted_content to read_state_description: {action_result.extracted_content}')
if action_result.long_term_memory:
- action_results += f'Action {idx + 1}/{result_len}: {action_result.long_term_memory}\n'
+ action_results += f'{action_result.long_term_memory}\n'
logger.debug(f'Added long_term_memory to action_results: {action_result.long_term_memory}')
elif action_result.extracted_content and not action_result.include_extracted_content_only_once:
- action_results += f'Action {idx + 1}/{result_len}: {action_result.extracted_content}\n'
+ action_results += f'{action_result.extracted_content}\n'
logger.debug(f'Added extracted_content to action_results: {action_result.extracted_content}')
if action_result.error:
@@ -201,13 +201,13 @@ class MessageManager:
error_text = action_result.error[:100] + '......' + action_result.error[-100:]
else:
error_text = action_result.error
- action_results += f'Action {idx + 1}/{result_len}: {error_text}\n'
+ action_results += f'{error_text}\n'
logger.debug(f'Added error to action_results: {error_text}')
self.state.read_state_description = self.state.read_state_description.strip('\n')
if action_results:
- action_results = f'Action Results:\n{action_results}'
+ action_results = f'Result:\n{action_results}'
action_results = action_results.strip('\n') if action_results else None
# Build the history item
diff --git a/browser_use/agent/message_manager/views.py b/browser_use/agent/message_manager/views.py
index 00926abd5..2f601fd07 100644
--- a/browser_use/agent/message_manager/views.py
+++ b/browser_use/agent/message_manager/views.py
@@ -32,30 +32,28 @@ class HistoryItem(BaseModel):
def to_string(self) -> str:
"""Get string representation of the history item"""
- step_str = f'step_{self.step_number}' if self.step_number is not None else 'step_unknown'
+ step_str = 'step' if self.step_number is not None else 'step_unknown'
if self.error:
return f"""<{step_str}>
{self.error}
</{step_str}>"""
elif self.system_message:
- return f"""<sys>
-{self.system_message}
-</sys>"""
+ return '' # empty string
else:
content_parts = []
# Only include evaluation_previous_goal if it's not None/empty
if self.evaluation_previous_goal:
- content_parts.append(f'Evaluation of Previous Step: {self.evaluation_previous_goal}')
+ content_parts.append(f'{self.evaluation_previous_goal}')
# Always include memory
if self.memory:
- content_parts.append(f'Memory: {self.memory}')
+ content_parts.append(f'{self.memory}')
# Only include next_goal if it's not None/empty
if self.next_goal:
- content_parts.append(f'Next Goal: {self.next_goal}')
+ content_parts.append(f'{self.next_goal}')
if self.action_results:
content_parts.append(self.action_results)
diff --git a/browser_use/agent/prompts.py b/browser_use/agent/prompts.py
index d654ceaad..0e5eccd4c 100644
--- a/browser_use/agent/prompts.py
+++ b/browser_use/agent/prompts.py
@@ -132,8 +132,13 @@ class AgentMessagePrompt:
pages_below = pi.pixels_below / pi.viewport_height if pi.viewport_height > 0 else 0
total_pages = pi.page_height / pi.viewport_height if pi.viewport_height > 0 else 0
current_page_position = pi.scroll_y / max(pi.page_height - pi.viewport_height, 1)
- page_info_text = f'Page info: {pi.viewport_width}x{pi.viewport_height}px viewport, {pi.page_width}x{pi.page_height}px total page size, {pages_above:.1f} pages above, {pages_below:.1f} pages below, {total_pages:.1f} total pages, at {current_page_position:.0%} of page'
-
+ page_info_text = '<page_info>'
+ page_info_text += f'Viewport size: {pi.viewport_width}x{pi.viewport_height}px, Total page size: {pi.page_width}x{pi.page_height}px, '
+ page_info_text += f'{pages_above:.1f} pages above, '
+ page_info_text += f'{pages_below:.1f} pages below, '
+ page_info_text += f'{total_pages:.1f} total pages'
+ page_info_text += '</page_info>\n'
+ # , at {current_page_position:.0%} of page
if elements_text != '':
if has_content_above:
if self.browser_state.page_info:
@@ -187,15 +192,15 @@ class AgentMessagePrompt:
Available tabs:
{tabs_text}
{page_info_text}
-{recent_events_text}{pdf_message}Interactive elements from top layer of the current page inside the viewport{truncated_text}:
+{recent_events_text}{pdf_message}Elements you can interact with inside the viewport{truncated_text}:
{elements_text}
"""
return browser_state
def _get_agent_state_description(self) -> str:
step_info_description = ''
- time_str = datetime.now().strftime('%Y-%m-%d %H:%M')
- step_info_description += f'Current date and time: {time_str}'
+ time_str = datetime.now().strftime('%Y-%m-%d')
+ step_info_description += f'Current date: {time_str}'
_todo_contents = self.file_system.get_todo_contents() if self.file_system else ''
if not len(_todo_contents):
@@ -207,10 +212,10 @@ Available tabs:
</user_request>
<file_system>
{self.file_system.describe() if self.file_system else 'No file system available'}
-</file_system>
<todo_contents>
{_todo_contents}
</todo_contents>
+</file_system>
"""
if self.sensitive_data:
agent_state += f'<sensitive_data>\n{self.sensitive_data}\n</sensitive_data>\n'
@@ -237,7 +242,7 @@ Available tabs:
state_description = (
'<agent_history>\n'
+ (self.agent_history_description.strip('\n') if self.agent_history_description else '')
- + '\n</agent_history>\n'
+ + '\n</agent_history>\n\n'
)
state_description += '<agent_state>\n' + self._get_agent_state_description().strip('\n') + '\n</agent_state>\n'
state_description += '<browser_state>\n' + self._get_browser_state_description().strip('\n') + '\n</browser_state>\n'