Skip to main content

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

instruction
string
required
Natural language description of the task
maxSteps
number
Maximum steps before stopping (default: 100)
variables
Variables
Variables for form filling (requires experimental: true)
output
StagehandZodObject
Zod schema for structured output
signal
AbortSignal
Signal to abort execution

Return Value

result
AgentResult

Examples

For complete examples and advanced usage, see the Agent class reference.

See Also

Build docs developers (and LLMs) love