{"record":{"id":"5aa888f9bb38357d","repo":"JuliusBrussee/caveman","slug":"max-loaded-tools-must-be-at-least-the-always-load","errorCode":null,"errorMessage":"max_loaded_tools must be at least the always_load tool count","messagePattern":"max_loaded_tools must be at least the always_load tool count","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"packages/sdk/python/caveman_cloud/core.py","lineNumber":686,"sourceCode":"                                  calls the server to load relevant tools on demand.\n\n        Returns a handle with ``.strategy``, ``.initial`` (a ``list[CaveTool]``),\n        and ``.search(query, *, max_tools, context, workflow, ranker)`` which\n        hits the gateway and returns a :class:`ToolSearchResult`. Mirrors the\n        TypeScript ``cave.tools({ catalog, strategy })``.\n        \"\"\"\n        if strategy not in {\"all\", \"deferred\"}:\n            raise ValueError(\"strategy must be all or deferred\")\n        if isinstance(initial_tool_count, bool) or not isinstance(initial_tool_count, int) or initial_tool_count < 0:\n            raise ValueError(\"initial_tool_count must be a non-negative integer\")\n        if max_loaded_tools is not None and (isinstance(max_loaded_tools, bool) or not isinstance(max_loaded_tools, int) or max_loaded_tools < 1):\n            raise ValueError(\"max_loaded_tools must be a positive integer\")\n        if strategy == \"all\":\n            initial = list(catalog)\n        else:\n            always = [t for t in catalog if t.always_load]\n            if max_loaded_tools is not None and len(always) > max_loaded_tools:\n                raise ValueError(\"max_loaded_tools must be at least the always_load tool count\")\n            lazy = [t for t in catalog if not t.always_load]\n            lazy_limit = initial_tool_count if max_loaded_tools is None else min(initial_tool_count, max_loaded_tools - len(always))\n            initial = always + lazy[:lazy_limit]\n        return _ToolsHandle(self, catalog, strategy, initial, max_loaded_tools)\n\n    def tool_search(\n        self,\n        tools: list[CaveTool],\n        query: str,\n        *,\n        context: str | None = None,\n        max_tools: int | None = None,\n        workflow: str | None = None,\n        ranker: str | None = None,\n        session_id: str | None = None,\n    ) -> ToolSearchResult:\n        \"\"\"POST the tool catalog + query to the gateway's /sdk/v1/tool-search endpoint.\n","sourceCodeStart":668,"sourceCodeEnd":704,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/sdk/python/caveman_cloud/core.py#L668-L704","documentation":"With the \"deferred\" strategy, tools flagged `always_load=True` are always included, so the `max_loaded_tools` budget must be able to hold all of them. If the always-load count exceeds the cap, no valid configuration exists and the SDK refuses to build the handle rather than silently dropping mandatory tools.","triggerScenarios":"Calling `client.tools(catalog, strategy=\"deferred\", max_loaded_tools=3)` when 5 tools in the catalog have `always_load=True`.","commonSituations":"Lowering a saved budget after adding always-load tools to the catalog, or reusing a hardcoded cap across catalogs of different sizes.","solutions":["Raise `max_loaded_tools` to at least `len([t for t in catalog if t.always_load])`.","Mark fewer tools with `always_load=True` so they fit under the existing budget.","Pass `max_loaded_tools=None` to remove the cap entirely (still subject to `initial_tool_count`).","Guard before calling: compute the always-load count and clamp the cap upward."],"exampleFix":"// before\nhandle = client.tools(catalog, strategy=\"deferred\", max_loaded_tools=3)  # 5 always_load tools\n// after\nalways = len([t for t in catalog if t.always_load])\nhandle = client.tools(catalog, strategy=\"deferred\", max_loaded_tools=max(3, always))","handlingStrategy":"validation","validationCode":"always = sum(1 for t in catalog if t.always_load)\ncap = max(cap, always) if cap is not None else None\nhandle = client.tools(catalog, strategy=\"deferred\", max_loaded_tools=cap)","typeGuard":"def cap_fits_catalog(cap: int | None, catalog: list[CaveTool]) -> bool:\n    always = sum(1 for t in catalog if t.always_load)\n    return cap is None or cap >= always","tryCatchPattern":"try:\n    handle = client.tools(catalog, strategy=\"deferred\", max_loaded_tools=cap)\nexcept ValueError as e:\n    always = sum(1 for t in catalog if t.always_load)\n    handle = client.tools(catalog, strategy=\"deferred\", max_loaded_tools=max(cap or 0, always))","preventionTips":["Derive the cap from the catalog (always_load count + headroom) instead of hardcoding it.","Run a catalog lint in CI that flags always_load additions pushing past the configured budget.","Audit always_load flags whenever you lower a budget."],"tags":["python","validation","range-check","deferred-strategy"],"backgroundTag":"value-out-of-range","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}