Open History to see the last commit
1---
2title: Switch between agent profiles
3id: harness-agents
4order: 10
5description: "Give one harness several agents, such as a read-only planner and a builder. Each profile has its own prompt, tools, model, rules, and step limit, in code or in Markdown files."
6keywords:
7 - tanstack ai
8 - harness
9 - agents
10 - agent profiles
11 - plan
12 - subagents
13---
14
15Your agent has two jobs. First it must plan a change without touching a file. Then it must build the change with every tool. The main model also needs a fast helper that searches the code. `agents()` gives the harness named agent profiles. Each profile has its own system prompt, tools, model, permission rules, and step limit.
16
17## 1. Add the plugin
18
19```ts group=harness-agents
20import { readFile, writeFile } from 'node:fs/promises'
21import { resolve } from 'node:path'
22import { toolDefinition } from '@tanstack/ai'
23import { createHarnessHost, defineHarness } from '@tanstack/ai-harness'
24import { permissions } from '@tanstack/ai-harness/plugins'
25import { agents } from '@tanstack/ai-harness/plugins/node'
26import { anthropicText } from '@tanstack/ai-anthropic'
27import { memoryPersistence } from '@tanstack/ai-persistence'
28import { z } from 'zod'
29
30const root = process.cwd()
31const sonnet = anthropicText('claude-sonnet-5-5')
32
33const readTool = toolDefinition({
34 name: 'read_file',
35 description: 'Read a file in the project',
36 inputSchema: z.object({ path: z.string() }),
37}).server(({ path }) => readFile(resolve(root, path), 'utf8'))
38
39const writeTool = toolDefinition({
40 name: 'write_file',
41 description: 'Write a file in the project',
42 inputSchema: z.object({ path: z.string(), content: z.string() }),
43}).server(async ({ path, content }) => {
44 await writeFile(resolve(root, path), content)
45 return `Wrote ${path}.`
46})
47
48const coder = defineHarness({
49 name: 'acme/coder',
50 adapter: sonnet,
51 tools: [readTool, writeTool],
52 plugins: () => [
53 permissions({ root, rules: [{ tool: 'write_file', decision: 'ask', kind: 'edit' }] }),
54 agents({ adapter: () => sonnet }),
55 ],
56})
57```
58
59`adapter` turns a model id into an adapter. Here every agent uses the same model.
60
61## 2. Plan, then build
62
63```ts group=harness-agents
64const host = createHarnessHost({ persistence: memoryPersistence() })
65const session = await host.open(coder, { threadId: 'dark-mode' })
66
67await session.runCommand({ name: 'agent', input: 'plan' })
68const plan = await session.send('How do I add a dark mode?')
69console.log(plan.text)
70
71await session.runCommand({ name: 'agent', input: 'build' })
72await session.send('Do step 1.')
73```
74
751. The `plan` agent reads the code and gives numbered steps. It has no tool that changes a file.
762. The `build` agent changes the files, after your answer to each [permission question](./permissions).
77
78- `session.runCommand({ name: 'agent' })` with no input shows the current agent and the list: `Agent: build. Agents: build, plan.`
79- A switch applies at the next turn.
80
81The same switch is the `agent` setting. `session.describe().config` lists its options for a menu:
82
83```ts group=harness-agents
84const setting = session.describe().config.find((entry) => entry.key === 'agent')
85if (setting?.option.type === 'select') {
86 console.log(setting.value, setting.option.options) // 'build' ['build', 'plan']
87}
88
89await session.setPluginSetting({ key: 'agent', value: 'plan' })
90```
91
92A client can change the setting only when the harness exposes it. Add `expose: { config: ['agent'] }` to `defineHarness`.
93
94## The built-in agents
95
96| Agent | Mode | Tools | What it does |
97| --- | --- | --- | --- |
98| `build` | `primary`, the default | Every tool | Reads code, changes files, and runs commands. |
99| `plan` | `primary` | The read tools and `todo_write` | Reads the code and gives a plan in numbered steps. |
100| `general` | `subagent` | Every tool | Does a task in many steps and reports back. |
101| `explore` | `subagent` | The read tools | Searches the code and reports the paths that it finds. |
102
103The read tools are `read_file`, `list_files`, `grep`, `webfetch`, `websearch`, and `question`. An agent gets only the tools that the harness has.
104
105The mode of a profile says who starts it:
106
107- `primary`: it answers the user's turns. Pick it with the `agent` command or setting.
108- `subagent`: the main model starts it as a child.
109- `all`: both.
110
111The `plan` agent and the `plan` mode of [permissions](./permissions#pick-a-mode) are separate. The agent has only the read tools and `todo_write`. The mode denies edits and commands for every agent. To drop the built-in agents, set `builtIns: false`.
112
113## Add your own agent
114
115Give `agents()` a profile. This one reviews the changes with a stronger model:
116
117```ts group=harness-agents
118import type { AnyTextAdapter } from '@tanstack/ai'
119import type { AgentProfile } from '@tanstack/ai-harness/plugins/node'
120
121const review: AgentProfile = {
122 name: 'review',
123 description: 'Reviews the changes and lists the risks. Does not change files.',
124 mode: 'all',
125 model: 'claude-opus-5-5',
126 system: 'You review code. Read the diff, then list each risk with its file and line.',
127 tools: ['read_file', 'bash'],
128 permissions: [{ tool: 'bash', resource: 'git diff*', decision: 'allow' }],
129 steps: 20,
130}
131
132const models: Record<string, AnyTextAdapter> = {
133 'claude-opus-5-5': anthropicText('claude-opus-5-5'),
134}
135
136export const withReview = agents({
137 adapter: (model) => models[model] ?? sonnet,
138 agents: [review],
139})
140```
141
142`name`, `description`, and `mode` are required. The other fields:
143
144| Field | What it does |
145| --- | --- |
146| `model` | A model id. `adapter` turns it into an adapter. Without it, a primary agent keeps the harness model, and a subagent uses the model of the turn that started it. |
147| `system` | Text for the system prompt of each turn of this agent. |
148| `tools` | The tools that this agent can call, by name. A `*` matches any text, for example `todo_*`. Without it, the agent gets every tool. |
149| `permissions` | [Permission rules](./permissions#write-rules) that count while this agent is the primary agent. At that time, they also apply in each subagent run. |
150| `steps` | The step limit. See [Limit the steps](#limit-the-steps). |
151| `hidden` | `true` leaves the agent out of the `agent` command and setting. The model can still start it as a subagent. |
152
153- A profile replaces an earlier profile with the same name. The order is the built-in agents, then `dirs`, then `agents`. So a profile named `plan` replaces the built-in `plan`.
154- `default` names the primary agent of a new session. Without it, the first primary agent is the default, which is `build`.
155- In the main turn, a model that the thread stored with `session.configure` wins over the profile `model`. See [What wins](./thread-settings#what-wins).
156- A `tools` list also filters the subagent tools. To let the agent start subagents, add their tool names, for example `explore`.
157
158## Load agents from Markdown files
159
160Keep your agents next to your code as Markdown files. Save this file as `.agents/agents/review.md`:
161
162```md
163---
164description: Reviews the changes and lists the risks. Does not change files.
165mode: all
166model: claude-opus-5-5
167tools: [read_file]
168steps: 20
169---
170
171You review code. Read the diff, then list each risk with its file and line.
172```
173
174Then give `agents()` the folder:
175
176```ts group=harness-agents
177export const fromFiles = agents({
178 adapter: (model) => models[model] ?? sonnet,
179 dirs: [`${root}/.agents/agents`],
180})
181```
182
183- The file name is the agent name, here `review`.
184- The body is the system prompt.
185- The frontmatter sets `description`, `mode`, `model`, `tools`, `steps`, and `hidden: true`. The default `mode` is `all`.
186- An unknown `mode`, or a `steps` that is not a whole number, stops `host.open` with an error, for example `Agent file review.md: "mode" is not valid.`
187- The plugin reads the folder when a session opens, and again at each `session.reloadPlugins()`. A folder that it cannot read gives no agents.
188
189## Limit the steps
190
191An agent can call tools for a long time. `steps` sets the most model calls with tools in one run. With `steps: 20`:
192
1931. The agent makes up to 20 model calls with tools.
1942. Call 21 gets `toolChoice: 'none'` and a note: answer now, and say what is left to do.
1953. Then the run stops.
196
197The limit counts again in each turn of a primary agent and in each run of a subagent.
198
199Some providers do not obey `'none'`. On Amazon Bedrock, `'none'` after a tool call goes to the model as `auto`. See [Provider notes](../tools/tools#provider-notes). If the model calls a tool on the last call anyway:
200
201- The tool does not run.
202- The model gets this tool result: `{ "error": "Step limit reached. Answer without calling tools." }`
203- The run stops. That turn can end with no text answer.
204
205## Let the model start a subagent
206
207A profile with the mode `subagent` or `all` is also a subagent. The main model gets a tool for each one, with the name of the agent, for example `explore`. Ask the build agent: `use explore to find where the theme is set`.
208
209A subagent run gets these parts of its profile:
210
211- The tools of the turn that started it, filtered by its `tools`.
212- Its `system` prompt.
213- Its `model`, or the model of the turn that started it.
214- Its step limit.
215
216A subagent run stops after 50 model calls, or when an answer is cut at the output limit. The harness also limits the depth and the number of children. See [Stay within limits](./subagents#stay-within-limits). To give one run from code its own budget and time limit, see [Give one run a budget and a time limit](./subagents#give-one-run-a-budget-and-a-time-limit).
217
218## Known limits
219
220- A subagent run uses the `permissions` of the primary agent, not the rules of its own profile. For example, when `build` is the primary agent and starts `review`, the `build` rules apply and the `review` rules do not. Put rules that must hold in every run in `permissions({ rules })`. See [Rules in subagents](./permissions#rules-in-subagents).
221- A Markdown agent cannot set `permissions`. Put those rules in a profile in code.
222- On Amazon Bedrock, the model can still call a tool on the last call. The tool does not run, but the turn can end with no text answer.
223- A subagent run gets the tools and the model of its turn from the memory of the host that ran that turn. On another host, or after a restart, the run fails with `Agent <name> cannot go on here`. Start it again from a turn.
224- A subagent run that your code starts, not a turn, has no tools of a turn. Give its profile a `model`.
225
226## What you have now
227
228- A read-only `plan` agent, and a `build` agent to do the work.
229- Your own agents in code or in Markdown files, each with its own prompt, tools, model, and rules.
230- A step limit that ends a run with a text answer.
231- Subagents that the main model starts to search and to research.