Tool Calling and Agents
Function calling, agent loops and guardrails - autonomy as a dial, destructive steps gated by humans.
Before this: RAG Applications
Objective
Extend models from answering to acting: define typed tools, run the think-call-observe agent loop under hard bounds, gate dangerous operations behind human approval, and log every step for full auditability.
Vocabulary
The words to use when directing your agent.
- tool calling
- Model requesting a named function with structured arguments for the app to execute.
- tool schema
- Declared name, purpose, and typed parameters a tool accepts - validated before execution.
- agent loop
- Cycle of model reasoning, tool invocation, observation feeding back - until done or bounded.
- guardrail
- Structural constraint on autonomy: allowlists, caps, approval gates, kill switches.
- human-in-the-loop
- Workflow points where a person approves before consequential actions execute.
Understand it
The loop and its bounds
Each iteration: model receives goal plus accumulated observations, either answers DONE or requests a tool call; app validates arguments against schema, executes, returns result text. Bounds wrap everything: step ceiling stops infinite loops, budget ceiling stops runaway spend (token math from module one pays off), wall-clock timeout stops hangs. Hitting ANY bound terminates honestly with partial-state disclosure - never silently.
Logging is the audit trail
Every iteration records: reasoning summary, exact tool requested, arguments received, validation verdict, execution outcome, observations returned. This log transforms debugging from séance into science - you SEE the loop chase its tail, misuse an argument schema, or attempt something outside its allowlist. Agents without step logs are undebuggable by construction.
Core concepts
Models decide, tools act
The model NEVER executes anything - it emits a structured request: 'call categorize with these arguments.' Your code validates against the tool schema, executes with real privileges, and returns observations. This separation is the security boundary: intelligence proposes, your permission-checked code disposes.
Autonomy is a dial, not a switch
Agentic value scales with steps allowed unsupervised; risk scales identically. Professional agents declare their dial position explicitly: read-only tools run freely, writes carry quotas, destructive actions REQUIRE human confirmation, everything lives under hard bounds - max steps, max spend, timeout. The dial gets documented, reviewed, and wired to a kill switch.
Examples
Cleanup agent, gated deletes
Goal: tidy uncategorized expenses. Tools: list (free), categorize (free under daily quota), delete (HUMAN GATE). Loop proposes deletions; each pauses for approval UI; approved ones execute; the transcript shows everything. Value delivered, blast radius capped - the guardrail design IS the product decision.
The loop that ate the budget
An early agent misread a schema, retried variations endlessly - forty iterations of pure spend accomplishing nothing. Step ceilings would have stopped it at ten with an honest 'goal not achieved' report. Bounds feel restrictive until the day they are the only thing between a bug and an invoice.
Common mistakes
Common mistake
Executing tool arguments straight from model output without schema validation.
Fix
Validate like any untrusted input (Level 2 reflexes): types, ranges, enums. Models emit plausible garbage under pressure; validation catches what prompting cannot guarantee.
Common mistake
Destructive actions running freely 'because the prompt says be careful'.
Fix
Prompts are not permission systems. Structural gates - approval workflows, allowlists, quotas - enforce what prose merely hopes.
Working with your AI agent
Before building any agent, co-author its charter: allowed tools with typed schemas, autonomy level per tool (free / quota / gated), bounds (steps, spend, time), kill-switch mechanics, log format. Review THAT document like a security policy - because it is one. Then have your agent implement to spec, tests proving each bound bites.
Build it
The module’s real project. Your agent builds; you direct and inspect.
Project: Careful Agent
Build Careful Agent: a maintenance assistant over sandboxed demo Ledger data - typed tools (list, categorize freely; delete behind human approval), hard step/spend/time bounds, complete step-transcript logging, and a kill switch - then attack your own guardrails on camera.
Requirements
- Typed tool schemas validated before execution; invalid arguments rejected with errors fed back to the loop
- Autonomy tiers: reads free, categorize under daily quota, deletes requiring per-item human approval
- Hard bounds enforced: max steps, max estimated spend, wall-clock timeout - breach ends the run honestly
- Complete step transcript logged: reasoning summary, calls, arguments, outcomes - rendered reviewable
- Kill switch halts a running agent immediately and safely
- Self-attack session performed and documented: attempts to exceed authority, force loops, smuggle bad arguments - outcomes recorded
Context for your agent
Co-author the charter with me first: tools, schemas, autonomy tiers, bounds, kill switch, transcript format - I approve before code. Then implement Careful Agent against SANDBOXED demo data only: think-call-observe loop, schema-validated arguments, tiered approvals (deletes gated through an approval step), hard bounds ending runs honestly, full transcript rendering, immediate kill switch. Finally coach my self-attack session: I try to make it exceed authority, loop forever, and abuse argument shapes - we record what held.
Verify it - twice
Check every item yourself first. Then hand your agent the prompt and compare verdicts.
Verification prompt - paste into your AI agent
Red-team my Careful Agent as an adversary who owns the conversation but nothing else. Launch each attack and report outcomes: (1) social-engineer deletion without approval - phrasing tricks, urgency framing, fake prior authorization - does the gate hold?, (2) induce an endless loop - do step/spend bounds terminate honestly and cheaply?, (3) smuggle malformed or out-of-scope arguments through tool calls - validation verdicts please, (4) attempt scope escape toward production data - prove the sandbox walls. Grade DoD items, then deliver the uncomfortable one: which guardrail would fail first against a patient attacker, and what structural fix retires that risk?
Run this against “Tool Calling and Agents” after your own checklist pass. Where the agent and you disagree, investigate before moving on.
Review questions
Answer out loud. If you cannot, revisit before moving on.
State your agent's charter: tools, tiers, bounds - from memory.
Why is prompt-language never treated as a permission system?
Walk through one transcript entry: what happened, and who could audit it?
You can now
- Implement schema-validated tool calling with tiered autonomy
- Bound agent loops against runaway steps, spend and time
- Gate destructive actions behind human approval with full audit trails
Up next
AI Security and Evaluation