It looks like you're running in a browser-like environment
The AI SDK refused to run in the browser, because your secret API key would be visible to anyone who opens the page.
Also appears as
- AnthropicError: It looks like you're running in a browser-like environment. This is disabled by default, as it risks exposing your secret API credentials to attackers. If you understand the risks and have appropriate mitigations in place, you can set the `dangerouslyAllowBrowser` option to `true`, e.g., new Anthropic({ apiKey, dangerouslyAllowBrowser: true });
Prompt to fix it
My app shows "It looks like you're running in a browser-like environment" from the OpenAI or Anthropic SDK. The SDK and my secret API key are in frontend code. Move every AI call into a server route that reads the key from a server-only environment variable (no VITE_ or NEXT_PUBLIC_ prefix), and have the frontend call that route with fetch. Remove the SDK from client code, require a signed-in user on the route if the app has accounts, and tell me whether the key was ever exposed so I can rotate it. Don’t set dangerouslyAllowBrowser: true.
Paste into Layerwise or any AI coding tool.
What it means
The OpenAI and Anthropic SDKs check where they’re running. When they detect a browser, they stop with this error before sending anything, because the API key you passed would ship inside your JavaScript. Anyone can open DevTools, copy the key, and run up charges on your account.
AI coding tools often put the API call inside a React component, next to the button that triggers it. The fix is to move the call to the server: a small route, such as a Next.js Route Handler, a Supabase Edge Function, a Cloudflare Worker, or an Express endpoint, holds the key and calls the model, and the page calls that route.
The message suggests dangerouslyAllowBrowser: true. That turns off the check and leaves the key exposed, so keep it for local experiments or apps where each person enters their own key. Also check variable names: anything prefixed VITE_ or NEXT_PUBLIC_ is built into the browser code, so a secret key stored there is already public.
Common causes
- The AI SDK is imported and called inside a React component or other browser code.
- The project has no backend yet, so the generated code calls the AI API straight from the page.
- In Next.js, the file that calls the SDK is a client component marked
"use client", or is imported by one. - The key was put in a
VITE_orNEXT_PUBLIC_variable so the browser could read it, which also publishes it.
How to fix it
- Create a server route that calls the AI SDK, such as
app/api/chat/route.tsin Next.js or a Supabase Edge Function. - Read the key there from a server-only variable like
OPENAI_API_KEY, with noVITE_orNEXT_PUBLIC_prefix. - Change the component to
fetchyour route, and remove the SDK import from browser code. - Require a signed-in user on the route and add a rate limit, so strangers can’t use it to spend your credit.
- If the key was ever in browser code or a public repository, revoke it and create a new one in the provider’s dashboard.
Example fix
// app/api/chat/route.ts runs on the server, so the key stays private.
import OpenAI from 'openai';
const openai = new OpenAI(); // Reads OPENAI_API_KEY.
export async function POST(request: Request) {
const { prompt } = await request.json();
const response = await openai.responses.create({
model: 'gpt-6-luna',
input: String(prompt)
});
return Response.json({ text: response.output_text });
}
// In the component, call the route instead of the SDK:
// const res = await fetch('/api/chat', { method: 'POST', body: JSON.stringify({ prompt }) });
// const { text } = await res.json();Related errors
All errorsIncorrect API key provided
The AI API rejected the key your code sent: it has a typo, was revoked, or isn’t the key you think the app is using.
You’re importing a component that needs useState
A Next.js Server Component used a browser-side React feature like useState, which only works in files marked as Client Components.
GH013: Push cannot contain secrets
GitHub found an API key or other secret in the commits you tried to push and blocked the whole push so the secret wouldn’t land in the repository.
Blocked by CORS policy
The browser hid a response from another site because that server never said your site is allowed to read it.
Build with an agent that checks its work
The Layerwise agent clicks through the live preview and reads the console, so it catches errors before you do.