Ankit KanojiyaAdvanced
Evergreen guide by Ankit Kanojiya · Advanced · AI / ML
Building AI Agents: Tool Use, Planning, and Guardrails
How to move from a single LLM call to an agent that calls tools, plans multi-step tasks, and operates safely. Covers the tool-use loop, planning patterns, and the guardrails that keep it reliable.
- Agents
- Tool Use
- Function Calling
- Guardrails
- Planning
- LLM
An agent is an LLM that does more than answer in one shot. It decides which actions to take, calls tools to take them, observes the results, and repeats until a goal is met. This unlocks tasks a single prompt cannot do, such as looking up live data, running code, or updating records. It also introduces new failure modes, so the engineering is as much about control as capability.
The tool-use loop
Tool use (also called function calling) is the foundation. You describe available tools to the model as structured definitions with names, descriptions, and parameter schemas. The model does not run the tools; it emits a request to call one, your code executes it, and you return the result.
loop:
response = model(messages, tools)
if response.wants_tool_call:
result = run_tool(response.tool, response.args)
messages.append(tool_result(result))
else:
return response.text # final answer
Clear tool descriptions matter as much as prompts. The model chooses tools based on their names and descriptions, so vague definitions lead to wrong or missing calls. Keep each tool focused, document parameters, and return concise, structured results the model can reason over.
Planning patterns
Beyond a single tool call, agents need a way to sequence steps. A few patterns recur:
- ReAct style interleaves reasoning and acting: the model thinks, calls a tool, observes, and thinks again. It is simple and works for many tasks.
- Plan-then-execute asks the model to draft a full plan first, then carry out each step. This helps on complex tasks and makes the plan inspectable before anything runs.
- Decomposition breaks a large goal into subtasks, sometimes handled by separate specialized agents. This adds power but also coordination overhead and cost.
Start with the simplest pattern that works. Multi-agent systems are harder to debug, and a single well-equipped agent handles most tasks.
Managing context and memory
Each loop iteration adds the tool call and its result to the conversation, so context grows quickly. Long-running agents need a strategy:
- Summarize older steps once they are no longer needed in full.
- Store large results externally and keep only a reference or a short digest in the prompt.
- Return compact tool outputs rather than dumping raw payloads.
Without this, agents hit the context limit, slow down, and lose track of the original goal.
Guardrails that keep agents reliable
Autonomy is the risk. An agent that can act can act wrongly, and small errors compound over steps. Build controls in from the start.
- Limit the loop. Cap the number of iterations and set a timeout so a confused agent cannot run forever or burn unbounded cost.
- Scope tool permissions. Give each tool the minimum access it needs. A read tool should not be able to write or delete.
- Require confirmation for risky actions. Sending money, deleting data, or emailing customers should pass through a human or a strict check.
- Validate tool inputs. The model may produce malformed or unsafe arguments, so validate against the schema and sanitize before executing.
- Guard against prompt injection. Tool results and fetched content are untrusted. Text from a web page or document may try to hijack the agent, so never let raw external content trigger privileged actions directly.
Handling errors and dead ends
Tools fail: APIs time out, inputs are rejected, searches return nothing. A robust agent treats errors as observations. Return a clear error message as the tool result and let the model decide whether to retry, try another tool, or stop and report. Build in explicit stopping conditions so the agent gives up gracefully instead of looping on an impossible task.
Observability
Because agents take many steps, you cannot debug them from the final answer alone. Log every step: the model's reasoning, the tool it chose, the arguments, and the result. Tracing the full trajectory is the only practical way to understand why an agent did something, and it is essential once agents run in production.
Where to start
A sensible progression is to add one reliable tool, confirm the model calls it correctly, then expand. Keep the loop short, the permissions tight, and the logs complete. Evaluate on realistic tasks and watch not just whether the agent succeeds but how many steps and how many tokens it spends getting there. Capability is easy to demo; the engineering work is making an agent that is reliable, bounded, and safe enough to trust with real actions.