A bounded tool-using workflow.
Let a planner request read-only tools while software enforces permissions and a step limit.
Why this workflow exists.
An agent can be understood as a loop: choose a step, call a tool, observe the result, then decide whether to continue. A loop without authority and stopping rules can repeat actions or expose data. This recipe shows the application side of the boundary: allowed tool names, user checks, and a maximum number of steps.
The example is read-only. It does not send messages, modify accounts, or perform external actions. That keeps the trust decision visible without hiding it inside a model prompt.
Code context. Python 3 standard library only. The planner and tools are local, deterministic stand-ins.
The whole example.
def search_docs(query):
return ["access-1"] if "access" in query.lower() else []
def read_order(order_id, user_id):
if user_id != "user-7":
raise PermissionError("not your order")
return {"order_id": order_id, "status": "shipped"}
TOOLS = {"search_docs": search_docs, "read_order": read_order}
def plan(goal, history):
if history:
return {"name": "finish", "arguments": {}}
return {"name": "search_docs", "arguments": {"query": goal}}
def run_agent(goal, user, max_steps=3):
history = []
for _ in range(max_steps):
call = plan(goal, history)
if call["name"] == "finish":
return {"status": "done", "history": history}
if call["name"] not in TOOLS:
return {"status": "blocked", "history": history}
args = call["arguments"]
if call["name"] == "read_order" and args.get("user_id") != user["id"]:
return {"status": "forbidden", "history": history}
result = TOOLS[call["name"]](**args)
history.append({"tool": call["name"], "result": result})
return {"status": "step_limit", "history": history}
print(run_agent("Find the access guide", {"id": "user-7"}))What each stage does.
plan simulates a model deciding the next step. On its first call it requests search_docs; after one tool result it requests finish. run_agent checks each requested name against TOOLS, checks ownership for read_order, executes a permitted function, and records the result. The range loop creates an absolute step limit.
A real model planner could choose different tools and arguments. The important contract remains: the model proposes; application code validates. A full implementation would use strict argument schemas, exception handling, trace IDs, and an approval step before any external side effect.
What the output means.
The script prints a done status with one search result in history. A forbidden or unsupported call would stop before tool execution.
Where the example stops.
The ownership check in read_order is deliberately simple. Real systems must derive user identity from authentication, check resource ownership server-side, and protect tool results from cross-user exposure. A step limit controls loops but does not prove the agent chose a useful step.