Use auto-caching to convert agent workflows into fast, deterministic scripts
Agent workflows are powerful for exploring and automating complex tasks, but they can be slow and non-deterministic. This guide shows you how to use Stagehand’s built-in auto-caching to convert agent-discovered workflows into fast, deterministic scripts that run 10-100x faster.
import { Stagehand } from "@browserbasehq/stagehand";const stagehand = new Stagehand({ env: "BROWSERBASE", cacheDir: "cache/github-search" // Enable caching});await stagehand.init();const page = stagehand.context.pages()[0];await page.goto("https://github.com");const agent = stagehand.agent({ cua: true, model: { modelName: "google/gemini-2.5-computer-use-preview-10-2025", apiKey: process.env.GOOGLE_GENERATIVE_AI_API_KEY }, systemPrompt: "You are a helpful assistant that can use a web browser.",});console.log("First run: Exploring with agent...");const startTime = Date.now();const result = await agent.execute({ instruction: "Search for 'stagehand' and click the first repository result", maxSteps: 10});const duration = Date.now() - startTime;console.log(`First run completed in ${duration}ms`);console.log(`Actions: ${result.actions.length}`);console.log(`Status: ${result.success}`);await stagehand.close();// Output (example):// First run completed in 25000ms// Actions: 8// Status: true
import { Stagehand } from "@browserbasehq/stagehand";const stagehand = new Stagehand({ env: "BROWSERBASE", cacheDir: "cache/github-search" // Same cache directory});await stagehand.init();const page = stagehand.context.pages()[0];await page.goto("https://github.com");const agent = stagehand.agent({ cua: true, model: { modelName: "google/gemini-2.5-computer-use-preview-10-2025", apiKey: process.env.GOOGLE_GENERATIVE_AI_API_KEY }, systemPrompt: "You are a helpful assistant that can use a web browser.",});console.log("Subsequent run: Using cached actions...");const startTime = Date.now();const result = await agent.execute({ instruction: "Search for 'stagehand' and click the first repository result", maxSteps: 10});const duration = Date.now() - startTime;console.log(`Subsequent run completed in ${duration}ms`);console.log(`Actions: ${result.actions.length}`);console.log(`Status: ${result.success}`);await stagehand.close();// Output (example):// Subsequent run completed in 2500ms ← 10x faster!// Actions: 8// Status: true
If the website structure changes, clear the cache to force fresh exploration:
Copy
Ask AI
import { rmSync } from 'fs';// Clear specific workflow cachermSync('cache/login-workflow', { recursive: true, force: true });// Then run with fresh explorationconst stagehand = new Stagehand({ env: "BROWSERBASE", cacheDir: "cache/login-workflow" // Will rebuild cache});
Commit cache directories to ensure consistent behavior across environments:
Copy
Ask AI
# .gitignore# Commit cache directories for deterministic CI/CD!cache/!cache/**/*.json
Copy
Ask AI
// CI/CD pipeline will use pre-generated cacheconst stagehand = new Stagehand({ env: "BROWSERBASE", cacheDir: "cache/production-workflow" // Committed to repo});
Cached agent workflows run 10-100x faster and consume zero LLM tokens on subsequent runs. The first run pays the exploration cost, every run after is nearly instant.