Sub topic 2
Tool Calls: The Model Asks, the Harness Acts
Assume you know basic Python, JSON, HTTP APIs, command-line tools, and code editors. A coding agent is a language model connected to a program called the harness. The model chooses actions, but the harness performs them.
Tool calls: the model asks, the harness acts
A tool is described with a name, a purpose, and an input schema. The schema defines valid arguments. The model does not directly read a file or execute a command. It returns a structured tool call:
{
"name": "read_file",
"arguments": {
"path": "src/app.py"
}
}The harness validates the arguments, checks permissions, calls the implementation, and adds the result to the conversation. The next model call can then use that result.
The usual toolbox: read, search, edit, run
Most coding agents expose tools with narrow responsibilities:
- Read returns selected files or ranges.
- Search finds symbols, text, or patterns.
- Edit applies a targeted change.
- Run executes tests, formatters, or other commands.
A useful tool returns bounded output. Without limits, a large log or generated file can consume the context window and hide important information.
The agent loop, step by step
The harness repeats this sequence:
- Send the current messages and tool definitions to the model.
- Receive text and zero or more tool calls.
- Stop if there are no tool calls.
- Execute each requested tool.
- Append each result with its tool-call identifier.
- Send the expanded message history to the model again.
def run_agent(task, call_model, tools, max_steps=10):
messages = [{"role": "user", "content": task}]
for _ in range(max_steps):
reply = call_model(messages, tools)
messages.append(reply)
if not reply.get("tool_calls"):
return reply.get("content", "")
for call in reply["tool_calls"]:
try:
result = tools[call["name"]](**call["arguments"])
except Exception as error:
result = f"tool error: {error}"
messages.append({
"role": "tool",
"tool_call_id": call["id"],
"content": str(result),
})
return "stopped: step limit reached"Knowing when to stop
A harness should stop when the model returns no tool calls, reaches a step limit, or encounters a policy or permission failure. A step limit prevents loops such as repeatedly rerunning a failing test. Error results should be explicit and bounded so the model can recover or report the failure.
Exercise
Why does the harness append a tool result before making another model call?
Show answer
The model is stateless between calls, so the appended result supplies the observation needed for its next decision.