Skip to content

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

Option to set new tasks and to do custom system prompt

magmueller2bba5d4617c6
diff --git a/browser_use/__init__.py b/browser_use/__init__.py
index 5d455d51b..cefa744e3 100644
--- a/browser_use/__init__.py
+++ b/browser_use/__init__.py
@@ -2,9 +2,10 @@ from browser_use.logging_config import setup_logging
setup_logging()
+from browser_use.agent.prompts import SystemPrompt as SystemPrompt
from browser_use.agent.service import Agent as Agent
from browser_use.browser.service import Browser as Browser
from browser_use.controller.service import Controller as Controller
from browser_use.dom.service import DomService
-__all__ = ['Agent', 'Browser', 'Controller', 'DomService']
+__all__ = ['Agent', 'Browser', 'Controller', 'DomService', 'SystemPrompt']
diff --git a/browser_use/agent/prompts.py b/browser_use/agent/prompts.py
index ed008f7f6..e7a2e75fc 100644
--- a/browser_use/agent/prompts.py
+++ b/browser_use/agent/prompts.py
@@ -5,9 +5,8 @@ from langchain_core.messages import HumanMessage, SystemMessage
from browser_use.browser.views import BrowserState
-class AgentSystemPrompt:
- def __init__(self, task: str, action_description: str, current_date: datetime):
- self.task = task
+class SystemPrompt:
+ def __init__(self, action_description: str, current_date: datetime):
self.default_action_description = action_description
self.current_date = current_date
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.py
index 34b7f2306..354b9a5bf 100644
--- a/browser_use/agent/service.py
+++ b/browser_use/agent/service.py
@@ -11,12 +11,12 @@ from typing import Any, Optional, TypeVar
from dotenv import load_dotenv
from langchain_anthropic import ChatAnthropic
from langchain_core.language_models.chat_models import BaseChatModel
-from langchain_core.messages import AIMessage, BaseMessage, HumanMessage
+from langchain_core.messages import AIMessage, BaseMessage, HumanMessage, SystemMessage
from langchain_openai import ChatOpenAI
from openai import RateLimitError
from pydantic import BaseModel, ValidationError
-from browser_use.agent.prompts import AgentMessagePrompt, AgentSystemPrompt
+from browser_use.agent.prompts import AgentMessagePrompt, SystemPrompt
from browser_use.agent.views import (
ActionResult,
AgentError,
@@ -52,6 +52,7 @@ class Agent:
save_conversation_path: Optional[str] = None,
max_failures: int = 5,
retry_delay: int = 10,
+ system_prompt: Optional[SystemPrompt] = None,
):
self.agent_id = str(uuid.uuid4()) # unique identifier for the agent
@@ -64,14 +65,16 @@ class Agent:
self.controller_injected = controller is not None
self.controller = controller or Controller()
+ self.system_prompt = system_prompt
+
# Telemetry setup
self.telemetry = ProductTelemetry()
# Action and output models setup
self._setup_action_models()
- # Message history setup
- self.messages = self._initialize_messages()
+ self.messages = []
+ self._initialize_messages()
# Tracking variables
self.history: list[AgentHistory] = []
@@ -98,19 +101,25 @@ class Agent:
# Create output model with the dynamic actions
self.AgentOutput = AgentOutput.type_with_custom_actions(self.ActionModel)
- def _initialize_messages(self) -> list[BaseMessage]:
+ def _initialize_messages(self) -> None:
"""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())
- system_prompt = AgentSystemPrompt(
- self.task,
- action_description=action_descriptions,
- current_date=datetime.now(),
- ).get_system_message()
+ self.set_task(self.task)
- first_message = HumanMessage(content=f'Your task is: {self.task}')
- return [system_prompt, first_message]
+ def set_task(self, task: str) -> None:
+ self.messages.append(HumanMessage(content=f'Your task is: {task}'))
@time_execution_async('--step')
async def step(self) -> None: