Skip to content

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

Add extend function for planning system prompt

mathisarends-viadee075d776bb3c1
diff --git a/browser_use/agent/prompts.py b/browser_use/agent/prompts.py
index a970bbf6a..c032cfeca 100644
--- a/browser_use/agent/prompts.py
+++ b/browser_use/agent/prompts.py
@@ -138,30 +138,53 @@ Interactive elements from top layer of the current page inside the viewport:
class PlannerPrompt(SystemPrompt):
- def get_system_message(self, is_planner_reasoning) -> Union[SystemMessage, HumanMessage]:
- planner_prompt_text = """You are a planning agent that helps break down tasks into smaller steps and reason about the current state.
+ def __init__(self, available_actions: str):
+ self.available_actions = available_actions
+
+ def get_system_message(
+ self, is_planner_reasoning: bool, extended_planner_system_prompt: Optional[str] = None
+ ) -> Union[SystemMessage, HumanMessage]:
+ """Get the system message for the planner.
+
+ Args:
+ is_planner_reasoning: If True, return as HumanMessage for chain-of-thought
+ extended_planner_system_prompt: Optional text to append to the base prompt
+
+ Returns:
+ SystemMessage or HumanMessage depending on is_planner_reasoning
+ """
+
+ system_prompt = """
+You are a planning agent that helps break down tasks into smaller steps and reason about the current state.
Your role is to:
1. Analyze the current state and history
2. Evaluate progress towards the ultimate goal
3. Identify potential challenges or roadblocks
4. Suggest the next high-level steps to take
+Available actions:
+{available_actions}
+
Inside your messages, there will be AI messages from different agents with different formats.
Your output format should be always a JSON object with the following fields:
-{
+{{
"state_analysis": "Brief analysis of the current state and what has been done so far",
"progress_evaluation": "Evaluation of progress towards the ultimate goal (as percentage and description)",
"challenges": "List any potential challenges or roadblocks",
"next_steps": "List 2-3 concrete next steps to take",
"reasoning": "Explain your reasoning for the suggested next steps"
-}
+}}
Ignore the other AI messages output structures.
-Keep your responses concise and focused on actionable insights."""
+Keep your responses concise and focused on actionable insights.
+"""
+
+ if extended_planner_system_prompt:
+ system_prompt += f'\n{extended_planner_system_prompt}'
if is_planner_reasoning:
- return HumanMessage(content=planner_prompt_text)
+ return HumanMessage(content=system_prompt)
else:
- return SystemMessage(content=planner_prompt_text)
+ return SystemMessage(content=system_prompt)
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.py
index 69603e081..c54d1054a 100644
--- a/browser_use/agent/service.py
+++ b/browser_use/agent/service.py
@@ -143,6 +143,7 @@ class Agent(Generic[Context]):
planner_llm: Optional[BaseChatModel] = None,
planner_interval: int = 1, # Run planner every N steps
is_planner_reasoning: bool = False,
+ extend_planner_system_message: Optional[str] = None,
# Inject state
injected_agent_state: Optional[AgentState] = None,
#
@@ -182,6 +183,7 @@ class Agent(Generic[Context]):
planner_llm=planner_llm,
planner_interval=planner_interval,
is_planner_reasoning=is_planner_reasoning,
+ extend_planner_system_message=extend_planner_system_message,
enable_memory=enable_memory,
memory_interval=memory_interval,
memory_config=memory_config,
@@ -1214,7 +1216,10 @@ class Agent(Generic[Context]):
# Create planner message history using full message history with all available actions
planner_messages = [
- PlannerPrompt(all_actions).get_system_message(self.settings.is_planner_reasoning),
+ PlannerPrompt(all_actions).get_system_message(
+ is_planner_reasoning=self.settings.is_planner_reasoning,
+ extended_planner_system_prompt=self.settings.extend_planner_system_message,
+ ),
*self._message_manager.get_messages()[1:], # Use full message history except the first
]
diff --git a/browser_use/agent/views.py b/browser_use/agent/views.py
index d46e42df8..c042a56b3 100644
--- a/browser_use/agent/views.py
+++ b/browser_use/agent/views.py
@@ -68,6 +68,7 @@ class AgentSettings(BaseModel):
planner_llm: Optional[BaseChatModel] = None
planner_interval: int = 1 # Run planner every N steps
is_planner_reasoning: bool = False # type: ignore
+ extend_planner_system_message: Optional[str] = None
# Procedural memory settings
enable_memory: bool = True