Sub topic 1
Where an approval step fits in the agent loop
Prerequisites
This chapter assumes you know the following:
- How an LLM agent calls tools: the model proposes a tool name and arguments, and your code runs the tool.
- How to read short Python programs.
- JSON and simple data structures such as dicts and lists.
- What a web request or a background job is.
The loop you are modifying
An agent runs a loop. The model reads the conversation so far and either answers or proposes a tool call. Your code runs the tool and appends the result to the conversation. The loop then repeats until the model answers.
An approval step is a checkpoint inserted between the model's proposal and the tool's execution. The model only proposes an action. Your code decides whether to run it. That split is what makes a human checkpoint possible.
The three parts of a gate
Every approval gate answers three questions.
- Does this call need approval? A risk check decides. Later chapters cover how to classify risk.
- What happens while we wait? The run pauses. For a quick prompt in a terminal, the program can block. For a reviewer who answers in an hour, you must save state and stop. A later chapter covers this.
- What goes back to the model? Whatever the human decides becomes a tool result. Approval returns the real output. Rejection returns a message such as
{"rejected": True, "reason": "..."}. The model sees that message and can choose another path.
The third point matters. A rejection is not an error that crashes the run. It is information the agent can use to adjust.
A minimal sketch
The sketch below shows where the gate sits. It uses a fake model and one tool so it runs as written. The gate is the if block around the tool call.
from dataclasses import dataclass
@dataclass(frozen=True)
class ToolCall:
name: str
args: dict
RISK = {"search_docs": "low", "send_email": "high"}
def risk_of(call):
# Unknown tools default to high risk.
return RISK.get(call.name, "high")
def fake_model(history):
# Proposes one email, then answers once it sees a tool result.
if not any(m["role"] == "tool" for m in history):
return ToolCall("send_email", {"to": "[email protected]", "body": "Hi"})
return None
TOOLS = {"send_email": lambda to, body: {"sent": True, "to": to}}
def ask_human(call):
print(f"Approve {call.name} {call.args}? (y/n)")
return {"action": "approve"} # stand-in for real input
def agent_loop():
history = [{"role": "user", "content": "Email Alice"}]
while True:
call = fake_model(history)
if call is None:
return history
if risk_of(call) == "low":
result = TOOLS[call.name](**call.args)
else:
decision = ask_human(call)
if decision["action"] == "approve":
result = TOOLS[call.name](**call.args)
else:
result = {"rejected": True}
history.append({"role": "tool", "content": result})
print(agent_loop())The loop code does not change shape. You add one branch and make sure both outcomes produce a tool result.
Where to place the gate
Put the gate in your own code, at the point where tool calls are executed. Do not rely on the model to ask permission in its text output. A model can skip the request, phrase it vaguely, or be manipulated into skipping it. Code that sits between proposal and execution cannot be skipped by the model.
Also put the gate on every path that reaches a tool. If one helper runs tools directly, the gate has a bypass.
What you will build next
The following chapters fill in each part: why checkpoints matter, how to classify risk, the decision types, and how to pause and resume safely. Each one adds to the loop shown above.
Exercise
In the sketch above, why does the rejection branch still append a tool result to the history instead of raising an exception?
Show answer
The model needs to see the outcome of its proposed call. A rejection result tells it the action did not happen and, ideally, why, so it can choose another approach or explain to the user. An exception would end the run and discard that chance to adapt.