Developer ToolingSep 14, 20264 min read

TanStack AI: Type-Safe Isomorphic Streaming and Tool Calling Architecture

Explore TanStack AI's provider-agnostic, type-safe architecture for streaming chat, isomorphic tool calling, tree-shakeable adapters, and multi-framework support.

Documentation & analysis · 5 source links

Building AI-powered features in TypeScript often forces developers to choose between tightly coupled vendor SDKs or monolithic abstractions that introduce runtime type drift and oversized bundle footprints. When client UI components, server endpoints, and external model APIs lack a unified contract, maintaining streaming chat state or tool execution pipelines becomes fragile.

TanStack AI addresses this challenge by providing a framework-agnostic, type-safe SDK for LLM integration. Built on composable activities and tree-shakeable provider adapters, it introduces single-contract isomorphic tools and native streaming handlers across React, Solid, Vue, Svelte, Preact, and headless runtimes. The principal tradeoff is navigating a modular package ecosystem that requires explicit activity imports, but in return, it delivers bundle efficiency and end-to-end static type checking across client and server boundaries.

TanStack AI Request Pipeline and Isomorphic Tool Flow

Loading diagram…

Maps the architectural path from framework client hooks down through streaming SSE transport, the core chat engine, isomorphic tool contracts, and activity-bound provider adapters.

Isomorphic Tool Definitions and Shared Schemas

A central architecture pattern in TanStack AI is the isomorphic tool contract. Traditional function-calling models require developers to maintain separate TypeScript interfaces for LLM JSON schemas, server execution logic, and client-side tool response handlers. This separation frequently causes schema drift when API contracts evolve.

Using toolDefinition(), developers define input and output schemas once using validation libraries like Zod, ArkType, or Valibot. That single definition is then bound to execution handlers using .server() or .client():

import { toolDefinition } from '@tanstack/ai'
import { z } from 'zod'

const getProductsDef = toolDefinition({
  name: 'getProducts',
  description: 'Search the product catalog',
  inputSchema: z.object({ query: z.string() }),
  outputSchema: z.array(
    z.object({
      id: z.string(),
      name: z.string(),
    }),
  ),
})

export const getProducts = getProductsDef.server(async ({ query }) => {
  return db.products.search(query)
})

This means the execution handler automatically inherits type inference from inputSchema and enforces that return values match outputSchema. For a team building agentic workflows across server functions and UI components, this structure eliminates manual payload casting and guarantees runtime safety before tool execution results are streamed back to the model.

Tree-Shakeable Provider Adapters and Activity Isolation

Monolithic AI SDKs often bundle client-side code for every supported capability—including audio transcription, image generation, and vector embeddings—even when an application only requires text generation. As documented in TanStack AI npm package details, TanStack AI structures provider integrations by activity subpaths.

Official adapters such as @tanstack/ai-openai, @tanstack/ai-anthropic, @tanstack/ai-gemini, and @tanstack/ai-openrouter expose granular function entries like openaiText, openaiImage, and geminiSpeech.

import { chat, toServerSentEventsResponse } from '@tanstack/ai'
import { openaiText } from '@tanstack/ai-openai'

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

  const stream = chat({
    adapter: openaiText('gpt-5.2'),
    messages: body.messages,
  })

  return toServerSentEventsResponse(stream)
}

This activity isolation ensures that applications importing only openaiText do not bundle unused image processing or audio conversion code. For teams deploying to edge runtimes or web clients with strict cold-start and bundle budget constraints, tree-shakeable activity imports reduce client payload size while retaining provider flexibility.

Multi-Framework Client Binding and SSE Streaming

While server endpoints handle raw model connections, frontend rendering requires reactive chat state, streaming backpressure management, and error recovery. TanStack AI Documentation outlines full client binding support across React (@tanstack/ai-react), Solid (@tanstack/ai-solid), Vue (@tanstack/ai-vue), Svelte (@tanstack/ai-svelte), and Preact (@tanstack/ai-preact), alongside a headless execution core (@tanstack/ai-client).

The server helper toServerSentEventsResponse(stream) standardizes model streams into Server-Sent Events (SSE). Client hooks like useChat parse these SSE streams into strongly typed message primitives containing text chunks, tool invocation states, and reasoning parts.

This decoupled client-server relationship allows applications to switch backend LLM providers—or swap local Ollama instances for OpenRouter—without modifying client-side UI render logic or state stores.

Agent Skills and Code Mode Execution

Beyond basic chat and function calling, TanStack AI supports Code Mode agents and AI coding assistant skills. Code Mode allows an LLM to generate and execute TypeScript inside an isolated sandbox to perform complex tool orchestration with control loops, conditional branching, and parallel execution.

For developer teams using AI assistants like Claude Code or Cursor, TanStack AI provides pre-built skills installed via CLI:

/plugin marketplace add TanStack/ai
/plugin install tanstack-ai

Or using universal agent skill managers:

npx skills add TanStack/ai -g --skill tanstack-ai tanstack-ai-migration

Executing npx @tanstack/intent@latest install configures workspace skill files (AGENTS.md / CLAUDE.md), guiding coding agents to use type-safe TanStack AI patterns when generating code.

Architectural Tradeoffs and Implementation Guidance

Strengths

  • End-to-End Type Safety: Single toolDefinition schemas prevent type drift between client state, server logic, and LLM payloads.
  • Framework Independence: Native hooks across React, Solid, Vue, Svelte, and Preact, plus a headless core.
  • Bundle Efficiency: Tree-shakeable adapters ensure you only ship the activities (openaiText, falVideo) you actually invoke.

Tradeoffs

  • Ecosystem Granularity: Developers must manage multiple package dependencies (@tanstack/ai, @tanstack/ai-client, @tanstack/ai-openai) rather than a single wrapper.
  • Evolving Standard: As a newer library in the TanStack ecosystem, team workflows may require updating skills and configurations as model provider capabilities change.

Sources

Keep exploring

Developer tools & observability: questions, tradeoffs, and guides →

Browse all reports · Suggest a correction