Skip to content

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

Multiple agent flow with pause

magmuellerfa8582fbafcb
diff --git a/examples/multiple_agents_same_browser.py b/examples/multiple_agents_same_browser.py
index c5246101e..013b33ff3 100644
--- a/examples/multiple_agents_same_browser.py
+++ b/examples/multiple_agents_same_browser.py
@@ -17,20 +17,48 @@ async def main():
browser = Browser()
async with await browser.new_context() as context:
model = ChatOpenAI(model='gpt-4o')
+ current_agent = None
- # Initialize browser agent
- agent1 = Agent(
- task='Open 2 tabs with wikipedia articles about the history of the meta and one random wikipedia article.',
- llm=model,
- browser_context=context,
- )
- agent2 = Agent(
- task='Considering all open tabs give me the names of the wikipedia article.',
- llm=model,
- browser_context=context,
- )
- await agent1.run()
- await agent2.run()
+ async def get_input():
+ return await asyncio.get_event_loop().run_in_executor(
+ None, lambda: input('Enter task (p: pause current agent, r: resume, b: break): ')
+ )
+
+ while True:
+ task = await get_input()
+
+ if task.lower() == 'p':
+ # Pause the current agent if one exists
+ if current_agent:
+ current_agent.pause()
+ continue
+ elif task.lower() == 'r':
+ # Resume the current agent if one exists
+ if current_agent:
+ current_agent.resume()
+ continue
+ elif task.lower() == 'b':
+ # Break the current agent's execution if one exists
+ if current_agent:
+ current_agent.stop()
+ current_agent = None
+ continue
+
+ # If there's a current agent running, pause it before starting new one
+ if current_agent:
+ current_agent.pause()
+
+ # Create and run new agent with the task
+ current_agent = Agent(
+ task=task,
+ llm=model,
+ browser_context=context,
+ )
+
+ # Run the agent asynchronously without blocking
+ asyncio.create_task(current_agent.run())
asyncio.run(main())
+
+# Now aad the cheapest to the cart