Spring AI – [ Tool Calling ]

Large Language Models (LLMs) are great at reasoning and generating text, but sometimes your application needs real data.

  • What’s the current weather?
  • Can you look up a customer record?
  • Could you trigger a business workflow?

Plain text alone can’t do this.

That’s where Tool Calling comes in.

Spring AI makes it simple to register functions (or “tools”) that the LLM can call when needed.

1.What is Tool Calling?

Tool Calling (also called function calling) allows an LLM to:

  • Recognize when a query requires external data or an action.
  • Call a registered function in your code.
  • Use the returned result to generate the final answer.

This bridges the gap between LLMs and real-world applications.

2.Define a Tool

In Spring AI, a tool is just a Java method annotated with @AiFunction.

Example: a simple weather lookup tool.

import org.springframework.ai.tool.annotation.AiFunction;
import org.springframework.stereotype.Component;

@Component
public class WeatherTool {

    @AiFunction
    public String getWeather(String city) {
        // In real life, call an API like OpenWeather here.
        if ("Paris".equalsIgnoreCase(city)) {
            return "It's sunny and 22°C in Paris.";
        }
        return "Weather data not available for " + city;
    }
}

3.Enable Tool Support in Chat

You pass your tools to the chat client, so the LLM knows what’s available.

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.stereotype.Service;

@Service
public class ChatService {

    private final ChatClient chatClient;

    public ChatService(ChatClient chatClient) {
        this.chatClient = chatClient;
    }

    public String ask(String question) {
        return chatClient.prompt()
                .tools(WeatherTool.class) // register the tool
                .user(question)
                .call()
                .content();
    }
}

Example 1: Asking a Normal Question

  • chatService.ask(“Who created Java?”);

LLM responds directly (no tool needed):

  • Java was created by James Gosling at Sun Microsystems in 1995.

Example 2: Asking About Weather

  • chatService.ask(“What’s the weather in Paris?”);

The LLM detects that your getWeather tool should be called, invokes it, and responds with:

  • It’s sunny and 22°C in Paris.

4.Multiple Tools

You can register multiple tools – the LLM will choose which one to call.

@Component
public class MathTool {

    @AiFunction
    public int add(int a, int b) {
        return a + b;
    }
}

Now the assistant can handle:

  • chatService.ask(“Add 42 and 58 for me”);

The result is 100.

5.Why Tool Calling Matters

With tool calling, your LLM app can:

  • Fetch real-time data (weather, stock prices, database queries).
  • Trigger business workflows (create tickets, update CRM records, send emails).
  • Do computations beyond what the LLM can reliably do.

Instead of being a static knowledge source, your AI assistant becomes an active participant in your system.

Conclusion

Spring AI makes tool calling simple:

  • Annotate methods with @AiFunction
  • Register them with your ChatClient
  • Let the LLM decide when to call them

This unlocks a whole new class of AI-powered applications that are interactive, dynamic, and useful in production.