Sub topic 2
How an Agent Turns Model Output into Tool Calls
This chapter assumes you know basic LLM function calling, can read short Python and JSON snippets, and understand tokens and API authentication. It shows where permissions can be enforced in an agent, which later chapters build on.
The model proposes, the runtime executes
A language model cannot run code, read files, or call APIs. It produces text. When you give it tool definitions, it can produce a structured request such as "call read_file with path = notes.txt". That request is only a proposal.
The agent runtime is the ordinary program around the model. It receives the proposal, decides whether to run it, runs the function, and sends the result back to the model. Every real effect passes through this runtime, so the runtime is where permissions are enforced.
What the model sees and what it returns
Each tool is described to the model by a name, a description, and a JSON schema for its arguments:
{
"name": "read_file",
"description": "Read a text file from the workspace.",
"input_schema": {
"type": "object",
"properties": {
"path": { "type": "string" }
},
"required": ["path"]
}
}When the model decides to use the tool, the API response contains a tool-call block instead of plain text:
{
"type": "tool_use",
"id": "call_01",
"name": "read_file",
"input": { "path": "notes.txt" }
}The name and input values are generated text. The model can produce a tool name that you never offered, or arguments that break the schema. A schema is a hint to the model, not a guarantee about its output.
A minimal loop
The loop below shows the shape of every agent. The call_model function stands in for your provider's API.
def run_agent(task: str, tools: dict, call_model) -> str:
messages = [{"role": "user", "content": task}]
while True:
reply = call_model(messages, tool_names=list(tools))
if reply["type"] == "text":
return reply["text"]
# The model asked for a tool. The runtime decides what happens next.
name, args = reply["name"], reply["input"]
if name not in tools:
result = f"error: unknown tool {name!r}"
else:
result = tools[name](**args)
messages.append({"role": "tool", "name": name, "content": str(result)})Three points in this loop matter for least privilege:
- Tool selection.
toolsis the set of functions the model can reach. A smaller dictionary means fewer possible actions. - The check point. Between reading
nameandargsand calling the function, the runtime can validate and authorize. This loop does almost nothing there, and later chapters add real checks. - The result. Whatever the tool returns is appended to the context and read by the model on the next turn.
Where permissions attach
The agent's authority comes from the tool functions and the credentials they use. The model has no authority of its own. If read_file can open any file the process can read, then the model can read any such file, because the runtime runs the call with the process's identity.
| Stage | What happens | Control that fits here |
|---|---|---|
| Tool definitions | Model learns what it may ask for | Offer only the tools the task needs |
| Proposed call | Model emits name and arguments | Treat as untrusted input |
| Check point | Runtime inspects the call | Authorization, argument validation, approval |
| Execution | Tool runs with credentials | Scoped credentials, sandboxing |
| Result | Output returns to the model | Consider what untrusted content enters context |
Results feed back into the model
The last row of the table needs attention. A tool result goes back into the model's context as text. If a file, web page, or email contains instructions, the model reads them alongside your task. The model has no reliable way to separate your instructions from text that arrived through a tool. The next chapter covers how attackers use this.
Exercise
In the loop above, the model asks for a tool named delete_record, which is not in tools. What does the runtime do, and why is that the right place to decide?
Show answer
The runtime finds that the name is not in tools and returns an error string instead of running anything. The runtime decides because it is ordinary code that controls what executes. The model only proposes calls, so a model that asks for an unlisted tool cannot run it.