Knowledge Base Sections ▾

Navigation

▸ Start here By roles

Categories

Tools 32
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 ready-made 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's price. The streaming chat interface sends dialogue history with every message, and the agent moves context through dozens of steps. With Anthropic ($3-15/1M) and OpenAI ($2.5-10/1M) pricing, even a modest pet project in production turns into a monthly bill of hundreds of dollars.

JoinGonka Gateway is an OpenAI-compatible endpoint on top of the decentralized Gonka network. Vercel AI SDK connects to it just like any other OpenAI-compatible provider—no forks, no custom adapters. Same Kimi K2.6 model, same streamText, but for $0.003/1M input tokens—hundreds to thousands of times cheaper.

Step 1: Get a key and connect the provider

JoinGonka API key: register at gate.joingonka.ai/register — we provide 10M free tokens at the start. In the Dashboard, create an API key with the jg- prefix.

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 needed. Store the key in the GONKA_API_KEY environment variable (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 Vercel AI SDK is streaming responses. The streamText function starts streaming tokens immediately, and the toUIMessageStreamResponse() helper delivers the finished stream directly from a 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 for 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 side, use the useChat hook from @ai-sdk/react — it handles requests to /api/chat and renders the message stream automatically. The backend communicates with Gonka instead of OpenAI.

Script without UI (Node, async-iterator over 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. Two models are available via Gateway, both with a context window of 200K tokens; the output limit (maxOutputTokens) is up to 8192 for each:

If maxOutputTokens is not specified, the Gateway defaults to returning up to 1500 tokens for non-stream requests — for streaming chats, it is better to set the value explicitly.

Cost Comparison

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

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

The JoinGonka price is about $0.003 per 1M input tokens, with output being roughly three times more expensive. For an app with thousands of requests per day, this is the difference between a bill of hundreds of dollars and a bill of cents. Free 10M tokens are enough to fully run through and debug your project before it hits production.

Tool calling and agents

Vercel AI SDK describes tools declaratively using the tools object and the zod schema. Kimi K2.6 supports 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, receives 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, gets the result, and generates a final answer. The entire cycle costs about $0.000048 via Gonka versus $0.03–0.10 with Anthropic or OpenAI. For agentic applications where each user request unfolds into 5-10 steps, production savings are measured in thousands of dollars per month.

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

Vercel AI SDK + Gonka = production-ready TypeScript AI applications 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.003/1M tokens instead of $2.5–15 with OpenAI and Anthropic.

Want to learn more?

Explore other sections or start earning GNK right now.

Get 10M free tokens →