Skip to content

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

Example for system prompt change

magmueller3d33a4ff944e
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.py
index 354b9a5bf..7bc4c9745 100644
--- a/browser_use/agent/service.py
+++ b/browser_use/agent/service.py
@@ -6,7 +6,7 @@ import os
import time
import uuid
from datetime import datetime
-from typing import Any, Optional, TypeVar
+from typing import Any, Optional, Type, TypeVar
from dotenv import load_dotenv
from langchain_anthropic import ChatAnthropic
@@ -52,7 +52,7 @@ class Agent:
save_conversation_path: Optional[str] = None,
max_failures: int = 5,
retry_delay: int = 10,
- system_prompt: Optional[SystemPrompt] = None,
+ system_prompt_class: Type[SystemPrompt] = SystemPrompt,
):
self.agent_id = str(uuid.uuid4()) # unique identifier for the agent
@@ -65,7 +65,7 @@ class Agent:
self.controller_injected = controller is not None
self.controller = controller or Controller()
- self.system_prompt = system_prompt
+ self.system_prompt_class = system_prompt_class
# Telemetry setup
self.telemetry = ProductTelemetry()
@@ -105,17 +105,8 @@ class Agent:
"""Initialize message history with system and first message"""
# Get action descriptions from controller's registry
action_descriptions = self.controller.registry.get_prompt_description()
- if self.system_prompt is not None:
- system_prompt = self.system_prompt
- else:
- system_prompt = SystemPrompt(
- action_description=action_descriptions,
- current_date=datetime.now(),
- )
- self.system_prompt = system_prompt
-
- self.messages.append(system_prompt.get_system_message())
-
+ self.system_prompt = self.system_prompt_class(action_descriptions, datetime.now())
+ self.messages.append(self.system_prompt.get_system_message())
self.set_task(self.task)
def set_task(self, task: str) -> None:
diff --git a/examples/custom_system_prompt.py b/examples/custom_system_prompt.py
new file mode 100644
index 000000000..d15dc1713
--- /dev/null
+++ b/examples/custom_system_prompt.py
@@ -0,0 +1,38 @@
+import json
+import os
+import sys
+
+sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
+
+import asyncio
+
+from langchain_openai import ChatOpenAI
+
+from browser_use import Agent, SystemPrompt
+
+
+class MySystemPrompt(SystemPrompt):
+ def important_rules(self) -> str:
+ existing_rules = super().important_rules()
+ new_rules = 'ALWAYS go first to url wikipedia.com no matter the task'
+ return f'{existing_rules}\n{new_rules}'
+
+ # other methods can be overriden as well (not recommended)
+ # example_response -> str
+ # input_format -> str
+ # response_format -> str
+ # get_system_message -> SystemMessage
+
+
+async def main():
+ task = 'do google search to find images of elon musk wife'
+ model = ChatOpenAI(model='gpt-4o')
+ agent = Agent(task=task, llm=model, system_prompt_class=MySystemPrompt)
+
+ print(json.dumps(agent.system_prompt.get_system_message().model_dump(), indent=4))
+
+ await agent.run()
+
+
+if __name__ == '__main__':
+ asyncio.run(main())