What is an agent, and how does tool calling work?
What is an AI agent, and how does tool calling (function calling) work?
TL;DR
Tool calling lets a model request an action instead of only producing text. You describe tools (name, description, JSON schema for the arguments); the model returns a structured call like get_weather({"city": "Tel Aviv"}); your code executes it and sends the result back; the model continues. An agent is an LLM running tools in a loop to reach a goal: it reasons, picks a tool, observes the result, and repeats until it's done, deciding its own steps instead of following a fixed workflow. Use agents for open-ended tasks where the steps can't be known in advance; use fixed workflows (predefined chains) when they can, since they're cheaper and more predictable.
Key points
- The model never executes anything itself; your application does, which is where you enforce permissions.
- Tool design matters most: clear names and descriptions, few overlapping tools, strict schemas, and helpful error messages the model can recover from.
- Return concise tool results; huge outputs bloat the context.
- Guardrails: step and cost limits, timeouts, validation of arguments, and human approval for risky actions (payments, deletes, emails).
- Patterns: ReAct (reason, act, observe), plan-and-execute, and routing between specialized prompts.
- Evaluate agents on end-to-end task success, and also inspect the trajectory (wrong tools, loops, wasted steps).
Example
tools = [{
"name": "get_order_status",
"description": "Look up the shipping status of a customer's order by order ID.",
"input_schema": {"type": "object",
"properties": {"order_id": {"type": "string"}},
"required": ["order_id"]},
}]
while True:
response = llm(messages, tools=tools)
if response.stop_reason != "tool_use":
break # final answer
for call in response.tool_calls:
result = run_tool(call.name, call.input) # your code, your permissions
messages.append(tool_result(call.id, result))Common follow-ups
- How do you stop an agent from looping? Max steps, detecting repeated identical calls, and a budget on tokens or cost.
- Workflow vs agent? A workflow is code-defined steps with LLM calls inside. An agent lets the model choose the steps. Prefer the simplest one that works.
What interviewers look for
Tip
The tool-call loop with your code in the middle, tool design quality, guardrails for risky actions, and knowing when not to use an agent.
- Red flag: letting the model run arbitrary actions without permission checks or limits.