How to Use DGrid AI Gateway with the Vercel AI SDK
Summary
This guide details how to integrate the Vercel AI SDK with the DGrid AI Gateway to build AI-powered applications. It outlines a step-by-step process for setting up a minimal Next.js project, configuring the DGrid provider, and implementing both simple text generation and streaming chat functionalities. The workflow was validated using DGrid's base URL `https://api.dgrid.ai/v1`, the `openai/gpt-5.4` model format, the Vercel AI SDK with `@ai-sdk/openai-compatible`, and the Next.js App Router. Key steps include installing `ai`, `@ai-sdk/openai-compatible`, and `@ai-sdk/react` packages, setting the `DGRID_API_KEY` environment variable, and creating a DGrid provider using `createOpenAICompatible` with the correct `baseURL` and provider-prefixed model IDs.
Key takeaway
For AI Engineers building Next.js applications with the Vercel AI SDK who need to route model traffic through DGrid AI Gateway, ensure you configure `createOpenAICompatible` with `baseURL: 'https://api.dgrid.ai/v1'` and use DGrid's provider-prefixed model IDs like `openai/gpt-5.4`. This setup enables both non-streaming and streaming chat functionalities, streamlining your model integration and deployment to platforms like Vercel.
Key insights
Integrate Vercel AI SDK with DGrid AI Gateway for AI applications using an OpenAI-compatible interface.
Principles
- Use provider-prefixed model naming.
- Secure API keys via environment variables.
Method
Configure `createOpenAICompatible` with DGrid's base URL and API key. Implement `generateText` for single completions and `streamText` with `convertToModelMessages` for streaming chat, connecting to a React UI via `useChat`.
In practice
- Use `openai/gpt-5.4` for DGrid OpenAI models.
- Add `HTTP-Referer` and `X-Title` headers for app identification.
- Restart dev server after `.env.local` changes.
Topics
- Vercel AI SDK
- DGrid AI Gateway
- OpenAI Compatible API
- Streaming Chat
- Next.js Development
Best for: AI Engineer, Software Engineer, AI Chatbot Developer
Related on AIssential
Editorial summary, takeaway, and curation by AIssential. Original article published by LLM on Medium.