Skip to content

Stagehand Computer use Agent not working with Browserbase #936

@neilsanghrajka

Description

@neilsanghrajka

Bug Report: StagehandNotInitializedError when using agent.execute() despite calling stagehand.init()

Description

When following the official documentation example for creating and executing a Stagehand agent, the code throws a StagehandNotInitializedError claiming that the API client is being called on an uninitialized Stagehand object, even though await stagehand.init() is properly called before creating the agent.

Expected Behavior

Based on the official documentation, this code should work without errors:

  const stagehand = new Stagehand({
    env: BROWSERBASE_ENV,
    logger: console.log,
    disablePino: true, // Needed to work with Next.js
    // Browserbase specific
    projectId: BROWSERBASE_PROJECT_ID,
    apiKey: BROWSERBASE_API_KEY,
    browserbaseSessionCreateParams: {
      projectId: BROWSERBASE_PROJECT_ID,
      region: "ap-southeast-1", 
      keepAlive: false,
      timeout: 3 * 60, // 10 minutes
    },
  });

await stagehand.init();

const agent = stagehand.agent({
  provider: "anthropic",
  model: "claude-3-7-sonnet-latest",
  instructions: "Search for robinhood stock price and return it",
  options: {
    apiKey: ANTHROPIC_API_KEY,
  },
});

await agent.execute({
  instruction: "Search for robinhood stock price and return it",
});

Actual Behavior

The code throws the following error:

StagehandNotInitializedError: You seem to be calling `API client` on a page in an uninitialized `Stagehand` object. Ensure you are running `await stagehand.init()` on the Stagehand object before referencing the `page` object.
    at Stagehand3.<anonymous> (/path/to/node_modules/@browserbasehq/stagehand/dist/index.js:22685:19)
    at Generator.next (<anonymous>)
    at /path/to/node_modules/@browserbasehq/stagehand/dist/index.js:79:61
    at new Promise (<anonymous>)
    at __async (/path/to/node_modules/@browserbasehq/stagehand/dist/index.js:63:10)
    at Object.execute (/path/to/node_modules/@browserbasehq/stagehand/dist/index.js:22676:42)

Works fine on local however

import { Stagehand } from "@browserbasehq/stagehand";

(async () => {
  
  // Same example without browserbase workds fine on local browser
  const BROWSERBASE_ENV = "LOCAL";

  const stagehand = new Stagehand({
    env: BROWSERBASE_ENV,
    logger: console.log,
    disablePino: true,
  });

  // This works fine
  await stagehand.init();
  await stagehand.page.goto("https://www.google.com/");

  // Agent creation works
  const agent = stagehand.agent({
    provider: "anthropic",
    model: "claude-3-7-sonnet-latest",
    instructions: "Search for robinhood stock price and return it",
    options: {
      apiKey: ANTHROPIC_API_KEY,
    },
  });

  await agent.execute({
    instruction: "Search for robinhood stock price and return it",
  });

  await stagehand.close();
})();

Environment Information

  • Stagehand Version: 2.4.2
  • Node.js Version: v24.2.0
  • Platform: macOS (darwin 24.5.0)
  • Package Manager: pnpm

Analysis

The issue appears to be that the agent object is not properly inheriting the initialized state from the parent Stagehand instance. While stagehand.init() initializes the main Stagehand object (as evidenced by stagehand.page.goto() working), the agent seems to be trying to access an uninitialized API client internally.

Impact

This bug makes the agent functionality completely unusable when following the official documentation, which is likely to affect many users trying to implement web AI agents with Stagehand.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions