## TL;DR

In most cases you set the env var and never pass a key in code. The quickstarts use this so you can try everything without collecting keys.

## Steps

1. Leaked provider keys are the most expensive AI SDK mistake. The wiring is simple once you see it.

2. Each provider reads its standard env var automatically: OPENAI_API_KEY for @ai-sdk/openai, ANTHROPIC_API_KEY for @ai-sdk/anthropic, and so on. In most cases you set the env var and never pass a key in code.
2. The Vercel AI Gateway needs AI_GATEWAY_API_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.
3. 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.AI_GATEWAY_API_KEY }).
4. 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.
5. 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.
6. 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.
7. For local dev, .env.local (Next.js) or .env plus dotenv (Node scripts). Never commit these files.

## When to use

You are seeing this: Leaked provider keys are the most expensive AI SDK mistake. Use this skill when you run into "Provider API keys: env wiring, gateway, and keeping keys off the client".

## When not to use

If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.

## Versions

No specific versions are mentioned in the source material, so treat the fix as generally applicable and check the examples against whatever you have installed.

## Why this happens

The original report does not dig into a root cause. It documents the symptom and the fix that resolved it.
