Chapter 5: Writing the agent loop by hand

This chapter replaces Spring AI’s tool loop with one you write and control.

Objectives:

  • Stop a tool call before it runs, and read it.
  • Write the four-step agent loop.
  • Return every run as a RunResult with one stop reason.

In Chapter 4, ChatClient ran the tool for you. Your code saw only the final text. A harness must see each tool call. It decides whether the call runs, and when the run stops.

Step 1: Halt the tool call

ChatClient runs tools through an advisor, the ToolCallingAdvisor. ChatClient adds it to every request that has tools. Chapter 4, Step 3 explains advisors. Remove this one, and the tool calls come back to your code.

ToolCallingAdvisor is a recursive advisor. It sends the request down the rest of the chain more than once. Figure 5.1 shows the chain with the advisor and without it.

Chapter 4: advisor onAssistantToolCallingAdvisorBeforeAfter?ChatModelCallAdvisorGemmaThe dashed box repeats while the reply has tool calls.Chapter 5: advisor offAgentLoopyour looprepeatsToolCallingAdvisorremoved bytoolCallingAdvisorAutoRegister(false)ChatModelCallAdvisorGemmaOne model call. The tool calls come back unrun.
Figure 5.1. The same chain, with and without ToolCallingAdvisor. The ? asks whether the reply has tool calls. Redrawn from the Spring AI reference, Recursive Advisors.
  • Left: Before sends the request down. After checks the reply. A tool call sends it back through Before, after the tool runs.
  • Right: the request goes down once. The reply comes back with its tool calls. AgentLoop decides what to do next.

Add this method to Assistant:

/**
 * Asks the model one question and stops before any tool runs.
 *
 * <p>{@code toolCallingAdvisorAutoRegister(false)} removes the {@code ToolCallingAdvisor}.
 * The model still sees the tools. Its tool calls come back to this code as data.
 *
 * @param question the user's question, in plain text
 * @return the tool calls the model requested; empty when it answered in text
 */
public List<ToolCall> askHaltToolCall(String question) {
    log.debug("[halt] ToolCallingAdvisor is off. No tool will run. Question: {}", question);
    AssistantMessage reply = chat.prompt()
            .system(SYSTEM)
            .user(question)
            .tools(logTools)
            .advisors(AdvisorParams.toolCallingAdvisorAutoRegister(false))
            .call()
            .chatResponse()
            .getResult()
            .getOutput();
    if (reply.hasToolCalls()) {
        for (ToolCall call : reply.getToolCalls()) {
            log.debug("[halt] Model requested {} {}. Not run.", call.name(), call.arguments());
        }
    } else {
        log.debug("[halt] Model answered in text, with no tool call: {}", reply.getText());
    }
    return reply.getToolCalls();
}
  • The request is the same as askAutoToolCall. One line differs: .advisors(…).
  • .chatResponse() returns the full response, not only the text.
  • getOutput() returns the model’s reply as an AssistantMessage.
  • getToolCalls() lists the tools the model wants to run.

Add the endpoint to TestController:

@GetMapping("/askHaltToolCall")
public List<ToolCall> askHaltToolCall(@RequestParam String input) {
    return assistant.askHaltToolCall(input);
}

Python has nothing to turn off. bind_tools sends the tool definitions with the request, and no agent runs them. Add this method to Assistant, and import SystemMessage and ToolCall from langchain_core.messages:

def ask_halt_tool_call(self, question: str) -> list[ToolCall]:
    """Asks the model one question and stops before any tool runs.

    ``bind_tools`` sends the tool definitions with the request. No agent runs them.
    The model's tool calls come back to this code as data.

    Args:
        question: the user's question, in plain text

    Returns:
        the tool calls the model requested; empty when it answered in text
    """
    log.debug("[halt] No agent. No tool will run. Question: %s", question)
    reply = self.chat.bind_tools(tools_from(self.log_tools)).invoke([SystemMessage(self.SYSTEM), HumanMessage(question)])
    if reply.tool_calls:
        for call in reply.tool_calls:
            log.debug("[halt] Model requested %s %s. Not run.", call["name"], call["args"])
    else:
        log.debug("[halt] Model answered in text, with no tool call: %s", reply.text)
    return reply.tool_calls
  • The request holds the same messages as the Chapter 3 version. bind_tools adds the tools.
  • .invoke(…) returns the reply as an AIMessage.
  • reply.tool_calls lists the tools the model wants to run. Each one is a ToolCall dictionary.

Add the route and the method to TestController:

self.router.add_api_route("/askHaltToolCall", self.ask_halt_tool_call)
def ask_halt_tool_call(self, input: str) -> list[ToolCall]:
    """Sends the question to the model and returns its tool calls without running them.

    Args:
        input: the question; the request fails without it

    Returns:
        the tool calls as JSON: name, args, id, and type
    """
    return self.assistant.ask_halt_tool_call(input)

Restart the app. Ask the Chapter 4 question:

curl -G http://localhost:8080/askHaltToolCall --data-urlencode "input=how many error events in checkout service?"

Gemma returns one tool call. Nothing ran:

[{"id":"3EFxibX93b79PlmooqxhndVzdVWXnAlJ","type":"function","name":"countErrors","arguments":"{\"minutes\":60,\"service\":\"checkout\"}"}]
  • A tool call is data: an id, a tool name, and JSON arguments.
  • The model chose minutes: 60. The question gave no window. Your value will differ between runs.
  • A question that needs no tool returns []. Try input=what is a harness?.
[{"name":"count_errors","args":{"minutes":15,"service":"checkout"},"id":"YK29gpSrDYhXhh77r88uVpGwRTIyAOHZ","type":"tool_call"}]
  • A tool call is data: a tool name, the arguments as a dictionary, and an id.
  • LangChain parses the arguments for you. Spring AI keeps them as a JSON string.
  • A question that needs no tool returns []. Try input=what is a harness?.

The log ends after one model call:

00:15:48.922 DEBUG DispatcherServlet                        GET "/askHaltToolCall?input=how+many+error+events+in+the+checkout+service+in+the+last+15+minutes%3F", parameters={masked}
00:15:48.923 DEBUG Assistant                                [halt] ToolCallingAdvisor is off. No tool will run. Question: how many error events in the checkout service in the last 15 minutes?
00:15:49.829 INFO  ChatModelCompletionObservationHandler    Chat Model Completion:
[]
00:15:49.830 INFO  ChatModelPromptContentObservationHandler Chat Model Prompt Content:
["You are a careful assistant. Answer in two sentences or fewer.", "how many error events in the checkout service in the last 15 minutes?"]
00:15:49.830 DEBUG Assistant                                [halt] Model requested countErrors {"minutes":15,"service":"checkout"}. Not run.
00:15:49.830 DEBUG DispatcherServlet                        Completed 200 OK
16:07:36.707 DEBUG harness.assistant                        [halt] No agent. No tool will run. Question: how many error events in the checkout service in the last 15 minutes?
16:07:36.711 INFO  ChatModelPromptContent                   Chat Model Prompt Content:
['You are a careful assistant. Answer in two sentences or fewer.', 'how many error events in the checkout service in the last 15 minutes?']
16:07:40.151 INFO  httpx2                                   HTTP Request: POST http://localhost:1234/v1/chat/completions "HTTP/1.1 200 OK"
16:07:40.155 INFO  ChatModelCompletion                      Chat Model Completion:
[]
16:07:40.155 DEBUG harness.assistant                        [halt] Model requested count_errors {'minutes': 15, 'service': 'checkout'}. Not run.
16:07:40.156 INFO  uvicorn.access                           127.0.0.1:63481 - "GET /askHaltToolCall?input=how%20many%20error%20events%20in%20the%20checkout%20service%20in%20the%20last%2015%20minutes%3F HTTP/1.1" 200

No Executing tool call line and no [tool] line appear. The tool never ran.

Your code now owns the call. It can run it, change it, or refuse it.

Step 2: The RunResult

A run can end in more than one way. Return the reason with the answer.

Create RunResult.java:

package com.garageeducation.harness;

/**
 * The outcome of one agent run.
 *
 * @param answer the model's final text; {@code null} when the run stopped before a final answer
 * @param reason the reason the run stopped
 * @param steps  the number of model calls the run made
 * @param tokens the total tokens the run used, as reported by the model server
 */
public record RunResult(String answer, StopReason reason, int steps, int tokens) {

    /**
     * Why a run stopped. Each run stops for exactly one reason.
     */
    public enum StopReason {

        /** The model replied without tool calls. Its text is the answer. */
        FINAL_ANSWER,

        /** The run made {@code MAX_STEPS} model calls without a final answer. */
        STEP_LIMIT
    }
}

Create run_result.py:

"""The outcome of one agent run."""

from dataclasses import dataclass
from enum import StrEnum


@dataclass(frozen=True)
class RunResult:
    """The outcome of one agent run.

    Every run returns one ``RunResult``. The ``StopReason`` says why the run ended.

    Attributes:
        answer: the model's final text; ``None`` when the run stopped before a final answer
        reason: the reason the run stopped
        steps: the number of model calls the run made
        tokens: the total tokens the run used, as reported by the model server
    """

    class StopReason(StrEnum):
        """Why a run stopped. Each run stops for exactly one reason."""

        FINAL_ANSWER = "FINAL_ANSWER"
        """The model replied without tool calls. Its text is the answer."""

        STEP_LIMIT = "STEP_LIMIT"
        """The run made ``MAX_STEPS`` model calls without a final answer."""

    answer: str | None
    reason: StopReason
    steps: int
    tokens: int
  • @dataclass(frozen=True) makes an object whose fields never change, like a Java record.
  • StopReason is a StrEnum nested in RunResult, like the nested Java enum. FastAPI writes each value as its name.
  • str | None means the answer can be missing, like a null String in Java.

Chapter 6 adds three more reasons.

Step 3: The loop

The loop has four steps. Figure 5.2 shows them.

Figure 5.2. The four steps and the step cap. Chapter 6 adds three more stop rules.

Create AgentLoop.java:

package com.garageeducation.harness;

import java.util.ArrayList;
import java.util.List;

import org.springframework.ai.chat.client.AdvisorParams;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.messages.AssistantMessage;
import org.springframework.ai.chat.messages.AssistantMessage.ToolCall;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.SystemMessage;
import org.springframework.ai.chat.messages.ToolResponseMessage;
import org.springframework.ai.chat.messages.ToolResponseMessage.ToolResponse;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.support.ToolCallbacks;
import org.springframework.ai.tool.ToolCallback;
import org.springframework.stereotype.Service;

import com.garageeducation.harness.RunResult.StopReason;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * The agent loop, written by hand.
 *
 * <p>Each step sends the full message list to the model. A reply without tool calls ends the run.
 * Otherwise, the loop runs each tool, adds the results to the messages, and repeats.
 *
 * <p>{@link ChatClient} runs tools by itself through its {@code ToolCallingAdvisor}.
 * This class turns that advisor off, so the model's tool calls come back to this code.
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class AgentLoop {

    /** The most model calls one run can make. */
    static final int MAX_STEPS = 5;


    private static final String SYSTEM = "You are a careful assistant. Answer in two sentences or fewer.";

    private final ChatClient chat;
    private final LogTools logTools;

    /**
     * Runs the loop for one question.
     *
     * @param question the user's question, in plain text
     * @return the answer, the stop reason, the step count, and the tokens used
     */
    public RunResult run(String question) {
        List<Message> messages = new ArrayList<>();
        messages.add(new SystemMessage(SYSTEM));
        messages.add(new UserMessage(question));

        int tokens = 0;
        log.debug("[loop] Start. Your code runs the tools. Question: {}", question);

        for (int step = 1; step <= MAX_STEPS; step++) {
            // 1. Send the messages to the model.
            log.debug("[loop] Step {}/{}: sending {} messages to the model", step, MAX_STEPS, messages.size());
            ChatResponse response = chat.prompt()
                    .messages(messages)
                    .tools(logTools)
                    .advisors(AdvisorParams.toolCallingAdvisorAutoRegister(false))
                    .call()
                    .chatResponse();
            AssistantMessage reply = response.getResult().getOutput();
            messages.add(reply);
            tokens += response.getMetadata().getUsage().getTotalTokens();

            // 2. A reply without tool calls is the answer.
            if (!reply.hasToolCalls()) {
                log.debug("[loop] Step {}: no tool calls. Stop: FINAL_ANSWER, {} tokens. Answer: {}", step, tokens, reply.getText());
                return new RunResult(reply.getText(), StopReason.FINAL_ANSWER, step, tokens);
            }
            log.debug("[loop] Step {}: model requested {} tool call(s), {} tokens so far", step, reply.getToolCalls().size(), tokens);

            // 3. Run each tool. 4. Add the results to the messages.
            List<ToolResponse> results = new ArrayList<>();
            for (ToolCall call : reply.getToolCalls()) {
                log.debug("[loop] Step {}: your code runs {} {}", step, call.name(), call.arguments());
                results.add(new ToolResponse(call.id(), call.name(), runTool(call)));
            }
            messages.add(ToolResponseMessage.builder().responses(results).build());
        }
        log.debug("[loop] Used all {} steps without a final answer. Stop: STEP_LIMIT", MAX_STEPS);
        return new RunResult(null, StopReason.STEP_LIMIT, MAX_STEPS, tokens);
    }

    /**
     * Runs one tool call and returns its result as text.
     *
     * <p>{@code ToolCallbacks.from} wraps each {@code @Tool} method in a {@link ToolCallback}.
     * The loop picks the callback whose name matches the call. An unknown name returns an
     * error text to the model instead of failing the run.
     *
     * @param call one tool call from the model's reply
     * @return the tool's result, or an error text for an unknown tool
     */
    private String runTool(ToolCall call) {
        for (ToolCallback tool : ToolCallbacks.from(logTools)) {
            if (tool.getToolDefinition().name().equals(call.name())) {
                return tool.call(call.arguments());
            }
        }
        log.warn("[loop] Model requested an unknown tool: {}", call.name());
        return "Unknown tool: " + call.name();
    }
}

Read the code in four parts:

  • The message list. It starts with the system and user messages. Each step adds the reply and the tool results. The model has no memory, so each call resends the full list.
  • The model call. It is the Step 1 request with .messages(messages) in place of .system() and .user().
  • The tool results. Each ToolResponse keeps the call’s id, so the model can match each result to its request. All results go back in one ToolResponseMessage.
  • runTool. ToolCallbacks.from(logTools) wraps each @Tool method in a ToolCallback. The loop finds the callback with the call’s name. tool.call(arguments) parses the JSON arguments and runs countErrors.

Create agent_loop.py:

"""The agent loop, written by hand."""

import logging
from typing import Final

from langchain_core.language_models import BaseChatModel
from langchain_core.messages import AIMessage, BaseMessage, HumanMessage, SystemMessage, ToolCall, ToolMessage

from harness.log_tools import LogTools
from harness.run_result import RunResult
from harness.tool_support import tools_from

log: Final = logging.getLogger(__name__)

StopReason = RunResult.StopReason


class AgentLoop:
    """The agent loop, written by hand.

    Each step sends the full message list to the model. A reply without tool calls ends the run.
    Otherwise, the loop runs each tool, adds the results to the messages, and repeats.

    ``bind_tools`` only sends the tool definitions. No agent runs the tools, so the model's
    tool calls come back to this code.
    """

    MAX_STEPS: Final = 5
    """The most model calls one run can make."""

    SYSTEM: Final = "You are a careful assistant. Answer in two sentences or fewer."

    def __init__(self, chat: BaseChatModel, log_tools: LogTools) -> None:
        self.chat = chat
        self.log_tools = log_tools

    def run(self, question: str) -> RunResult:
        """Runs the loop for one question.

        Args:
            question: the user's question, in plain text

        Returns:
            the answer, the stop reason, the step count, and the tokens used
        """
        messages: list[BaseMessage] = [SystemMessage(self.SYSTEM), HumanMessage(question)]
        model = self.chat.bind_tools(tools_from(self.log_tools))

        tokens = 0
        log.debug("[loop] Start. Your code runs the tools. Question: %s", question)

        for step in range(1, self.MAX_STEPS + 1):
            # 1. Send the messages to the model.
            log.debug("[loop] Step %d/%d: sending %d messages to the model", step, self.MAX_STEPS, len(messages))
            reply: AIMessage = model.invoke(messages)
            messages.append(reply)
            tokens += reply.usage_metadata["total_tokens"] if reply.usage_metadata else 0

            # 2. A reply without tool calls is the answer.
            if not reply.tool_calls:
                log.debug("[loop] Step %d: no tool calls. Stop: FINAL_ANSWER, %d tokens. Answer: %s", step, tokens, reply.text)
                return RunResult(reply.text, StopReason.FINAL_ANSWER, step, tokens)
            log.debug("[loop] Step %d: model requested %d tool call(s), %d tokens so far", step, len(reply.tool_calls), tokens)

            # 3. Run each tool. 4. Add the results to the messages.
            for call in reply.tool_calls:
                log.debug("[loop] Step %d: your code runs %s %s", step, call["name"], call["args"])
                messages.append(ToolMessage(self.run_tool(call), tool_call_id=call["id"], name=call["name"]))
        log.debug("[loop] Used all %d steps without a final answer. Stop: STEP_LIMIT", self.MAX_STEPS)
        return RunResult(None, StopReason.STEP_LIMIT, self.MAX_STEPS, tokens)

    def run_tool(self, call: ToolCall) -> str:
        """Runs one tool call and returns its result as text.

        ``tools_from`` wraps each ``@tool`` method in a LangChain tool. The loop picks the tool
        whose name matches the call. An unknown name returns an error text to the model instead
        of failing the run.

        Args:
            call: one tool call from the model's reply

        Returns:
            the tool's result, or an error text for an unknown tool
        """
        for tool in tools_from(self.log_tools):
            if tool.name == call["name"]:
                return str(tool.invoke(call["args"]))
        log.warning("[loop] Model requested an unknown tool: %s", call["name"])
        return "Unknown tool: " + call["name"]

Read the code in four parts:

  • The message list. It starts with the system and user messages. Each step adds the reply and the tool results. The model has no memory, so each call resends the full list.
  • The model call. bind_tools runs once, before the loop. Each step calls model.invoke(messages).
  • The tool results. Each ToolMessage keeps the call’s id in tool_call_id, so the model can match each result to its request. Each result goes back in its own ToolMessage.
  • run_tool. tools_from(self.log_tools) wraps each @tool method in a LangChain tool. The loop finds the tool with the call’s name. tool.invoke(args) runs count_errors.

The loop reads two message types and builds one more. Figure 5.3 shows how a tool call and its result fit together.

AssistantMessagethe model's replyhasToolCalls()getToolCalls()0..*ToolCallidtypenamearguments (JSON)From the Step 1 log:id: 3EFxibX9…type: functionname: countErrors{"minutes":15,"service":"checkout"}ToolResponseMessageyour code builds itbuilder().responses(list)1..*ToolResponseidnameresponseDataAfter runTool:id: 3EFxibX9…name: countErrorsresponseData: 412same idsame id
Figure 5.3. The tool call and the tool result, as Spring AI classes. Redrawn and cut down from the Spring AI reference, Prompts (Message API).
  • ToolCall comes from the model. Your code reads it. It never changes it.
  • ToolResponse comes from your code. It copies the call’s id and name, and adds the result.
  • One ToolResponseMessage carries every result from one step.
  • AIMessage.tool_calls plays the part of getToolCalls(). Each ToolCall is a dictionary with name, args, and id.
  • ToolMessage plays the part of ToolResponse. Your code sets tool_call_id to the call’s id, and the content to the result.
  • One ToolMessage per call. Python has no wrapper message for a step’s results.

Figure 5.4 shows one run. Compare it with Figure 4.4. AgentLoop takes the place of ToolCallingAdvisor and ToolCallingManager.

Figure 5.4. A two-step run. Your loop runs the tool, not Spring AI. Step 2 resends every message from step 1.

The framework’s version of this loop

Spring AI ships a helper for steps 3 and 4: ToolCallingManager. The Spring AI reference drives the same loop with it:

while (response.hasToolCalls()) {
    ToolExecutionResult result = toolCallingManager.executeToolCalls(prompt, response);
    prompt = new Prompt(result.conversationHistory(), chatOptions);
    response = chatModel.call(prompt);
}

(Source: Spring AI reference, ChatModel Tool Calling, 2.0.1.)

executeToolCalls runs every tool call in the reply. conversationHistory() returns the old messages plus the reply and the tool results. Table 5.1 compares it with runTool.

AgentLoop.runToolToolCallingManager.executeToolCalls
Finds the toolYour for loop, by nameThe manager, by name
Builds the next messagesYour code adds the reply and a ToolResponseMessageReturns the full history
RunsOne call at a time, so your code can check each oneEvery call in the reply at once
Call limitsNone40 per tool and 150 in total
A tool throws an exceptionThe HTTP request failsThe error text goes back to the model

Table 5.1. Your runTool and Spring AI's helper. This course writes the steps by hand to show each one.

  • Writing the loop by hand skips Spring AI’s tool limits and error handling. The Spring AI reference warns about this. Chapter 6 adds limits of your own.
  • Checking one call at a time is what Chapter 7 needs. It checks each call before it runs.

LangChain has no helper for steps 3 and 4 outside an agent. Its tools node, ToolNode, runs only inside a LangGraph graph. The closest match is create_agent from Chapter 4. Table 5.1 compares it with run_tool.

AgentLoop.run_toolcreate_agent tools node
Finds the toolYour for loop, by nameThe tools node, by name
Builds the next messagesYour code adds the reply and one ToolMessage per callThe agent adds them to its state
RunsOne call at a time, so your code can check each oneEvery call in the reply at once
Call limitsNoneNone by default. Add ToolCallLimitMiddleware.
A tool throws an exceptionThe HTTP request failsBad arguments go back to the model. Other errors fail the run.

Table 5.1. Your run_tool and LangChain's agent. This course writes the steps by hand to show each one.

  • Checking one call at a time is what Chapter 7 needs. It checks each call before it runs.

Step 4: Run the loop

Add the endpoint to TestController. Spring returns the record as JSON.

@GetMapping("/run")
public RunResult run(@RequestParam String input) {
    return agentLoop.run(input);
}

Add a private final AgentLoop agentLoop; field next to assistant. Lombok adds it to the constructor.

This is the full test_controller.py. FastAPI returns the dataclass as JSON:

"""Exposes Assistant and AgentLoop over HTTP for manual tests."""

from fastapi import APIRouter
from fastapi.responses import PlainTextResponse
from langchain_core.messages import ToolCall

from harness.agent_loop import AgentLoop
from harness.assistant import Assistant
from harness.run_result import RunResult


class TestController:
    """Exposes ``Assistant`` and ``AgentLoop`` over HTTP for manual tests.

    It plays the part of the Spring ``@RestController``. Each ``add_api_route`` line plays
    the part of one ``@GetMapping``.

    Example: ``GET http://localhost:8080/askAutoToolCall?input=how many error events in checkout service?``
    """

    def __init__(self, assistant: Assistant, agent_loop: AgentLoop) -> None:
        self.assistant = assistant
        self.agent_loop = agent_loop
        self.router = APIRouter()
        self.router.add_api_route("/askAutoToolCall", self.ask_auto_tool_call, response_class=PlainTextResponse)
        self.router.add_api_route("/askHaltToolCall", self.ask_halt_tool_call)
        self.router.add_api_route("/run", self.run)

    def ask_auto_tool_call(self, input: str) -> str:
        """Sends the question to the model. The agent runs the tools.

        Args:
            input: the question; the request fails without it

        Returns:
            the model's text reply
        """
        return self.assistant.ask_auto_tool_call(input)

    def ask_halt_tool_call(self, input: str) -> list[ToolCall]:
        """Sends the question to the model and returns its tool calls without running them.

        Args:
            input: the question; the request fails without it

        Returns:
            the tool calls as JSON: name, args, id, and type
        """
        return self.assistant.ask_halt_tool_call(input)

    def run(self, input: str) -> RunResult:
        """Runs the handwritten agent loop for the question.

        Args:
            input: the question; the request fails without it

        Returns:
            the run result as JSON: answer, reason, steps, and tokens
        """
        return self.agent_loop.run(input)

Create the AgentLoop in create_app() and pass it to the controller:

def create_app() -> FastAPI:
    """Creates every object and wires them together.

    It plays the part of Spring's application context: one place builds each object once
    and passes it to the constructors that need it.

    Returns:
        the web app, with the ``TestController`` routes
    """
    properties = ApplicationProperties.load()
    chat = AiConfig(properties).chat_model()
    log_tools = LogTools()
    controller = TestController(Assistant(chat, log_tools), AgentLoop(chat, log_tools))
    app = FastAPI(title="harness")
    app.include_router(controller.router)
    return app

Restart the app, then run:

curl -G http://localhost:8080/run --data-urlencode "input=how many error events in checkout service?"
{"answer":"There were 412 error events in the checkout service over the last 15 minutes.","reason":"FINAL_ANSWER","steps":2,"tokens":727}

The log shows each step of your loop:

00:15:50.946 DEBUG DispatcherServlet                        GET "/run?input=how+many+error+events+in+the+checkout+service+in+the+last+15+minutes%3F", parameters={masked}
00:15:50.946 DEBUG AgentLoop                                [loop] Start. Your code runs the tools. Question: how many error events in the checkout service in the last 15 minutes?
00:15:50.946 DEBUG AgentLoop                                [loop] Step 1/5: sending 2 messages to the model
00:15:52.071 INFO  ChatModelCompletionObservationHandler    Chat Model Completion:
[]
00:15:52.071 INFO  ChatModelPromptContentObservationHandler Chat Model Prompt Content:
["You are a careful assistant. Answer in two sentences or fewer.", "how many error events in the checkout service in the last 15 minutes?"]
00:15:52.071 DEBUG AgentLoop                                [loop] Step 1: model requested 1 tool call(s), 263 tokens so far
00:15:52.072 DEBUG AgentLoop                                [loop] Step 1: your code runs countErrors {"minutes":15,"service":"checkout"}
00:15:52.073 DEBUG LogTools                                 [tool] countErrors(service=checkout, minutes=15) returned 412
00:15:52.073 DEBUG AgentLoop                                [loop] Step 2/5: sending 4 messages to the model
00:15:52.368 INFO  ChatModelCompletionObservationHandler    Chat Model Completion:
["There were 412 error events in the checkout service in the last 15 minutes."]
00:15:52.368 INFO  ChatModelPromptContentObservationHandler Chat Model Prompt Content:
["You are a careful assistant. Answer in two sentences or fewer.", "how many error events in the checkout service in the last 15 minutes?", "", ""]
00:15:52.368 DEBUG AgentLoop                                [loop] Step 2: no tool calls. Stop: FINAL_ANSWER, 469 tokens. Answer: There were 412 error events in the checkout service in the last 15 minutes.
00:15:52.370 DEBUG DispatcherServlet                        Completed 200 OK
  • Step 1/5: sending 2 messages, then sending 4 messages. The list grows by the reply and the tool result.
  • your code runs countErrors replaces Spring’s Executing tool call line from Chapter 4.
  • Stop: FINAL_ANSWER names the reason the run ended.
16:07:45.200 DEBUG harness.agent_loop                       [loop] Start. Your code runs the tools. Question: how many error events in the checkout service in the last 15 minutes?
16:07:45.200 DEBUG harness.agent_loop                       [loop] Step 1/5: sending 2 messages to the model
16:07:45.200 INFO  ChatModelPromptContent                   Chat Model Prompt Content:
['You are a careful assistant. Answer in two sentences or fewer.', 'how many error events in the checkout service in the last 15 minutes?']
16:07:46.336 INFO  httpx2                                   HTTP Request: POST http://localhost:1234/v1/chat/completions "HTTP/1.1 200 OK"
16:07:46.337 INFO  ChatModelCompletion                      Chat Model Completion:
[]
16:07:46.337 DEBUG harness.agent_loop                       [loop] Step 1: model requested 1 tool call(s), 263 tokens so far
16:07:46.337 DEBUG harness.agent_loop                       [loop] Step 1: your code runs count_errors {'minutes': 15, 'service': 'checkout'}
16:07:46.339 DEBUG harness.log_tools                        [tool] count_errors(service=checkout, minutes=15) returned 412
16:07:46.339 DEBUG harness.agent_loop                       [loop] Step 2/5: sending 4 messages to the model
16:07:46.340 INFO  ChatModelPromptContent                   Chat Model Prompt Content:
['You are a careful assistant. Answer in two sentences or fewer.', 'how many error events in the checkout service in the last 15 minutes?', '', '412']
16:07:46.634 INFO  httpx2                                   HTTP Request: POST http://localhost:1234/v1/chat/completions "HTTP/1.1 200 OK"
16:07:46.634 INFO  ChatModelCompletion                      Chat Model Completion:
['There were 412 error events in the checkout service in the last 15 minutes.']
16:07:46.634 DEBUG harness.agent_loop                       [loop] Step 2: no tool calls. Stop: FINAL_ANSWER, 472 tokens. Answer: There were 412 error events in the checkout service in the last 15 minutes.
16:07:46.635 INFO  uvicorn.access                           127.0.0.1:63489 - "GET /run?input=how%20many%20error%20events%20in%20the%20checkout%20service%20in%20the%20last%2015%20minutes%3F HTTP/1.1" 200
  • Step 1/5: sending 2 messages, then sending 4 messages. The list grows by the reply and the tool result.
  • your code runs count_errors replaces LangChain’s Executing tool call line from Chapter 4.
  • Stop: FINAL_ANSWER names the reason the run ended.

The Chapter 5 requests in requests/harness.http cover every run in this chapter.

Table 5.2 compares the three endpoints for the same question.

EndpointWho runs the toolReturnsLog tag
/askAutoToolCallThe framework: Spring AI’s ToolCallingAdvisor, or LangChain’s create_agentThe final text[auto], then Executing tool call
/askHaltToolCallNobodyThe tool calls, unrun[halt] … Not run.
/runYour AgentLoopA RunResult: answer, reason, steps, and tokens[loop] Step N: …

Table 5.2. One question, three levels of control.

A question with two services runs two tool calls in one step:

{"answer":"Checkout is significantly worse right now with 412 errors compared to only 3 errors for search.","reason":"FINAL_ANSWER","steps":2,"tokens":480}

Exercise: Set MAX_STEPS = 1. Ask the checkout question again. The run stops after the first tool call:

{"answer":null,"reason":"STEP_LIMIT","steps":1,"tokens":288}

Set it back to 5.

Summary: The loop sends messages, runs tools, and stops with a named reason. Your code sees every tool call. Chapter 6 adds the stop rules that protect cost and time.

docs