FLUX 3 and Gemini 3.7 Flash are now live on CometAPI โ†’
Frameworks, agents and developer tools

Vercel AI SDK + CometAPI: Build a Multi-Model Chat Route

Create one streaming chat endpoint that can switch model IDs without replacing the application integration.

Connected code, cloud and messaging modules forming an AI integration workflow
CA
CometAPI Research
AI model and API engineering
August 6, 2026 7 min read

Key takeaways

Keep the CometAPI key on the server and expose only an approved model alias to the client.
Use one provider configuration and change the model ID per workload.
Stream responses for better perceived latency.
Add request budgets, validation and fallback before production launch.

Install and configure the provider

The Vercel AI SDK can use an OpenAI-compatible provider. Configure the CometAPI base URL once and keep the API key in a server-side environment variable.

import { createOpenAI } from '@ai-sdk/openai';

export const cometapi = createOpenAI({
  apiKey: process.env.COMETAPI_KEY,
  baseURL: 'https://api.cometapi.com/v1',
});

Create a streaming chat route

Choose the model on the server from an allowlist. This prevents clients from selecting an unexpected expensive model while preserving multi-model flexibility.

import { streamText } from 'ai';
import { cometapi } from '@/lib/cometapi';

const allowedModels = {
  fast: 'your-fast-model-id',
  quality: 'your-quality-model-id',
};

export async function POST(request: Request) {
  const { messages, route = 'fast' } = await request.json();
  const modelId = allowedModels[route as keyof typeof allowedModels];

  const result = streamText({
    model: cometapi(modelId),
    messages,
  });

  return result.toDataStreamResponse();
}

Switch models by workload

Use the faster route for conversational turns and the quality route for complex analysis, long code changes or high-value review. Keep routing logic observable so cost and quality can be compared later.

  • Route by explicit product mode before attempting automatic classification.
  • Record selected route, model ID, tokens, latency and request result.
  • Apply account and request-level spending limits.

Production checklist

Before launch, add schema validation, timeout handling, retries for transient failures and a compatible fallback route. Never expose the upstream API key in browser code.

Frequently asked questions

Can the Vercel AI SDK use an OpenAI-compatible API?

Yes. Configure a compatible provider with the alternative base URL and API key, then provide the model ID when creating the request.

Should users choose any model ID from the browser?

No. Production applications should map client-facing route names to a server-side allowlist so cost, capability and security remain controlled.

Continue with Integrations & Workflows
Return to the section overview and future articles.
View section