Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion docs/advanced/typed-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,43 @@ const stream = chat({
})
```

OpenRouter adapters take the same option, for example to set provider routing:

```typescript
import { createChatOptions } from '@tanstack/ai'
import { openRouterText } from '@tanstack/ai-openrouter'

export const routedChatOptions = createChatOptions({
adapter: openRouterText('openai/gpt-6.1-sol', {
defaultModelOptions: { provider: { sort: 'latency' } },
}),
systemPrompts: ['You are a customer-support assistant for Acme Corp.'],
})
```

This route sends `provider: { sort: 'latency' }` and `maxCompletionTokens: 500`:

```typescript
import { chat } from '@tanstack/ai'
import { routedChatOptions } from './lib/ai/chat-options'

const stream = chat({
...routedChatOptions,
messages: [{ role: 'user', content: 'Where is my order?' }],
threadId: 'thread-1',
modelOptions: { maxCompletionTokens: 500 },
})
```

When more than one place sets the same key, the value closest to the request wins:

1. `defaultModelOptions` on the adapter.
2. `modelOptions` on the `chat()` call, spread options included.
3. The config that a middleware returns from `onConfig`.

A key in the `chat()` call's `modelOptions` replaces the same key in `defaultModelOptions`. A call key set to `undefined` keeps the default.

`chat()` merges one level deep. A nested key, like `text`, is replaced in full.
`chat()` merges one level deep. A nested key, like `provider` or `text`, is replaced in full.

A middleware sees the merged value in `onConfig`. If it returns `modelOptions`, that object replaces the merged one in full. Spread `config.modelOptions` to keep the defaults and the call's keys:

Expand Down