# Deferred tools

A deferred tool is registered, but its schema reaches the model only after `tool_search` loads it. Search is the only way to load. The loaded set is read from the request's own messages, and inside an epoch the tools array only grows at its end.

| Topic | Design | Follows |
| --- | --- | --- |
| [Loading on Anthropic](#anthropic) | `tool_reference` result plus a `defer_loading` array entry | Claude Code |
| [Catalog](#catalog) | Beside the user message, full once, then Added / Removed | Claude Code, Codex |
| [A search that loads nothing](#codex) | Says why in words the model receives; a tool already in the array is "already callable" | Claude Code, Codex |
| [Guides and applications](#activation) | Name their tools; the model loads them | Codex |
| [Source gone](#unavailable) | Entry stays; the call returns "unavailable" | Codex |
| [Called before loading](#not-loaded) | Runs if valid; a failed check says how to load | Claude Code |
| [Mode and deny changes](#epoch) | Array fixed per epoch; dispatch refuses | Codex |
| [Ranking](#ranking) | BM25 over name, namespace, description and schema | Pi, Claude Code |
| [Old search results](#aging) | Never aged | Claude Code, Codex |
| [Branches and nested runs](#isolation) | Loads belong to the run whose messages hold them | Pi |

## Loading on Anthropic

A provider declares a native form under `tool_search` in `provider.json`. Anthropic declares `anthropic-reference` for `anthropic-messages`, with `models_off: ["haiku"]`.

The Grok subscription endpoint declares none: it answers a `tool_search_call` item with 422 "unknown item type", so a load there appends to the array after every other tool.

| Part | With the form | Without it |
| --- | --- | --- |
| Search result | `tool_reference` blocks only, plus a sibling text block: the search's [note](#codex), or `Tool loaded.` when no text is there; the result text stays on the node for the UI | The result text |
| Array | Loaded tools appended with `defer_loading: true`, in first-load order, from the definition the load record kept | Loaded tools appended as ordinary entries after every other tool, in first-load order (`split_tools_for_dispatch`); the request records an expected break `tool_loaded` |
| Beta header | `advanced-tool-use-2025-11-20`; on Vertex `tool-search-tool-2025-10-19` | None |
| Cache | Fingerprint skips deferred entries; breakpoint on the last ordinary tool | Unchanged |
| No match, error, summary-carried tool | Plain text result; a carried tool is an ordinary entry | Same |

**Why this design**

| Compared | Does |
| --- | --- |
| Claude Code | References in the result, `defer_loading` entries in the array; excludes haiku |
| Anthropic `tool_addition` | Announces a tool in history; few models and platforms support it |
| Codex | Definition in a `tool_search_output` history item |
| Pi | Appends to the array; one known reread |
| Hermes | Bridge tools; deferred tools never enter the array |

A `defer_loading` entry is validated only once a result names it, and appending one is allowed; adding an ordinary tool or rewriting one is rejected. Claude Code runs this in production, and with search the only loading path, references cover every case.

## Catalog

The list of loadable tools is the `deferred_catalog` section of `turn_context`, beside the user message. The conversation's system prompt holds none, so a server connecting mid-conversation changes no prefix byte.

| Rule | Value |
| --- | --- |
| Built-in deferred tool | Listed by name |
| MCP server or application | One namespace line, `N tools: a, b, …`, cut to 250 characters; an open application says so |
| Full list | No baseline: the first turn, and the first turn after a summary |
| Later turns | `Added deferred tools` / `Removed deferred tools`; nothing when unchanged |
| Size | At most 4 KiB, names first, then a count of what was left out |
| Baseline | The last catalog the rendered history shows |
| Own system prompt (Agent method, `runtime.exec`) | Catalog stays inline in that prompt |
| No `tool_search` in the turn's tools | Nothing is deferred: every tool of the turn goes out with its schema, so there is no catalog. Without the loader a deferred tool could never be called; Claude Code also sends every tool up front when its tool search is off. `DEFAULT_TOOLS` holds the loader and a selected list with a deferred tool gets it, so this is an Agent that disabled `tool_search` |
| Closing sentence | Says only the listed tools need loading, and that every tool already in the tools list is callable as it is and is never searched for. The loader's description says the same |

**Why this design**

| Compared | Does |
| --- | --- |
| Claude Code | Append-only names in a user-side reminder; all names again after compaction |
| Codex | Developer-message delta by namespace, 250 characters a line, 4 KiB cap |
| Pi | System-prompt section by server, same caps |
| Hermes | Inside the search tool's description, degraded by budget |

History is the only place that changes nothing already sent. `turn_context` already resends only changed sections. A `role: system` message is not used on Anthropic: Claude Code uses the user side there, and some models reject it.

## A search that loads nothing

A `tool_search` result has two parts: the loads (`details["loads"]`) and a note (`details["note"]`), the words for everything it did not load. Both are stored on the tool node, so every later request sends the same bytes.

| Asked for | Note | Loads |
| --- | --- | --- |
| A tool already in the array: resident, or made resident for the turn | `` `send_file` is already callable: it is in your tools list with its full schema. Call it now; it never needs tool_search. `` | None; the array is unchanged |
| A name the turn does not offer | `No tool named x is offered in this conversation.` plus which tools need a search | None |
| Keywords with no match | `No deferred tools matched query …` plus which tools need a search | None |
| A deferred tool, loaded before or not | None | Its definition, recorded as a load |
| A mix | The lines for what was not loaded | The rest |

| Wire | Where the note goes |
| --- | --- |
| Codex (`tool_search_output`) | The output carries the loaded definitions, `tools: []` when there are none; the note follows as a user item wrapped in `<tool-search-result>` |
| Anthropic references | With loads: the text block beside the `tool_reference` blocks. Without: the result text, which is the note |
| Every other wire | The result text, which starts with the note |

**Why this design**

| Compared | Does |
| --- | --- |
| Claude Code | `select:` of a tool that is already loaded returns that tool as a match, "a harmless no-op that lets the model proceed without retry churn"; the file-delivery tool is never deferred |
| Codex | Searches deferred tools only and sends an empty `tool_search_output` for every miss; its loader's description names what a search covers |
| Pi | Returns text on its own wire |

An empty `tool_search_output` alone reads as "no such tool". A model that searched for `send_file`, which the array already held, answered that it could not send the file: the answer was in the result text, which this wire never sends.

Claude Code's answer lets the model go on, so a search for a callable tool says so first and in the positive.

| Rejected | Because |
| --- | --- |
| The tool again in the output, kept in the array | Two entries of one name, a shape no client is known to send |
| The tool in the output, out of the array | The array changes and the prefix is re-read |
| Recording it as a load | A resident tool becomes a `defer_loading` entry on Anthropic |

A message after a tool output is a shape every wire already uses for tool attachments.

The search is avoided in the first place by saying which tools need one: the catalog and the loader's description name the deferred list as the only tools to search for.

## Guides and applications

A guide that teaches deferred tools, and an application when it opens, end with `activation_note`: ``Tools for this: `a`, `b`. Load them with `tool_search select:a,b`.`` Neither loads anything; an open application's tools stay deferred.

**Why this design**

| Compared | Does |
| --- | --- |
| Codex | An explicitly mentioned app's tools stay deferred |
| Claude Code, Pi | Loading only through search |
| Hermes | Only searchable tools can be called |

One loading path changes the array one way on every provider, and opening an application no longer rereads the prefix. The cost is one cheap, recorded `select:` call.

## Source gone

| Event | Reason given |
| --- | --- |
| Application closed | `its application '<id>' was closed` |
| Owner unloaded | `'<owner>' was unloaded` |
| MCP server disconnected | `MCP server '<name>' is disconnected` |

The tools leave the registry and the catalog sends a Removed line. A loaded tool keeps its array entry; calling it returns `Tool <name> is unavailable: <reason>.` as an error. No `tool_removal` is sent.

**Why this design**

Codex keeps history and answers `unsupported call`; Anthropic advises keeping the definition and returning an ordinary error. Claude Code rewrites old references, which is a history edit. An array that never shrinks cannot fail validation or reread the prefix.

## Called before loading

| Call | Result |
| --- | --- |
| Valid arguments | Runs |
| Failed check, not loaded | The error ends with ``This tool's schema was not sent. Load it with `tool_search select:<name>`, then retry.`` |
| Failed check, loaded | The error alone |

**Why this design**

This is Claude Code's `buildSchemaNotSentHint`. Refusing every unloaded call would waste a round trip on calls that were right.

## Mode and deny changes

An epoch is keyed by provider, model and first message (`tool_epoch.epoch_key`), so a summary or a model switch starts one. Its first request stores the withheld set with the tool snapshot.

| Change | Array | Dispatch |
| --- | --- | --- |
| Plan mode on or off | Unchanged | `permission_decision` refuses |
| `exit_plan_mode` outside plan mode | Unchanged | Refused, `PLAN_MODE_NOT_ACTIVE` |
| Whole-tool deny rule, missing capability | Removed from the next epoch | Refused meanwhile |
| Structured-output submit tool appears | New epoch record with it | — |

**Why this design**

Codex refuses at dispatch in plan mode; Claude Code refuses plan-mode calls but filters denied tools out; Anthropic advises against swapping tool lists in place. Changing the array mid-conversation fails on models that validate the tool set, and dispatch already enforces permission.

## Ranking

| Rule | Value |
| --- | --- |
| Ranker | `tool_rank`, Pi's BM25: k1 1.2, b 0.75, name weight 3, namespace weight 0.5 |
| Indexed | Name, namespace (MCP server or application), description, hint, schema property names and descriptions |
| Tokens | Split on camelCase and `__`; stop words dropped; stemmed; CJK one character each |
| Order | Exact name, then query terms matched, then score |
| A tool's name | `select:a,b` loads exactly the named tools. A query that is exactly one tool's name does the same and ranks nothing: `scheduler` loads `scheduler`, not `cron` and `remind` with it. A name is a tool; applications and MCP servers are not selectable |
| Kept | `select:`, comma lists, `+term` filters first, the Agent's allowed set; loaded tools included |
| Default count | 5 |

**Why this design**

Pi and Codex use BM25; Claude Code uses fixed keyword scores. Pi's ranker spells out every rule and ports without a dependency. Every match is loaded for good, so the default stays at Claude Code's 5 rather than 8.

The argument is itself named `select`, so a model asking for one tool writes `select: scheduler` as often as `select:scheduler`. Claude Code answers a query that is an exact tool name with that tool before any scoring; the same rule here makes both spellings load one tool.

The catalog lists tool names only, so a group has no name to select: a keyword query reaches a server or application through the namespace field.

## Old search results

A `tool_search` result is never shortened to a stub on any provider; tool output is not aged at all ([Tool output](https://fzkuji.github.io/OpenProgram/docs/reference/design/context/overview.html#cross-turn-tool-context)).

**Why this design**

Claude Code's microcompact skips ToolSearch, and Codex never ages. The definition lives in the load record, so a stub saves little and is a history edit.

## Branches and nested runs

| Run | Tools it is sent |
| --- | --- |
| Two branches at once | Each, what its own messages loaded |
| Nested `runtime.exec` | Its own loads; the outer chat never sees them |
| Stopped or failed search | Nothing loaded |

**Why this design**

Pi writes loads into each branch's own state. Branches run in parallel, so loads cannot be conversation-wide state.

## Implementation status

| Open item | Waiting for |
| --- | --- |
| `loaded_deferred_tools` in journal, safe points, checkpoints | Read-only for one release, then removed |
| Anthropic three-request self-test with cache diagnosis | A paid live run |
| Codex cache after a large load and after a summary | A live Codex run |
| OpenAI and Azure Responses search form | A live check; not declared until then |
| Copilot, Vercel, opencode with references | A live check; they keep the array-append path |
| Empty thinking blocks skipped on Anthropic | A request comparison with a paid run |
| Context breakdown and budget | Still estimate the catalog from the inline text |
| Epoch records | Kept per conversation; branches sharing a first message share one |

**Implementation files**

- `openprogram/programs/_host/runtime.py`, `tool_rank.py`
- `openprogram/programs/_authoring/guides.py`, `openprogram/extensions/mcp/registry.py`
- `openprogram/models/providers/settings/tool_search_spec.py`, `adapters/anthropic/anthropic.py`, `shared/tool_loads.py`
- `openprogram/runtime/agent/loop/streaming.py`, `tool_dispatch.py`, `tool_epoch.py`
- `openprogram/runtime/context/prompt_assembly/turn_context.py`, `components.py`
- `openprogram/runtime/permissions/policy.py`, `lifecycle.py`
