Knowledge Base Sections ▾

Tools

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. Unified generateText and streamText API, streaming output to UI, native tool calling, ready-to-use 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. A streaming chat interface sends dialogue history with each message, an agent cycles context through dozens of steps. At Anthropic's prices ($3-15/1M) and OpenAI's ($2.5-10/1M), even a modest pet project in production turns into a bill for hundreds of dollars per month.

JoinGonka Gateway is an OpenAI-compatible endpoint on top of the decentralized Gonka network. Vercel AI SDK connects to it like any OpenAI-compatible provider—without forking, without custom adapters. The same Qwen3-235B model, the same streamText, but for $0.0005/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 give 10M free tokens to start. In the Dashboard, create a key with the jg- prefix.

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

npm install ai @ai-sdk/openai-compatible

Minimal connection—create an instance of the provider 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('Qwen/Qwen3-235B-A22B-Instruct-2507-FP8'),
  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 setup is needed. Store the key in an environment variable GONKA_API_KEY (e.g., in .env.local), not directly in the code.

Alternative—the @ai-sdk/openai package with the createOpenAI({ baseURL, apiKey }) factory. Both methods work; for endpoints not belonging to OpenAI, the AI SDK documentation specifically advises @ai-sdk/openai-compatible—it doesn't pull in unnecessary OpenAI-specific assumptions.

Step 2: Streaming and Next.js route handler

The main feature of Vercel AI SDK is streaming response delivery. The streamText function starts streaming tokens immediately, and the toUIMessageStreamResponse() helper delivers the ready stream directly from the route handler in Next.js App Router.

Server 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('Qwen/Qwen3-235B-A22B-Instruct-2507-FP8'),
    system: 'You are a helpful assistant. Answer briefly and to the point.',
    messages: convertToModelMessages(messages),
    maxOutputTokens: 8192, // Qwen3-235B ceiling via Gateway
  });

  return result.toUIMessageStreamResponse();
}

On the client, connect the useChat hook from @ai-sdk/react—it automatically accesses /api/chat and renders the message stream. The backend, meanwhile, communicates with 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('Qwen/Qwen3-235B-A22B-Instruct-2507-FP8'),
  prompt: 'Write a haiku about distributed computing',
});

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

Model parameters. Three models are available via the Gateway; all have 128K context tokens; the output ceiling (maxOutputTokens) varies:

  • Qwen/Qwen3-235B-A22B-Instruct-2507-FP8 — default, up to 8192 output tokens;
  • moonshotai/Kimi-K2.6 — up to 3072 (Kimi K2.6);
  • MiniMaxAI/MiniMax-M2.7 — up to 4096 (MiniMax M2.7).

If maxOutputTokens is not specified, for a non-stream request, the Gateway will default to returning up to 1500 tokens—for streaming chats, it's better to specify the value explicitly.

Cost Comparison

Vercel AI SDK typically powers interactive interfaces—chat, agent, in-app assistant. Each message carries the dialogue history, each agent step carries tool context. Therefore, the actual cost is calculated not per single request, but by production load. Let's compare typical scenarios:

ScenarioTokensAnthropic / OpenAIJoinGonka Gonka
One chat message~3K$0.01 — $0.05$0.000003
Dialogue of 20 turns~150K$0.50 — $2.25$0.00015
RAG response (search + generation)~5K$0.015 — $0.05$0.000005
Agent step with tool calling~10K$0.03 — $0.10$0.00001
10,000 requests per day (prod)~50M$150 — $500$0.05

JoinGonka's price is about $0.0005 per 1M input tokens, with output approximately three times more expensive. For an application with thousands of requests per day, this is the difference between a bill for hundreds of dollars and a bill for cents. The 10M free tokens are enough to fully run and debug a project before going into production.

Tool calling and agents

Vercel AI SDK describes tools declaratively using a tools object and a zod schema. Qwen3-235B supports native function calling, so the AI SDK receives structured tool_calls without parsing text responses. The stopWhen: stepCountIs(n) parameter allows several consecutive steps—the model calls the 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('Qwen/Qwen3-235B-A22B-Instruct-2507-FP8'),
  stopWhen: stepCountIs(5),
  tools: {
    weather: tool({
      description: 'Get the weather in a city',
      inputSchema: z.object({ city: z.string() }),
      execute: async ({ city }) => ({ city, tempC: 17 }),
    }),
  },
  prompt: 'What's the weather like in Moscow? Answer in one sentence.',
});

console.log(text);

The model calls the weather tool, receives the result, and forms the final answer. The entire cycle costs about $0.00001 via Gonka versus $0.03-0.10 via Anthropic or OpenAI. For agent applications where each user request unfolds into 5-10 steps, the production savings amount to thousands of dollars per month.

If you're building an AI application in Python, check out the guide for LangChain—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.0005/1M tokens instead of $2.5-15 for OpenAI and Anthropic.

Want to learn more?

Explore other sections or start earning GNK right now.

Get 10M free tokens →