Skip to main content
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.

Why Use Auto-Caching with Agent?

Speed

Cached agent workflows run 10-100x faster by skipping LLM inference on subsequent runs

Cost

Eliminate repeated LLM calls—first run uses inference, subsequent runs use cache

Reliability

Cached actions are deterministic and more predictable than fresh agent exploration

Simplicity

Works automatically—just specify cacheDir and Stagehand handles everything

How Auto-Caching Works

When you specify a cacheDir:
  1. First run: Agent explores and executes workflow using LLM inference
  2. Actions cached: All actions are automatically saved to local cache
  3. Subsequent runs: Same workflow reuses cached actions (no LLM calls)
  4. Performance: 10-100x faster execution, zero LLM tokens
The cache key is automatically generated based on:
  • Agent instruction
  • Start URL
  • Agent execution options
  • Agent configuration

Basic Auto-Caching with Agent

Simply add cacheDir when initializing Stagehand:
That’s it! The second time you run this script, it will reuse the cached agent actions automatically.

Organizing Caches by Workflow

Use descriptive cache directories for different workflows:

Complete Example: First vs Subsequent Runs

First Run (Exploration Mode)

Subsequent Runs (Cached Mode)

Run the exact same script again:

Using History for Analysis

While caching handles execution automatically, you can still use stagehand.history to analyze what happened:

Cache Management

Clear Cache When Site Changes

If the website structure changes, clear the cache to force fresh exploration:

Programmatic Cache Control

Advanced Patterns

Fallback to Fresh Exploration

Combine caching with fallback for resilience:

Version Control for Caches

Commit cache directories to ensure consistent behavior across environments:

Best Practices

Organize caches by workflow or feature:
Implement a strategy for refreshing caches:
Always test cached workflows in staging before production:
Track cache usage for optimization:

Performance Comparison

Without Caching (Every Run):
With Auto-Caching (First Run):
With Auto-Caching (Subsequent Runs):
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.

Troubleshooting

Problem: Workflow still slow on subsequent runsSolutions:
  • Verify cacheDir path is correct and consistent across runs
  • Ensure instruction, URL, and agent config are identical
  • Check file permissions on cache directory
  • Look for cache hit/miss logs in verbose mode
Problem: Cached actions fail on subsequent runsSolutions:
  • Website may have changed—clear cache to re-explore
  • Enable self-healing to adapt to minor changes
  • Implement fallback logic to retry with fresh exploration
Problem: Cache directories growing uncontrolledSolutions:
  • Use version prefixes for cache directories
  • Implement automatic cleanup of old caches
  • Share cache directories for similar workflows

Next Steps

Agent Guide

Learn more about agent capabilities and configuration

Caching Guide

Complete guide to auto-caching with act() and agent()

Observability

Monitor and track history and metrics

Speed Optimization

Additional techniques for faster automation