langchain-ai/deepagents · error · ValueError

max_continuations cannot be negative

Error message

max_continuations cannot be negative

What it means

DeepAgentRuntime.__init__ raises this ValueError when max_continuations is negative. Continuation limits control how many times the agent may resume after interrupts; a negative count is meaningless and rejected at construction time.

Source

Thrown at libs/talon/deepagents_talon/runtime.py:260

        checkpointer: Checkpointer | None = None,
        include_web_tools: bool = True,
        recursion_limit: int = DEFAULT_RECURSION_LIMIT,
        max_retries: int = DEFAULT_MAX_RETRIES,
        max_continuations: int = DEFAULT_MAX_CONTINUATIONS,
        env: Mapping[str, str] | None = None,
    ) -> None:
        """Initialize without constructing the graph."""
        values = os.environ if env is None else env
        resolved_recursion_limit = _recursion_limit_from_env(values, recursion_limit)
        if resolved_recursion_limit <= 0:
            msg = "recursion_limit must be positive"
            raise ValueError(msg)
        if max_retries < 1:
            msg = "max_retries must be at least 1"
            raise ValueError(msg)
        if max_continuations < 0:
            msg = "max_continuations cannot be negative"
            raise ValueError(msg)

        self.model = model
        self.tools = tuple(tools)
        self.system_prompt = system_prompt
        self.subagents = tuple(subagents) if subagents is not None else None
        self._has_async_subagents = _has_async_subagents(self.subagents)
        self.assistant_dir = assistant_dir
        self.cron_store = cron_store
        self.env = dict(os.environ if env is None else env)
        self.backend = backend if backend is not None else _default_backend(self.env)
        self.skills = tuple(skills) if skills is not None else None
        self.middleware = tuple(middleware)
        self.interrupt_on = interrupt_on_with_env_overlay(interrupt_on, self.env)
        self.memory = tuple(memory) if memory is not None else None
        self.checkpointer = checkpointer if checkpointer is not None else InMemorySaver()
        self.include_web_tools = include_web_tools
        self.recursion_limit = resolved_recursion_limit
        self.max_retries = max_retries

View on GitHub (pinned to a1af029e6e)

Solutions

  1. Pass max_continuations >= 0; use 0 to disable continuations, or a large positive number for effectively unlimited
  2. Replace any -1 sentinel with 0 or None/omit the argument per the API default
  3. Clamp: `max_continuations=max(0, computed)`

Example fix

// before
runtime = DeepAgentRuntime(..., max_continuations=-1)  # meant unlimited
// after
runtime = DeepAgentRuntime(..., max_continuations=100)  # or omit to use the default
Defensive patterns

Strategy: validation

Validate before calling

def sanitize_continuations(n: int) -> int:
    return max(0, int(n))  # -1 sentinels become 0 (disabled)

runtime = DeepAgentRuntime(..., max_continuations=sanitize_continuations(config.continuations))

Type guard

def non_negative_int(v: object) -> bool:
    return isinstance(v, int) and not isinstance(v, bool) and v >= 0

Prevention

When it happens

Trigger: Constructing DeepAgentRuntime with max_continuations < 0 (e.g. -1 used as a sentinel for 'unlimited').

Common situations: Using -1 as an 'unlimited' sentinel as is common in other libraries; arithmetic on counters that underflows below zero before constructing the runtime.

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


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