Continuing an existing session within Browser Use

In this cookbook, we'll explore how to create powerful hybrid workflows that combine human and AI interactions in the same browser session. This approach allows you to:

  • Start a browsing session manually and then hand it off to an AI agent to complete specific tasks
  • Pre-perform certain actions, ones that might be more suitable for humans, or come from a stored state.
  • Maintain full context and state between human and AI interactions
  • Create workflows that leverage both human judgment and AI efficiency

Unlike fully automated solutions that struggle with authentication or context-specific decisions, or fully manual processes that waste human time on repetitive tasks, hybrid workflows give you the best of both worlds - human intelligence where it matters most and AI assistance for everything else.

Prerequisites

Before starting, you'll need:

  1. A Hyperbrowser API key (sign up at hyperbrowser.ai if you don't have one)
  2. Python 3.9+ installed
  3. Basic familiarity with Python async programming

Your API key should be stored in a .env file in the same directory as this notebook:

HYPERBROWSER_API_KEY=your_hyperbrowser_key_here

Step 1: Import Libraries and Initialize Hyperbrowser

We start by importing the necessary packages and setting up our environment. These imports provide access to:

  • AsyncHyperbrowser: The main client for interacting with Hyperbrowser services
  • StartBrowserUseTaskParams: Parameters for configuring the AI agent's task
  • CreateSessionParams: Options for browser session creation
  • IPython.display: Utilities for rendering rich output in the notebook
import os
from dotenv import load_dotenv
from hyperbrowser import AsyncHyperbrowser
from hyperbrowser.models import (
StartBrowserUseTaskParams,
CreateSessionParams
)
from IPython.display import Markdown, display
load_dotenv()

Step 2: Create a Hyperbrowser Client

Next, we initialize the Hyperbrowser client with our API key. This client will handle all communication with the Hyperbrowser API throughout our workflow.

hb = AsyncHyperbrowser(api_key=os.getenv("HYPERBROWSER_API_KEY"))

Step 3: Create an Interactive Browser Session

This is where the hybrid workflow begins. Instead of having an AI agent start from scratch, we create a session that a human can interact with first. The live_url provides a link to a browser that you can access directly through your web browser.

This approach is particularly valuable for situations that require:

  • Logging into accounts with captchas or 2FA verification
  • Making initial selections or navigating to specific areas of a site
  • Setting up a context that would be difficult to describe to an AI agent

In this example, we'll navigate to Amazon, log in to our account, and add items to the cart before handing it off to Browser Use for further interactions.

session = await hb.sessions.create(CreateSessionParams(use_proxy=True))
print(session.live_url)
https://app.hyperbrowser.ai/live?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzZXNzaW9uSWQiOiI5MzMyYzljMi1kODM4LTRkNTItOTk1NC01NDQ2NDMyYzQ2ODkiLCJ0ZWFtSWQiOiIzNDA4NWIxZS0wMjAxLTQ2MTAtODIxYy0xMGZhOWRhZTY2ZDQiLCJpYXQiOjE3NDI0MzE2MzYsImV4cCI6MTc0MjQ3NDgzNn0.5cV1UjfRO7EgIEXfa3giewz5tLPqe5DqI9OPX9QJHKI&keepAlive=true

Step 4: Define the AI Task Function

After you've completed your part of the workflow in the live browser, you can define what you want the AI agent to do next. This function:

  1. Takes the existing session ID (where we've already been working)
  2. Gives the AI a natural language instruction for what to complete
  3. Enables vision capabilities so the AI can "see" the browser state you left it in
  4. Returns the results of the AI's actions

In this example, we're asking the AI to clear out an Amazon shopping cart. This assumes you might have already logged in and navigated to Amazon during your part of the workflow.

async def clear_out_amazon_cart():
resp = await hb.agents.browser_use.start_and_wait(
StartBrowserUseTaskParams(
task=f"Clear out my amazon cart", use_vision=True, session_id=session.id
)
)
if resp.data is not None:
return resp.data.final_result
return None

Step 5: Execute the AI Task to Complete the Workflow

Now that we've set up the task, we can run it to have the AI continue working within the browser session we started. The AI will:

  1. Analyze the current state of the browser you've been using
  2. Find the Amazon cart if you're already on Amazon, or navigate to it if not
  3. Perform the necessary interactions to clear the cart
  4. Return a confirmation message

This seamless handoff between human and AI actions demonstrates the power of hybrid workflows - combining human navigation and decision-making with AI efficiency for task completion.

response = await clear_out_amazon_cart()
if response is not None:
display(Markdown(response))
else:
print("No response from the agent")

Cleared the amazon cart. There were 2 items in the cart and both were deleted.

Live example on Amazon

When to Use Hybrid Workflows

Hybrid human-AI workflows shine in scenarios where either a fully manual or fully automated approach would fall short:

  1. Authentication Challenges: When websites have complex login processes, captchas, or 2FA that would be difficult for AI to handle alone

  2. Context-Heavy Tasks: When you need to navigate complex interfaces or make judgment calls before delegating the repetitive parts

  3. Sensitive Operations: When you want to verify the initial steps before allowing automation to complete a process

Conclusion

Hybrid workflows represent the next step in human-AI collaboration. Rather than viewing automation as an all-or-nothing proposition, this approach recognizes that humans and AI each have distinct strengths that can be combined seamlessly.

By starting processes manually in a live browser and then delegating completion to an AI agent, you can:

  • Overcome common automation challenges like authentication
  • Ensure processes begin with appropriate human judgment
  • Save time by delegating repetitive parts of workflows
  • Maintain full visibility and control throughout the process

As AI capabilities continue to evolve, these hybrid patterns will become increasingly powerful, enabling new forms of collaboration between human expertise and machine efficiency.