Topic 2 of 4 · 4 min read
Provider-neutral AI SDK abstractions
Goal: Port a feature to a provider-neutral SDK and list the provider-specific features and settings it loses on the way.
A provider-neutral SDK lets you write one call and swap the model behind it, but every abstraction leaks: provider-specific features either disappear or need escape hatches. Porting is mostly easy. The risk is in what you silently lose.
Check yourself · 1 of 3
You call a server that is "OpenAI-compatible" using the official OpenAI client with a changed base URL. Which risk is real?
Why it matters
Providers differ in request shape, streaming events, tool-call format and error types. A neutral layer hides that. The Vercel AI SDK (TypeScript) gives you generateText and streamText with a model object. LiteLLM (Python, or as a proxy) gives you an OpenAI-style call for many providers. Many hosted and self-hosted servers also offer an OpenAI-compatible endpoint, so the official OpenAI client works if you change the base URL.
How to port a feature
- List what the feature uses. Plain text in and out is easy. Tool calls, structured output, image input, prompt caching, extended thinking and citations are where providers differ.
- Swap the call. Replace the provider client with the neutral call and a model identifier.
- Diff the behaviour. Run the same test prompts on the old and new path. Compare output format, token counts, latency and errors.
- Write down the losses. Anything the old code set that the new call cannot pass is a loss, or needs a provider-specific option.
Where provider features leak
- Caching controls. Anthropic prompt caching is enabled with markers on content blocks. A neutral call may pass them only through a provider options field, or drop them.
- Reasoning settings. Thinking budgets and effort levels have different names and ranges per provider.
- Tool and schema support. One provider may enforce a JSON schema strictly; another only follows it as a hint.
- Errors and limits. Rate-limit and context-length errors arrive in different shapes, so your retry logic may stop matching.
- New features. A feature released yesterday is in the vendor SDK first and in the neutral layer later.
Before and after
// Before: tied to one vendor SDK
const res = await anthropic.messages.create({
model: "claude-sonnet-4-5",
max_tokens: 500,
messages: [{ role: "user", content: text }],
});
const out = res.content[0].text;
// After: AI SDK, model is a swappable value
import { generateText } from "ai";
import { anthropic } from "@ai-sdk/anthropic";
const { text: out } = await generateText({
model: anthropic("claude-sonnet-4-5"),
maxOutputTokens: 500,
prompt: text,
});
The ported call is shorter and the model is one line to change. But if the old code also sent a cache marker on a long system prompt, check that the new call still does. If it does not, cost rises and nothing errors.
What goes wrong
- Assuming "compatible" means identical. An OpenAI-compatible endpoint may ignore parameters it does not support, without an error.
- Porting without a regression set. Output differences between models show up in production.
- Using the lowest common denominator everywhere. You give up a feature that was the reason you chose the provider.
Examples
The cache marker that vanished
A summariser sends a long system prompt with a cache marker, using the vendor SDK. After the move to a neutral call the marker is not passed. Answers look the same, but input cost per request rises. The fix is to pass the marker through the SDK's provider options, and to add a cost check to the regression run.
Same code, three models
With the AI SDK, a comparison script loops over three model objects, calls generateText with one prompt and prints text, token usage and latency for each. No provider-specific client code is needed, which is the main benefit of the abstraction.
Key points
- A neutral SDK turns the provider into a swappable value, which makes switching and comparing models cheap.
- Plain text calls port easily; tools, structured output, caching, reasoning and images are where providers differ.
- An OpenAI-compatible endpoint may silently ignore parameters it does not support.
- Port with a regression set of real prompts, and write down every setting the new call cannot pass.
- New provider features usually reach the vendor SDK before the neutral layer.
Common mistakes
- Treating an OpenAI-compatible endpoint as identical to the real OpenAI API.
- Porting without running old and new paths on the same test prompts.
- Dropping provider-specific settings such as cache markers or reasoning budgets without noticing.
- Reducing every feature to the lowest common denominator and losing the reason the provider was chosen.
Check yourself · 2 of 3
Which feature is most likely to need a provider-specific option after porting to a neutral SDK?
Check yourself · 3 of 3
After porting a feature, outputs look the same on three test prompts. What is the best next check?
Practice items, not counted towards your mastery.
Start here
- LiteLLM documentation (opens in a new tab)DocsFree
One interface and proxy server for many providers, with keys, budgets and logging.
- OpenRouter quickstart (opens in a new tab)DocsFree
Calling many models through one OpenAI-compatible endpoint with one key.
- AI SDK: providers and models (opens in a new tab)DocsFree
How a provider-neutral SDK swaps models behind one function call.