Repository navigation
feat(core): deferred tools loaded through a search tool - #110
Conversation
gopaljigaur
left a comment
There was a problem hiding this comment.
Reviewed 88c485d. The design matches the brief and the efficiency claims hold: kwargs and the system prompt are rebuilt only after a search adds tools, the word index is built once per run, and found tools live only in the run's _ToolSearch. defer_loading is the name Anthropic, OpenAI and Pydantic AI use, and search_tools is Pydantic AI's local tool name. Returning names and descriptions with schemas on the next step matches Pydantic AI, which returns names only. Pydantic AI's default strategy is native search on Anthropic and OpenAI and its keyword overlap elsewhere (max_results=10, a queries: list[str] argument), so "as Pydantic AI's default keywords strategy" is accurate for its local fallback only.
Findings:
- Prompt caching misses on tools, system prompt and full history after each search that adds tools, and again at the start of the next run in a session. Not documented.
- The docs say no Anthropic path exists, but Anthropic's client-side
tool_referencepath passes through LiteLLM 1.105.1 and would keep the prefix fixed. The server-side drop is real (reproduced) and fixed in BerriAI/litellm#46440, merged today, not yet released. - Found tools reset each run, where Anthropic, OpenAI and Pydantic AI keep discoveries for the conversation.
- Already-found tools can take all five result slots from tools not yet found.
ToolPermissionswith a deny default blockssearch_tools, so no deferred tool can load; undocumented.- An unwrapped 110 character docstring line in
MCPClient.attach. - Import order nit in
agent.py. - Test gaps: parallel searches, re-finding a loaded tool, permissions.
Checked and fine: naive mode lists found tools with their parameters; a search with no hits returns a message and adds nothing; two searches in one step each add their tools once; a deferred tool is denied by ToolPermissions like any other; subagents get their own _ToolSearch per run; the skills tool is never deferred. uv run pytest (485 passed, 1 xfailed), pre-commit and mypy cyclops pass locally, CI is green, and the confidentiality and dash checks are clean.
88c485d to
5303823
Compare
gopaljigaur
left a comment
There was a problem hiding this comment.
Reviewed 5303823 and its merge with main (40aebfb). One inline finding and one about the PR body.
- The branch is not rebased onto #104. Merged with main, four tests in
tests/test_tool_search.pyfail because #104 removedAgent.messages. CI is green only because it ran before #104 merged. See the inline thread. - The PR body still says "Search is stdlib word overlap, as Pydantic AI's default
keywordsstrategy". Pydantic AI defaults to native search on Anthropic and OpenAI; keyword overlap is its local fallback. The Decisions section also says "Passingdefer_loadingthrough can follow once LiteLLM keeps those blocks", which leaves out the client-sidetool_referencepath that #113 now tracks; linking #113 there would match the guide. Refresh the test count after the rebase too.
The eight earlier threads are fixed in the code and docs:
- The guide states the cache miss after a search and at the next run's first completion, and links #113 for Anthropic's and OpenAI's own search. #113 covers the
tool_referencepath, the LiteLLM fix in BerriAI/litellm#46440, and session-wide found tools. - Found tools staying per run is documented, in a session too.
- Ranking sorts on
(t in self.loaded, -score)with up to ten results. - The deny default note and its test are in the guide and the tests.
- The
MCPClient.attachdocstring is rewrapped, and thetool_searchimport is in order. - New tests cover two searches in one step, ranking tools not loaded ahead of loaded ones, the ten result limit, and
search_toolsunder a deny default.
I reran my probe on the merge with main. The results match the code: two searches in one step add each tool once, and a repeat search that overlaps lists the tool not yet loaded first. A deny default blocks search_tools, and a denied deferred tool is found and then refused. After #104, two sessionless runs each start from one message and the always-sent tools, and the second run of a session starts from the always-sent tools with the session's history. Per-run found-tool state matches both.
On 5303823 itself, uv run pytest (463 passed, 1 xfailed), pre-commit and mypy cyclops pass. The confidentiality grep and the dash check are clean over the title, body, commits, added lines and thread replies.
Tools marked defer_loading are left out of the completions until the model finds them with a search_tools tool, which matches keywords against tool names and descriptions. Found tools are sent from the next step to the end of the run. MCPClient.attach(defer_loading=True) defers a server's tools.
A search now returns up to ten tools, tools not loaded yet ahead of loaded ones. The guide states that a search that adds tools makes the next completion miss the prompt cache, that found tools last for the run, and that a deny default must allow search_tools.
5303823 to
1e5f660
Compare
|
PR body updated: the Pydantic AI comparison now says its default is native search on Anthropic and OpenAI, with keyword overlap as its local fallback; the Decisions line on client-side search names Anthropic's client-side |
Every tool schema is sent with every completion. This lets a tool opt out until the model asks for it.
What changed
defer_loading: bool = FalseonBaseTool,Tooland@tool, the name Anthropic's and OpenAI's tool definitions and Pydantic AI use.MCPClient.attach(..., defer_loading=True)(andMCPBridge.attach) defers every tool of a server, including tools it lists later.cyclops/core/tool_search.py:_ToolSearchholds one run's tools. With any tool deferred it adds asearch_tools(query)tool (read only) and sends only the tools not deferred plus the search tool. A search ranks deferred tools by how many query words appear in their name and description (names split at underscores), returns up to ten (tools not loaded yet first), and adds them for the rest of the run._RunLoop._stepsbuilds the completion kwargs and system prompt when the run starts and again only after a search added tools; other steps reuse them. In naive mode the found tools join the tools listed in the system prompt.AgentraisesValueErrorif a tool is namedsearch_toolswhile any tool is deferred.Decisions
defer_loading), OpenAI (defer_loading) and Pydantic AI (defer_loading=Trueplus asearch_toolsfallback) all mark tools individually; none switches on by token count. A threshold would needtoken_counterover the schemas andget_model_infofor every deployment behind a router, and would change what the model sees as tools are added, without the caller choosing it. Tools not marked are the always-loaded ones._ToolSearch, created in_steps, so a new run (or the naive fallback's retry) starts again from the tools always sent. Nothing is stored on the agent or the session.defer_loadingandtool_search_tool_regex_20251119/bm25for Anthropic, but its chat completion response drops thetool_search_tool_resultblock and returns theserver_tool_useas an ordinary tool call. Checked against LiteLLM's real request builder with a mocked HTTP reply: the next request sends that call back as a clienttool_usethat the caller must answer, so a run could not continue. Anthropic also documents a client-side path: deferred tools stay intoolswithdefer_loading: trueand a custom search tool returnstool_referenceblocks, which keeps the prompt prefix and its cache stable. That path, the server-side one and session-wide found tools are tracked in feat(core): keep the cached prefix fixed when deferred tools are found #113.keywordsfallback (its default is the provider's native search on Anthropic and OpenAI, and the keyword search elsewhere), including its limit of 10 results and ranking tools not found yet first. The words of each deferred tool are computed once per run.Tests
tests/test_tool_search.py: what is sent with and without deferred tools, a search adding tools for later steps and the run only (not the next run in a session), always-loaded tools, naive mode, ranking and the ten result limit, two searches in one step,search_toolsunderToolPermissions, the reserved name, MCP tools deferred throughMCPClientandMCPBridgeagainst the demo server, and found tools reaching Anthropic through LiteLLM's real request builder.uv run pytest(470 passed, 1 xfailed),uv run pre-commit run --all-filesanduv run mypy cyclopspass.Closes #74