Skip to chapter content
navan✳THE BUILD LABAI ENGINEERING / GUIDE
0/21 read
CHAPTER 13 / 21 · KNOWLEDGE AND ACTION

Tools, actions, and agents.

See how a model proposes tool calls, how software grants authority, and why multi-step agents need limits.

BY NAVAN SAMILONG READTHEORY FIRST
01 / FROM THE BEGINNING

Start with the meaning.

A tool is a capability exposed to an AI application, such as searching a document index, checking an order, or drafting a calendar event. A model may propose a tool name and arguments. The application decides whether to execute it. That distinction is essential: a generated request is not permission. The tool layer must check the user, arguments, and allowed operation.

An agent is a workflow in which the system chooses among steps, observes results, and continues until a goal or stopping condition is reached. Some agents are simple: search, read, answer. Others can make several tool calls. The more steps and authority an agent has, the more ways it can drift from the user's intention.

02 / THE SYSTEM

How the idea works.

Give each tool the smallest useful capability. A read_order tool should not also modify orders. A draft_email tool should return text for review rather than sending it. When an action is irreversible or external, require a clear approval step with the exact proposed change. Validate arguments in ordinary code. A model-supplied account ID should never bypass the user's authorization.

An agent needs budgets: maximum steps, time, tokens, and cost. It also needs an end condition and a failure state. Without these, a model may repeat searches, call a tool with slightly different arguments, or continue after the evidence is exhausted. Log each step with its inputs and outputs so a failure can be reconstructed.

Tool results are untrusted content. A webpage or document can include instructions addressed to the model, but those instructions are part of the data being read. They must not override the application task. The software can reduce risk by limiting tool outputs, separating evidence from instructions, and validating every action.

An agent loop can be modeled as states: planning, validating, executing, observing, and stopping. The application should know which state it is in. A tool result may trigger another plan, but the loop must stop when its budget is exhausted or a required approval is missing. Persisting state can help a long task resume, yet it also requires a way to avoid repeating an action after a crash. Read-only tools are easier to retry than tools that create or send something.

Give the model a narrow tool vocabulary. A search tool might accept a query and return only permitted document summaries. A send_message tool, if truly needed, should require a reviewed recipient and body, and execution should wait for explicit user approval. Tool arguments are data generated by a model, so validate their types, ranges, ownership, and intended effect exactly as you would validate data from an untrusted client.

01PlanProposed step02ValidateTool and access03ExecuteBounded action04ObserveResult or stop
Tool calls need validation, permissions, budgets, and a stopping rule.
03 / PYTHON, EXPLAINED

Follow the code.

tools-and-agents.py
ALLOWED = {"search_docs", "read_order"}

def dispatch(call, user, tools):
    name = call.get("name")
    args = call.get("arguments")
    if name not in ALLOWED or not isinstance(args, dict):
        raise ValueError("unsupported tool call")
    if name == "read_order" and args.get("user_id") != user["id"]:
        raise PermissionError("order belongs to another user")
    return tools[name](**args)

The dispatcher accepts only two named tools and dictionary arguments. Before read_order executes, it checks that the requested user ID matches the authenticated user. This is an application permission check, not a prompt instruction. A production dispatcher would also validate the exact argument schema and derive user identity from a trusted session rather than from model output.

The function illustrates why model-generated tool calls are proposals. The software has the final say. For actions such as payment or sending messages, the flow should present the exact action to the user before execution.

04 / THE BOUNDARY

What the result does not say.

More autonomy is useful only when it improves the task outcome. A fixed workflow can be easier to evaluate, faster, and safer when the steps are known.

A tool call can succeed technically but still be wrong for the user. Evaluate tool selection, argument correctness, permissions, and final answer separately.

05 / KEEP THIS

One clear idea.

The model may suggest an action; application code owns authority and stopping rules.

THE CODE IN CONTEXT

The AI cookbook

Complete, explained local workflows sit alongside this reading course.

Browse the cookbook
Finished reading?
Built with curiosity. By Navan Sami. ↗AI engineering · From model calls to reliable systems.