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")
import { Stagehand } from "@browserbasehq/stagehand";// Initialize with Browserbase (API key and project ID from environment variables)// Set BROWSERBASE_API_KEY and BROWSERBASE_PROJECT_ID in your environmentconst stagehand = new Stagehand({ env: "BROWSERBASE", model: "anthropic/claude-sonnet-4-20250514"});await stagehand.init();const page = stagehand.context.pages()[0];// Create agent with default configurationconst agent = stagehand.agent();// Navigate to a pageawait page.goto("https://www.google.com");// Execute a taskconst result = await agent.execute("Search for 'Stagehand automation' and click the first result");console.log(result.message);console.log(`Completed: ${result.completed}`);console.log(`Actions taken: ${result.actions.length}`);
Copy
Ask AI
// Create agent with custom model and system promptconst agent = stagehand.agent({ model: "openai/computer-use-preview", systemPrompt: "You are a helpful assistant that can navigate websites efficiently. Always verify actions before proceeding.", executionModel: "openai/gpt-4o-mini" // Use faster model for tool execution});const page = stagehand.context.pages()[0];await page.goto("https://example.com");const result = await agent.execute({ instruction: "Fill out the contact form with test data", maxSteps: 10, highlightCursor: true});
Copy
Ask AI
// Using AgentModelConfig for advanced configurationconst agent = stagehand.agent({ model: { modelName: "anthropic/claude-sonnet-4-20250514", apiKey: process.env.ANTHROPIC_API_KEY, baseURL: "https://custom-proxy.com/v1" }});const result = await agent.execute("Complete the checkout process");
Copy
Ask AI
const page1 = stagehand.context.pages()[0];const page2 = await stagehand.context.newPage();const agent = stagehand.agent();// Execute on specific pageawait page2.goto("https://example.com/dashboard");const result = await agent.execute({ instruction: "Export the data table", page: page2});
Copy
Ask AI
import { Client } from "@modelcontextprotocol/sdk/client/index.js";// Create agent with MCP integrationsconst agent = stagehand.agent({ model: "anthropic/claude-sonnet-4-20250514", integrations: [ "https://mcp-server.example.com", // MCP server URL mcpClientInstance // Or pre-connected Client object ]});const result = await agent.execute("Use the external tool to process this data");
Copy
Ask AI
import { tool } from "ai";import { z } from "zod/v3";// Define custom tools using AI SDK formatconst customTools = { calculateTotal: tool({ description: "Calculate the total of items in cart", parameters: z.object({ items: z.array(z.object({ price: z.number(), quantity: z.number() })) }), execute: async ({ items }) => { const total = items.reduce((sum, item) => sum + (item.price * item.quantity), 0); return { total }; } })};const agent = stagehand.agent({ model: "openai/computer-use-preview", tools: customTools});const result = await agent.execute("Calculate the total cost of items in the shopping cart");