The LLM writes a tool call as JSON, and the code checks it before running it. The LLM proposes and the code executes, so everything between the proposal and the execution (checks, limits, approval) is under our control. The basic mechanism is in LLM Tool Calling.
Schema
Each tool is described to the LLM by a JSON schema: its name, a description and typed inputs. A finish tool lets the LLM say it is done.
TOOL_SCHEMAS = [
{"name": "search_products", "description": "Search the shop catalogue for products.",
"parameters": {"type": "object",
"properties": {"query": {"type": "string"}, "max_price": {"type": "number"}},
"required": ["query"]}},
{"name": "check_compat", "description": "Check that a charger fits the user's laptop.",
"parameters": {"type": "object",
"properties": {"product_id": {"type": "string"}, "laptop": {"type": "string"}},
"required": ["product_id", "laptop"]}},
{"name": "add_to_cart", "description": "Put one product in the cart and show the full total.",
"parameters": {"type": "object", "properties": {"product_id": {"type": "string"}},
"required": ["product_id"]}},
{"name": "checkout", "description": "Pay for the cart.",
"parameters": {"type": "object", "properties": {}}},
{"name": "finish", "description": "Stop and report the result to the user.",
"parameters": {"type": "object", "properties": {"answer": {"type": "string"}},
"required": ["answer"]}},
]
SCHEMAS = {s["name"]: s for s in TOOL_SCHEMAS}The LLM never sees the Python code of the tools. It sees only these schemas.
Validation
JSON_TYPES = {"string": str, "number": (int, float)}
def validate_call(call):
"""Check a proposed tool call against its schema. Returns a list of problems (empty means OK)."""
schema = SCHEMAS.get(call.get("name"))
if schema is None:
return [f"unknown tool '{call.get('name')}'"] # not on the allow-list
params, args = schema["parameters"], call.get("arguments", {})
problems = [f"missing argument '{a}'" for a in params.get("required", []) if a not in args]
for name, value in args.items():
if name not in params["properties"]:
problems.append(f"unexpected argument '{name}'")
elif not isinstance(value, JSON_TYPES[params["properties"][name]["type"]]):
problems.append(f"'{name}' should be a {params['properties'][name]['type']}")
return problems
print(validate_call({"name": "add_to_cart", "arguments": {"product_id": "VM-65"}})) # []
print(validate_call({"name": "transfer_money", "arguments": {"to": "TechDealz88"}})) # unknown tool
print(validate_call({"name": "check_compat", "arguments": {"product_id": "VM-65"}})) # missing laptopNotes:
- an empty list means the call is fine
- the first check is an allow-list: a tool that is not in the schemas, such as
transfer_money, is never run - the problems can be sent back to the LLM as an observation, so it can correct the call
- validation checks the form of the call, and limits on values (such as a spending limit) need their own checks, see Agent Mandate (AP2)
A standard way to offer the same tools to many agents is Model Context Protocol (MCP).