DeepReach
GenAI11 interview questions

LLM Function Calling and Tool Use Patterns

Implement structured tool use with Claude and OpenAI APIs. Covers multi-step reasoning, parallel tool calls, error handling, and building agents in Java.

LLMTool UseAgentsSpring AI

LLM Function Calling and Tool Use Patterns

What is Function Calling?

Function calling allows LLMs to request the execution of specific functions with structured arguments. The model doesn't execute code — it outputs a structured call that your application executes.

Spring AI Tool Definition

java
@Component
public class AccountTools {

    @Tool(description = "Get account balance for a given account ID")
    public AccountBalance getBalance(
        @ToolParam(description = "The account ID") String accountId
    ) {
        return accountService.getBalance(accountId);
    }

    @Tool(description = "List recent transactions for an account")
    public List<Transaction> getTransactions(
        @ToolParam(description = "Account ID") String accountId,
        @ToolParam(description = "Number of days to look back") int days
    ) {
        return transactionService.getRecent(accountId, days);
    }
}

Wiring Tools to Chat

java
@Service
public class FinancialAssistant {
    private final ChatClient chatClient;
    private final AccountTools accountTools;

    public String chat(String userMessage) {
        return chatClient.prompt()
            .user(userMessage)
            .tools(accountTools)
            .call()
            .content();
    }
}

Agentic Loop

User Message
    → LLM decides to call tool
    → Application executes tool
    → Result added to context
    → LLM reasons over result
    → LLM responds or calls another tool

Interview Questions

  1. 1.How do you prevent an agent from calling tools in an infinite loop?
  2. 2.What is the difference between function calling and RAG?
  3. 3.How do you handle tool execution failures gracefully?
  4. 4.What are the security implications of giving an LLM tool access?