Knowledge Base Sections ▾

Navigation

▸ Start here By roles

Categories

Tools 37
Glossary 12

Tools

TanStack AI + Gonka — AI applications in TypeScript for pennies

TanStack AI (@tanstack/ai) is a type-safe SDK for TypeScript from the TanStack team (creators of Query, Router, Table). Provider-agnostic architecture: streaming chat, native tool calling, agents, structured output, and multimodality through a single set of adapters. Ready-to-use bindings for React, Vue, Svelte, Solid, and Preact (useChat and other hooks) plus a headless client for the server.

The problem is the same as with any AI framework: inference cost. TanStack AI supports OpenAI, Anthropic, and Gemini out of the box, but these providers' direct rates ($2.50–15 per 1M tokens) make production chats and agents expensive: streaming dialogs and tool-cycles quickly consume millions of tokens.

A key feature of TanStack AI is the openaiCompatible() function: a first-class way to connect any OpenAI-compatible endpoint. This means JoinGonka Gateway integrates without custom adapters—just specify the baseURL, key, and model list. The result: the same type-safe chat and agents, but at $0.0069/1M tokens via the decentralized Gonka network instead of $2.50–15 at OpenAI.

Step 1: Install TanStack AI and get a key

Package installation (core + OpenAI-adapter, where openaiCompatible lives):

# pnpm
pnpm add @tanstack/ai @tanstack/ai-openai

# npm
npm install @tanstack/ai @tanstack/ai-openai

For a React chat interface, add the client and hooks:

pnpm add @tanstack/ai-client @tanstack/ai-react

JoinGonka API key: if you don't have one yet — register at gate.joingonka.ai/register, get 3M free tokens and create a jg-xxx key in Dashboard → API Keys. One key and one balance work for both OpenAI and Anthropic formats.

Step 2: Connect Gonka via openaiCompatible

In TanStack AI, a custom OpenAI-compatible provider is configured using the openaiCompatible() function: you define the baseURL, apiKey, and list of models once, then select the model for each call. Our Gateway speaks the Chat Completions format, so we keep api: 'chat-completions' (the default value).

import { openaiCompatible } from '@tanstack/ai-openai'

// Gonka provider — configured once
export const gonka = openaiCompatible({
  name: 'gonka',
  baseURL: 'https://gate.joingonka.ai/v1',
  apiKey: process.env.GONKA_API_KEY!, // jg-your-key
  api: 'chat-completions',
  models: [
    'MiniMaxAI/MiniMax-M2.7', // default
    'deepseek-ai/DeepSeek-V4-Flash-0731',
    'zai-org/GLM-5.3-Flash', // reasoning model
  ],
})

Streaming chat on the server (e.g., a route handler in any fullstack framework or TanStack Start). We set the response length via modelOptions — this is the single point for native wire parameters (max_tokens, temperature):

import { chat, toServerSentEventsResponse } from '@tanstack/ai'
import { gonka } from './gonka'

export async function POST(request: Request) {
  const { messages } = await request.json()

  const stream = chat({
    adapter: gonka('MiniMaxAI/MiniMax-M2.7'),
    messages,
    modelOptions: { max_tokens: 8192 }, // output limit via Gateway
  })

  return toServerSentEventsResponse(stream)
}

React client via the useChat hook — streams responses from the server to the UI:

import { useChat } from '@tanstack/ai-react'

function Chat() {
  const { messages, sendMessage, status } = useChat({ api: '/api/chat' })

  return (
    <div>
      {messages.map((m) => (
        <p key={m.id}><b>{m.role}:</b> {m.content}</p>
      ))}
      <button onClick={() => sendMessage('What is Gonka?')}>
        Ask
      </button>
    </div>
  )
}

Without a server: the same provider works directly in a script or backend — call chat() and read the stream. Connecting to Gonka is the same for all variants.

Model parameters via Gateway: context — 200K tokens for MiniMax M2.7, 380K for DeepSeek V4 Flash, and 390K for GLM-5.3 Flash. max_tokens limit — 8192 for MiniMax M2.7 and GLM-5.3 Flash, 32768 for DeepSeek V4 Flash. If max_tokens is not specified, the default for non-stream is 1500, so specify it explicitly for long responses.

Cost Comparison

TanStack AI works equally well through direct OpenAI/Anthropic rates or via Gonka—only the baseURL changes. But the cost differs by orders of magnitude. Let’s compare typical production loads for a TanStack AI application:

ScenarioTokensOpenAI / AnthropicJoinGonka Gonka
One streaming chat response~3K$0.008 — $0.045$0.000028
Agent cycle with tool calling~15K$0.04 — $0.22$0.00014
1,000 dialogues per day~3M$7.50 — $45$0.028
Production month (~100M)~100M$250 — $1,500$0.96

TanStack AI’s provider-agnostic approach means switching to Gonka is just a one-line change (baseURL), not a code rewrite. Meanwhile, your type-safe tools, structured output, and React hooks remain unchanged. For an application with thousands of users, the difference is tens of thousands of dollars per month.

Gonka pricing: input ~$0.0069 per 1M tokens, output ×3. This is hundreds to thousands of times cheaper than the direct rates of OpenAI and Anthropic.

Type-safe tools and model selection

The main feature of TanStack AI is a unified toolDefinition() contract: the tool is described once (input/output via Zod, ArkType, Valibot, or JSON Schema), and the implementation is linked on the server or client. MiniMax M2.7, DeepSeek V4 Flash, and GLM-5.3 Flash support native tool calling via Gonka, so agents work reliably—without parsing text responses.

import { chat, toolDefinition } from '@tanstack/ai'
import { gonka } from './gonka'
import { z } from 'zod'

const getWeather = toolDefinition({
  name: 'getWeather',
  description: 'Get the weather in a city',
  inputSchema: z.object({ city: z.string() }),
  outputSchema: z.object({ tempC: z.number() }),
}).server(async ({ city }) => {
  return { tempC: 21 } // your real API call
})

const stream = chat({
  adapter: gonka('MiniMaxAI/MiniMax-M2.7'),
  messages: [{ role: 'user', content: 'What is the weather in Moscow?' }],
  tools: [getWeather],
  modelOptions: { max_tokens: 8192 },
})

Which model to choose:

  • MiniMaxAI/MiniMax-M2.7 — default, balance of speed and quality, long context. Response limit is 8192.
  • deepseek-ai/DeepSeek-V4-Flash-0731 — one of the longest contexts in the network (380K), cheap large prompts. Response limit is 32768.
  • zai-org/GLM-5.3-Flash — reasoning model: thinks before answering, strong at complex logic; longest context in the network (390K). Response limit is 8192; reasoning consumes part of max_tokens.

Thanks to runtime adapter switching in TanStack AI, you can keep all three models in one provider and switch between them on the fly—for example, reasoning tasks on GLM-5.3 Flash, quick responses on MiniMax.

TanStack AI + Gonka = type-safe AI applications on TypeScript for pennies. Connect via openaiCompatible — one baseURL change, and streaming-chat, agents, and tools work for $0.0069/1M tokens instead of $2.50–15 at OpenAI. Starting tokens let you try it without a card.

Want to learn more?

Explore other sections or start earning GNK right now.

Get free tokens →