ai
Five assistants, one loop

The admin side of our platform has a chat box, and behind the chat box there are several assistants. A router reads the message and picks one of five (configuration, bookings, member communication, insights, help), and a website builder and a couple of member-facing ones sit alongside. Each has its own prompt and its own tools, but they all run on the same loop, and the loop is short enough to print.
The loop
Strip the types and it's this. Bind the assistant's tools to the model, put the system prompt and the conversation in front of it, and go round:
for (let round = 0; round < maxRounds; round++) {
const response = await llmWithTools.invoke(messages)
if (!response.tool_calls?.length) return response.content // done: text for the user
messages.push(response)
for (const call of response.tool_calls) {
let result
try {
result = await toolsByName[call.name].invoke(call.args)
} catch (err) {
result = `Error: ${err.message}` // the model sees failures as text, not exceptions
}
messages.push(new ToolMessage({ content: result, tool_call_id: call.id }))
}
}
// Out of rounds: one last call with no tools, so the user gets a sentence, not silence.
return (await llm.invoke(messages)).content
The model decides which tools to call. We run them, append each result as a
ToolMessage, and ask again. When a turn comes back with no tool calls, the
text is the answer. LangChain's bindTools does the schema plumbing, and the
model behind it is gpt-4o-mini unless an assistant asks for something else.
Three details in there matter more than they look.
Failures are data. A tool that throws doesn't end the conversation. Its error message becomes the tool's result and goes back to the model, and we want that. "Contact not found" is information the model can act on (ask the user to spell the name, try a broader search) and an exception would have thrown it away.
The cap is per assistant. Help gets six rounds, bookings and configuration eight, the website builder ten, because building a page legitimately takes more steps than answering a how-to question. No assistant gets unlimited rounds.
Running out of rounds is not an error. The final call has the tools removed, so the model has no option but to write to the user with whatever it has. The alternative is a timeout and an empty bubble, which is what every early version of this did, and users remember that.
The router and the tools
The router is an assistant with no tools. It returns a JSON object with which of the five to hand off to, a confidence, and (when confidence is under 0.8) a clarifying question instead of a guess. We could have had one assistant carrying everyone's tools, and that fails the way you'd predict. Forty tool schemas in front of the model on every turn, and a much better chance of the configuration tool being called for a question about revenue. Five narrow assistants with six to ten tools each behave better. They're also easier to test, because each one's tool set is small enough to write assertions about.
The tools themselves are closures over the organisation. The insights
assistant's get_aggregated_metrics takes a date range and that's all. The
organisation ID was bound when the tool was created for this request, and the
model cannot supply a different one. There's no text-to-SQL anywhere in this.
Every insights tool calls a scoped domain service that already existed for the
dashboard, so the assistant is a new front door onto old, tested code.
The retry that would not stop
This post started with a failure in the help assistant. It answers from
documentation, and one of its tools fetches a document by name. A user asked
about a feature whose document didn't exist yet. The tool returned
Document not found. The model, reasonably, tried again with the same name and
arguments, and got the same result. Six rounds later the cap fired and the user
received a confident summary of nothing.
It happened often enough that we could describe it. There's a failed call with a clear error, and a model that reads the error, decides the right response is to try again, and tries again identically. Not with a different document, not with a broader search. A person doesn't do that, but a model on a low temperature does it with some enthusiasm.
The fix that landed on Tuesday is small enough to quote:
const argsKey = JSON.stringify(call.args ?? {})
const priorFailure = executed.find(
(p) => p.name === call.name && JSON.stringify(p.args ?? {}) === argsKey
&& /^Error:|not found|not implemented/i.test(p.result)
)
if (priorFailure) {
result = `Stop: this exact tool call already failed above with: ${priorFailure.result}.
Do NOT retry with the same arguments. Reply to the user explaining the problem
and what they need to do.`
} else {
result = await tool.invoke(call.args) // as before
}
Before running a tool, look back at what has already run in this conversation. If the same tool was called with byte-identical arguments and the result was a failure, do not run it. Feed back an instruction instead, one that names the earlier error and tells the model to talk to the user. The loop still counts the round, so a stubborn model still hits the cap, but in practice it takes the hint on the first try. The user gets "there is no documentation for that yet; here is what I can tell you" instead of a wall.
Where the judgement is
The parts of agent code that matter tend not to look like AI at all. Bind tools, call model, run tools, repeat - that's a for loop, and any framework will hand you one. What we had to decide ourselves was at the edges: what the model sees when a tool fails, how many times it may go round, what happens when it runs out, and what you refuse to let it do twice. Ordinary engineering decisions, just with a slightly unusual caller.
The guard itself is a regular expression over a string. It took an afternoon, and it removed the single most visible failure our assistants had.


