The OpenAI SDK will refuse to run in a browser unless you pass
dangerouslyAllowBrowser: true. This is intentional — shipping your OpenAI
API key in a client bundle exposes it to anyone who opens devtools.
- Local development.
- Internal-only tools behind authentication.
- Demos and prototypes.
- Public-facing apps.
- Any environment where untrusted users can inspect the bundle.
Proxy requests through your own backend:
- Backend endpoint that accepts
{ messages, tools }from the client. - Backend injects
OPENAI_API_KEYfrom environment and calls OpenAI. - Backend returns the response to the client.
- Client implements
LlmProviderto call your backend instead of OpenAI directly.
const proxyProvider: LlmProvider = {
async chat({ messages, tools }) {
const res = await fetch("/api/llm", {
method: "POST",
body: JSON.stringify({ messages, tools }),
});
return res.json();
},
};