Vercel AI SDK with Any Model Provider: Setup + Streaming Guide

Vercel AI SDK with Any Model Provider: Setup + Streaming Guide

The Vercel AI SDK's OpenAI provider works with any OpenAI-compatible endpoint. Here's how to wire it to AICredits for INR billing and access to every major model in your Next.js app.

Author

AICredits Team

Published

9 Jul 2026

Reading time

6 min read

The default setup assumes a direct OpenAI account

The Vercel AI SDK ships with @ai-sdk/openai configured for OpenAI's own endpoint by default — which means USD billing and an international card if you're building from India. Because the SDK's OpenAI provider accepts a custom baseURL, you can point it at AICredits instead and keep the exact same useChat, streamText, and generateObject calls you'd already written.

Install and configure

npm install ai @ai-sdk/openai
// lib/ai.ts
import { createOpenAI } from "@ai-sdk/openai";
 
export const aicredits = createOpenAI({
  baseURL: "https://api.aicredits.in/v1",
  apiKey: process.env.AICREDITS_API_KEY,
});

Streaming a chat response in a route handler

// app/api/chat/route.ts
import { streamText } from "ai";
import { aicredits } from "@/lib/ai";
 
export async function POST(req: Request) {
  const { messages } = await req.json();
 
  const result = streamText({
    model: aicredits("anthropic/claude-3-5-sonnet-20241022"),
    messages,
  });
 
  return result.toDataStreamResponse();
}

The client-side useChat hook from ai/react works unchanged — it consumes the streamed response the same way regardless of which provider is behind aicredits(...).

Switching models per request

Because the model ID is just a string argument, you can let users pick a model at runtime without changing your route logic:

const MODEL_MAP: Record<string, string> = {
  fast: "google/gemini-2.0-flash",
  balanced: "openai/gpt-4o-mini",
  strongest: "anthropic/claude-3-5-sonnet-20241022",
};
 
const result = streamText({
  model: aicredits(MODEL_MAP[tier] ?? MODEL_MAP.balanced),
  messages,
});

Structured output with generateObject

import { generateObject } from "ai";
import { z } from "zod";
import { aicredits } from "@/lib/ai";
 
const { object } = await generateObject({
  model: aicredits("openai/gpt-4o-mini"),
  schema: z.object({
    sentiment: z.enum(["positive", "neutral", "negative"]),
    summary: z.string(),
  }),
  prompt: "Analyse this customer review: 'Delivery was late but the product quality is excellent.'",
});

generateObject relies on the provider's JSON/schema support — this works reliably on GPT-4o and Claude models through AICredits, and response healing on the backend repairs the occasional malformed JSON before it reaches your schema validator.

Cost per streamed response

A typical chat message — 300 input tokens, 200 output tokens — costs well under ₹0.10 on Gemini 2.0 Flash or GPT-4o-mini, and roughly ₹0.30 on Claude 3.5 Sonnet. For a Next.js app with moderate traffic, set a per-key budget cap in the AICredits dashboard so a misconfigured retry in your route handler can't compound into an unexpected bill.

Frequently Asked Questions

Does this work with the Vercel AI SDK's edge runtime?

Yes — createOpenAI with a custom baseURL works identically in Node.js and edge runtimes, since it's a standard fetch-based HTTP client under the hood.

Can I use non-OpenAI models like Gemini or Claude through the OpenAI provider?

Yes — because AICredits exposes an OpenAI-compatible endpoint, any supported model ID (google/gemini-2.0-flash, anthropic/claude-3-5-sonnet-20241022) works through @ai-sdk/openai's provider, regardless of the model's actual origin.

Do tool calling and function calling work through this setup?

Yes — the AI SDK's tools parameter in streamText and generateText maps to the same function-calling format AICredits forwards to the underlying provider.

Get started

AICredits

Build with any AI model. Pay in INR.

One OpenAI-compatible API for every major model, with a prepaid rupee wallet. Top up via UPI — no international credit card, no forex surprises.

Top up via UPI · Prepaid INR wallet · No international card needed

Related Articles

Continue in Docs

Need implementation commands and endpoint details? Go to quickstart or API reference.