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.
