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 everythingHow Auto-Caching Works
When you specify acacheDir:
- First run: Agent explores and executes workflow using LLM inference
- Actions cached: All actions are automatically saved to local cache
- Subsequent runs: Same workflow reuses cached actions (no LLM calls)
- Performance: 10-100x faster execution, zero LLM tokens
- Agent instruction
- Start URL
- Agent execution options
- Agent configuration
Basic Auto-Caching with Agent
Simply addcacheDir when initializing Stagehand:
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 usestagehand.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
Use Descriptive Cache Names
Use Descriptive Cache Names
Organize caches by workflow or feature:
Cache Invalidation Strategy
Cache Invalidation Strategy
Implement a strategy for refreshing caches:
Test in Staging First
Test in Staging First
Always test cached workflows in staging before production:
Monitor Cache Hit Rates
Monitor Cache Hit Rates
Track cache usage for optimization:
Performance Comparison
Without Caching (Every Run):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
Cache not being used
Cache not being used
Problem: Workflow still slow on subsequent runsSolutions:
- Verify
cacheDirpath 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
Cached workflow fails
Cached workflow fails
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
Too many cache directories
Too many cache directories
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

