Optimize Stagehand performance for faster automation and reduced latency
Stagehand performance depends on several factors: DOM processing speed, LLM inference time, browser operations, and network latency. This guide provides proven strategies to maximize automation speed.
Use a single observe() call to plan multiple actions, then execute them efficiently:
Copy
Ask AI
// Instead of sequential operations with multiple LLM callsawait page.act("Fill name field"); // LLM call #1await page.act("Fill email field"); // LLM call #2await page.act("Select country dropdown"); // LLM call #3// Use single observe to plan all form fields - one LLM callconst formFields = await page.observe("Find all form fields to fill");// Execute all actions without LLM inferencefor (const field of formFields) { await page.act(field); // No LLM calls!}
Performance Tip: Acting on observe results avoids LLM inference entirely. This approach is 2-3x faster than direct act() calls and is the recommended pattern for multi-step workflows.
Reduce DOM complexity before Stagehand processes the page:
Copy
Ask AI
// Remove heavy elements that slow down processingawait page.evaluate(() => { // Remove video elements document.querySelectorAll('video, iframe').forEach(el => el.remove()); // Hide complex animations document.querySelectorAll('[style*="animation"]').forEach(el => { (el as HTMLElement).style.animation = 'none'; });});// Then perform Stagehand operationsawait page.act("Click the submit button");