AI / LangChain4j interview questions
What are Tools in LangChain4j and how does tool calling work?
Tools (also called function calling) give LLMs the ability to invoke real Java methods during a conversation. Instead of answering entirely from its training knowledge, the model can recognize when a specific capability is needed — fetching live data, running calculations, calling APIs — and request that the application execute a registered tool and return the result to the model for incorporation into its final answer.
In LangChain4j, tools are defined by annotating Java methods with @Tool on a plain Java object. Parameters can be annotated with @P (or @ToolParam) to provide descriptions that help the model understand when and how to use them.
class WeatherTools { @Tool("Returns the current weather in a given city in Celsius") String currentWeather(@P("City name, e.g. 'London'") String city) { return weatherApiService.fetchCurrent(city); // real API call } @Tool("Returns the 5-day forecast for a city") String forecast(@P("City name") String city, @P("Number of days 1-5") int days) { return weatherApiService.fetchForecast(city, days); } } // Register with AI Services TravelAssistant assistant = AiServices.builder(TravelAssistant.class) .chatLanguageModel(model) .tools(new WeatherTools()) .build();
The flow is: user sends a message → LLM decides a tool should be called → LangChain4j intercepts the tool-use response → executes the Java method → appends the result to the conversation → re-calls the LLM with the result → LLM generates the final answer. All of this happens transparently within the assistant.chat() call. The model may call tools multiple times before producing a final answer, and LangChain4j handles those multi-step loops automatically.
More Related questions...