Skip to main content

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. Agent

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:
  1. Use a Computer Use Agent (CUA mode)
  2. Use Agent with any LLM (DOM mode)
  3. 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, with mode set to "cua". To compare the performance of different computer use models, you can visit our evals page.
Deprecation Notice: The cua: true option is deprecated and will be removed in a future version. Use mode: "cua" instead.
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.
Model Requirements: Hybrid mode requires models that can reliably perform coordinate-based actions from screenshots. The following models are recommended:
  • Anthropic: any Claude model (e.g. anthropic/claude-sonnet-4-6, anthropic/claude-haiku-4-5-20251001)
  • OpenAI: openai/gpt-5.4, openai/gpt-5.4-mini
  • Google: google/gemini-3-flash-preview, google/gemini-3.1-flash-lite-preview, google/gemini-3.1-pro-preview
Other models may not reliably produce accurate coordinates for clicking and typing.
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 the tool helper exported from @browserbasehq/stagehand to define custom tools:

Custom Tools vs MCP Integrations

Use custom tools when you need specific functionality within your application. Use MCP integrations when connecting to external services or when you need standardized cross-application tools.

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:

Use Cases

Enable the search 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

Variables use the same type as act(). You can pass simple values or rich objects with descriptions:

How Variables Work

  1. LLM receives descriptions only: The agent sees variable names and descriptions in its system prompt, but never the actual values
  2. Placeholder syntax: The LLM uses %variableName% syntax when it needs to use a variable (e.g., “type %password% into the password field”)
  3. Runtime substitution: Actual values are substituted just before the action executes
  4. Secure logging: Variable values are never logged or returned in tool outputs

Supported Tools

Variables work with the following agent tools:

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

Use descriptive names and descriptions for variables. The LLM relies on the description to understand when and how to use each variable.

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.
MCP integrations enable agents to be more powerful by combining browser automation with external APIs, databases, and services. The agent can intelligently decide when to use browser actions versus external tools.

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.
Non-CUA agents only. Streaming, callbacks, abort signals, and message continuation are only available when using the standard agent (without mode: "cua"). These features are not supported with Computer Use Agents.
These are experimental features. Set experimental: true in your Stagehand constructor to enable them.

Enabling Streaming Mode

Set stream: 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

When stream: false (default), these callbacks are available:
Streaming-only callbacks (onChunk, onFinish, onError, onAbort) will throw an error if used without stream: true. If you need these callbacks, enable streaming in your agent configuration.

Abort Signal

Cancel agent execution at any time using an AbortSignal. 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 the messages 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.
Use .describe() on schema fields to help the agent understand what data to extract.

Agent Execution Configuration

Stagehand uses a 1288x711 viewport by default. Other viewport sizes may reduce performance. If you need to modify the viewport, you can edit in the Browser Configuration.
Control the maximum number of steps the agent can take to complete the task using the maxSteps 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:

Be Specific

Provide detailed instructions for better results:

Troubleshooting

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: 30 or higher
  • Break very complex tasks into smaller sequential executions
Problem: Agent clicks on wrong elements or fails to interact with the correct UI componentsSolutions:
  • Ensure proper viewport size: Stagehand uses 1288x711 by 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