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-20250514") - An object with
modelNameand additional provider-specific options
"openai/computer-use-preview""openai/computer-use-preview-2025-03-11""anthropic/claude-3-7-sonnet-latest""anthropic/claude-haiku-4-5-20251001""anthropic/claude-sonnet-4-20250514""anthropic/claude-sonnet-4-5-20250929""google/gemini-2.5-computer-use-preview-10-2025"
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.
Execute Method
- TypeScript
Execute Parameters
High-level task description in natural language.
Maximum number of actions the agent can take before stopping.Default: Varies by configuration
Optional: 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:
falseResponse
Returns:Promise<AgentResult>
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.
Token usage and performance metrics.
Example Response
Code Examples
- Basic Usage
- Custom Configuration
- Advanced Model Config
- Multi-Page
- MCP Integrations
- 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

