Persistence (Advanced)
- Build your first harness
- Host options
- Send and show media
- Connect clients
- Write a client in any language
- Access and tenants
- Run in the terminal
- Deploy a harness
- Send inputs safely
- Run a tool without a model
- Durable sessions
- Run background work on workers
- List, rename, and fork sessions
- Lifecycle and retention
- Run side effects once
- Wait for a person
- Build on the session log
- Control how a turn ends
- Store settings per thread
- Compact a long session
- Fork and reset a thread
- One log for a session tree
- Write a plugin
- Track usage and cost
- Ask before risky tool calls
- Give your agent coding tools
- Sandbox and extend the coding tools
- Run the coding tools for many threads
- Undo a turn
- Build a coding agent
- Delegate to coding agents
- Switch between agent profiles
- Auth and connectors
- Share a thread between people
- Connect model providers
- Build your own UI
- Code mode in a harness
- Work until a goal is met
- Run agents from a harness
- Hand off a conversation
- Add skills
- Use MCP servers
- Give your agent a computer
- Use from any MCP client
- Use a harness from an editor (ACP)
- Overview
- Quick Start
- ProvidersUpdated
- Harnesses
- Harness Auth
- Workspace
- Tools
- Policy
- Lifecycle & Snapshots
- Portable Snapshots
- Keep Files After Reload
- Save a Named Version
- Branch From a Version
- Send a Frozen File
- Let the Agent Save and Fork
- Pick Which Files to Keep
- What a Snapshot Stores
- Instance Durability
- Durable Runs
- Events
Sandboxes (Advanced)
- Runtime Context
- Bring Your Own Key
- Keep Keys on the Server
- Debug Logging
- Multimodal Content
- Files API
- Per-Model Type Safety
- Runtime Adapter Switching
- Tree-Shaking
- Extend Adapter
- Extend Adapter: Advanced
- Typed Pre-Configured Options
- Test with a Fake Model
- Prompt Caching
- Mid-Conversation Changes
- Approval Flow Processing
Typed Pre-Configured Options
createChatOptions stores one adapter, system prompt, and tool list. Each route spreads that object and keeps the model types.
Define the config once with createChatOptions. The object keeps the full type of the adapter, so modelOptions, content types, and outputSchema stay checked where you spread it.
Quick setup#
1. Define the options once#
// lib/ai/chat-options.ts
import { createChatOptions, toolDefinition } from '@tanstack/ai'
import { openaiText } from '@tanstack/ai-openai'
import { z } from 'zod'
const orders = new Map([['A-100', { id: 'A-100', status: 'shipped' }]])
const lookupOrder = toolDefinition({
name: 'lookupOrder',
description: 'Look up a customer order by ID',
inputSchema: z.object({ orderId: z.string() }),
}).server(async ({ orderId }) => orders.get(orderId) ?? null)
export const supportChatOptions = createChatOptions({
adapter: openaiText('gpt-6.1-sol'),
systemPrompts: ['You are a customer-support assistant for Acme Corp.'],
tools: [lookupOrder],
reasoning: 'medium',
// Sampling settings go in modelOptions, under the key of the provider.
modelOptions: {
temperature: 0.3,
},
})2. Spread the options into chat()#
// routes/api/support/chat.ts
import {
chat,
chatParamsFromRequest,
toServerSentEventsResponse,
} from '@tanstack/ai'
import { supportChatOptions } from '../../../lib/ai/chat-options'
export async function POST(request: Request) {
const { messages, threadId, runId } = await chatParamsFromRequest(request)
const stream = chat({ ...supportChatOptions, messages, threadId, runId })
return toServerSentEventsResponse(stream)
}Every route that spreads supportChatOptions now uses the same model, prompt, and tools, with full type checks.
Change one field per route#
The spread wins from the right. Add or override any field at the call site. This route uses the same options, and adds a schema for a structured answer:
// routes/api/support/draft-reply.ts
import { chat } from '@tanstack/ai'
import { z } from 'zod'
import { supportChatOptions } from '../../../lib/ai/chat-options'
export async function POST(request: Request) {
const body: unknown = await request.json()
const ticket =
typeof body === 'object' && body !== null && 'ticket' in body
? String(body.ticket)
: ''
const draft = await chat({
...supportChatOptions,
messages: [{ role: 'user', content: `Draft a reply to: ${ticket}` }],
threadId: 'thread-1',
outputSchema: z.object({ subject: z.string(), body: z.string() }),
stream: false,
})
return Response.json(draft)
}When to use it#
- You share one config across many routes.
- You pass options through a layer (a server function, a wrapper, a test fixture) and want to keep the types of the model.
- You pick between two configs at runtime. Build two options objects and choose one, so the chat() call has no conditions inside it.
- You keep tools, system prompts, and middleware next to the adapter that they target.
If you call an activity once in one place, you do not need the helper. Write the options inline.
The same pattern for media#
Each activity has its own helper. For example, an image:
import { createImageOptions, generateImage } from '@tanstack/ai'
import { openaiImage } from '@tanstack/ai-openai'
const heroImageOptions = createImageOptions({
adapter: openaiImage('gpt-image-2.5-sunburst'),
prompt: 'A glass sphere refracting a sunset over a calm sea',
threadId: 'thread-1',
size: '1536x1024',
numberOfImages: 1,
})
const result = await generateImage(heroImageOptions)Available helpers#
All helpers come from @tanstack/ai. Each one takes the same options as its activity.
| Helper | Activity | Adapter examples |
|---|---|---|
| createChatOptions | chat() | openaiText, anthropicText |
| createSummarizeOptions | summarize() | openaiSummarize |
| createEmbedOptions | embed() | openaiEmbedding, geminiEmbedding |
| createRerankOptions | rerank() | cohereRerank |
| createImageOptions | generateImage() | openaiImage, falImage |
| createAudioOptions | generateAudio() | falAudio, geminiAudio |
| createVideoOptions | generateVideo(), getVideoJobStatus() | falVideo, openaiVideo |
| createLiveVideoOptions | generateLiveVideo() | reactorVideo, falLiveVideo |
| createWorldOptions | generateWorld() | reactorWorld, worldlabsWorld |
| createSpeechOptions | generateSpeech() | openaiSpeech, elevenlabsSpeech |
| createVoiceOptions | generateVoice() | elevenlabsVoiceDesign |
| createTranscriptionOptions | generateTranscription() | openaiTranscription, falTranscription |
Advanced#
How it works#
At runtime, each helper is the identity function: createChatOptions(opts) returns opts. The value of the helper is in the types. The returned object carries the full type of the adapter and model, so you do not write the generic types of the options yourself.
What the helper does not do#
- No runtime behavior. There is no validation, freezing, cloning, or memoization. If you change the returned object, the next call sees the change. Treat the object as read-only.
- No partial types. The helper expects the full options shape. To build options step by step, give the partial state your own type (a Partial<> of the full options shape). Call the helper only when the shape is complete.
- No request. The helper does not call the model. Only the activity function (chat, generateImage, and the others) sends the request.
Related pages#
- Per-Model Type Safety: how the adapter and model choose the type of modelOptions.
- Tree-Shaking: why each adapter and helper is a separate export.
- Extend Adapter: add custom models to an adapter, with the same typed options.