How Do I Migrate Off the Raw OpenAI SDK?
You can migrate in two independent stages. First, point the existing OpenAI client at AI Gateway. That change is production-ready on its own.
Later, migrate individual calls to the AI SDK when its typed Gateway options and metadata access are useful to your application.
Outcome
Run the same feature with the OpenAI SDK and the AI SDK, then compare the request code and available metadata.
Fast Track
- Start with lesson 1.2's
scripts/existing-stack.ts: base URL, key, and model prefix - Write the AI SDK twin:
scripts/migrated-stack.ts - Run both; compare the same feature behavior and what the code around each answer looks like
Hands-on exercise
Lesson 1.2 configured the existing client with baseURL, apiKey, and creator-prefixed model names. That version can remain in production.
Now build the AI SDK version beside it:
- A
scripts/migrated-stack.tsthat does exactly whatexisting-stack.tsdoes, in the AI SDK - Same two models, same order prompt
- Print the request cost from
providerMetadata.gateway.cost
Put the files side by side and compare them. Both versions have a similarly short basic request path. The AI SDK becomes more useful when you add Gateway features: fallbacks and other options are first-class typed fields under providerOptions.gateway, and response metadata is directly available.
Try It
pnpm existing-stack && pnpm migrated-stack[openai/gpt-5.4-mini via OpenAI SDK]
Order up: two Al Pastor Meteors, one Agua Fresca. That'll be $12.00.
[openai/gpt-5.4-mini via AI SDK]
Order up: two Al Pastor Meteors, one Agua Fresca. That'll be $12.00.
Cost: $0.0000714
Both scripts perform the same feature. The AI SDK version also exposes the Gateway cost metadata directly.
Two issues you may encounter:
Your codebase wraps the OpenAI SDK in a service layer. A service wrapper makes incremental migration straightforward. Update one method at a time without changing its callers.
You use an OpenAI feature and aren't sure it survives the move. Streaming, tool calls, structured outputs, and embeddings all pass through the compat endpoint, and all have AI SDK equivalents. Check the compatibility documentation for that feature before migrating it.
Commit
git commit -m "feat(migration): add AI SDK twin of the legacy order flow"Done-When
- Both scripts produce the same feature behavior
- You can name the two things move two buys (native gateway options, metadata access) without expecting separate model calls to produce identical wording
- You know which of your own code moves first (the wrapper insides, if you have one)
Solution
import { generateText } from "ai";
const order =
"Take this order and confirm it back with a total: two Al Pastor Meteors " +
"($4.50 each) and one agua fresca ($3.00).";
for (const model of ["openai/gpt-5.4-mini", "anthropic/claude-sonnet-4.6"]) {
const result = await generateText({ model, prompt: order });
const cost = result.finalStep.providerMetadata?.gateway?.cost;
console.log(`[${model} via AI SDK]`);
console.log(result.text.trim());
console.log(`Cost: $${cost}`);
console.log();
}Compared with existing-stack.ts, this version removes client construction and reads Gateway metadata from the result. Migrate the remaining calls when those options improve the application.
Related Questions
Was this helpful?