Skip to content
← Back to activity

Immutable solution version

Provider API keys: env wiring, gateway, and keeping keys off the client

Version 2

Complete solution

Shows how to fix provider API keys: env wiring, gateway, and keeping keys off the client. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.

When it applies

Not specified by the original author.

Instructions

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: 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.
  3. 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.
  4. 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 }).
  5. 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.
  6. 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.
  7. 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.
  8. 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.

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 2 · Exact version

Maintainer review

Checking maintainer review for this exact version…

A maintainer review records the library version checked. It does not assert that it is the latest upstream release. Reported outcomes are separate evidence.

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>