Quick revision
The short answer
We annotate a method with @Tool(description = "...") from org.springframework.ai.tool.annotation, describe its parameters with @ToolParam, and pass an instance to the ChatClient with .tools(new OrderTools(...)) or register it as a default with defaultTools(...). The model never runs our code: it replies with a request to call the tool with arguments it chose, Spring AI runs the Java method and sends the result back, and the model then writes its answer. Because the model chooses the arguments, the tool must validate them and check the caller's permissions, and clear descriptions are what the model uses to pick the right tool.
What to remember
In this answer, we wrote an order status tool as a method annotated with @Tool and @ToolParam, registered it with tools() on a ChatClient request, and followed the loop in which the model returns a tool call, Spring AI runs the Java method and sends the result back, and the model writes the answer. Because the model chooses the arguments, the tool validates them and limits data to the authenticated customer, whose id comes from our code rather than the model, and clear descriptions help the model pick the right tool.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We annotate a method with @Tool(description = "...") from org.springframework.ai.tool.annotation, describe its parameters with @ToolParam, and pass an instance to the ChatClient with .tools(new OrderTools(...)) or register it as a default with defaultTools(...). The model never runs our code: it replies with a request to call the tool with arguments it chose, Spring AI runs the Java method and sends the result back, and the model then writes its answer. Because the model chooses the arguments, the tool must validate them and check the caller's permissions, and clear descriptions are what the model uses to pick the right tool.
Why this question is asked
Tool calling is what turns a chat model from something that talks into something that can look things up and act. It is also widely misunderstood: many developers believe the model executes their method, which leads to tools that trust whatever arguments arrive. Interviewers ask for the code and the explanation together because both matter: the annotations are simple, and the safety comes from knowing exactly who runs what. In this answer, we will write an order status tool, register it with a ChatClient, follow the messages between our service and the model, and then make the tool safe and easy for the model to choose.
1A Quick Recap of Tool Calling
A tool, sometimes called a function, is a capability we describe to the model by name, purpose and parameter types. When we send a request, we include the descriptions of the available tools. If the model decides it needs one, it does not answer the user directly. It returns a special message saying, in effect, please call getOrderStatus with orderNumber 1042. Our side runs that method and sends the result back in a follow-up request, and the model uses it to write the final answer. This loop can repeat if the model needs several tools.
2Writing the Tool
In Spring AI 1.0 and later, a tool is an ordinary method with the @Tool annotation. The description tells the model what the tool does and when to use it, and @ToolParam describes each parameter. Spring AI generates a JSON schema for the parameters from the method signature.
Notice that the customer id is not a tool parameter. It comes from our own code when we create the tools object, so the model has no way to choose it. The tool returns a small record, which Spring AI serialises to JSON for the model; returning only the fields the model needs keeps tokens and exposure low.
3Giving the Tool to the ChatClient
We pass the object to a single request with tools(), or to every request of a client with defaultTools() when the tools hold no per-user state. Here the tools object is created per request because it carries the authenticated customer.
4What the Model Actually Does
Let us follow the conversation behind that single call(). Spring AI sends the system and user messages along with the tool's name, description and parameter schema. The model reads the question and the description and decides that getOrderStatus fits. Instead of text, it returns a tool call: the name and the arguments as JSON, {"orderNumber": "1042"}. Spring AI finds the matching method, converts the JSON into the String parameter, and invokes it in our JVM. The result goes back to the model as a tool result message in a second request, and the model writes the final sentence. So the model chose the tool and the arguments, and our code did the work.
This has two consequences. First, every tool call costs an extra round trip to the model, which adds latency and tokens. Second, anything the model puts in the arguments is untrusted input, exactly like a request parameter from a browser.
8 messages in order. Play them, or step through them one at a time.
The model asks; our code runs the method; the model writes the answer.
5Validating Arguments and Checking Permissions
Because the model picks the arguments, it can pick wrong ones. A user may type someone else's order number, a document in the context may contain instructions that steer the model, or the model may simply misread a number. So the tool behaves like a public API endpoint. It checks the format of each argument, looks data up only within what the authenticated user may see, and returns a clear error or not-found result instead of throwing stack traces into the conversation. In the example, findForCustomer limits the query to the current customer, so asking about order 2077, which belongs to Bob, returns NOT_FOUND for Alice. The same rule applies even more strongly to tools that change data.
6Writing Descriptions the Model Can Use
The model chooses tools by reading their names and descriptions, so these are part of the program. A vague description such as Gets orders leads to tools being called at the wrong time or not at all. A good description says what the tool returns, when to use it and what the parameters look like, as in the example. With several tools, the descriptions should make it obvious which one fits which question, and we should keep the set small per request, since more tools mean more tokens and more chances to choose wrongly. We test tool selection with a handful of realistic questions whenever we change a description.
7Follow-up questions
Can a tool method receive context that the model does not see?
Yes. Besides constructor state, Spring AI supports a ToolContext: we pass a map with toolContext(...) on the request, declare a ToolContext parameter on the tool method, and read values such as the customer id from it. The model never sees or fills that parameter.
What happens if the tool throws an exception?
Spring AI has a configurable exception handling strategy. By default an error message is sent back to the model as the tool result, so it can tell the user something went wrong; it can also be configured to rethrow. Check the behaviour for your version and keep error messages free of internal details.
Can the tool's result go straight to the user without another model call?
Yes, a tool can be marked to return its result directly, which skips the final model call. This is useful when the result is already user-ready, and it saves one round trip.
8Conclusion
In this answer, we wrote an order status tool as a method annotated with @Tool and @ToolParam, registered it with tools() on a ChatClient request, and followed the loop in which the model returns a tool call, Spring AI runs the Java method and sends the result back, and the model writes the answer. Because the model chooses the arguments, the tool validates them and limits data to the authenticated customer, whose id comes from our code rather than the model, and clear descriptions help the model pick the right tool.
Open with
In Spring AI 1.x I write a class with a method annotated with @Tool, from org.springframework.ai.tool.annotation, with a description that says what it returns and when to use it, for example get the status and delivery date of one of the current customer's orders.
Then, if they want more
Each parameter gets @ToolParam with a description, like the order number in digits. Then I pass an instance to the ChatClient with .tools(new OrderTools(...)) on the request, or defaultTools when the object has no per-user state. The key point is what the model does: it does not execute anything.
Spring AI sends the tool's name, description and JSON schema with the prompt, the model replies with a tool call containing the name and arguments it chose, Spring AI invokes my Java method, sends the result back as a tool result message, and the model writes the final answer.
So every tool call is an extra round trip, and the arguments are untrusted input. That is why the tool validates arguments and checks permissions itself: in my example the customer id comes from the authenticated user through the constructor or the ToolContext, never from the model, and lookups are limited to that customer's orders.
And because the model chooses tools from their descriptions, I keep descriptions precise and the tool set small.
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 "Show how to expose a Java method as a tool that a model can call in Spring AI 1.x, for...": https://scrivio-interview-library.netlify.app/spring-boot/spring-ai/8249e44962/