langchain-ai/deepagents · error · ValueError

model_retries must be >= 0, got {self.model_retries}

Error message

model_retries must be >= 0, got {self.model_retries}

What it means

`ModelResult.__post_init__` validation raising ValueError because `model_retries` is negative. The retry middleware cannot honor a negative attempt budget, so negative values are rejected at construction.

Source

Thrown at libs/code/deepagents_code/config.py:5573

        `_model_max_retries` and `CodeModelRetryMiddleware.__init__` reject it:
        `True` would silently read as a budget of one.

        `cli_max_retries` gets the same gate. It is the field that carries the
        explicit flag onward to a re-resolution for a different provider, and
        it was the one budget field in dcode without the check -- which is the
        argument for a single validated budget type rather than a ninth copy of
        this predicate.

        Raises:
            TypeError: If `model_retries` or `cli_max_retries` is a `bool`.
            ValueError: If `model_retries` or `cli_max_retries` is negative.
        """
        if isinstance(self.model_retries, bool):
            msg = f"model_retries must be an int, got {self.model_retries!r}"
            raise TypeError(msg)
        if self.model_retries < 0:
            msg = f"model_retries must be >= 0, got {self.model_retries}"
            raise ValueError(msg)
        if isinstance(self.cli_max_retries, bool):
            msg = (
                f"cli_max_retries must be None or an int, got {self.cli_max_retries!r}"
            )
            raise TypeError(msg)
        if self.cli_max_retries is not None and self.cli_max_retries < 0:
            msg = f"cli_max_retries must be >= 0, got {self.cli_max_retries}"
            raise ValueError(msg)

    def apply_to_runtime_state(self) -> None:
        """Commit this result's metadata to global `runtime_state`."""
        state = _get_runtime_state()
        state.model_name = self.model_name
        state.model_provider = self.provider
        state.model_context_limit = self.context_limit
        state.model_unsupported_modalities = self.unsupported_modalities

View on GitHub (pinned to a1af029e6e)

Solutions

  1. Clamp the computed value before construction: `max(0, computed_retries)`.
  2. Pass `0` to disable retries rather than a negative number.
  3. Fix the upstream calculation that produced the negative budget.

Example fix

// before
ModelResult(..., model_retries=n_used - n_total)  # can be negative
// after
ModelResult(..., model_retries=max(0, n_used - n_total))
Defensive patterns

Strategy: validation

Validate before calling

if model_retries < 0:
    model_retries = 0  # clamp before ModelResult(...)

Try / catch

try:
    result = ModelResult(..., model_retries=retries)
except ValueError:
    result = ModelResult(..., model_retries=0)

Prevention

When it happens

Trigger: Constructing `ModelResult(...)` with `model_retries < 0` (e.g. -1) directly; upstream config coercion (`_coerce_max_retries`, `non_negative_int`) normally prevents this, so it signals a hand-built result.

Common situations: Computing a retry budget with subtraction that can go negative, tests constructing ModelResult with sentinel values, misread CLI flags.

Related errors


AI-assisted analysis of langchain-ai/deepagents@a1af029e6e (2026-08-29). Data as JSON: /api/errors/c1c69351c23c875a. Report an issue: GitHub.