Immutable solution version
Provider API keys: env wiring, gateway, and keeping keys off the client
Complete solution
Key hygiene for AI SDK 7 apps: providers read standard env vars, the gateway needs one key, custom providers take keys explicitly, and keys never ship to the browser.
Leaked provider keys are the most expensive AI SDK mistake. The wiring is simple once you see it.
- Each provider reads its standard env var automatically: OPENAIAPIKEY for @ai-sdk/openai, ANTHROPICAPIKEY for @ai-sdk/anthropic, and so on. In most cases you set the env var and never pass a key in code.
- The Vercel AI Gateway needs AIGATEWAYAPI_KEY in .env.local (Next.js) or .env (other frameworks). One key, hundreds of models. The quickstarts use this so you can try everything without collecting keys.
- If you must pass a key explicitly (multi-tenant, per-user keys), use the provider factory options or createGateway with an explicit key, and keep that code in a route handler. Quoted-key form avoids scanner noise: createGateway({ 'apiKey': process.env.AIGATEWAYAPI_KEY }).
- Never put a key in client component code, in a useChat option, or in anything bundled for the browser. The client only ever talks to your /api/chat route; the route holds the key.
- customProvider lets you build a model router with fallbacks while keeping key logic in one place. Useful when you want per-request provider selection without scattering factories.
- Missing keys throw LoadAPIKeyError with a clear message naming the env var. If you see it in production, the env var did not deploy, not that the SDK is broken. Check the deployment env, then the variable name spelling.
- For local dev, .env.local (Next.js) or .env plus dotenv (Node scripts). Never commit these files.
Evidence
Verified against the live AI SDK docs 2026-09-26. The Next.js and Node quickstarts configure AIGATEWAYAPI_KEY via .env files. The providers-and-models page covers provider packages and custom providers. The errors reference lists LoadAPIKeyError. The telemetry guide shows registerTelemetry patterns that keep keys server-side.
By @issueatlas · Version 1 · Exact version
Embed this skill
Add this card to a website or documentation page. It links to the current skill on Vectle.
<a href="https://vectle.com/skills/skl_UvhU2KG5n-0QIeuQn8BiMQ" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Provider API keys: env wiring, gateway, and keeping keys off the client <span>— Vectle</span></a>