Knowledge Base Sections ▾

Navigation

▸ Start here By roles

Categories

Tools 37
Glossary 12

Tools

Vercel AI SDK + Gonka AI — AI applications in TypeScript for pennies

Vercel AI SDK is the most popular SDK for building AI applications in TypeScript and JavaScript. A unified generateText and streamText API, streaming to UI, native tool calling, and built-in helpers for Next.js App Router — everything you need for chatbots, agents, and RAG pipelines on the web.

The problem is the same as with any LLM application — the provider cost. A streaming chat interface sends the dialogue history with every message, and an agent cycles context through dozens of steps. At Anthropic ($3-15/1M) and OpenAI ($2.5-10/1M) prices, even a modest pet project in production turns into a bill for hundreds of dollars a month.

JoinGonka Gateway is an OpenAI-compatible endpoint over the decentralized Gonka network. Vercel AI SDK connects to it like any OpenAI-compatible provider — no forks, no custom adapters. The same open models as with commercial hosts — MiniMax M2.7, DeepSeek V4 Flash, and GLM-5.3 Flash — the same streamText, but at $0.0069/1M input tokens — tens of times cheaper.

Step 1: Get a key and connect the provider

JoinGonka API key: register at gate.joingonka.ai/register — we provide 3M free tokens to get you started. Create a key with the jg- prefix in the Dashboard.

Package installation. For a custom OpenAI-compatible endpoint, the Vercel AI SDK recommends the @ai-sdk/openai-compatible provider:

npm install ai @ai-sdk/openai-compatible

Minimum connection — create a provider instance via createOpenAICompatible and call generateText:

import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
import { generateText } from 'ai';

const gonka = createOpenAICompatible({
  name: 'gonka',
  baseURL: 'https://gate.joingonka.ai/v1',
  apiKey: process.env.GONKA_API_KEY, // jg-your-key
});

const { text } = await generateText({
  model: gonka('MiniMaxAI/MiniMax-M2.7'),
  prompt: 'Explain what a decentralized inference network is',
});

console.log(text);

The apiKey parameter automatically adds the Authorization: Bearer jg-your-key header — no separate configuration is needed. Store the key in an environment variable GONKA_API_KEY (e.g., in .env.local), not in the code.

Alternative — the @ai-sdk/openai package with the createOpenAI({ baseURL, apiKey }) factory. Both methods work; for non-OpenAI endpoints, the AI SDK documentation specifically recommends @ai-sdk/openai-compatible as it avoids unnecessary OpenAI-specific assumptions.

Step 2: Streaming and Next.js route handler

The main feature of the Vercel AI SDK is streaming responses. The streamText function starts streaming tokens immediately, and the toUIMessageStreamResponse() helper returns a ready-to-use stream directly from the route handler in Next.js App Router.

Server-side handler app/api/chat/route.ts:

import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
import { streamText, convertToModelMessages, type UIMessage } from 'ai';

const gonka = createOpenAICompatible({
  name: 'gonka',
  baseURL: 'https://gate.joingonka.ai/v1',
  apiKey: process.env.GONKA_API_KEY,
});

// allow streaming up to 30 seconds
export const maxDuration = 30;

export async function POST(req: Request) {
  const { messages }: { messages: UIMessage[] } = await req.json();

  const result = streamText({
    model: gonka('MiniMaxAI/MiniMax-M2.7'),
    system: 'You are a helpful assistant. Answer briefly and to the point.',
    messages: convertToModelMessages(messages),
    maxOutputTokens: 8192, // output limit via Gateway
  });

  return result.toUIMessageStreamResponse();
}

On the client, connect the useChat hook from @ai-sdk/react — it automatically calls /api/chat and renders the stream of messages. The backend hits Gonka, not OpenAI.

Script without UI (Node, async iterator over the stream):

import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
import { streamText } from 'ai';

const gonka = createOpenAICompatible({
  name: 'gonka',
  baseURL: 'https://gate.joingonka.ai/v1',
  apiKey: process.env.GONKA_API_KEY,
});

const result = streamText({
  model: gonka('MiniMaxAI/MiniMax-M2.7'),
  prompt: 'Write a haiku about distributed computing',
});

for await (const chunk of result.textStream) {
  process.stdout.write(chunk);
}

Model parameters. Three models are available through the Gateway: 200K token context for MiniMax M2.7, 380K for DeepSeek V4 Flash, and 390K for GLM-5.3 Flash; response limit (maxOutputTokens) is 8192 for MiniMax M2.7 and GLM-5.3 Flash, 32768 for DeepSeek V4 Flash:

If maxOutputTokens is not specified, the Gateway will return up to 1500 tokens by default for non-stream requests — for streaming chats, it is better to specify the value explicitly.

Cost Comparison

The Vercel AI SDK typically sits behind an interactive interface — chat, agent, or in-app assistant. Each message carries the dialogue history, and each agent step carries tool context. Therefore, the actual cost is calculated not by a single request, but by the production load. Let's compare typical scenarios:

ScenarioTokensAnthropic / OpenAIJoinGonka Gonka
One chat message~3K$0.01 — $0.05$0.000028
20-turn dialogue~150K$0.50 — $2.25$0.0014
RAG-response (search + generation)~5K$0.015 — $0.05$0.000048
Agent step with tool calling~10K$0.03 — $0.10$0.000096
10,000 requests per day (prod)~50M$150 — $500$0.48

The JoinGonka price is about $0.0069 per 1M input tokens, with output being roughly three times more expensive. For an application with thousands of requests per day, this is the difference between a bill of hundreds of dollars and a bill of mere cents. The free 3M tokens are enough to run your project's first requests.

Tool calling and agents

Vercel AI SDK describes tools declaratively through a tools object and zod schema. Network models support native function calling, so the AI SDK receives structured tool_calls without parsing text responses. The stopWhen: stepCountIs(n) parameter allows multiple steps in a row — the model calls a tool, gets the result, and continues.

import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
import { generateText, tool, stepCountIs } from 'ai';
import { z } from 'zod';

const gonka = createOpenAICompatible({
  name: 'gonka',
  baseURL: 'https://gate.joingonka.ai/v1',
  apiKey: process.env.GONKA_API_KEY,
});

const { text } = await generateText({
  model: gonka('MiniMaxAI/MiniMax-M2.7'),
  stopWhen: stepCountIs(5),
  tools: {
    weather: tool({
      description: 'Get weather in a city',
      inputSchema: z.object({ city: z.string() }),
      execute: async ({ city }) => ({ city, tempC: 17 }),
    }),
  },
  prompt: 'What is the weather in Moscow? Answer in one sentence.',
});

console.log(text);

The model calls the weather tool, receives the result, and generates a final response. The entire cycle costs about $0.000048 via Gonka compared to $0.03-0.10 with Anthropic or OpenAI. For agent applications where each user request expands into 5-10 steps, savings in production are measured in thousands of dollars per month.

If you are building an AI application in Python, check out the guide on LangChain — it uses the same approach via an OpenAI-compatible class.

Vercel AI SDK + Gonka = production-ready AI applications in TypeScript for pennies. createOpenAICompatible connects the Gateway without forks, generateText and streamText work as usual, native tool calling and Next.js route handlers — all for $0.0069/1M tokens instead of $2.5-15 at OpenAI and Anthropic.

Want to learn more?

Explore other sections or start earning GNK right now.

Get free tokens →