What is agent()?
agent turns high level tasks into fully autonomous browser workflows. You can customize the agent by specifying the LLM provider and model, setting custom instructions for behavior, and configuring max steps.

Why use agent()?
Multi-Step Workflows
Execute complex sequences automatically.
Visual Understanding
Sees and understands web interfaces like humans do using computer vision.
Using agent()
There are three ways to create agents in Stagehand:
- Use a Computer Use Agent (CUA mode)
- Use Agent with any LLM (DOM mode)
- Use Agent with vision and DOM (Hybrid mode)
Feature Availability
Some advanced features are only available with certain agent modes:Computer Use Agents
You can use specialized computer use models from Google, OpenAI, Anthropic, or Microsoft as shown below, withmode set to "cua". To compare the performance of different computer use models, you can visit our evals page.
View or run the example template here
Use Stagehand Agent with Any LLM
Use the agent without specifying a provider to utilize any model or LLM provider:Non CUA agents are currently only supported in TypeScript
TypeScript
Available Agent Models
Check out the guide on how to use different models with Stagehand Agent.
Hybrid Mode
Both DOM and CUA modes have their strengths and weaknesses. Hybrid mode combines them, giving the agent access to both coordinate-based and DOM-based tools to better account for where each may fall short.Hybrid mode requires
experimental: true in your Stagehand constructor.Return value of agent()?
When you use agent(), Stagehand will return a Promise<AgentResult> with the following structure:
Customizing Agent Tools
Stagehand agents come with built-in tools for browser automation, but you can customize the toolset by adding your own custom tools or excluding built-in ones.Adding Custom Tools
Custom tools enhance agents with additional capabilities for more granular control and better performance. Unlike MCP integrations, custom tools are defined inline and execute directly within your application.Custom tools provide a cleaner, more performant alternative to MCP integrations when you need specific functionality.
Defining Custom Tools
Use thetool helper exported from @browserbasehq/stagehand to define custom tools:
Custom Tools vs MCP Integrations
Excluding Built-in Tools
Prevent the agent from using specific built-in tools during execution. This is useful when you want to restrict the agent’s capabilities or avoid certain behaviors.Non-CUA agents only. Requires
experimental: true. Not available when cua: true.Basic Usage
Available Tools by Mode
The tools you can exclude depend on the agent mode:- DOM Mode
- Hybrid Mode
Use Cases
Web Search
Enable thesearch tool by setting useSearch: true in agent.execute(). This gives the agent the ability to perform web searches using the Browserbase Search API, which is useful when the agent needs to find URLs or gather information before navigating.
Requires a valid Browserbase API key. Set
BROWSERBASE_API_KEY in your environment, or pass apiKey in the Stagehand constructor.Variables
Use variables to pass sensitive data (like passwords, API keys, or personal information) to the agent without exposing the actual values to the LLM. The agent sees only variable names and descriptions, while the actual values are substituted at runtime.Non-CUA agents only. Variables are not available with Computer Use Agents.
Basic Usage
act(). You can pass simple values or rich objects with descriptions:
How Variables Work
- LLM receives descriptions only: The agent sees variable names and descriptions in its system prompt, but never the actual values
- Placeholder syntax: The LLM uses
%variableName%syntax when it needs to use a variable (e.g., “type %password% into the password field”) - Runtime substitution: Actual values are substituted just before the action executes
- Secure logging: Variable values are never logged or returned in tool outputs
Supported Tools
Variables work with the following agent tools:- DOM Mode
- Hybrid Mode
Cache Optimization
Variables are cache-friendly by design:- Cache keys use only variable names, not values
- Changing variable values (e.g., different passwords) won’t invalidate cached executions
- This enables efficient replay of the same workflow with different credentials
Best Practices
- Do this
- Don't do this
MCP Integrations
Agents can be enhanced with external tools and services through MCP (Model Context Protocol) integrations. This allows your agent to access external APIs and data sources beyond just browser interactions.Streaming
Enable streaming mode to receive incremental responses from the agent. This is useful for building real-time UIs that show the agent’s reasoning as it progresses.These are experimental features. Set
experimental: true in your Stagehand constructor to enable them.Enabling Streaming Mode
Setstream: true in the agent configuration to enable streaming:
Stream Properties
When streaming is enabled,execute() returns an AgentStreamResult with:
Callbacks
Callbacks let you hook into the agent’s execution lifecycle to monitor progress, log events, or modify behavior.Non-CUA agents only. Callbacks require
experimental: true and are not available with Computer Use Agents.Available Callbacks
- Non-Streaming
- Streaming
When
stream: false (default), these callbacks are available:Abort Signal
Cancel agent execution at any time using anAbortSignal. This is useful for implementing timeouts or allowing users to stop long-running tasks.
Non-CUA agents only. Abort signals require
experimental: true and are not available with Computer Use Agents.Basic Usage
Abort with Streaming
Abort signals also work with streaming mode:Custom Abort Reasons
You can pass a reason when aborting:Message Continuation
Continue a conversation across multiple agent executions by passing themessages from a previous result. This is useful for multi-turn interactions or breaking complex tasks into steps while maintaining context.
Non-CUA agents only. Message continuation requires
experimental: true and is not available with Computer Use Agents.Basic Continuation
Structured Output
Define a Zod schema to receive structured data when the agent completes its task. This is useful when you need specific information extracted from the agent’s execution, such as prices, dates, or other structured data.Non-CUA agents only. Structured output requires
experimental: true and is not available with Computer Use Agents.Agent Execution Configuration
Control the maximum number of steps the agent can take to complete the task using themaxSteps parameter.
Best Practices
Following these best practices will improve your agent’s success rate, reduce execution time, and minimize unexpected errors during task completion.Start on the Right Page
Navigate to your target page before executing tasks:- Do this
- Don't do this
Be Specific
Provide detailed instructions for better results:- Do this
- Don't do this
Troubleshooting
Agent is stopping before completing the task
Agent is stopping before completing the task
Problem: Agent stops before finishing the requested taskSolutions:
- Check if the agent is hitting the maxSteps limit (default is 20)
- Increase maxSteps for complex tasks:
maxSteps: 30or higher - Break very complex tasks into smaller sequential executions
Agent is failing to click the proper elements
Agent is failing to click the proper elements
Problem: Agent clicks on wrong elements or fails to interact with the correct UI componentsSolutions:
- Ensure proper viewport size: Stagehand uses
1288x711by default (optimal for Computer Use models) - Avoid changing viewport dimensions as other sizes may reduce performance
Next steps
Act
Execute actions efficiently using observe results
Extract
Extract structured data from observed elements

