Skip to content

studio: agentic loop dead-ends with context-overflow error when many tool calls accumulate #5562

Closed

Description

@danielhanchen

Repro

Run a 4B-MTP (or 9B-MTP) model with max_seq_length=16384 and a prompt that needs many tool calls. The Billboard Hot 100 prompt from PR 4700 is a clean repro:

Load: unsloth/Qwen3.5-4B-MTP-GGUF (UD-Q4_K_XL), 16384 ctx, f16 KV
Toggles: Think on, Search on, Code on
Prompt: "List and categorize all songs that charted #3 on the Billboard Hot 100 in 2015."

Observed

Around the 12th tool call the agentic loop pushes the prompt over the context window and llama-server returns:

request (23782 tokens) exceeds the available context size (16384 tokens)

routes/inference.py:_friendly_error catches the regex and the chat surfaces a "Message too long" red banner plus a "Context limit reached" toast. The conversation is then dead-ended: there is no partial answer, no useful summary, and the only recovery paths are (a) bump Context Length, (b) start a new chat.

Why this is a bug, not a model-size issue

The agentic loop in studio/backend/core/inference/llama_cpp.py:generate_chat_completion_with_tools has a tool-iteration cap (max_tool_iterations=25) and reprompt caps (_MAX_REPROMPTS=3, _MAX_CONTINUES=3) but no token-budget check. Each round-trip appends:

  • the assistant's tool-call message
  • the full tool result (web_search results can be 500-2000 tokens each)

Once the conversation grows past n_ctx, llama-server errors and the whole turn is lost. The user only sees the toast, not the work the model already did.

Suggested fix

Two parts, smallest-blast-radius first:

  1. Stop adding tool results when next iteration would not fit. Estimate the next prompt length (rough: char count divided by 3-4, or a /v1/tokenize round-trip when available) and break the loop early when it would exceed something like 0.85 * context_length. Emit a {"type": "status", "text": "context budget exhausted, producing final answer with what was gathered"} event and run one last non-tool generation step so the user gets a synthesized answer instead of a red banner.

  2. Trim the oldest tool role messages from the conversation when the budget is tight, keeping the user turn, the system tool definitions, and the most recent N tool round-trips. The model loses the early search context, but produces a useful final answer rather than failing entirely.

Either step alone is enough to fix the dead-end. The PR 4700 benchmark numbers (multiple OK runs at 16k context on 27B with avg_tools of 13-17) already show this loop is real-world common, so this gap shows up in normal usage.

Workaround

Bump Context Length to 32768 or higher in the chat Settings panel. Works but most users will not know to do this until they hit the error.

Environment

Activity

  1. danielhanchen commented on May 21, 2026

    @danielhanchen
    MemberAuthor

    Confirmed: studio/backend/core/inference/llama_cpp.py::generate_chat_completion_with_tools enforces max_tool_iterations=25, _MAX_REPROMPTS=3, _MAX_CONTINUES=3, but no token-budget check. Each iteration appends the assistant's tool-call message + the full tool result (web-search results can be ~500-2000 tokens each), so on a 16k-ctx model with Think+Search+Code toggled on, ~12 tool calls is enough to hit llama-server's request (23782 tokens) exceeds the available context size (16384 tokens) error path. routes/inference.py::_friendly_error catches it and surfaces a "Context limit reached" toast, but the whole turn is lost — no partial answer, no summary, no recovery beyond bumping Context Length or starting a new chat.

    Real bug, especially given the PR #4700 benchmark numbers (multiple OK runs at 16k context on 27B with avg_tools 13-17) show this loop length is real-world common.

    The two-part fix is the right shape:

    1. Estimate next-iteration prompt length (cheap: char // 3 or a /v1/tokenize round-trip), break the loop and run one final non-tool generation step when it would exceed ~0.85 * context_length. Emit a status event explaining the budget-exhausted reason.
    2. Trim oldest tool role messages (keep user turn, system tool defs, last N round-trips) when the budget is tight.

    Either alone gets you out of the dead-end; the first is the smaller blast-radius and probably worth landing first.

  2. danielhanchen commented on Sep 6, 2026

    @danielhanchen
    MemberAuthor

    Fixed, tool results are budgeted and old turns evicted so the loop ends in a final answer instead of a 400.

    To update: the desktop app updates itself (Settings > General), otherwise unsloth studio update, or in Docker docker exec <container> unsloth-studio-update.

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