Guardrails and Approval

Time: ~12 minutes. Tangible win: add a hard step limit and a human approval gate to one write action, and test the rejection path.

An agent can call any tool you give it, as often as the loop allows. Guardrails are the controls that decide what it may do, how long it may run, and when a person must step in. Anthropic's guidance on agents calls for stopping conditions such as a maximum number of iterations, and for extensive testing in sandboxed environments with appropriate guardrails.

Primary source Using function tools with human in the loop approvals, Microsoft Learn. The Python tab shows the approval pattern in a framework-neutral form. The concept applies to any stack.

Four kinds of guardrail

The model should never be the only enforcer. A system prompt that says "never delete records" is a request, not a control. The control is a tool that does not exist for that run, or an approval gate that stops the call.

How an approval pauses a run

Microsoft's approval pattern shows the mechanics clearly. A tool marked to require approval does not execute when the model requests it. The run returns early with a request for user input instead of a final answer. The caller shows the pending call to a person, collects the decision, and resumes the run with an approval or rejection. The caller checks every response until no requests remain.

# Framework-neutral sketch of an approval gate
APPROVAL_REQUIRED = {"delete_customer_record", "issue_refund"}

def run_tool(call):
    if call.name in APPROVAL_REQUIRED:
        return {"status": "pending_approval", "call": call}   # do not execute
    return execute(call)

# The caller resumes only with a recorded decision:
decision = ask_human(pending_call)          # True or False
if decision:
    result = execute(pending_call)
else:
    result = {"status": "rejected_by_user"}
messages.append(tool_result(pending_call.id, result))
Bind approvals to the request they answer Microsoft's documentation warns that an approval response should authorize a call only when it matches a pending request in the same session. An unbound response can let a fabricated or replayed answer approve a privileged action. Your gate needs the same binding, using the call ID.
Credentials are a guardrail too The same documentation advises against using a broad default credential in production, and suggests a specific credential such as a managed identity. Scope each tool to the least privilege it needs.

Practice

  1. List every tool your agent could call and mark each as read or write. Mark any write action that is hard to reverse.
  2. Add an approval gate to one of those write actions, using the sketch above or your framework's equivalent.
  3. Test the rejection path. Confirm that a rejected call returns a clear result to the model and does not run. Then confirm the step limit still stops a looping run.

Retrieval check

An agent is about to delete a customer record. Which control is strongest?

Correct. A person confirms the action before it runs. Instructions are requests, while an approval gate is an enforced control.

Not quite. Instructions and logs do not stop the action. Only a gate that holds the call until a person approves it is an enforced control.

When a run pauses for approval, what must the caller do next?

Correct. The run is paused, not finished. The caller gathers the decision and resumes the run with that response.

Not quite. A paused run needs a decision from a person. Retrying or raising limits does not supply one, and the call should not run without it.

Ask the agent: "Which of my tools should require approval, and how should I log each decision?" Next lesson covers evaluation, which tells you whether the agent is actually doing its job.