Skip to content

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

Add follow up tasks examples

magmueller82aa17d8fb5f
diff --git a/browser_use/agent/message_manager/service.py b/browser_use/agent/message_manager/service.py
index c10284e6b..5a3c27941 100644
--- a/browser_use/agent/message_manager/service.py
+++ b/browser_use/agent/message_manager/service.py
@@ -100,6 +100,13 @@ class MessageManager:
content = f'Your ultimate task is: {task}. If you achieved your ultimate task, stop everything and use the done action in the next step to complete the task. If not, continue as usual.'
return HumanMessage(content=content)
+ def add_new_task(self, new_task: str) -> None:
+ content = (
+ f'Your new ultimate task is: {new_task}. Take the previous context into account and finish your new ultimate task. '
+ )
+ msg = HumanMessage(content=content)
+ self._add_message_with_tokens(msg)
+
def add_state_message(
self,
state: BrowserState,
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.py
index 9b6503716..5b6d820f8 100644
--- a/browser_use/agent/service.py
+++ b/browser_use/agent/service.py
@@ -220,6 +220,9 @@ class Agent:
else:
return None
+ def add_new_task(self, new_task: str) -> None:
+ self.message_manager.add_new_task(new_task)
+
@time_execution_async('--step')
async def step(self, step_info: Optional[AgentStepInfo] = None) -> None:
"""Execute one step of the task"""
diff --git a/examples/follow_up_tasks.py b/examples/follow_up_tasks.py
new file mode 100644
index 000000000..aa326691a
--- /dev/null
+++ b/examples/follow_up_tasks.py
@@ -0,0 +1,37 @@
+import asyncio
+
+from dotenv import load_dotenv
+from langchain_openai import ChatOpenAI
+
+from browser_use import Agent
+from browser_use.agent.views import ActionResult
+from browser_use.controller.service import Controller
+
+load_dotenv()
+
+# Initialize the model
+llm = ChatOpenAI(
+ model='gpt-4o',
+ temperature=0.0,
+)
+controller = Controller()
+
+
+task = 'Find the founders of browser-use and draft them a short personalized message'
+
+agent = Agent(task=task, llm=llm, controller=controller)
+
+
+async def main():
+ await agent.run()
+
+ # new_task = input('Type in a new task: ')
+ new_task = 'Find an image of the founders'
+
+ agent.add_new_task(new_task)
+
+ await agent.run()
+
+
+if __name__ == '__main__':
+ asyncio.run(main())
diff --git a/examples/simple.py b/examples/simple.py
index 2803955b0..0d586b8bf 100644
--- a/examples/simple.py
+++ b/examples/simple.py
@@ -12,9 +12,9 @@ llm = ChatOpenAI(
model='gpt-4o',
temperature=0.0,
)
+task = 'Find the founders of browser-use and draft them a short personalized message'
-# Create agent with the model
-agent = Agent(task='Go to amazon.com and search for "laptop"', llm=llm)
+agent = Agent(task=task, llm=llm)
async def main():