Scrivio Interview library
ConceptFoundations5 min read · updated 12 November 2026

What is a tool-calling loop in an LLM agent? Describe the steps between the user's message and the final answer when the model decides to call a Java method such as getOrderStatus(orderId).

A tool-calling loop is the cycle in which the application sends the conversation and a list of tool definitions to the model, the model answers with a request to call a tool such as getOrderStatus with arguments, the application validates the arguments, runs the Java method and sends the result back, and this repeats until the model gives a final text answer or a step limit is reached. The model never runs code itself; the application decides what runs, which is exactly where safety checks belong.

Why this question is asked

Almost every useful LLM agent is built on this loop, because it is how a model that only produces text can look things up and take actions in our systems. Interviewers ask about it because many developers use Spring AI or LangChain4j without knowing what happens underneath, and that knowledge is what lets us debug runaway agents, secure tools and control cost. In this answer, we will recap what a tool definition is, follow one request for an order status message by message, see how Java frameworks run the loop for us, and look at why safety checks live in our code and how to stop an agent that loops forever.

1A Quick Recap: What a Tool Definition Is

A large language model reads text and writes text. It cannot open a database connection. To let it use our systems, we describe functions it may ask for. Each tool definition has a name, a description in plain language of what the tool does and when to use it, and a JSON schema, a machine-readable description of the parameters and their types.

The model is trained to read these definitions and, when a tool would help, to answer with a structured tool call instead of normal text. So the description is not documentation for humans only: it is how the model decides whether and how to call the tool.

JSON
{
"name": "getOrderStatus",
"description": "Get the current status of one order by its order id",
"parameters": {
"type": "object",
"properties": { "orderId": { "type": "string" } },
"required": ["orderId"]
}
}

2Following One Request Through the Loop

Suppose the user writes 'Where is my order A-1001?'. First, the application sends the conversation and the tool definitions to the model. Second, the model replies, not with text for the user, but with a tool call: the name getOrderStatus and the arguments {"orderId": "A-1001"}. Third, the application checks that the tool exists and the arguments match the schema and the user's permissions, then calls the Java method. Fourth, it appends the result to the conversation as a tool message, for example {"status": "SHIPPED", "eta": "2026-11-08"}, and calls the model again. Fifth, the model now has what it needs and writes the final answer: 'Your order A-1001 has shipped and should arrive on 8 November.'

Notice that the model was called twice for one user message. If the model needed more information, for example to look up a tracking link too, it would request another tool call and the loop would go around again.

Step throughOne question, two model calls
UserApplicationModelOrderService
Where is my order A-1001?
conversation + tool definitions
tool call getOrderStatus(A-1001)
validate name, arguments, permissions
getOrderStatus("A-1001")
SHIPPED, eta 2026-11-08
conversation + tool result
final text answer
Your order has shipped...

9 messages in order. Play them, or step through them one at a time.

3When the Loop Ends

The loop ends in one of two ways. Normally, the model returns a final text answer with no further tool calls. But a model can also keep calling tools, for example retrying a tool that keeps failing, or searching again and again for information that does not exist. So the application needs a step limit, such as a maximum of 8 tool calls per user message, plus limits on tokens and total time. When a limit is reached, the application stops calling the model and returns a graceful message or hands the conversation to a person.

4How Spring AI and LangChain4j Run the Loop

We rarely write this loop by hand. In Spring AI we annotate methods with @Tool and pass the object to a ChatClient call; the framework builds the definitions from the method signature, runs the loop and returns the final text. LangChain4j does the same with its own @Tool annotation and AiServices, which generates an implementation of an interface we declare.

Java
class OrderTools {
@Tool(description = "Get the current status of one order by its order id")
String getOrderStatus(String orderId) {
return orderService.statusOf(orderId); // "SHIPPED, eta 2026-11-08"
}
}
String answer = chatClient.prompt()
.user("Where is my order A-1001?")
.tools(new OrderTools())
.call()
.content(); // "Your order A-1001 has shipped ..."

The convenience hides the steps, but they are all still there. Check your version for the exact annotation packages and options, since both frameworks evolved quickly.

5Why Safety Checks Belong in Our Code

The most important fact about the loop is that the model never executes anything. It only asks. Our application decides whether a requested call runs, with which arguments and on whose behalf. That is good news, because it gives us a single place to enforce rules: validate arguments, check that the order belongs to the logged-in user, refuse actions that need approval, and log every call.

So a tool method must be written as if its caller were untrusted, because in effect it is. The model can be tricked by text in its input, or simply make a mistake, and the tool is our last line of defence.

6Follow-up questions

How do you stop an agent that loops forever?

Set hard limits in code on the number of tool calls per request, the tokens used and the elapsed time, and end the loop gracefully with a fallback answer or a hand-off when any limit is reached. Then look at why it looped: usually a tool returns an unhelpful error, two tools overlap so the model keeps switching between them, or the task is impossible. Returning clear error messages from tools, such as 'order not found', often fixes it.

Can the model call several tools at once?

Many models support parallel tool calls, returning several tool requests in one reply. The application runs them, possibly concurrently, and sends all the results back together before the next model call.

Why does the tool description matter so much?

Because it is the only thing the model knows about the tool. A vague description leads to the model calling the wrong tool or not calling any; a clear one that says what the tool does, when to use it and the format of each parameter leads to correct calls.

7Conclusion

In this answer, we saw that a tool-calling loop sends the conversation and tool definitions to the model, receives a tool call request, validates and runs the Java method, returns the result as a tool message and repeats until a final answer or a limit. Spring AI and LangChain4j run this loop for us through @Tool methods, but the model never executes code, so our application is where validation, authorization and limits belong.

What to say in the interviewAbout 80 seconds aloud

Open with

A tool-calling loop is how an LLM uses our code. The application sends the conversation plus tool definitions, each with a name, a description and a JSON schema for its parameters.

Then, if they want more

If the model decides a tool helps, it does not answer with text; it replies with a tool call, for example getOrderStatus with orderId A-1001. The application checks the tool exists, validates the arguments and the user's permissions, runs the Java method, and adds the result to the conversation as a tool message. Then it calls the model again.

That repeats until the model returns a final text answer, or until we hit a limit on steps, tokens or time, which we always set so a confused agent cannot loop forever.

In Java we usually let a framework run this: in Spring AI I annotate methods with @Tool and pass them to ChatClient with tools(), and LangChain4j does the same with @Tool and AiServices. The key point is that the model never executes anything itself.

It only asks, and our code decides what runs, so that is where I put validation, authorization against the logged-in user, approval for risky actions and logging.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Spring AI reference
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What is a tool-calling loop in an LLM agent? Describe the steps between the user's...": https://scrivio-interview-library.netlify.app/spring-boot/ai-agents-and-mcp/0fc66272da/