CubeLoop — a Pythonic, async-native agent harness core designed for high performance, readability, and production-grade persistence.
Deferred Tools v2 — Dispatch Strategy (Zero Cache Invalidation)
Date: 2026-06-10
Branch: 2026-06-10-deferred-dispatcher
Supersedes (partially): 2026-06-09-deferred-tool-group.md (v1, shipped in v0.10.0)
Problem
Deferred tool groups v1 (inject strategy) solves context bloat but pays a heavy prompt-cache
price. On every load_tools expansion:
- Tools-array append. Expanded tools are injected into
ctx.context.toolsand serialized into the providertoolsparameter on the next iteration. Tools render at position 0 of the prompt — appending inserts bytes before system + messages, so the entire conversation history is re-read uncached. One full-cost re-read of system + history per expansion event. - System-prompt mutation. The catalog entry is rewritten (remaining counts) and an "Expanded tool groups" section is appended — a second, independent invalidator.
- Double billing. Expanded schemas are rendered in both the tools array and the system prompt ("supplementary context" per the v1 spec). Every expanded tool's parameter JSON is billed twice per turn, at cache-read rate, forever.
(1) is inherent to client-side injection on every prefix-cached provider (Anthropic, OpenAI):
any tool that must appear in the native tools parameter sits in the cached prefix. The only
way to reach zero invalidation provider-agnostically is to never mutate the tools parameter or
the system prompt after the first request.
Goals
- A dispatch strategy with zero prompt-cache invalidation: static tools array, static system prompt, schemas delivered through tool results (append-only message suffix).
strategyparameter onDeferredToolsMiddlewareandAgent(deferred_tool_strategy=...); default"dispatch".- Hook transparency: middleware hooks, HITL/permissions, and tracing see the real tool name and validated args for dispatched calls — never the dispatcher envelope.
- Implicit load: dispatching a not-yet-loaded tool loads it on the fly and validates; a validation failure returns the full schema so the model can self-correct in one round trip.
- Keep the inject strategy for hosts that prioritize native tool-calling quality, and fix its double-billing (system-prompt schema section removed).
Non-goals
- Anthropic native Tool Search integration (
defer_loading+tool_referenceexpansion). Recorded as the future"native"strategy below; out of scope here because it requires all deferred schemas to be available at request time (conflicts with lazyloader()) and provider-capability plumbing. - Compaction preserve rules for schema-bearing tool results. The idempotent-
load_toolsself-rescue path (below) is the v2 mitigation; transcript-preservation rules are a possible later enhancement. - BM25 / embedding retrieval over the catalog. The catalog stays small enough to read.
Prior art
Researched 2026-06-10 (Anthropic docs, pi-agent-core, langgraph, Claude Code):
- Anthropic native Tool Search Tool (GA 2026-02): tools carry
defer_loading: truebut their full schemas still ship in thetoolsrequest param every turn — the API merely withholds them from the rendered prompt. Discovery returnstool_referencemarkers that the server expands inline into message history, re-applied across the whole conversation on every request; invocation stays a native, strict-mode-validatedtool_use. Documented as cache-preserving. A client tool may returntool_referenceblocks in a normaltool_resultand get the same server-side expansion — this escape hatch is what makes the future"native"strategy cheap to add. - Claude Code ToolSearch: deferred tools announced name-only in a system reminder; the
ToolSearch tool result carries full JSONSchema definitions; calling an unloaded tool fails
fast. Rides the native
tool_referenceexpansion under the hood; permissions see real tool calls. - langgraph
bigtool: RAG-over-tools; retrieved tool ids accumulate in graph state and the tools array is rebound per request — cache-hostile, undocumented as such. One idea worth stealing: "which tools are loaded" persists as durable agent state, not as an artifact of surviving messages. - pi-agent-core: no deferral at all; tools change by reassigning
agent.state.tools(rebind pattern). Hooks always see first-class tool calls. - hermes-agent (v1 spec prior art;
tools/tool_search.py): the direct precedent for the dispatch strategy — three bridge tools (search/describe/call) wherecallis exactly a generic dispatcher. Per-tool granularity, BM25 retrieval, stateless catalog rebuilt every turn. Its production lessons map onto this design: (1) core tools must never defer → cubepi only defers what hosts explicitly group; (2) catalog drift silently drops tools (OpenClaw #84141) → the dispatch-mode catalog is static, immune to drift by construction; (3) transparent unwrap so hooks see real tool names → theresolve_tool_callengine hook below.
Divergence callout — the surveyed systems split into two camps. Anthropic native, Claude
Code, langgraph, and pi-agent-core all keep invocation as a native, provider-validated
tool_use (paying for it with either a server-side Anthropic-only feature or cache-hostile
rebinding). hermes-agent — and now cubepi — instead route deferred calls through a generic
dispatcher, trading provider-side arg constraint for provider-agnostic zero invalidation.
cubepi differs from hermes in granularity (group-level load_tools vs per-tool
search/describe) and in keeping the catalog static rather than rebuilt per turn. The known
costs of dispatching and their mitigations:
| Cost of dispatching | Mitigation |
|---|---|
| No provider-side arg constraint/strict mode for inner args | Framework-side pydantic validation (same model_validate path as native calls); validation failure returns the full schema |
| Hooks/permissions/tracing would see the envelope | Engine-level unwrap before the hook layer (below) |
| Schemas in message history can be compacted away | load_tools is idempotent and re-callable; loaded-state is durable in extra (bigtool lesson) |
| Slightly degraded calling ergonomics vs native | Acknowledged trade-off; hosts that care choose strategy="inject" |
Settled decisions
- Two strategies, default
dispatch. Zero cache invalidation is the right default for the large-toolset use case this feature exists for. Hosts that need native calling quality opt intoinject. - Engine-level unwrap to real names. All observability and control surfaces operate on the real tool call (hermes-agent lesson #3; consistent with every surveyed system).
- Static system prompt in dispatch mode. Catalog lists groups + tool names only — no
remaining counts, no schema section. Load state is communicated through
load_toolsresults. - Implicit load + validate. A dispatched call to an unloaded tool auto-loads its group
(loader cache + per-group lock reused from v1), then validates. The model can skip the
explicit
load_toolsround trip when it can already produce correct args from catalog names. - Compaction self-rescue via idempotent
load_tools. Re-calling returns the same schemas. - Inject mode slimmed. The "Expanded tool groups" system-prompt section is removed entirely — full schemas already live in the tools array. The catalog (with remaining counts) stays, since inject mode's per-expansion invalidation is unavoidable anyway and the counts carry signal.
Design
API surface
DeferredStrategy = Literal["dispatch", "inject"] # "native" reserved for future
class DeferredToolsMiddleware(Middleware):
def __init__(
self,
*,
groups: list[DeferredToolGroup],
extra_ref: Callable[[], dict[str, Any]],
strategy: DeferredStrategy = "dispatch",
catalog_header: str | None = None, # default depends on strategy
...,
) -> None: ...
# Agent-level convenience (mirrors v1):
Agent(deferred_tool_groups=[...], deferred_tool_strategy="dispatch")
DeferredToolGroup is unchanged.
Dispatch strategy mechanics
Static surfaces (never mutated after construction):
tools(model-visible): regular agent tools +load_tools+deferred_tool_call. The middleware'stoolsattribute is[load_tools, deferred_tool_call].- System prompt: catalog header + one line per group (
group_id, display name, description, full tool-name list, sorted bygroup_id). Rendered once; byte-identical every turn.
load_tools(group_id, tool_names=None) — same input schema as v1. The tool result now
carries the schemas instead of the middleware mutating prompt state:
{
"group_id": "mcp:github",
"loaded": ["create_issue", "list_issues"],
"schemas": [
{"name": "create_issue", "description": "...", "parameters": {...}},
{"name": "list_issues", "description": "...", "parameters": {...}}
]
}
parametersserialized withsort_keys=True(determinism — the result becomes part of the cached prefix on subsequent turns).- Idempotent: repeat calls return the same payload (compaction self-rescue). Loader still runs
exactly once per group per run (v1 loader cache + per-group
asyncio.Lockreused). - The calling convention ("invoke via
deferred_tool_call") lives once in the static catalog header and the dispatcher's description — not repeated in every result.
deferred_tool_call(tool_name: str, arguments: dict) — the dispatcher. Its execute never
runs; calls are rewritten by the engine before the standard pipeline (next section).
Internal tool registry. Loaded AgentTools are appended to context.tools with a new
AgentTool.expose_to_model: bool = False field. loop.py filters them out of the provider
payload:
# loop.py — the only loop change
tools_defs = [t.to_definition() for t in context.tools if t.expose_to_model]
Keeping loaded tools in context.tools (rather than a middleware-private dict) means the
existing _prepare_tool_call name lookup, fork snapshotting, and subagent context inheritance
all work unchanged. Bonus robustness: if the model hallucinates a direct native call to a
loaded tool's real name, the engine resolves it anyway.
Engine-level unwrap: resolve_tool_call
A new optional hook threaded through execute_tool_calls, symmetric with
before_tool_call/after_tool_call:
ResolveToolCall = Callable[[ToolCall, AgentContext], Awaitable[ToolCall | None]]
Called at the top of _prepare_tool_call. Returning a ToolCall (same id, rewritten
name/arguments) replaces the original for the entire downstream pipeline — pydantic
validation, before_tool_call, execution, after_tool_call, ToolExecutionUpdateEvent,
tracing all see the real name. The tool result is keyed by the unchanged tool_call.id, so the
wire protocol (assistant tool_use named deferred_tool_call ↔ tool_result by id) stays
consistent for the provider.
The middleware's resolver:
tool_call.name != "deferred_tool_call"→ returnNone(no-op for everything else).- Parse
{tool_name, arguments}. Unknowntool_name(not in any group) → leave the call unresolved with a structured error result listing valid names. - Implicit load (ephemeral, state-free): if
tool_nameisn't already incontext.tools, run the loader (shared cache + per-group lock), append the requested tool tocontext.tools(expose_to_model=False). Noextra["expanded_groups"]write, noon_tools_expanded— state recording belongs to explicitload_toolsonly (fork-safety; see §Forks). A loader failure raises, which the engine converts into the call's error result. - Non-dict
arguments→ returnNone(the dispatcher's own schema validation rejects the call). Otherwise returnToolCall(id=同, name=tool_name, arguments=arguments).
Validation failure includes the schema. When a dispatch-resolved call fails
model_validate, the error result appends the tool's full schema JSON, so the model retries
with correct args in one round trip. (Mechanism: the resolver marks the rewritten call;
_format_validation_error gains an optional schema suffix. Exact plumbing settled in the plan.)
Forks
Forked agents (fork_once-style) are built with middleware=[] but receive the parent's
hook callables explicitly. The fork construction must forward resolve_tool_call the same
way it already forwards before_tool_call — otherwise dispatch-mode forks cannot invoke any
loaded deferred tool (the dispatcher's execute is fork-denied, hidden tools are not in the
payload, and no resolver runs), a regression vs v1 where expanded tools stayed usable in
forks. load_tools remains fork-denied as in v1 (its execute is wrapped); the forwarded
resolver, however, does not go through that execute — so dispatched calls work in forks for
already-loaded tools, and dispatching an unloaded tool triggers an implicit load from the
fork. Implicit loads are ephemeral and state-free (revised per PR codex review): they
share only the loader cache and per-group locks (the loader still runs at most once across
parent + forks — process-level resource acquisition), append the hidden tool to the local
context only, and never write extra["expanded_groups"] or call on_tools_expanded — a
fork-forwarded resolver therefore cannot mutate the parent agent's state or tool list.
Recording expansion state stays the job of an explicit load_tools call. An implicit load
whose loader fails surfaces the loader error as the dispatched call's error result (not a
misleading "tool not found").
prepare_resumed_state takes a required strategy kwarg (no default) so a host cannot
resume with a strategy that mismatches its middleware construction.
State persistence & resume
extra["expanded_groups"] keeps the v1 shape (dict[group_id, list[str] | None]) — durable
load state outside the transcript. Dispatch-mode resume becomes trivial:
- Schemas need no rebuilding —
load_toolsresults live in message history and come back via the checkpointer. prepare_resumed_stateonly restores the loader cache and re-appends loaded tools (expose_to_model=False) so dispatched calls resolve immediately. No ordering invariant to preserve (nothing renders into the prefix), which retires v1's resume order-fidelity caveat.
Inject strategy changes (v1 fix)
transform_system_promptrenders the catalog only. The "Expanded tool groups" schema section,render_expanded_schemas, and the_expanded_schemasappend-only bookkeeping are deleted — schemas exist solely in the tools array.- Everything else (injection via
after_tool_call, expansion-order tool append, loader cache, locks) is unchanged.
Cache behavior comparison
| tools param | system prompt | per-expansion cache cost | calling path | |
|---|---|---|---|---|
| inject (v1 → v2) | grows per expansion | catalog counts change (v2: schema section removed) | full re-read of system + history | native tool_use |
| dispatch (v2 default) | static | static | zero (schemas are message-suffix appends) | dispatcher, engine-unwrapped |
| native (future) | static bytes (all schemas always present, defer_loading: true) |
static | zero (server-side tool_reference expansion) |
native tool_use |
Breaking changes (v0.10 → next minor)
Per project policy: break loudly, no compatibility shims.
- Default strategy is
dispatch. Hosts upgrading from v0.10 get a different catalog wording, a newdeferred_tool_callbuiltin, and no tools-array growth. Restoring v1 behavior isdeferred_tool_strategy="inject". - Inject mode system prompt slims — the expanded-schema section disappears (pure token saving; behavior-neutral for the model since schemas remain in the tools array).
DeferredToolsMiddlewareconstructor:resumed_schemasparameter removed (dispatch resume doesn't need it; inject resume rebuilds nothing now);strategyadded.
CHANGELOG must call out (1) explicitly with the one-line opt-out.
Testing
Unit (FauxProvider, no API calls):
- Byte-stability (the headline property): render two consecutive request payloads across a
load_tools+deferred_tool_callsequence in dispatch mode; asserttoolsandsystemare byte-identical, and message history is append-only. - Resolver unwrap: hooks (
before_tool_call/after_tool_call) and emitted events receive the real tool name and validated args; result keyed to the dispatcher'stool_use_id. - Implicit load: dispatching an unloaded tool loads exactly once (concurrent dispatches to the same group race-safe via the per-group lock), then executes.
- Validation failure returns
is_error=Truewith the full schema in the result. - Unknown
tool_name→ structured error listing valid names; no load attempted. load_toolsidempotency: repeat call returns identical schemas payload.- Direct native call to a loaded
expose_to_model=Falsetool resolves and executes. - Inject mode: system prompt contains catalog only; schemas absent; injection still works.
- Resume (dispatch): loader cache + loaded set restored; dispatched call works without a
fresh
load_tools. expose_to_modelfiltering: provider payload excludes hidden tools in both strategies.
Open questions
- Dispatcher tool name:
deferred_tool_callvscall_tool. Leaningdeferred_tool_call(self-describing, low collision risk with host tools). - Should
load_toolsin dispatch mode also acceptgroup_id=Noneto dump all schemas of all groups? Leaning no (defeats progressive disclosure). - Whether the
"native"strategy lands as a thirdstrategyvalue or as anAnthropicProvidercapability that the middleware queries. Deferred to its own spec.