Skip to main content
Stagehand is an AI-powered web browsing framework that enables intelligent browser automation. By integrating Cerebras models, you can leverage ultra-fast inference for web scraping, form filling, and automated testing tasks.

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

Configure Stagehand with Cerebras

1

Install required dependencies

Install Stagehand and the necessary libraries. We’ll use Zod for defining structured data schemas and dotenv for managing API keys securely:
npm install @browserbasehq/stagehand@latest
2

Configure environment variables

Create a .env file in your project directory with your API credentials. You’ll need both Cerebras and Browserbase credentials to enable AI-powered browser automation:
CEREBRAS_API_KEY=your-cerebras-api-key-here
BROWSERBASE_API_KEY=your-browserbase-api-key-here
BROWSERBASE_PROJECT_ID=your-browserbase-project-id-here
You can find your Browserbase API Key and Project ID in the Browserbase Dashboard under the Overview section.
3

Initialize Stagehand with Cerebras

Set up Stagehand to use Cerebras models for AI-powered browser automation. The modelName configuration allows you to use Cerebras’s ultra-fast inference for all AI operations:
import 'dotenv/config';
import { Stagehand } from "@browserbasehq/stagehand";
import { z } from "zod";

(async () => {
  try {
    const stagehand = new Stagehand({
      env: "BROWSERBASE",
      apiKey: process.env.BROWSERBASE_API_KEY,
      projectId: process.env.BROWSERBASE_PROJECT_ID,
      model: {
        modelName: "cerebras/gpt-oss-120b",
        apiKey: process.env.CEREBRAS_API_KEY,
      }
    });

    await stagehand.init();
    const page = stagehand.context.pages()[0];
    
    await page.goto("https://example.com");
    
    const { heading } = await stagehand.extract(
      "Extract the main heading",
      z.object({
        heading: z.string(),
      })
    );
    
    console.log("Extracted heading:", heading);
    await stagehand.close();
  } catch (error) {
    console.error("Error:", error);
    process.exit(1);
  }
})();
This configuration initializes Stagehand directly with the Cerebras gpt-oss-120b model. By specifying the cerebras/ prefix in modelName and providing the API key in the model configuration, Stagehand automatically leverages Cerebras for ultra-fast inference.
These examples require valid Browserbase credentials (BROWSERBASE_API_KEY and BROWSERBASE_PROJECT_ID) to execute. The AI operations (extract(), observe(), act()) will make calls to Cerebras’s API for inference.
4

Perform AI-powered browser actions

Now you can use Stagehand’s AI capabilities to interact with web pages intelligently. Here’s a complete example that navigates to a website and extracts information:
import 'dotenv/config';
import { Stagehand } from "@browserbasehq/stagehand";
import { z } from "zod";

(async () => {
  try {
    const stagehand = new Stagehand({
      env: "BROWSERBASE",
      apiKey: process.env.BROWSERBASE_API_KEY,
      projectId: process.env.BROWSERBASE_PROJECT_ID,
      model: {
        modelName: "cerebras/gpt-oss-120b",
        apiKey: process.env.CEREBRAS_API_KEY,
      }
    });

    await stagehand.init();
    const page = stagehand.context.pages()[0];

    // Navigate to a page
    await page.goto("https://example.com");

    // Extract multiple fields using Cerebras
    const data = await stagehand.extract(
      "Extract the heading and all paragraph text",
      z.object({
        heading: z.string(),
        paragraphs: z.array(z.string()),
      })
    );

    console.log("Extracted data:", data);

    await stagehand.close();
  } catch (error) {
    console.error("Error:", error);
    process.exit(1);
  }
})();
This example demonstrates key Stagehand capabilities:
  • extract() - Pulls structured data from pages using AI
  • act() - Executes actions based on natural language instructions (can be added similarly)
  • observe() - AI analyzes the page and suggests possible actions (can be added similarly)

Key Features

Ultra-Fast AI Operations

By using Cerebras models, Stagehand’s AI operations (observe, act, extract) run at unprecedented speeds, reducing automation time significantly.

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.

Structured Data Extraction

Use Zod schemas to define exactly what data you want to extract, and Stagehand will find and structure it for you.

Flexible Browser Control

Stagehand provides both AI-powered actions and direct page access, allowing you to combine intelligent automation with precise control when needed.

Next Steps

FAQ

Cerebras has generous rate limits, but if you’re running many concurrent operations, consider:
  1. Adding delays between operations
  2. Using Browserbase’s session management to reuse browser contexts
  3. Implementing retry logic with exponential backoff
  • observe() analyzes the page and returns suggestions without taking action. Use this when you want to preview options before committing.
  • act() executes an action based on your instruction or a suggestion. This performs the actual browser interaction.
Both methods can leverage Cerebras’s fast inference to understand page context and determine the best course of action.
For most browser automation tasks, we recommend:
  • gpt-oss-120b - Excellent performance for general automation and complex reasoning
  • llama-3.3-70b - Strong alternative for multi-step workflows
  • qwen-3-32b - Great balance of speed and capability for general automation
  • llama3.1-8b - Fastest option for simple, repetitive tasks

Additional Resources