Skip to content

apiKey in options is ignored when creating an agent #1084

@farhadatwindmill

Description

@farhadatwindmill

Before submitting an issue, please:

Environment Information

Please provide the following information to help us reproduce and resolve your issue:

Stagehand:

  • Language/SDK: Typescript
  • Stagehand version: 2.5.0

AI Provider:

  • Provider: Anthropic
  • Model: claude-sonnet-4-20250514

Issue Description

I'm using this piece of code

  const agent = stagehand.agent({
    provider: "anthropic",
    model: "claude-sonnet-4-20250514",
    instructions: "You are a helpful assistant that can use a web browser.",
    options: {
      apiKey: ANTHROPIC_API_KEY,
    },
  });

And i have this error:

StagehandError: API key not found for `anthropic` provider. Please set the ANTHROPIC_API_KEY environment variable or pass an apiKey in the options object.

I do not have the ANTHROPIC_API_KEY env var set as I do not want to set it, and it seems the apiKey I set in options is ignored.

It seems here is the faulty part in lib/index.ts:

        if (this.usingAPI) {
          if (!this.apiClient) {
            throw new StagehandNotInitializedError("API client");
          }

          if (!options.options) {
            options.options = {};
          }

          // HERE apiKey is overwritten even if initially set
          if (options.provider === "anthropic") {
            options.options.apiKey = process.env.ANTHROPIC_API_KEY;
          } else if (options.provider === "openai") {
            options.options.apiKey = process.env.OPENAI_API_KEY;
          } else if (options.provider === "google") {
            options.options.apiKey = process.env.GOOGLE_API_KEY;
          }

          if (!options.options.apiKey) {
            throw new StagehandError(
              `API key not found for \`${options.provider}\` provider. Please set the ${options.provider === "anthropic" ? "ANTHROPIC_API_KEY" : "OPENAI_API_KEY"} environment variable or pass an apiKey in the options object.`,
            );
          }

          return await this.apiClient.agentExecute(options, executeOptions);
        }

Steps to Reproduce

  1. Do not set env var
  2. Try the code above
  3. See error

Minimal Reproduction Code

// there are multiple modes to add as header: //nobundling //native //npm //nodejs
// https://www.windmill.dev/docs/getting_started/scripts_quickstart/typescript#modes
//nodejs

// import { toWords } from "number-to-words@1"
import * as wmill from "windmill-client"
import { z } from "zod";
import { Stagehand } from "@browserbasehq/stagehand";
import { Browserbase } from "@browserbasehq/sdk";


// fill the type, or use the +Resource type to get a type-safe reference to a resource
// type Postgresql = object


async function main(url: string, instructions: string, sessionId?: string) {
  const BROWSERBASE_API_KEY = [...]
  const BROWSERBASE_PROJECT_ID = [...]
  const ANTHROPIC_API_KEY = [...]
  const OPENAI_API_KEY = [...]

  const bb = new Browserbase({
    apiKey: BROWSERBASE_API_KEY,
  });

  let session = null;
  if (!sessionId) {
    session = await bb.sessions.create({
      keepAlive: true,
      projectId: BROWSERBASE_PROJECT_ID,
    });
  }

  const actualSessionId = session ? session.id : sessionId;

  // Initialize Stagehand
  const stagehand = new Stagehand({
    env: "BROWSERBASE",
    apiKey: BROWSERBASE_API_KEY,
    projectId: BROWSERBASE_PROJECT_ID,
    modelName: "claude-sonnet-4-20250514",
    modelClientOptions: {
      apiKey: ANTHROPIC_API_KEY,
    },
    browserbaseSessionID: actualSessionId
  });
  await stagehand.init();
  const page = stagehand.page;
  await page.goto(url);

  const agent = stagehand.agent({
    provider: "anthropic",
    model: "claude-sonnet-4-20250514",
    instructions: "You are a helpful assistant that can use a web browser.",
    options: {
      apiKey: ANTHROPIC_API_KEY,
    },
  });

  await agent.execute(instructions);

  await stagehand.close();
  return 0
}

Error Messages / Log trace

StagehandError: API key not found for `anthropic` provider. Please set the ANTHROPIC_API_KEY environment variable or pass an apiKey in the options object.

Screenshots / Videos

[Attach screenshots or videos here]

Related Issues

Are there any related issues or PRs?

  • Related to: #[issue number]
  • Duplicate of: #[issue number]
  • Blocks: #[issue number]

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