Skip to content

[Bug] Auto-compact ignores image tokens on local GGUF chats, so tool loops with screenshots overflow the context window #14968

Closed

Description

@setsey

Environment

Where are you using Unsloth?

  • Unsloth desktop application
  • Unsloth web UI (unsloth studio)
  • Unsloth CLI
  • Python package or notebook
  • Colab or Kaggle

Operating system and version:
Windows 11 IoT Enterprise LTSC 2024 (10.0.26100)

GPU model(s) and accelerator backend:
NVIDIA RTX 4080 SUPER 16 GB, CUDA

Versions:

  • Unsloth / Desktop: 0.1.909-beta
  • Package: 2026.10.7
  • llama.cpp: b11798-mix-f8a7950 (unslothai prebuilt, cuda13)

What happened?

On a local GGUF vision model with Auto-compact on, a chat that carries several images overflows the context window during the tool loop. Studio never compacts it, because the token counts that drive compaction and tool-result sizing leave image tokens out.

Steps to reproduce:

  1. Load a vision GGUF with its mmproj (here a Qwen3.8-27B IQ3_S GGUF + mmproj-Qwen3.8-27B-Q8_0.gguf), Context Length 65536, Max Tokens 32768, Auto-compact on, tools on.
  2. Send one message with 10 screenshots (about 2,040 image tokens each with --image-max-tokens 2048, about 2,400 each without it).
  3. Ask for something that makes the model read several files in a tool loop.

Expected behavior:
As with text-only chats since #5562, older tool exchanges are evicted, and tool results are sized to the room left, before the prompt reaches the window.

Actual behavior:
No compaction or truncation is logged at any step. The loop ends with:

Message too long: 66651 tokens exceeds the 65536-token context window. Try increasing the Context Length in Model settings, or shorten the conversation.

llama-server prompt sizes for the last steps of that turn: 45,706 → 47,273 → 48,476 → 62,239 (one tool result of about 13.4k tokens was admitted in full) → 66,651 (refused).

Model and operation involved:
GGUF inference through Studio's built-in llama-server, multimodal (images), agentic tool loop.

Diagnostics and logs

llama-server:

I slot  operator (): id  0 | task 6090 | new prompt, n_ctx_slot = 65536, n_keep = 0, task.n_tokens = 62239
I slot  operator (): id  0 | task 6914 | new prompt, n_ctx_slot = 65536, n_keep = 0, task.n_tokens = 66651
E srv    send_error: task id = 6914, error: request (66651 tokens) exceeds the available context size (65536 tokens), try increasing it

Studio server log:

Error during GGUF tool streaming: llama-server returned 400: {"error":{"code":400,"message":"request (66651 tokens) exceeds the available context size (65536 tokens), try increasing it","type":"exceed_context_size_error","n_prompt_tokens":66651,"n_ctx":65536}}

Cause: core/inference/context_window.py::_message_without_unpriced_media drops every image_url / input_image part before counting. Every count on the GGUF tool-loop path goes through it: messages_without_unpriced_media(...) for count_chat_tokens, and estimate_message_tokens_without_unpriced_media for fit_rolling_context and the tool-result size probes in generate_chat_completion_with_tools. At 64k the prompt budget is 65536 - min(32768, 65536 // 4) = 49152. The fitter saw about 46k text tokens and decided the chat fit, while llama-server charged about 20.4k more for the 10 images. The same undercount is why the 13.4k-token tool result was admitted whole. Any chat whose images exceed the reply reserve (16k at 64k, roughly 8 full-size screenshots) can hit this.

Workaround confirmed locally: in the counting copy only, replace each image part with a text stand-in of N tokens (N = the per-image cap; the request still sends the real image):

_PRICED_IMAGE_TYPES = frozenset(("image_url", "input_image"))
_STILL_UNPRICED_TYPES = _UNPRICED_MEDIA_TYPES - _PRICED_IMAGE_TYPES
_IMAGE_TOKEN_PRICE = max(0, int(os.environ.get("UNSLOTH_IMAGE_TOKEN_PRICE", "2048")))
_IMAGE_STAND_IN = {"type": "text", "text": " the" * _IMAGE_TOKEN_PRICE}


def _message_without_unpriced_media(message: dict) -> dict:
    content = message.get("content")
    if not isinstance(content, list):
        return message
    countable = [
        _IMAGE_STAND_IN
        if isinstance(part, dict) and part.get("type") in _PRICED_IMAGE_TYPES
        else part
        for part in content
        if not (isinstance(part, dict) and part.get("type") in _STILL_UNPRICED_TYPES)
    ]
    if len(countable) == len(content) and all(a is b for a, b in zip(content, countable)):
        return message
    copy = dict(message)
    copy["content"] = countable or ""
    return copy

The stand-in tokenizes to exactly N tokens through llama-server /tokenize. With it, a simulated 80k-token chat of this shape (system prompt, one user message with 10 images, 30 tool rounds) is fitted to 48k: old tool rounds are dropped and the message with the images is kept.

A fixed N is only a stopgap. Studio could price each image from what the loaded mmproj will charge: its image_max_pixels / --image-max-tokens and patch/merge size give an upper bound, and the image's dimensions give a close estimate. Alternatively, it could calibrate from the prompt_tokens llama-server reports after each step.

Additional context

Related but different: #14708 (attachments unreadable after a compaction has happened), #10155 (pre-send check against the full history), #5562 (the text-only version of this, fixed by budgeting tool results).

Activity

  1. debojyoti-dev commented on Oct 23, 2026

    @debojyoti-dev
    Contributor

    Hi, I'd like to work on this one. I'll open a PR shortly.

  2. danielhanchen commented on Oct 26, 2026

    @danielhanchen
    Member

    Fixed! The counts behind Auto-compact and tool-result sizing dropped images entirely, so a chat with many screenshots looked about 20k tokens smaller than it was and was never compacted before llama-server refused it. Images are now charged close to what the loaded projector really costs (from the image size on Qwen-VL, a fixed cost on Gemma 3, the admission ceiling otherwise), so older tool rounds are evicted in time and the screenshots are kept. Fixed in #15014, thanks @debojyoti-dev for the PR and @setsey for the very detailed report.
    To update: Unsloth Desktop will have an update banner. For Studio, use unsloth studio update. For Docker, use docker exec <container> unsloth-studio-update. Reopen if it still happens.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

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