Deferred Tool Loading
Stop paying context for tools the task never needs: load a core set eagerly and expand the toolset on demand.
The route
4 steps to Done
- 01
Measure usage and pick the core
The data decides what stays eager.
Preview prompt + verify gate ▾ Hide ▴
Analyze tool usage to define the core set. From transcript logs, compute per-tool: call frequency across sessions, task-type coverage (which task categories touch it), and first-call timing (tools needed in early turns favor eager loading). Also measure the token cost of each tool's definition. Core-set criteria: high frequency OR early-turn necessity OR safety-critical (permission gates should not be deferred). Everything else defers. Deliver: the ranked usage table, the core list (aim for the smallest set covering ~90% of calls), and the projected context savings from deferring the rest.
- ✓ Frequency and timing measured from real logs
- ✓ Safety-critical tools kept eager
- ✓ Savings projection computed
- 02
Build the catalog and discovery
The model can only load what it can find.
Preview prompt + verify gate ▾ Hide ▴
Implement discovery. Build the deferred-tool catalog: one line per tool - name plus a purpose phrase written like a steering description in miniature ('code_metrics: complexity and duplication reports for source files'). Include the catalog in context under a header explaining: 'additional tools exist; call load_tools([names]) to activate the ones your task needs'. Group the catalog by capability area for scannability. Budget the whole block tightly (it must be far cheaper than the definitions it replaces). Add a search_tools(query) function for large catalogs. Verify the model reads it: give a task needing a deferred tool and watch for the load call.
- ✓ One steering line per deferred tool
- ✓ Catalog cost is a small fraction of definitions
- ✓ Model issues load_tools unprompted in the test
- 03
Implement dynamic registration
Mid-session loading that keeps the loop and cache sane.
Preview prompt + verify gate ▾ Hide ▴
Implement load_tools(names). On call: validate names against the catalog; register the requested tools' full definitions into the session's active toolset; return a confirmation listing the loaded tools with their descriptions; keep them active for the session's remainder. Cache-awareness: append newly-loaded definitions in a stable position per your provider's tool-definition handling (accept the one-time cache reset per load and log it; batch-load when the model requests several). Handle the errors: unknown names get nearest-match suggestions; already-loaded is an idempotent no-op. Verify a loaded tool round-trips: load, call, correct result.
- ✓ Loaded tools callable immediately
- ✓ Unknown names get suggestions
- ✓ Cache resets logged and batched
- 04
Measure savings and guard capability
Prove the win and prove nothing broke.
Preview prompt + verify gate ▾ Hide ▴
Run the evaluation. SAVINGS: measure tool-definition tokens per session before vs after deferral across 10 representative tasks; report average and range. CAPABILITY: run a regression suite of tasks that collectively require several deferred tools; success requires the model discovering, loading, and using each without human help; compare completion rates against the all-eager baseline. DISCOVERY FAILURES: for any task where the model struggled without the right tool, diagnose (catalog wording? missing search? tool should be core?) and fix. Institute a monthly usage re-check - core membership follows the data as usage evolves.
- ✓ 10-task savings report produced
- ✓ Regression matches all-eager completion
- ✓ Discovery failures diagnosed and fixed
Research-backed
Sources behind this flow
Tier 1 · claude-code-internals
Claude Code Orange Book
A book-length analysis ('Orange Book') dissecting Anthropic's AI engineering decisions from the 510,000 lines / 1,902 TypeScript files that shipped inside an npm package. Chapters cover the agent loop, context compaction, the hook system, and sub-agents - the concepts, not just the code.
Tier 1 · claude-code-internals
claude-code-decompiled (research reports)
A documentation-only collection of 20 independent research reports on Claude Code v2.1.88 covering architecture, agent behavior, permission design, prompt assembly, MCP integration, and context management. Deliberately ships analysis rather than extracted code.
Tier 5 · mcp-tooling
mcp-use
A popular open library for connecting any LLM to any MCP server and building custom agents with tool access - the fastest path from zero to a working MCP-tool-using agent, with support for multi-server clients and restricted tool sets.