0Pricing
Spring Boot 4 Complete Guide · Lesson

Tool Calling and Agent Advisors

Extend models with callable Java tools and compose behavior using request and response advisors.

Why Tool Calling Matters

Large language models can reason over text, but they cannot fetch a live order status, query your database, or call a payment gateway on their own. Tool calling (also called function calling) bridges that gap.

  • The model decides when a tool is needed based on the user's request.
  • Spring AI invokes the matching Java method and feeds the result back into the conversation.
  • The model then produces a grounded, final answer.

In Spring AI, a tool is just an ordinary Java method annotated with @Tool. The framework reads the method signature and JavaDoc-style description, generates a JSON schema, and exposes it to the model.

Defining a Tool with @Tool

Annotate a method with @Tool and give it a clear description. The model uses that description to decide whether to call it, so write it as if you were instructing a junior developer.

  • Use @ToolParam to describe individual arguments.
  • Return types are serialized to JSON automatically.
  • Keep the method deterministic and side-effect-aware.
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Component;
import java.time.LocalDate;

@Component
class DateTools {

    @Tool(description = "Get the number of days between today and a given target date")
    int daysUntil(@ToolParam(description = "Target date in ISO-8601 format, e.g. 2026-12-31") String targetDate) {
        LocalDate target = LocalDate.parse(targetDate);
        return (int) java.time.temporal.ChronoUnit.DAYS.between(LocalDate.now(), target);
    }
}

All lessons in this course

  1. ChatClient, Prompts, and Structured Output
  2. Embeddings and Vector Store Retrieval
  3. Retrieval-Augmented Generation Pipelines
  4. Tool Calling and Agent Advisors
← Back to Spring Boot 4 Complete Guide