Overview
The execute() method runs an autonomous agent to complete multi-step tasks. The agent can navigate, interact with elements, extract data, and make decisions to achieve the specified goal.
This method is available on agent instances created with stagehand.agent().
Syntax
const agent = stagehand . agent ( config ? );
const result = await agent . execute ( instructionOrOptions );
See the Agent class reference for comprehensive documentation including:
Creating agents
Configuration options
Streaming mode
Callbacks
Examples
Quick Reference
Basic Usage
const agent = stagehand . agent ();
const result = await agent . execute ( "Find and click the login button" );
With Options
const result = await agent . execute ({
instruction: "Complete the checkout process" ,
maxSteps: 50 ,
variables: {
email: "[email protected] " ,
creditCard: "4111111111111111" ,
},
});
Streaming
const agent = stagehand . agent ({ stream: true });
const streamResult = await agent . execute ({
instruction: "Search for TypeScript repositories" ,
callbacks: {
onChunk : async ( chunk ) => {
process . stdout . write ( chunk . text );
},
},
});
const result = await streamResult . result ;
Key Parameters
Natural language description of the task
Maximum steps before stopping (default: 100)
Variables for form filling (requires experimental: true)
Zod schema for structured output
Signal to abort execution
Return Value
Structured output if schema provided
Examples
For complete examples and advanced usage, see the Agent class reference .
See Also