- MCP Server ToolsUpdated
- Managed MCP with chat()Updated
- Manual MCPUpdated
- MCP Type GenerationUpdated
- MCP Client InputUpdated
- MCP Server Tools: AdvancedNew
- MCP EventsNew
- MCP AppsUpdated
- Serve Tools over HTTPUpdated
- Serve Resources and PromptsUpdated
- Ask for InputUpdated
- Draft Text from a ToolUpdated
- MCP Server TasksUpdated
- MCP Server AuthUpdated
- MCP Server SessionsUpdated
- MCP Server on stdioUpdated
Persistence (Advanced)
- GenerationsUpdated
- Realtime Voice ChatUpdated
- Text-to-SpeechUpdated
- TranscriptionUpdated
- Transcription: AdvancedNew
- Voice CreationUpdated
- Audio RecordingUpdated
- Audio GenerationUpdated
- Image GenerationUpdated
- Image Generation: AdvancedNew
- Video GenerationUpdated
- Video Generation: AdvancedNew
- Live GenerationUpdated
- World GenerationUpdated
- Generation HooksUpdated
- Build your first harnessUpdated
- Host optionsNew
- Send and show mediaUpdated
- Connect clientsUpdated
- Write a client in any languageNew
- Access and tenantsNew
- Run in the terminalUpdated
- Deploy a harnessUpdated
- Send inputs safelyUpdated
- Run a tool without a model
- Durable sessionsUpdated
- Run background work on workersNew
- List, rename, and fork sessionsUpdated
- Lifecycle and retentionNew
- Run side effects onceUpdated
- Wait for a personNew
- Build on the session logUpdated
- Control how a turn endsUpdated
- Store settings per threadUpdated
- Compact a long sessionUpdated
- Fork and reset a threadUpdated
- One log for a session treeUpdated
- Write a pluginUpdated
- Track usage and costUpdated
- Ask before risky tool callsUpdated
- Give your agent coding toolsUpdated
- Sandbox and extend the coding toolsUpdated
- Run the coding tools for many threadsNew
- Undo a turnUpdated
- Build a coding agentUpdated
- Delegate to coding agentsUpdated
- Switch between agent profilesUpdated
- Auth and connectorsUpdated
- Share a thread between peopleUpdated
- Connect model providersUpdated
- Build your own UIUpdated
- Code mode in a harnessUpdated
- Work until a goal is metUpdated
- Run agents from a harnessUpdated
- Hand off a conversationNew
- Add skillsUpdated
- Use MCP serversUpdated
- Give your agent a computerNew
- Use from any MCP clientUpdated
- Use a harness from an editor (ACP)Updated
- OverviewUpdated
- Quick StartUpdated
- ProvidersUpdated
- HarnessesUpdated
- Harness AuthUpdated
- WorkspaceUpdated
- ToolsUpdated
- PolicyUpdated
- Lifecycle & SnapshotsUpdated
- Portable SnapshotsUpdated
- Keep Files After ReloadUpdated
- Save a Named VersionUpdated
- Branch From a VersionUpdated
- Send a Frozen FileUpdated
- Let the Agent Save and ForkUpdated
- Pick Which Files to KeepUpdated
- What a Snapshot StoresUpdated
- Instance DurabilityUpdated
- Durable RunsUpdated
- EventsUpdated
Sandboxes (Advanced)
- Runtime ContextUpdated
- Bring Your Own KeyUpdated
- Keep Keys on the ServerNew
- Debug LoggingUpdated
- Multimodal ContentUpdated
- Files APIUpdated
- Per-Model Type SafetyUpdated
- Runtime Adapter SwitchingUpdated
- Tree-ShakingUpdated
- Extend AdapterUpdated
- Extend Adapter: AdvancedNew
- Typed Pre-Configured OptionsUpdated
- Test with a Fake ModelUpdated
- Prompt CachingUpdated
- Mid-Conversation ChangesUpdated
- Approval Flow ProcessingUpdated
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.