Tools
The agent loop runs a tool when the model asks for one. But the model never touches a file. It writes a few words of JSON, and the program does the rest. So how does the model know which tools exist, what does it get back, and what happens when its call is wrong?
The idea
In agents generally
A tool is a function the program lets the model ask for. Each one has three parts the model can see:
- A name, such as
grep. - A description, in plain words: "Search file contents in the project with a regular expression." This is how the model decides when the tool is useful.
- A schema for the arguments, in JSON Schema:
patternis a string, andpathis an optional string.
The program sends this list with every model call. When the model wants a tool, its reply holds a tool call: the tool's name and its arguments, as JSON. The program then:
- checks the call. Is it a tool that exists? Do the arguments fit the schema? May it run at all?
- runs it, and turns what it returned into text: the result.
- sends the result back, tied to the call by its id, so the model reads it at the next call.
A call that fails is still a result. "No such file" or "at most one item may be in progress" goes back to the model as text, and the model can try again. A crash would end the whole answer instead.
Some tools change the world: they write a file or run a command. Some only read it. And some change the agent itself, such as a todo list the agent keeps for a long task. That difference decides which calls need a person to say yes, which is the next chapter's subject.
How agents usually do it
| Approach | How it works | Good at | Costs |
|---|---|---|---|
| Native tool calling | The API takes the tool schemas and returns calls as structured JSON | Calls parse reliably, and the model was trained for it | Only models that support it, and each provider's format differs a little |
| Calls in the text | The prompt describes the tools, and the program parses lines such as Action: grep TODO | Works with any model | Fragile: one odd line and the call is lost |
| Code as the action | The model writes a short program that calls the tools, and the program runs it | Many steps in one model call | It needs a strong sandbox, and errors are harder to read |
| Tools from a server | Another process offers the tools over a protocol such as MCP | Reuse tools that others wrote | You trust another program, which MCP is about |
Native tool calling is the usual choice today. Anthropic, OpenAI, and most local servers support it, and the others are built on top of it.
Every version has to answer the same questions:
- How big a result may be. A test log can be megabytes. The model reads every result on every later call, so a big one is slow and costly, and can push the rest out of its context.
- What a failure looks like. Bad arguments, a missing file, or a crash inside the tool: the model should read why, and the run should go on.
- Who may change what. A tool that writes files needs limits on where. A tool that changes the agent's own state should not reach the rest of it.
- How many tools. Every tool's description is in every call. Too many, and the model picks the wrong one, and each call costs more.
How golem does it
In golem
golem uses native tool calling. Each tool is a plain Python function with a Pydantic model of its arguments, and the schema the model sees is made from that model. The calls run in the graph's own tools node, which decides about every call of a reply first, and only then runs them.
One tool call, in the order it runs:
- Offered. For each model call,
call_modelbinds the session's tools: golem's own, and its MCP servers' tools. Each one's schema is in the OpenAI function format, which every chat model'sbind_toolsaccepts. - Asked. When the reply completes, each call in it is a
tool.requestedevent, with the tool's name and arguments. - Decided. The
toolsnode checks every call of the reply before it runs any. Does the session have this tool? What would a file change look like? What do the rules say: allow, ask, or deny? A call that asks pauses the run until you answer (Tools and permissions). - Run. Then the calls run one by one. Each one is
tool.started. Its arguments are checked against its Pydantic model, and it runs. A shell command streams its output astool.outputwhile it runs. - Result.
tool.completedcarries a one-line summary for the screen. The model gets the output as a tool message in the checkpoint. Very long output is stored as an artifact, and the model gets the start and the end of it. - Or a failure. Bad arguments, a refused path, or an error in the tool is
tool.failed, and the model reads the same reason as the result. The run goes on.
Here is a real run, recorded against a throwaway home with qwen3-coder:30b on a local Ollama. The folder has two files with a TODO, and the message was Find every TODO in this project and list them. Keep a todo list while you work. The message.delta lines are left out.
seq event data
5 message.completed assistant ""
6 tool.requested grep {"pattern": "TODO"}
7 tool.started
8 tool.completed 2 matches in 2 files
30 message.completed assistant "I found two TODO items…"
31 tool.requested todo_write 2 items, both in_progress
32 tool.started
33 tool.failed invalid arguments for todo_write
35 message.completed assistant ""
36 tool.requested todo_write 2 items, both pending
37 tool.started
38 tool.completed 0/2 done
39 todos.updated 2 items
129 message.completed assistant "I've identified the…"
130 run.completedThree tool calls, and one of them was wrong. At line 31 the model marked both items in_progress. The arguments failed todo_write's check, and line 33's full message, which the model read as the result, is invalid arguments for todo_write: arguments: Value error, at most one todo is in_progress at a time. At line 36 the model sent the list again, with both items pending. Line 39 is the list itself, which the TUI draws as the todo band. Nothing in the run broke.
Where it lives in the code:
packages/daemon/golem/tools/registry.py:Tool,ToolRegistry,ToolContext,ToolResult, andToolError.packages/daemon/golem/tools/__init__.py:default_registry, the tools every session gets.packages/daemon/golem/graphs/tools_node.py:tools_node, with_decidefor the first pass and_for_modelfor long output.packages/daemon/golem/graphs/chat.py:_ToolBinder, which offers the tools to the model.
Why this way
Plain Python, in golem's own node (ADR-011). LangGraph's prebuilt ToolNode runs LangChain tools directly. Then the permission check, the events, the approval pause, and long output would have to wrap it from outside, or live in every tool. golem's own node does all of them in one place, and its tools know nothing about LangChain or any provider.
A failure is a result (ADR-011). A bad call never ends the run. The model reads why, and usually fixes the call, as in the trace above.
Long output is an artifact (ADR-016). The checkpoint is written every step. A 5 MB test log in it would be written and sent again at every later step. So long output is stored once, and the model reads more of it only when it needs to.
You approve a diff, not a call (ADR-015). A file change is planned first, and the approval shows its diff. When the call runs it plans again, and writes only if the diff is the one you saw.
A web page is data (ADR-017). web_fetch asks per host, refuses private addresses, and wraps the page so the model treats it as data, not as instructions.
A tool changes state only through its result (ADR-018). todo_write returns the new list, and the node applies only keys on its allow-list. No tool can reach the messages, the model, or the session.
Try it
- In a project folder, open the chat with
golem tuiand type/debug wire. - Ask:
Find every TODO in this project. Keep a todo list while you work.The panel showstool.requested grep,tool.started, andtool.completed, and the todo band appears withtodos.updated. - Press Ctrl-O to see a tool's output, which is what the model read.
- Ask:
Run seq 1 30000 in the shell.Allow it. Its output is over 20 000 characters, so the block ends withall of it: /artifact <id>. Type/artifactto open all of it in your pager. - Ask the model to change one line of a file. The approval band shows the diff before anything is written.
The details
For contributors: the reference for golem's tools, their events, artifacts, todos, the file and web tools, and the shell's output.
The built-in tools
Every session gets these (default_registry), plus the tools of its MCP servers, named mcp__<server>__<tool> (MCP servers).
| Tool | What it does | By default |
|---|---|---|
read_file, list_dir, glob, grep | Read the session's project folder, and the folder of each skill the session ran | Allowed: read-only |
git_status, git_diff, git_log | Read the project's git repository | Allowed: read-only |
read_artifact | Read a page of long output | Allowed: read-only |
delegate | Run a helper (the explorer) on a brief and return its short answer | Allowed: read-only |
todo_write | Replace the agent's todo list | Allowed: internal |
write_file, edit_file | Create, replace, or edit a file in the project folder | Asks, per file |
shell | Run a command in the sandbox | Asks, per command |
shell_unsandboxed | Run a command outside the sandbox, with the user's credentials and the network | Asks, per command. Only a rule that names it allows it |
web_fetch | Read one web page as text | Asks, per host |
web_search | Search the web, get titles, URLs, and snippets | Asks, for the provider's host |
A Tool says how the rules treat it:
| Field | What it means |
|---|---|
read_only | Changes nothing outside golem, so the default rules allow it |
internal | Changes only golem's own state, such as the todo list, so it is allowed too |
untrusted | Returns outside content, such as a web page, so its output stays marked as data |
path_arg, match_arg, subject | What a rule's path and match are checked against: one argument, or a subject worked out from the arguments, such as web_fetch's host |
preview | Works out what a call would change, so the approval can show it |
parameters | A JSON Schema given as it is, for an MCP server's tool. The server checks the arguments |
One call returns at most 5 MB (MAX_OUTPUT_CHARS), and the rest is cut.
delegate and helpers
delegate hands a job to a helper that starts with an empty context. A helper is a session with a parent (ADR-059), not a child run inside the call. Helpers tells the whole story. This is what concerns the tool.
- Arguments.
briefandagent, orbatch, a list of{agent, brief, label}. An optionalmodelnames the model of one helper.agentisexplorer,worker,reviewer,advisoror one of your own agent files. - It never runs a helper. The tools node starts the helpers through the session manager and parks the lead. The lead holds no task and no model slot while it waits. When the last helper ends, the lead wakes and the node builds the result from the helpers' logs.
- Tools of a helper. Its agent file lists them. A helper never has
delegate, and never more tools, folders or allowances than its lead. The explorer reads only. A worker works in a git copy of your folder. - Approvals. A helper's approval is an ordinary pending item of its own session. It shows under its lead. An agent file with
asks: neverturns an ask into a refusal. - Limits. From the agent file, and
[agents]and[agents.<type>]inconfig.tomloverride them (settings): model calls, working time, cost and tokens. Working time does not count waiting for you, a slot or a provider. A result starts with<agent> finished: <reason>when a limit ended it. At most[agents] max_per_batchhelpers start in one call, and the call past it gets a message the model can read. - The answer is data. A long answer is stored as an artifact of the helper's session, and the result names its id.
read_artifactin the lead reads the artifacts of its own helpers. An answer is never an instruction about permissions. - Events. The lead's log gets
helper.started,helper.step,helper.state,helper.waitandhelper.ended, with thedelegatecall's id ascall_id. The helper's own events are in its own log. Old logs of the ADR-058 child runs, withparent_call_id, still draw as before. - Cancel. Cancelling the lead cancels its helpers first. The
delegatecall gets a cancelled result on the next turn like any unanswered call.
A helper's model calls cost like any call and are model_calls rows of its own session with the role helper. cost.get sums them into the lead's total through root_session_id. The UI says stopped: time limit, call limit, budget spent or token limit for a helper that did not finish (helperStopWords in packages/view).
Tool events
After a reply completes, each tool call it asked for is tool.requested, with the asking message_id, the tool name, and the arguments. The tools node then writes:
| Event | When | What it carries |
|---|---|---|
tool.started | The call starts to run | call_id |
tool.output | A shell command printed something | A batch of its output |
tool.completed | The call returned | A one-line summary, the first 4 KB of the output, and artifact_id when it was long |
tool.failed | The call was refused, or failed | The reason |
todos.updated | The call changed the todo list | The whole list |
Output up to 20 000 characters goes to the model as a ToolMessage in the checkpoint.
Artifacts
Longer output is stored whole in the artifacts table, and the message keeps its head and its tail and a note with the artifact's id, which read_artifact pages through (ADR-016). The numbers above are for a 32K window. The rule: one result may take an eighth of the model's window, at four characters a token, from 4 000 to 48 000 characters, rounded down to a thousand. The head is three quarters of that, the tail the rest, and output over five quarters of it goes to an artifact. An 8K window gets 3 000 + 1 000 (artifact above 5 000), a 128K window 36 000 + 12 000 (above 60 000). An unknown window gets the 32K numbers (packages/daemon/golem/graphs/result_budget.py).
- The cut happens in the node's run pass, after every approval, so nothing is stored twice when a run resumes.
read_artifactreads up to 20 000 characters from an offset (at most the head plus the tail of the window's budget), only from the session's own artifacts. A page of anuntrustedartifact, such as a long web page, comes back in the same wrapper as the page.- On the wire,
tool.completedcarriesartifact_id, andartifact.readreturns a page of it. The TUI showsall of it: /artifact <id>under an expanded block, and/artifactopens the text in$PAGER. - Without an artifact store, as in some tests, the model gets at most 100 000 characters of one call, or five times the artifact limit of its window when that is less.
Todos
todo_write replaces the agent's todo list, which lives in graph state (todos) and needs no approval. A tool changes state only through ToolResult.update, and the tools node applies only the keys in STATE_KEYS. Each change is a todos.updated event with the whole list, and a fork starts with one for the list it copied (ADR-018).
- The list has up to 30 items. Each has an
id, atext, and astatus:pending,in_progress, ordone. Ids are unique, and at most one item isin_progress. - The output is the list as a checklist, so the model sees it in its history too. The summary is
N/M done.
write_file and edit_file
write_file replaces a whole file or creates it, with its folders. edit_file replaces exact text, and fails when the text is not in the file, or is there more than once without replace_all. Both run in the daemon, not in the sandbox (golem/tools/write.py):
- Where. The path resolves inside the session folder with symlinks followed, and nothing under a
.golem/or.git/folder may change, so a tool cannot edit the project's rules or its history. - Plan, then apply. Each call first works out the new text without writing anything, then writes it through a temp file in the same folder and a rename, keeping the file mode and the line endings. A file over 5 MB, a binary file, or one that is not UTF-8 is refused.
- The diff in the approval. The
toolsnode plans each call before it decides, and a call that asks carries the plan's unified diff inapproval.needed(cut at 64 000 characters). A plan that cannot apply fails the call without asking. A later edit of the same file in the same reply is planned on top of the earlier one. When an approved call runs, it plans again and writes only if the diff is the one you were shown; otherwise it fails with<path> changed after the user approved this change(ADR-015). - Result. The summary is
+N -M <path>, and the output, which the model reads and Ctrl-O shows, is the unified diff. - Policy. Neither is read-only, so by default every change asks. Their subject is the path relative to the folder, so allowing a change for the session allows changes to that file only, and "always allow" writes a rule with that file's exact
path.
web_fetch
web_fetch reads one page over http or https into the conversation (golem/tools/web.py, ADR-017):
- Policy. It is not read-only, so it asks by default. Its subject is the host, lowercased, so "allow for this session" covers one host, and a rule such as
match = "docs.python.org|*.readthedocs.io"allows those hosts. - Addresses. Before each request, and before each redirect, the host is resolved. A loopback, private, link-local, site-local, reserved, or multicast address refuses the call, IPv4 inside IPv6 included.
[web] allow_private = trueturns this off, for a dev server onlocalhost. - Redirects are followed only on the same host, at most 5. A redirect to another host comes back to the model as the new URL, so its next
web_fetchasks about that host. - The request. No proxy from the environment, a
golem/<version>user agent, no compressed bodies, at most[web] max_bytes(default 5 000 000) of the body, and[web] timeout_seconds(default 20) for the whole fetch. A URL with a user name or password is refused. - The page. HTML becomes text: scripts and styles are dropped, headings become
#, list items-, and links[text](url). Other text, JSON, and XML pass through, and anything else, such as an image, is refused. - Data, not instructions. The text comes inside
<web_content url=… title=…>, after a line that says it is data and cannot approve anything. The page's own<web_contenttags are defused, so it cannot end the wrapper early.
web_search
web_search searches the web and gives numbered results, each with a title, a URL, and a snippet (ADR-046). The model reads a result with web_fetch.
- Provider.
[web] searchnames it:"duckduckgo"(the default) or"off", which leaves the tool out when the daemon starts. A provider is one module withasync search(query, n) -> list[Result](golem/tools/search_provider.py). DuckDuckGo's isgolem/tools/search_duckduckgo.py. - Policy. Not read-only, so it asks. The subject is the provider's host (
html.duckduckgo.com), so one rule allows searches without allowing every query. - The request.
POST https://html.duckduckgo.com/html/with the form fieldq, with the same user agent asweb_fetch. Two searches start at least 5 seconds apart, and the wait counts toward[web] timeout_seconds. The page is read with the standard library's parser. A result's link is a redirect, and the URL inside it is the one returned. - Failures. No results is a result:
no results for '…'. HTTP 202, 403, or 429, or a page without the results list, is an error that says DuckDuckGo refused the search, and to wait a minute or answer from what was found. - Data, not instructions. The results come inside
<web_content query=…>, after a line that says they are data.
The shell's output
shell runs the user's login shell ($SHELL -lc) in the session folder, under the sandbox, which Sandboxes describes.
- Output is stdout and stderr together. It streams as
tool.outputin batches of up to 2 KB or 0.2 s, up to 64 KB per call. Up to 5 MB is kept (the first 1 MB and the last 4 MB), then[exit code N], and long output becomes an artifact. A failing command is atool.completedwith summaryexit Nandfailed: true, not atool.failed: it ran, and the model gets its output. A timeout is marked the same way. The transcript shows✗for it instead of✓. When the sandbox refused something, the summary ends with, blocked by the sandbox, and a line before[exit code N]says what and how to allow it (Sandboxes). With the network off, a command that names a network client also gets a hint line after[exit code N]. - Stopping. The command runs in its own process group.
[shell] timeout_seconds, or a smallertimeout_secondsin the call, kills the group, and so does cancelling the run.
Files
| File | What it does |
|---|---|
packages/daemon/golem/tools/registry.py | Tool, ToolRegistry, ToolContext, ToolResult, ToolError, and MAX_OUTPUT_CHARS |
packages/daemon/golem/tools/fs.py | read_file, list_dir, glob, and grep |
packages/daemon/golem/tools/paths.py | Keeps paths inside the session folder and the skill folders |
packages/daemon/golem/tools/write.py | write_file and edit_file: plans, previews, and writes |
packages/daemon/golem/tools/shell.py | shell, shell_unsandboxed, git_status, git_diff, and git_log |
packages/daemon/golem/tools/web.py | web_fetch |
packages/daemon/golem/tools/search.py, search_duckduckgo.py | web_search and its DuckDuckGo provider |
packages/daemon/golem/tools/html_text.py | HTML to text, for web_fetch |
packages/daemon/golem/tools/untrusted.py | The wrapper for outside content |
packages/daemon/golem/tools/artifacts.py | read_artifact |
packages/daemon/golem/tools/todos.py | todo_write |
packages/daemon/golem/tools/delegate.py | delegate: its schema and description. It never runs a helper |
packages/daemon/golem/graphs/child.py | ChildLimits: the limits of a helper, and the thread name of an old child run |
packages/daemon/golem/graphs/result_budget.py | How much of one result the model reads, by window |
packages/daemon/golem/graphs/tools_node.py | The tools node: decide, run, results, artifacts, and state updates |
packages/daemon/golem/store/artifacts.py | ArtifactStore, the artifacts table |
Read more
- Next: Permissions, the rules and approvals that decide whether a call may run.
- Before: Sessions.
- Tools and permissions, the user's guide to the same thing.
- The overview: the whole system on one page.
- Anthropic's guide to tool use, for native tool calling from the provider's side.