cacheDir when initializing Stagehand, and actions are automatically cached and reused across runs.
How Caching Works
When you specify acacheDir:
- First run: Actions use LLM inference and results are cached to a local file
- Subsequent runs: Cached actions are reused automatically (no LLM calls)
- Cost savings: Eliminate redundant inference calls for repeated actions
- Performance: Faster execution by skipping LLM inference
Caching with act()
Cache actions from act() by specifying a cache directory in your Stagehand constructor.
Caching with agent()
Cache agent actions (including Computer Use Agent actions) the same way - just specify a cacheDir. The cache key is automatically generated based on the instruction, start URL, agent execution options, and agent configuration - subsequent runs with the same parameters will reuse cached actions.
Cache Directory Organization
You can organize your caches by using different directory names for different workflows:Best Practices
Use descriptive cache directories
Use descriptive cache directories
Organize caches by workflow or feature for easier management:
Clear cache when DOM changes
Clear cache when DOM changes
If the website structure changes significantly, clear your cache directory to force fresh inference:Or programmatically:
Commit cache for CI/CD
Commit cache for CI/CD
Consider committing your cache directory to version control for consistent behavior across environments:This ensures your CI/CD pipelines use the same cached actions without needing to run inference on first execution.

