Vercel AI SDKAI SDK 7 · TypeScript · Node.js 20+
0%
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.

Examplekeys.ts
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);
}
Example
npx tsx keys.ts

openai("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.

PackageModelKey
@ai-sdk/openaiopenai("gpt-4.1-mini")OPENAI_API_KEY
@ai-sdk/anthropicanthropic("claude-sonnet-4-5")ANTHROPIC_API_KEY
@ai-sdk/googlegoogle("gemini-2.5-flash")GOOGLE_GENERATIVE_AI_API_KEY
none: ai itself"openai/gpt-4.1-mini", a stringAI_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" with createOpenAI from @ai-sdk/openai. What error do you get then, and why is that one slower?
  • Print model.specificationVersion.
  • Read which environment variable @ai-sdk/anthropic uses in its docs.

Slow is fine. Stopping is the only problem.