Repository navigation
Description
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:
-
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/tokenizeround-trip when available) and break the loop early when it would exceed something like0.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. -
Trim the oldest
toolrole 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
- Studio at
unslothai/unsloth@a2f3793plus PR studio: engage draft-mtp on vision MTP GGUFs (drop incorrect vision gate) #5560 (MTP-on-vision) applied - llama-server bundled binary supports
--spec-type draft-mtp - Models tested:
unsloth/Qwen3.5-4B-MTP-GGUF:UD-Q4_K_XL,unsloth/Qwen3.5-9B-MTP-GGUF:UD-Q4_K_XL - Repro at 16384 context, fresh chat, all toggles on
Activity
danielhanchen commentedon May 21, 2026
Confirmed:
studio/backend/core/inference/llama_cpp.py::generate_chat_completion_with_toolsenforcesmax_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 withThink+Search+Codetoggled on, ~12 tool calls is enough to hit llama-server'srequest (23782 tokens) exceeds the available context size (16384 tokens)error path.routes/inference.py::_friendly_errorcatches it and surfaces a "Context limit reached" toast, but the whole turn is lost — no partial answer, no summary, no recovery beyond bumpingContext Lengthor starting a new chat.Real bug, especially given the PR #4700 benchmark numbers (multiple OK runs at 16k context on 27B with
avg_tools13-17) show this loop length is real-world common.The two-part fix is the right shape:
char // 3or a/v1/tokenizeround-trip), break the loop and run one final non-tool generation step when it would exceed ~0.85 * context_length. Emit astatusevent explaining the budget-exhausted reason.toolrole 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.
danielhanchen commentedon Sep 6, 2026
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 Dockerdocker exec <container> unsloth-studio-update.Studio GGUF: charge images in the fit and tool-result sizing counts (u…