Good prompts make Stagehand reliable. Bad prompts cause failures. Here’s how to write prompts that work consistently.
Act method
Use act() for single actions on web pages. Each action should be focused and clear.
Use element types, not colors
Describe elements by their type and function rather than visual attributes like color.
Use descriptive language
Choose the right action verbs
- Click for buttons, links, checkboxes
- Type for text inputs
- Select for dropdowns
- Check/uncheck for checkboxes
- Upload for file inputs
Protect sensitive data
Variables keep sensitive information out of prompts and logs. Stagehand shows the model only the variable name and its optional description, then substitutes the real value into the resolved action right before it runs, so the secret never reaches the model and never appears in the logged action.
Variables are part of the payload that builds the cache key. Caching is off by default, so the values stay inside the run unless you opt in. Once you enable the cache option on create or on a call, the variables you pass travel to the cache service with the rest of the request, so turn it off on the calls that carry credentials.
Set the log level to off in your Stagehand config to prevent secrets from appearing in logs.
Use extract() to pull structured data from pages. Define clear schemas and provide context.
Schema best practices
Use descriptive field names, correct types, and detailed descriptions. Field descriptions provide context that helps the model understand exactly what to extract.
Use proper URL types
Type link fields as URLs so Stagehand resolves them to real addresses instead of the internal element IDs the model chooses.
Observe method
Use observe() to discover actionable elements before acting on them.
Check elements first
Verify elements exist before taking action to avoid errors.
Be specific about element types
Sequencing multi-step work
Stagehand v4 has no autonomous agent, so multi-step flows are your control flow. That is a feature: you decide the order, the retries, and the stopping condition.
Navigate first
Don’t put navigation inside an instruction. Handle it separately with goto.
Break work into steps
One instruction per action. Sequence them yourself so each step is independently debuggable and cacheable.
Include success criteria
Verify each step landed instead of assuming it did.
Common mistakes to avoid
- Combining multiple actions: Keep each
act() call to one action
- Using vague descriptions: Be specific about which elements to interact with
- Exposing sensitive data: Always use variables for credentials
- Skipping validation: Check results before proceeding
Testing your prompts
- Start simple: Test basic functionality first
- Add complexity gradually: Build up to complex workflows
- Monitor results: Use logging to understand what’s happening
- Iterate based on failures: Refine prompts when they don’t work
Remember: Good prompting is iterative. When in doubt, be more specific rather than less.