Skip to content

[Bug] Deep Research: max_tokens is never clamped to the context window, and the failure blames the wrong limit #7965

Closed

Description

@LeoBorcherding

Two symptoms, one root cause, so filing together.

core/research_runs.py:1700 clamps against constants only:

"max_tokens": min(
    int(max_tokens or inference.get("maxTokens") or 4096),
    16384 if max_tokens is not None else 8192,
),

Synthesis requests 16384 at :2711 and :2735. _loaded_context_length() exists and is used to size evidence (:833) and gate auto-scrape (:2114), but never reaches the token budget.

Probed against the live server, gemma-4-12B at n_ctx_slot = 12288, from a UI Context Length of 12160:

max_tokens requested : 16384
finish_reason        : length
prompt_tokens        : 11032
completion_tokens    :  1256
total_tokens         : 12288   <- exactly the context wall

The server accepts the oversized value and truncates at the context boundary, returning length. That's the condition :2654 branches on and :2683 raises from, so synthesis fails, retries with a slightly longer prompt, and fails again.

The message is then wrong: "Local model report reached its output limit before completion" points at an output cap the UI does not expose, when the binding constraint was the Context Length slider the user already set. The tooltip actively steers people here by telling them to lower context if the model will not fit in memory.

Fix: clamp max_tokens to n_ctx - prompt_tokens, and distinguish context exhaustion from a genuine output-cap hit so the message names the lever that matters.

Activity

  1. added a commit that references this issue on Aug 7, 2026
    edbbef0
  2. added a commit that references this issue on Aug 7, 2026
    1229fc5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions