Agent
See how to use agent() to create autonomous AI agents for multi-step browser workflows
Agent Creation
- TypeScript
Agent Configuration
Custom system prompt to provide to the agent. Overrides the default system prompt and defines agent behavior.
The model to use for agent functionality. Can be either:
- A string in the format
"provider/model"(e.g.,"openai/computer-use-preview","anthropic/claude-sonnet-4-6") - An object with
modelNameand additional provider-specific options
"anthropic/claude-haiku-4-5-20251001""anthropic/claude-sonnet-4-6""anthropic/claude-sonnet-4-5-20250929""anthropic/claude-opus-4-5-20251101""anthropic/claude-opus-4-6""anthropic/claude-opus-4-8""google/gemini-2.5-computer-use-preview-10-2025""google/gemini-3-flash-preview""google/gemini-3-pro-preview""microsoft/fara-7b""openai/computer-use-preview""openai/computer-use-preview-2025-03-11"
The model to use for tool execution (observe/act calls within agent tools). If not specified, inherits from the main model configuration.Format:
"provider/model" (e.g., "openai/gpt-4o-mini", "google/gemini-2.0-flash-exp")Indicates whether Computer Use Agent (CUA) mode is enabled. When false, the agent uses standard tool-based operation instead of computer control.
MCP (Model Context Protocol) integrations for external tools and services.Array of: MCP server URLs (strings) or connected Client objects
Custom tool definitions to extend agent capabilities using the AI SDK ToolSet format.
Enable streaming mode for the agent. When
true, execute() returns AgentStreamResult with textStream for incremental output. When false (default), execute() returns AgentResult after completion.Default: falseTool mode for the agent. Determines which set of tools are available to the agent.Modes:
"dom": Uses DOM based tools (act,fillForm) for structured page interactions. Works with any model."hybrid": Uses both DOM-based and coordinate-based tools (act,click,type,dragAndDrop,clickAndHold,fillForm) for visual/screenshot-based interactions. Requires models with reliable coordinate-based action capabilities."cua": Uses Computer Use Agent (CUA) providers like Anthropic Claude, Google Gemini, or OpenAI for screenshot-based automation. This is the preferred way to enable CUA mode (replaces the deprecatedcua: trueoption).
"hybrid" for hybrid-capable models (Claude, GPT-5.4, Gemini 3) and "dom" for everything else.Execute Method
- Non-Streaming
- Streaming
Execute Parameters
High-level task description in natural language.
Maximum number of actions the agent can take before stopping.Default:
20Optional: Specify which page to perform the agent execution on. Supports multiple browser automation libraries:
- Playwright: Native Playwright Page objects
- Puppeteer: Puppeteer Page objects
- Patchright: Patchright Page objects
- Stagehand Page: Stagehand’s wrapped Page object
Whether to show a visual cursor on the page during agent execution. Useful for debugging and demonstrations.Default:
falsePrevious conversation messages to continue from. Pass the
messages from a previous AgentResult to continue that conversation.An
AbortSignal that can be used to cancel the agent execution. When aborted, the agent will stop and throw an AgentAbortError.Tools to exclude from this execution. Pass an array of tool names to prevent the agent from using those tools.Available tools by mode:DOM mode:
act, fillForm, ariaTree, extract, goto, scroll, keys, navback, screenshot, think, wait, searchHybrid mode: click, type, dragAndDrop, clickAndHold, fillFormVision, act, ariaTree, extract, goto, scroll, keys, navback, screenshot, think, wait, searchA Zod schema defining structured output data to return when the task completes. The agent will populate this data based on the information it gathered during execution. The result will be available in
AgentResult.output.Callbacks to hook into the agent’s execution lifecycle. The available callbacks depend on whether streaming is enabled.
Response
Returns:Promise<AgentResult> (non-streaming) or Promise<AgentStreamResult> (streaming)
- Non-Streaming
- Streaming
AgentResult Interface:
Whether the task was completed successfully.
Description of the execution result and status.
Array of individual actions taken during execution. Each action contains tool-specific data.
Whether the agent believes the task is fully complete.
Additional execution metadata and debugging information.
The conversation messages from this execution. Pass these to a subsequent
execute() call via the messages option to continue the conversation.Non-CUA agents only. Requires
experimental: true.Custom structured output data extracted based on the
output Zod schema provided in execute options. Only populated if an output schema was provided.Non-CUA agents only. Requires
experimental: true.Token usage and performance metrics.
Example Response
Code Examples
- Basic Usage
- Custom Configuration
- Advanced Model Config
- Multi-Page
- Streaming
- Callbacks
- Abort Signal
- Message Continuation
- MCP Integrations
- Exclude Tools
- Hybrid Mode
- Structured Output
- Custom Tools
Error Types
The following errors may be thrown by theagent() method:
- StagehandError - Base class for all Stagehand-specific errors
- StagehandInitError - Agent was not properly initialized
- MissingLLMConfigurationError - No LLM API key or client configured
- UnsupportedModelError - The specified model is not supported for agent functionality
- UnsupportedModelProviderError - The specified model provider is not supported
- InvalidAISDKModelFormatError - Model string does not follow the required
provider/modelformat - MCPConnectionError - Failed to connect to MCP server
- StagehandDefaultError - General execution error with detailed message
- AgentAbortError - Thrown when agent execution is cancelled via an
AbortSignal - StreamingCallbacksInNonStreamingModeError - Thrown when streaming-only callbacks (
onChunk,onFinish,onError,onAbort) are used withoutstream: true - ExperimentalNotConfiguredError - Thrown when experimental features (callbacks, signal, messages, streaming) are used without
experimental: truein Stagehand constructor

