Prerequisites
Before you begin, ensure you have:- Cerebras API Key - Get a free API key here
- Browserbase Account - Visit Browserbase and create an account to get your API key and Project ID
- Node.js 20 or higher - Stagehand requires a modern Node.js environment
Stagehand works best with fast inference providers like Cerebras. The ultra-low latency of Cerebras models (llama-3.3-70b, qwen-3-32b, llama3.1-8b) enables near-instantaneous browser control decisions, making your automation agents significantly more responsive than traditional approaches.
Configure Stagehand with Cerebras
1
Create a new Stagehand project
The fastest way to get started is using the official Stagehand project creator, which sets up everything you need including dependencies:Then follow the setup instructions:
The project template already includes
zod and dotenv as dependencies—no need to install them separately.2
Configure environment variables
Edit your
.env file with your API credentials. You’ll need both Cerebras and Browserbase credentials to enable AI-powered browser automation:You can find your Browserbase API Key and Project ID in the Browserbase Dashboard under the Overview section.
3
Extract structured data from Hacker News
Let’s start with a practical example—extracting the top stories from Hacker News. This demonstrates how Stagehand can intelligently parse and structure real web content:
We’re using Cerebras’s
qwen-3-32b model here, which excels at structured data extraction tasks due to its strong reasoning capabilities and fast inference speed.4
Perform actions with natural language
Now let’s use Stagehand’s This example demonstrates Stagehand’s
act() method to interact with a website. This example searches GitHub for “stagehand browserbase” and extracts results:act() method—you describe what you want to do in plain English, and Stagehand figures out how to interact with the page. With Cerebras’s llama-3.3-70b model, these decisions happen in milliseconds.Best Practice: Use atomic, single-step instructions for reliable automation. Instead of “Click the search box and search for X”, break it into: “Click the search button”, “Type X into the input”, “Press Enter”.
5
Observe and analyze page content
Before taking action, you can use The
observe() to understand what’s available on a page. This is useful for dynamic decision-making in your automation workflows:observe() method analyzes the page and returns suggestions without taking action. Use this when you want to preview options before committing, or when building adaptive automation that responds to different page states.6
Build multi-step workflows
Combine all three methods—Cerebras’s fast inference enables multi-step workflows to complete quickly, making it practical to build efficient browser automation that navigates multiple sites and makes intelligent decisions at each step.
extract(), act(), and observe()—to create sophisticated automation workflows. This example navigates multiple sites and gathers comparative data:Why Use Cerebras with Stagehand?
Cerebras’s ultra-fast inference provides several key advantages for browser automation:- Real-time responsiveness - Sub-second inference enables agents to react instantly to page changes and dynamic content, making automation feel natural
- Complex reasoning - Models like
llama-3.3-70bhandle sophisticated multi-step workflows and make intelligent decisions about ambiguous page elements - Cost-effective scaling - Fast inference means lower costs for long-running automation tasks and dramatically reduced API usage compared to slower providers
- Reliable execution - Low latency reduces timeouts and improves task completion rates, especially for time-sensitive operations and dynamic web applications
- Better developer experience - Near-instantaneous responses during development make debugging and iteration significantly faster
Key Features
Four Powerful Primitives
Stagehand provides four complementary approaches to browser automation:- act() - Execute actions using natural language instructions (click, type, navigate, scroll)
- extract() - Pull structured data from pages using AI, with Zod schema validation
- observe() - Discover available actions on any page without executing them
- agent() - Automate entire workflows autonomously for complex multi-step tasks
Natural Language Control
Describe what you want to do in plain English, and Stagehand’s AI will figure out how to interact with the page. No need for brittle CSS selectors or XPath queries.Structured Data Extraction
Use Zod schemas to define exactly what data you want to extract, and Stagehand will find and structure it for you with built-in validation.Works Everywhere
Stagehand v3 is compatible with all Chromium-based browsers. It also offers integrations with Playwright, Puppeteer, and Selenium for developers who want to combine AI-powered automation with traditional browser control.Choosing the Right Cerebras Model
Different tasks benefit from different models. Here’s a guide:| Model | Best For | Speed | Reasoning |
|---|---|---|---|
llama-3.3-70b | Complex multi-step workflows, ambiguous elements | Fast | Excellent |
qwen-3-32b | Structured data extraction, form analysis | Very Fast | Strong |
llama3.1-8b | Simple navigation, repetitive tasks | Ultra Fast | Good |
Next Steps
- Explore Stagehand’s full documentation for advanced features like custom selectors and context management
- Try different Cerebras models to optimize for your use case (speed vs. reasoning capability)
- Check out Stagehand GitHub for more automation patterns and community examples
- Migrate to GLM4.6: Ready to upgrade? Follow our migration guide to start using our latest model
Troubleshooting
Stagehand can't find an element on the page
Stagehand can't find an element on the page
If Stagehand struggles to locate page elements:
- Use atomic instructions - Break complex actions into single steps. Instead of “Click the search box and search for ‘query’”, use three separate calls: “Click the search button”, “Type ‘query’ into the search input”, “Press Enter”
- Be more specific in your instructions - Instead of “click the button”, try “click the blue submit button in the bottom right corner”
- Wait for dynamic content - Some elements load via JavaScript. Add explicit waits:
await new Promise(r => setTimeout(r, 2000)); - Verify the element exists - Use
observe()first to see what Stagehand detects on the page - Try a different model -
llama-3.3-70bgenerally has better reasoning for complex pages than faster models
How do I handle authentication and sessions?
How do I handle authentication and sessions?
Stagehand can handle authenticated sessions through Browserbase’s session management. For sites that require authentication:Option 1: Let Stagehand handle the login flowOption 2: Use Browserbase session persistenceCreate a session that persists authentication:The session will maintain cookies and authentication state across runs.
What's the difference between act() and observe()?
What's the difference between act() and observe()?
- observe() analyzes the page and returns suggestions without taking action. Use this when you want to preview options before committing, or when building agents that need to dynamically decide their next step.
- act() executes an action based on your natural language instruction. This performs the actual browser interaction (clicking, typing, scrolling, etc.).
What's the difference between extract() and traditional web scraping?
What's the difference between extract() and traditional web scraping?
Traditional web scraping requires:Stagehand:The Stagehand version will work even if the CSS classes change.
- Writing brittle CSS selectors that break when the page layout changes
- Manual handling of dynamic content and pagination
- Complex parsing logic to structure data
extract() method:- Uses AI to intelligently locate data regardless of page structure changes
- Handles dynamic content automatically by understanding visual context
- Structures data according to your Zod schema with built-in validation
- Works across different websites with similar content types without code changes
How do I handle rate limits and CAPTCHAs?
How do I handle rate limits and CAPTCHAs?
For rate limits:
- Add delays between operations using
await new Promise(r => setTimeout(r, 1000)) - Use Browserbase’s session management to spread requests across multiple browser contexts
- Implement exponential backoff retry logic
- Consider using Cerebras’s faster models (
llama3.1-8b) to reduce overall execution time
- Residential proxies to appear as real users
- Realistic browser fingerprints
- Proper browser context management
Which Cerebras model should I use for different tasks?
Which Cerebras model should I use for different tasks?
Choose your model based on the complexity of your automation task:llama-3.3-70b - Best for:
- Complex, multi-step workflows with decision branching
- Pages with ambiguous or hard-to-locate elements
- Tasks requiring sophisticated reasoning about page content
- When accuracy is more important than speed
- Structured data extraction from well-formatted pages
- Balanced performance between speed and reasoning
- General-purpose automation tasks
- Great middle-ground for most use cases
- Simple, repetitive tasks with clear page structure
- High-volume automation where speed is critical
- Extracting data from consistent page layouts
- When you need maximum throughput
How do I debug when Stagehand isn't working as expected?
How do I debug when Stagehand isn't working as expected?
Several strategies can help diagnose issues:
- Use observe() to see what Stagehand detects:
- Enable verbose logging to see Stagehand’s decision-making:
-
Use Browserbase’s live view to watch the browser in real-time during development. The session URL is logged when you call
stagehand.init(). - Start with simpler instructions and gradually add complexity to isolate where things break.
Additional Resources
- Stagehand GitHub Repository - Source code, examples, and community discussions
- Stagehand Documentation - Comprehensive guides and API reference
- Browserbase Documentation - Browser infrastructure and session management
- Cerebras Model Documentation - Learn about available models and their capabilities
- GLM4.6 migration guide - Upgrade to the latest model
To find navigation and other pages in this documentation, fetch the llms.txt file at: https://inference-docs.cerebras.ai/llms.txt

