Part of the Determesh series.
What
Determesh talks to the models through Spring AI, but it does not let Spring AI run the tool loop. The model returns tool calls, Determesh runs them one round at a time through Spring AI’s ToolCallingManager, records each step in the thread, and decides whether there is another round.
Why
A loop you cannot see into cannot be judged
The convenient mode of any agent library is: give it tools, give it a prompt, get the final answer. The tool calls happen inside. For a demo that is ideal. For Determesh it hides exactly the things the system needs.
The evaluator rates work in progress every few steps. That needs the steps. The company’s admin reads a thread’s internal work in the panel. That needs the steps written down as they happen. Billing counts each method call as an action. The audit log records each access decision. All of it needs control between rounds, not after the last one.
Headless agents were the first idea
The original concept ran a headless coding agent per thread, with the model plugged in through an OpenAI-compatible endpoint. It was dropped before any code was written. Coding agents ship with file and shell tools that would all need switching off, a process per thread is heavy when one message can start several threads, their session files need their own isolation on disk, and Determesh already has its own loop. A CLI agent would have been a loop inside a loop.
How
One round at a time
Spring AI’s chat model is called with tool execution turned off on the request, so it returns the model’s tool calls instead of running them. Determesh then:
- Records the model’s message, with any reasoning, in the role’s conversation.
- Runs the tool calls through
ToolCallingManager, which resolves each call to our Kotlin function. - Records each result as a step, marked with which role took it.
- Updates the step count for the whole request.
- Asks the progress check whether it is time to evaluate.
- Decides: another round, or the run ends.
The run ends when the orchestrator calls finish, or one of the tools that hands work off, like splitting into tasks.
One count for every model on the request
The orchestrator asks a resource assistant, which runs its own loop of method calls. Both loops feed the same counter for the run. Every ten steps, the evaluator sees all of them together, each marked with who took it, and rates whether the work is moving toward the goal.
This is why the assistants’ loops have to be ours too. A stop decided in the middle of an assistant’s work must reach the orchestrator’s loop.
A stop must not become text
That turned out to be the subtle part. A tool function that throws an exception does not stop anything: the exception becomes the tool’s result, and the model reads it as text, “Error: stopped by evaluator”, and carries on.
So a stop decided inside a tool call is stored on the run, and thrown from the loop’s own step callback after the tool returns, where it ends the loop instead of being handed to the model. And when a stop ends an ask halfway, whatever the resource had already found goes up with it, so the user still gets what was fetched. That second part was a bug before it was a rule.
Reasoning has to go back
DeepSeek’s thinking mode refuses a tool round if the earlier reasoning_content is missing from the history. Spring AI keeps the reasoning in the message metadata and sends it back, which only works because our recorded conversation keeps that metadata intact. Rebuilding messages by hand from stored text would have broken it.
When the provider is down
Any failure from the model provider other than “you sent something invalid” marks the provider down, in Redis, shared by every instance. The run goes back to the queue exactly as it was, with its attempt given back. While the provider is down, workers take no new runs and the meta extractor waits. A tiny call every few seconds checks when it is back.
The effect is that an outage only delays replies. No thread fails because of it, and users get no error message. They get the reply late.
A trace for debugging
A trace file, off by default and used only on dev, appends every model call in full: the messages, the tools offered, the answer, the tool calls and the reasoning. Several of the bugs later in this series were found by replaying a traced round ten times with one change and counting the outcomes. Without the loop in our hands, there would have been nothing to replay.
Every call is logged
Each model call writes a row with token counts, cache hits, latency and errors. That row is the meter for billing, and it is how we know that most of the orchestrator’s prompt is served from the provider’s cache. That is the next post.
Previous: Threads as Postgres Rows, With No Queue Server
Next: Prompt Caching by Design