{"record":{"id":"1188f21c2d04c080","repo":"JuliusBrussee/caveman","slug":"max-loaded-tools-must-be-a-positive-integer","errorCode":null,"errorMessage":"max_loaded_tools must be a positive integer","messagePattern":"max_loaded_tools must be a positive integer","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"packages/sdk/python/caveman_cloud/core.py","lineNumber":680,"sourceCode":"\n        ``strategy=\"all\"``      → ``initial`` is the whole catalog; ``search()``\n                                  still calls the server.\n        ``strategy=\"deferred\"`` → ``initial`` is the ``always_load`` tools plus\n                                  the first ``initial_tool_count`` of the catalog\n                                  (a subset sent on the first turn); ``search()``\n                                  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,","sourceCodeStart":662,"sourceCodeEnd":698,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/sdk/python/caveman_cloud/core.py#L662-L698","documentation":"`max_loaded_tools` caps the total number of tools kept loaded and must be a positive integer (>= 1); `None` is allowed (no cap), but bools, zero, negatives, floats, and strings are rejected. It only takes effect with the \"deferred\" strategy, where it bounds `always_load` tools plus the lazily admitted tools.","triggerScenarios":"Calling `tools(catalog, strategy=\"deferred\", max_loaded_tools=0)`, `max_loaded_tools=-5`, `max_loaded_tools=True`, or a float/string value from computed or config-sourced limits.","commonSituations":"Computing a budget with integer division producing 0 for tiny catalogs, or passing a percentage (0–100 float) instead of an absolute count.","solutions":["Pass a positive integer (>= 1), or pass `None` to leave the cap unlimited.","Coerce safely: `cap = int(raw) if raw is not None else None; cap = max(cap, 1)`.","Ensure the cap is at least the number of `always_load` tools, otherwise the later check (error 884) fires."],"exampleFix":"// before\nhandle = client.tools(catalog, strategy=\"deferred\", max_loaded_tools=0)\n// after\nhandle = client.tools(catalog, strategy=\"deferred\", max_loaded_tools=max(1, len([t for t in catalog if t.always_load])))","handlingStrategy":"validation","validationCode":"if cap is not None:\n    if isinstance(cap, bool) or not isinstance(cap, int) or cap < 1:\n        cap = max(int(cap), 1)\nhandle = client.tools(catalog, strategy=\"deferred\", max_loaded_tools=cap)","typeGuard":"def is_positive_int(v: object) -> TypeGuard[int]:\n    return isinstance(v, int) and not isinstance(v, bool) and v >= 1","tryCatchPattern":"try:\n    handle = client.tools(catalog, strategy=\"deferred\", max_loaded_tools=cap)\nexcept ValueError as e:\n    handle = client.tools(catalog, strategy=\"deferred\", max_loaded_tools=None)","preventionTips":["Store limits as integers in config, never percentages or floats.","Map \"unlimited\" to None, not 0.","Clamp computed budgets with max(1, value) before passing them."],"tags":["python","validation","range-check","tools"],"backgroundTag":"argument-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"}