## TL;DR

The message names the variable it wanted. import { openai } from '@ai-sdk/openai'; // throws LoadAPIKeyError at call time if OPENAI_API_KEY is unset const { text } = await generateText({ model: openai('gpt-5'), prompt: 'Hi' }); What to know: 1.

## Steps

1. LoadAPIKeyError means the provider looked for its key in the environment and found nothing. The message names the variable it wanted.

2. import { openai } from '@ai-sdk/openai';

3. // throws LoadAPIKeyError at call time if OPENAI_API_KEY is unset
const { text } = await generateText({ model: openai('gpt-5'), prompt: 'Hi' });

4. What to know:
1. It throws when you make the call, not when you import or construct the provider. A file that imports fine can still fail at runtime.
2. Read the message: it tells you the exact env var name. Copy it character for character into your env config; OPENAI_API_KEY vs OPEN_AI_API_KEY typos are the classic.
3. In Next.js, server-only env vars must not have the NEXT_PUBLIC_ prefix, and they must be set in the deployment environment, not just .env.local. Vercel preview vs production env mismatch is the top cause.
4. In edge runtime the same env vars are available; a LoadAPIKeyError on edge means the variable is genuinely unset, not an edge limitation.
5. If you pass keys explicitly (multi-tenant), a LoadAPIKeyError means your key-passing code did not run, e.g. the factory was constructed without the apiKey option.
6. Never "fix" this by hardcoding the key in source. Put it in the environment and keep the file out of version control.

## When to use

You are seeing this: LoadAPIKeyError means the provider looked for its key in the environment and found nothing. Use this skill when you run into "LoadAPIKeyError: the provider key env var is missing".

## 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.
