1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
17 small wins to finish your pathNext lesson →
Providers, models and API keys
A provider package turns a model name into a model object. It reads the provider's API key from an environment variable when the model is called.
import { generateText } from "ai";
import { openai } from "@ai-sdk/openai";
const model = openai("gpt-4.1-mini");
console.log(model.provider, model.modelId);
try {
await generateText({ model, prompt: "Say hello." });
} catch (error) {
console.log(error.name);
console.log(error.message);
}npx tsx keys.tsopenai("gpt-4.1-mini") creates the model without contacting OpenAI. The key is loaded when generateText calls it, and without OPENAI_API_KEY the call fails with AI_LoadAPIKeyError before any request is sent. openai.responses shows this provider uses OpenAI's Responses API.
| Package | Model | Key |
|---|---|---|
@ai-sdk/openai | openai("gpt-4.1-mini") | OPENAI_API_KEY |
@ai-sdk/anthropic | anthropic("claude-sonnet-4-5") | ANTHROPIC_API_KEY |
@ai-sdk/google | google("gemini-2.5-flash") | GOOGLE_GENERATIVE_AI_API_KEY |
none: ai itself | "openai/gpt-4.1-mini", a string | AI_GATEWAY_API_KEY, Vercel's AI Gateway |
A plain string as model goes through Vercel's AI Gateway, one key for many providers. The course never sends a request there; the stand-in model from lesson 4 goes where these would.
Try it yourself
- Pass
apiKey: "sk-test"withcreateOpenAIfrom@ai-sdk/openai. What error do you get then, and why is that one slower? - Print
model.specificationVersion. - Read which environment variable
@ai-sdk/anthropicuses in its docs.
Slow is fine. Stopping is the only problem.