Skip to main content
New: automatic model selection. Omit model and let the Model Gateway pick one for every act, extract, and observe call. One key, one bill, no provider accounts needed.

Model Gateway

Model Gateway lets you use Stagehand without wiring up model providers yourself. When you omit model, Browserbase automatically selects a model for each act, extract, and observe call. You can still provide a model explicitly when you want to pin one.

Setup

When no model is configured, Stagehand routes inference through Model Gateway without a model field and Browserbase selects one automatically. Selection happens server-side on every call, so your code never pins a model name and picks up new models as Browserbase adds them. What you pass decides where a call goes:
Model Gateway requires Browserbase-hosted browsers. It does not work with local browsers, because those have no Browserbase session to bill and authorize against.
Omitting model from Stagehand.create() enables routing for that Stagehand instance. Selection itself happens per call, so one run can use different models at different steps. Per-call overrides enable selecting a specific model instead of routing it.
Model Gateway rejects stopSequences. Pin a model with its own provider API key when you need them.

Switching models

With Model Gateway, switching between providers is a config change: no new accounts, API keys, or code rewiring required.

Key benefits

  • One key, one bill: LLM inference, browser infrastructure, and caching all run through your Browserbase API key.
  • Market-price tokens: Browserbase charges the same price as going direct to the provider. No markup.
  • Built-in reliability: Browserbase handles retries, backoff, and rate limits.
  • No tier-gating: Access new models immediately without hitting provider spend thresholds.
  • Action caching: Model Gateway works with Stagehand’s managed action caching, so repeated steps are reused instead of re-run. Both features run off the same Browserbase session, so turning on cache costs you nothing extra to set up.

Supported providers

Need a provider that isn’t listed? Reach out: Browserbase is happy to work with teams on additional model support.

Configuration setup

Quick start

Read your provider key from the environment and pass it on the model configuration. Stagehand does not read environment variables for you.
Get started with Google Gemini (recommended for speed and cost):
Model names for the providers below carry a provider/ prefix, and the provider must be one of the five. Stagehand ships a list of known model IDs per provider and validates the full name when you call Stagehand.create() or pass a per-call override, so a name it does not recognize fails before any request reaches the provider. Upgrade the SDK to pick up newly released models. The prefix is never optional: to reach an Azure OpenAI deployment, a self-hosted model, or anything else outside those five providers, use the bring-your-own-LLM callback.

First-class models

Use any model from the following supported providers.
Commonly used: google/gemini-3.1-pro-preview, google/gemini-3-flash-preview, google/gemini-3.5-flash, google/gemini-2.5-flash, google/gemini-flash-latest.View all supported Google models →

Custom models

Any provider Stagehand does not call natively is supported by bringing your own LLM. Instead of a model name, pass a function that Stagehand calls whenever it needs an inference. Your function runs in your process, on your machine, with your own SDKs and credentials: Amazon Bedrock, Cohere, Azure OpenAI, a self-hosted model, anything you can reach from code. Stagehand sends a provider-neutral request (messages, system prompt, temperature, and a response format) and expects a matching result back. When the response format is a JSON schema, return the parsed object in the structured content field.
1

Install dependencies

Install your provider’s SDK.
2

Write the generate callback

3

Pass the callback to Stagehand

1

Install dependencies

Install your provider’s SDK.
2

Write the generate callback

Reporting token usage is optional. To report it, add a usage field to the result and map your provider’s own usage field names onto Stagehand’s: input tokens, output tokens, and total tokens are required, while reasoning tokens and cached input tokens are optional. Providers spell these differently, so read the names off your provider’s response type rather than assuming they match Stagehand’s.
3

Pass the callback to Stagehand

The pattern is the same for every provider: implement one function, wire it in, done. Your callback never crosses the wire; Stagehand records only that the model lives on the client and calls back to you over the same connection.
1

Install dependencies

Install your provider’s SDK.
2

Write the generate callback

A message’s content is one content block or an array of them. Text blocks carry a string, and image blocks carry base64 data plus a MIME type. extract() with the screenshot option sends a viewport screenshot as an image block, so map image blocks onto your provider’s own image format. A callback that reads only the text blocks answers a visual extraction from the accessibility tree alone.
3

Pass the callback to Stagehand

The result is validated against the response format Stagehand asked for. If you return text when a JSON schema was requested, or structured content that does not match the schema, the call fails loudly rather than silently degrading.

Choose a model

Different models excel at different tasks. Consider speed, accuracy, and cost for your use case.

Model selection guide

Find detailed model comparisons and recommendations on the Stagehand model evaluation page.
Quick recommendations

Advanced options

Per-call model overrides

Every primitive accepts a model configuration for a single call, so you can run cheap inference by default and reach for a stronger model only where it matters:
Model configuration is deliberately excluded from the cache key, so switching models per call does not invalidate cached results.

Custom headers

Some enterprise gateways require extra headers on every model request. Attach them to the model configuration:
There is no base URL option. A model configuration always names one of the five supported providers, so Azure OpenAI deployments, self-hosted models, and any other custom endpoint go through the bring-your-own-LLM callback instead, where you own the client, the transport, and the credentials.

Extending your LLM client

For advanced use cases like custom retries or caching logic, wrap your generate callback. Because the callback is ordinary code in your process, you can layer whatever behavior you need around it:
Need result caching rather than request retries? Use the built-in caching feature, which skips the inference call entirely.

Troubleshooting

Error: An LLM was not configured during Stagehand initializationYou omitted model to get automatic routing, but the browser is not a Browserbase session, so there is no Model Gateway to route through. Automatic selection has no local fallback. Stagehand.create() still succeeds, because Stagehand resolves the model when a call needs one, so the first act(), extract(), or observe() raises this instead.Solutions:
  • Launch with browserbase.launch({ apiKey }) so the session has a Browserbase API key and session ID to authorize against
  • Or pass a model with its own provider apiKey, which works on any browser including local ones
  • Or supply a client-side LLM callback, which runs inference in your own process
Error: Model inference requires a provider API key or a Browserbase sessionYou pinned a model but gave it no apiKey, and the browser is not a Browserbase session. A model without a key is a Model Gateway request, and Gateway needs a Browserbase session to bill and authorize against. This is the usual result of copying a Gateway example onto a local browser.Solutions:
  • Add the provider apiKey to the model configuration to call the provider directly
  • Or launch with browserbase.launch({ apiKey }) to keep the call on Gateway
Error: Browserbase Model Gateway does not support stop sequencesstopSequences is not available on Gateway inference, whether Browserbase selected the model or you pinned one without a provider key.Solutions:
  • Pass a model with its own provider apiKey so the call goes straight to the provider
  • Or drop stopSequences and constrain the output with an extract() schema instead
Error: API key not foundSolutions:
  • Read the provider key from the environment and pass it on the model configuration; Stagehand never reads it for you
  • Confirm you are reading the variable name your provider expects
  • If you intended to use automatic Model Gateway routing, omit model entirely and pass only your Browserbase key
  • To pin a Gateway model, provide its name but omit the model provider API key
Error: Unsupported modelSolutions:
  • Use the provider/model format: openai/gpt-5. The prefix is required; bare model names are rejected
  • Use one of the five supported providers: openai, anthropic, google, groq, cerebras
  • Check the model ID against the lists on this page. Stagehand validates the whole name, so a typo fails at Stagehand.create() rather than on the first inference
  • Upgrade the SDK if the model shipped after your installed version
  • For a provider outside that list, use the bring-your-own-LLM callback
A model that passes validation can still fail at request time if your model API key cannot reach it. That surfaces as the provider’s own error, usually a 400.
Error: Model does not support structured outputsSolutions:
  • Every Stagehand primitive requests a JSON schema response, so the model must support structured outputs
  • Check the Stagehand model evaluation page for recommended models
Symptoms: Automation is expensive or slowSolutions:
  • Switch to cost-effective models (check evals for comparisons)
  • Use a fast model for simple tasks and reach for a stronger one per call with a model override
  • Implement caching for repeated patterns
Python is a first-class SDK in Stagehand v4 with the same surface as TypeScript.Solutions:
  • Use the language selector on this page to see every sample in Python
  • Pass an async callable as the model to bring your own LLM

Need help? Contact support

Can’t find a solution? Have a question? Reach out to the Browserbase support team:

Contact support

Email Browserbase at support@browserbase.com

Next steps

Prompting guide

Learn how to prompt LLMs for optimal results

Observability

Track token usage and inference latency per operation

Caching guide

Cache responses to reduce costs and improve speed

Optimize costs

Reduce LLM spending with caching and smart model selection