JuliusBrussee/caveman · error · ValueError

max_loaded_tools must be a positive integer

Error message

max_loaded_tools must be a positive integer

What it means

`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.

Solutions

  1. Pass a positive integer (>= 1), or pass `None` to leave the cap unlimited.
  2. Coerce safely: `cap = int(raw) if raw is not None else None; cap = max(cap, 1)`.
  3. Ensure the cap is at least the number of `always_load` tools, otherwise the later check (error 884) fires.

Example fix

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

Strategy: validation

Validate before calling

if cap is not None:
    if isinstance(cap, bool) or not isinstance(cap, int) or cap < 1:
        cap = max(int(cap), 1)
handle = client.tools(catalog, strategy="deferred", max_loaded_tools=cap)

Type guard

def is_positive_int(v: object) -> TypeGuard[int]:
    return isinstance(v, int) and not isinstance(v, bool) and v >= 1

Try / catch

try:
    handle = client.tools(catalog, strategy="deferred", max_loaded_tools=cap)
except ValueError as e:
    handle = client.tools(catalog, strategy="deferred", max_loaded_tools=None)

Prevention

When it happens

Trigger: 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.

Common situations: Computing a budget with integer division producing 0 for tiny catalogs, or passing a percentage (0–100 float) instead of an absolute count.

Related errors


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

Appendix: source

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

        ``strategy="all"``      → ``initial`` is the whole catalog; ``search()``
                                  still calls the server.
        ``strategy="deferred"`` → ``initial`` is the ``always_load`` tools plus
                                  the first ``initial_tool_count`` of the catalog
                                  (a subset sent on the first turn); ``search()``
                                  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,

View on GitHub (pinned to 3ee70a1026)