KatInterview

Building Dependable Tool-Using LLM Agents End to End

Machine LearningModel answer
MediumModel answerMachine Learningllm-agentstool-callingstructured-decoding

Last updated

Explain how an LLM agent invokes tools, keeps calls well-formed, recovers from bad calls, and stops runaway tool loops, with concrete engineering trade-offs.

You are responsible for an LLM-powered agent that completes tasks by invoking external tools (functions or APIs). Walk through the four areas below, in order, as you would in a technical discussion.

Context you may want to clarify first

  • Is the model a hosted API with native function calling, or an open-weights model where you control decoding?
  • Are the tools read-only, or can some cause side effects (writing records, sending messages, spending money)?
  • What does "too long" mean: steps, context tokens, latency, or cost?
  • How many tools are there, and how often do they change?

Part 1: The mechanics of tool calling Describe the full path of a tool call: what the model is shown, what it outputs, which component actually runs the tool, and how the result returns to the model.

Part 2: Enforcing the call format How do you make sure calls are valid, for example JSON that conforms to each tool's argument schema?

Part 3: Stability and correcting bad tool use How do you make tool use consistent from run to run? What do you do when the model picks the wrong tool, supplies wrong arguments, or fails to call a tool when it should?

Part 4: Loops that run too long How do you handle an agent whose tool-call loop keeps going without finishing?

Aim for a structured answer that names concrete mechanisms, trade-offs, and failure modes rather than generic advice.

Hints

Hint 1

Start from the right mental model: the model only proposes a call as text or structured output; your application code validates and executes it, then feeds the result back as a new message.

Hint 2

For format control, think in layers: clear schemas and examples in the prompt, constrained decoding or native function-calling modes, then validation with a bounded repair or retry step.

Hint 3

For reliability, first build an evaluation set and categorize failures (wrong tool, wrong args, missing call, unnecessary call) so each gets a targeted fix. For loops, combine hard budgets with detection of repeated calls and a graceful final answer.