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 omitmodel, 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
- TypeScript
- Python
- Go
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 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.Switching models
With Model Gateway, switching between providers is a config change: no new accounts, API keys, or code rewiring required.- OpenAI
- Anthropic
- Google
- TypeScript
- Python
- Go
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
cachecosts you nothing extra to set up.
Supported providers
Configuration setup
Quick start
Get started with Google Gemini (recommended for speed and cost):- TypeScript
- Python
- Go
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.- Google
- Anthropic
- OpenAI
- Groq
- Cerebras
- TypeScript
- Python
- Go
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.Amazon Bedrock
Amazon Bedrock
Install dependencies
- TypeScript
- Python
- Go
Write the generate callback
- TypeScript
- Python
- Go
Pass the callback to Stagehand
- TypeScript
- Python
- Go
OpenAI-compatible SDKs
OpenAI-compatible SDKs
Install dependencies
- TypeScript
- Python
- Go
Write the generate callback
- TypeScript
- Python
- Go
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.Pass the callback to Stagehand
- TypeScript
- Python
- Go
All providers
All providers
Install dependencies
Write the generate callback
- TypeScript
- Python
- Go
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.Pass the callback to Stagehand
- TypeScript
- Python
- Go
Choose a model
Different models excel at different tasks. Consider speed, accuracy, and cost for your use case.Model selection guide
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:- TypeScript
- Python
- Go
Custom headers
Some enterprise gateways require extra headers on every model request. Attach them to the model configuration:- TypeScript
- Python
- Go
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:- TypeScript
- Python
- Go
Troubleshooting
Error: an LLM was not configured during Stagehand initialization
Error: an LLM was not configured during Stagehand initialization
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
modelwith its own providerapiKey, 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 session
Error: model inference requires a provider API key or a Browserbase session
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
apiKeyto 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 sequences
Error: Browserbase Model Gateway does not support stop sequences
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
modelwith its own providerapiKeyso the call goes straight to the provider - Or drop
stopSequencesand constrain the output with anextract()schema instead
Error: API key not found
Error: API key not found
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
modelentirely and pass only your Browserbase key - To pin a Gateway model, provide its name but omit the model provider API key
Error: model not supported
Error: model not supported
Unsupported modelSolutions:- Use the
provider/modelformat: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
400.Model doesn't support structured outputs
Model doesn't support structured outputs
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
High costs or slow performance
High costs or slow performance
- 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 SDK or custom models
Python SDK or custom models
- 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

