liveBook · Manning

@Tool annotation

As explained in Software Testing with Generative AI

A LangChain4j annotation that marks a method as an invocable tool and supplies natural-language metadata describing its operation.

The @Tool annotation marks a method that an LLM may invoke through LangChain4j function calling. Its natural-language description helps the model match a user instruction to the appropriate method. A different prompt may cause the tool not to run. section 9.2.1 section 9.4

Annotating database assistant operations

A tool class can expose focused operations such as @Tool("Create room records") on createRooms, @Tool("Create booking records") on createBookings, and @Tool("Show results of database") on displayDatabase. With these tools registered in AiServices, a prompt such as “Can you create me 4 rooms and 2 bookings and tell me what’s in the db” can lead the agent to call the matching methods. Use @P descriptions on parameters such as count to help pass extracted prompt values into the methods. section 9.2.1 section 9.4

public class DataAssistantTools {
 
    @Tool("Create room records")
    public void createRooms(@P("Amount of room records to create")
    ➥int count) {
        System.out.println("You want me to create " + count + "
        ➥rooms.");
    }
 
    @Tool("Create booking records")
    public void createBookings(@P("Amount of booking records to
    ➥create") int count) {
        System.out.println("You want me to create " + count + "
        ➥bookings.");
    }
 
    @Tool("Show results of database")
    public void displayDatabase() {
        System.out.println("I'll then share current database
        ➥details");
    }
 
}

Shows the annotation on three Java methods, including natural-language descriptions and @P parameter metadata. section 9.2.1

How Software Testing with Generative AI covers it

How deep it goes
Explains annotation-based method matching, parameter extraction, registration, and execution, but does not detail model decision internals.
Examples
Demonstrates room creation, booking creation, database display, and a combined prompt requesting all three operations.
What it assumes
Assumes familiarity with Java methods, LLM prompts, and LangChain4j service configuration.
What you can do after
Apply it via LangChain4j to expose Java methods that an LLM can select and invoke from natural-language prompts.
How it connects
Connects function calling, AI agents, LangChain4j, AiServices, and the @P annotation.

Also explained in

Compare how 2 books explain @Tool annotation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Software Testing with Generative AI. The section links point to the parts of the book it draws on.