Open History to see the last commit
1---
2title: Store settings per thread
3id: harness-thread-settings
4order: 4
5description: "Store the model, reasoning, instructions, tools, plugins, and working folder of each thread, so the choice stays after a reload and a restart."
6keywords:
7 - tanstack ai
8 - harness
9 - threadSettings
10 - updateThreadSettings
11 - model picker
12 - cwd
13---
14
15Each conversation in your app has its own choices: a faster model for one chat, extra instructions for another, a folder for a coding task. These choices must stay after a reload and a restart. `session.updateThreadSettings()` stores them with the thread, and every next turn uses them.
16
17## 1. Name the models a thread can pick
18
19Give the harness a `models` map. A thread picks a model by its name:
20
21```ts group=harness-thread-settings
22import { createHarnessHost, defineHarness } from '@tanstack/ai-harness'
23import { memoryPersistence } from '@tanstack/ai-persistence'
24import { anthropicText } from '@tanstack/ai-anthropic'
25
26export const assistant = defineHarness({
27 name: 'acme/assistant',
28 adapter: anthropicText('claude-sonnet-5-5'),
29 models: {
30 fast: anthropicText('claude-haiku-5-5'),
31 strong: anthropicText('claude-opus-5-5'),
32 },
33})
34
35const host = createHarnessHost({ persistence: memoryPersistence() })
36```
37
38## 2. Store the settings of a thread
39
40```ts group=harness-thread-settings
41const session = await host.open(assistant, { threadId: 'review-thread' })
42
43const receipt = await session.updateThreadSettings({
44 settings: {
45 model: 'strong',
46 reasoning: 'high',
47 instructions: 'Answer in French.',
48 },
49})
50console.log(receipt.status) // 'accepted'
51console.log(session.threadSettings()) // { model: 'strong', reasoning: 'high', instructions: 'Answer in French.' }
52```
53
54The settings apply from the next turn. A field that you leave out stays as it is. `updateThreadSettings` merges your fields into the latest stored settings, also when another host changed them. Set a field to `null` to clear it:
55
56```ts group=harness-thread-settings
57await session.updateThreadSettings({ settings: { instructions: null } })
58```
59
60The six fields:
61
62| Field | What it does |
63| --- | --- |
64| `model` | A name from `models`. |
65| `reasoning` | The reasoning level of every turn, as `chat({ reasoning })` takes it. |
66| `instructions` | Text added as the last system prompt of every turn. |
67| `tools` | Exactly these tools by name, or `{ remove: [names] }` for every tool but those. |
68| `plugins` | `{ remove: [names] }`: the turns leave out what those plugins give. |
69| `cwd` | The working folder of the thread. See [The working folder](#the-working-folder). |
70
71An unknown field, model, or plugin, or a bad value, gets a receipt with `status: 'rejected'` and the reason. The settings are in the log, so a durable log store keeps them after a restart. A [fork](./fork-and-reset) copies them.
72
73`updateThreadSettings` stores an input, so the log keeps who changed what. Each change sends a `harness.settings.changed` event with the new `settings`. A session on another host reads the change too, so its next turn uses the new settings.
74
75A client can change only the fields in `expose.settings` of the harness. See [Choose what clients can change](./connect#choose-what-clients-can-change).
76
77## 3. Build a model menu
78
79`session.describe()` returns the stored `settings` and the names of the `models`, so a UI can build a menu:
80
81```ts group=harness-thread-settings
82const { models, settings } = session.describe()
83console.log(models) // ['fast', 'strong']
84console.log(settings.model) // 'strong'
85
86await session.updateThreadSettings({ settings: { model: 'fast' } })
87```
88
89## What wins
90
91A turn can get its model from five places. The first one that is set wins:
92
931. The `overrides.adapter` of the prompt. See [Give one prompt its own settings](./turn-control#give-one-prompt-its-own-settings).
942. A plugin `adapter()` pick that returns `{ adapter, override: true }`. See [Win over the stored model](./plugins#win-over-the-stored-model).
953. The stored `model`.
964. A plugin `adapter()` pick. See [Pick the model of a turn](./plugins#pick-the-model-of-a-turn).
975. The `adapter` of the harness.
98
99For `reasoning`, the `overrides` of the prompt win, then the stored setting, then the `reasoning` of the harness.
100
101## Tools and plugins
102
103- A `tools` list keeps only the tools with those names. `{ remove }` drops the tools with those names. Either way, the tools of the prompt `overrides` stay.
104- The harness applies `tools` before the plugins prepare the tool list (`prepareTools`). So a plugin that wraps tools sees only the tools that a thread keeps.
105- `plugins: { remove }` leaves everything that those plugins give out of the thread: tools, prompts, middleware, the model pick, agents, subagents, commands, and the items that they add for other plugins. For example, a thread without `agents()` gets no subagent tools, no `/agent` command, and no permission rules of its agent profiles. The thread also cannot run an agent of a removed plugin by its name.
106- Plugins that run once per session stay mounted, but they give the thread nothing.
107- If a plugin that stays (of any lifetime) or a harness `middleware` entry needs a capability of a removed plugin, the setting is rejected.
108
109## The working folder
110
111`cwd` is the working folder of the thread, for the plugins whose tools work in a folder. A plugin reads it at each call with `ctx.session.threadSettings().cwd`, so a change applies from the next turn:
112
113```ts group=harness-thread-settings
114const task = await host.open(assistant, { threadId: 'fix-login' })
115await task.updateThreadSettings({ settings: { cwd: 'web-app' } })
116console.log(task.threadSettings().cwd) // 'web-app'
117```
118
119When `cwd` changes, the model gets a short note before its next call. So it stops using the paths of the old folder:
120
121- A new `cwd` adds "The working folder is now web-app. Paths are relative to it."
122- `cwd: null` adds "The working folder is the default folder again."
123- The same `cwd` again adds no note.
124- A thread with no messages yet gets no note.
125
126The note is an assistant message in the transcript, so it stays after a restart. A change while a turn runs adds the note before the next turn. The log keeps that waiting note, and the next host adds it once after a crash.
127
128## What you have now
129
130- A model menu per thread, from the `models` of the harness.
131- Instructions, reasoning, tools, and plugins that each thread keeps.
132- A working folder per thread for the plugins that read it.
133- Settings that stay after a reload and a restart.