JuliusBrussee/caveman · error · ValueError

max_loaded_tools must be at least the always_load tool count

Error message

max_loaded_tools must be at least the always_load tool count

What it means

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.

Solutions

  1. Raise `max_loaded_tools` to at least `len([t for t in catalog if t.always_load])`.
  2. Mark fewer tools with `always_load=True` so they fit under the existing budget.
  3. Pass `max_loaded_tools=None` to remove the cap entirely (still subject to `initial_tool_count`).
  4. Guard before calling: compute the always-load count and clamp the cap upward.

Example fix

// before
handle = client.tools(catalog, strategy="deferred", max_loaded_tools=3)  # 5 always_load tools
// after
always = len([t for t in catalog if t.always_load])
handle = client.tools(catalog, strategy="deferred", max_loaded_tools=max(3, always))
Defensive patterns

Strategy: validation

Validate before calling

always = sum(1 for t in catalog if t.always_load)
cap = max(cap, always) if cap is not None else None
handle = client.tools(catalog, strategy="deferred", max_loaded_tools=cap)

Type guard

def cap_fits_catalog(cap: int | None, catalog: list[CaveTool]) -> bool:
    always = sum(1 for t in catalog if t.always_load)
    return cap is None or cap >= always

Try / catch

try:
    handle = client.tools(catalog, strategy="deferred", max_loaded_tools=cap)
except ValueError as e:
    always = sum(1 for t in catalog if t.always_load)
    handle = client.tools(catalog, strategy="deferred", max_loaded_tools=max(cap or 0, always))

Prevention

When it happens

Trigger: Calling `client.tools(catalog, strategy="deferred", max_loaded_tools=3)` when 5 tools in the catalog have `always_load=True`.

Common situations: Lowering a saved budget after adding always-load tools to the catalog, or reusing a hardcoded cap across catalogs of different sizes.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/5aa888f9bb38357d. Report an issue: GitHub.

Appendix: source

Thrown at packages/sdk/python/caveman_cloud/core.py:686

                                  calls the server to load relevant tools on demand.

        Returns a handle with ``.strategy``, ``.initial`` (a ``list[CaveTool]``),
        and ``.search(query, *, max_tools, context, workflow, ranker)`` which
        hits the gateway and returns a :class:`ToolSearchResult`. Mirrors the
        TypeScript ``cave.tools({ catalog, strategy })``.
        """
        if strategy not in {"all", "deferred"}:
            raise ValueError("strategy must be all or deferred")
        if isinstance(initial_tool_count, bool) or not isinstance(initial_tool_count, int) or initial_tool_count < 0:
            raise ValueError("initial_tool_count must be a non-negative integer")
        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):
            raise ValueError("max_loaded_tools must be a positive integer")
        if strategy == "all":
            initial = list(catalog)
        else:
            always = [t for t in catalog if t.always_load]
            if max_loaded_tools is not None and len(always) > max_loaded_tools:
                raise ValueError("max_loaded_tools must be at least the always_load tool count")
            lazy = [t for t in catalog if not t.always_load]
            lazy_limit = initial_tool_count if max_loaded_tools is None else min(initial_tool_count, max_loaded_tools - len(always))
            initial = always + lazy[:lazy_limit]
        return _ToolsHandle(self, catalog, strategy, initial, max_loaded_tools)

    def tool_search(
        self,
        tools: list[CaveTool],
        query: str,
        *,
        context: str | None = None,
        max_tools: int | None = None,
        workflow: str | None = None,
        ranker: str | None = None,
        session_id: str | None = None,
    ) -> ToolSearchResult:
        """POST the tool catalog + query to the gateway's /sdk/v1/tool-search endpoint.

View on GitHub (pinned to 3ee70a1026)