AI / LangChain4j interview questions
What is an Agent in LangChain4j and how does it differ from a simple AI Services call?
In LangChain4j, an Agent is an AI Services instance that has been equipped with a set of Tools and operates in an autonomous reasoning loop. Instead of a single-shot prompt-and-respond interaction, an agent decides at each step whether to answer directly from its knowledge or to invoke one of the available tools to gather more information, then loops until it has enough to produce a final answer.
A simple AI Services call is a single round-trip: user message in → LLM response out. An agent uses a ReAct-style (Reasoning + Acting) loop:
- User message is sent to the LLM along with tool descriptions
- LLM reasons: "I need current stock prices" → requests a
getStockPrice("AAPL")tool call - LangChain4j executes the tool and appends the result to the conversation
- LLM reasons with the result: maybe needs another tool call, or produces the final answer
- Loop ends when the LLM generates a final text response (no more tool requests)
class FinanceTools {
@Tool("Gets the current stock price for a ticker symbol")
double getStockPrice(@P("Ticker symbol like AAPL") String ticker) {
return marketDataService.getPrice(ticker);
}
@Tool("Gets the P/E ratio for a company ticker")
double getPERatio(@P("Ticker symbol") String ticker) {
return fundamentalsService.getPERatio(ticker);
}
}
FinancialAnalyst analyst = AiServices.builder(FinancialAnalyst.class)
.chatLanguageModel(model)
.tools(new FinanceTools())
.chatMemory(MessageWindowChatMemory.withMaxMessages(20))
.build();
// The agent may call both tools before answering
String answer = analyst.analyze("Is Apple stock overvalued relative to its P/E ratio?");The critical difference: a simple AI Services call completes in one LLM round-trip with no tool access. An agent orchestrates multiple LLM calls and tool executions autonomously to answer questions that require real data.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
