Using Images Instead of Text Summaries for LLM Context Compression

    This article explores a context compression strategy for large language models (LLMs) that replaces traditional text summaries with locally rendered images. By retaining exact text at the beginning and end of a conversation and converting dense middle history into visual frames, this approach reduces token consumption and information loss without relying on additional model calls. The article details the workflow, suitable use cases, and engineering best practices for implementation.

    1. Why Traditional Text Summarization is Not Enough

    The context window of large language models (LLMs) is a limited resource. As conversation turns increase, token consumption grows linearly, leading to significant increases in cost and latency. A common industry practice is “rolling compression”: when the history reaches a threshold, the middle section of the conversation is replaced with a text summary generated by a model.

    This approach has two inherent drawbacks:

    1. Information Loss. Summarization is essentially a lossy compression. The original output of tool calls, log details, and exact numerical values are often discarded during the summarization process. When a user needs to “trace back the exact output of a certain intermediate step,” the summary cannot provide it.
    2. Compression Itself Consumes Tokens. Generating a summary requires an additional model call, and the summary quality depends on the model’s capability, introducing uncertainty.

    This leads to an interesting question: Is there a compression method that retains all details, saves tokens, and does not rely on extra model calls?

    2. Core Idea: Render History as Images

    The answer comes from observing the billing and encoding mechanisms of multimodal models. When visual models process images, token consumption primarily depends on the image area (resolution), not how many characters are rendered in the image. In other words, a densely formatted image can carry much more information per token than plain text.

    Based on this, we can design the following compression strategy:

    • Retain Exact Text at the Beginning and End: The beginning of the session (task definition, key constraints) and the end (recent interactions) are retained in their original text form, ensuring the model’s precise perception of the task goals and current state.
    • Render the Middle Section as Images: The lengthy historical content in the middle (especially large blocks of tool output and logs) is locally rendered into densely formatted “frames” and injected into the context as images.
    • Attach an Exact Text Index: Each compression result is accompanied by a copyable text index and file map (e.g., FILES index), recording “which information is in which area of which frame.” When the model or user needs details, they can retrieve them based on the index.

    The key to this process is that rendering is entirely completed locally without calling any models. This means the compression step has zero additional cost and is completely deterministic—the same input always yields the same output, facilitating testing and replay.

    3. Workflow Breakdown

    A typical implementation can be broken down into the following stages:

    1. Session reaches compression threshold
    2. |
    3. v
    4. Determine safe zone division (calculate head and tail retention limits)
    5. |
    6. v
    7. Deterministic serialization
    8. (head and tail limit control, omit overly long logs, exact token marking)
    9. |
    10. v
    11. Local rendering into visual frames (no model call)
    12. |
    13. v
    14. Generate checkpoint:
    15. head/tail exact text + frames + text index + token list

    Several steps deserve further elaboration:

    Deterministic serialization is the cornerstone of reliability. Before compression, the true token count of each message block must be calculated, and rules dictate what enters a “frame” and what only keeps a head/tail excerpt. The rules must be purely functional: given the same history, the output is identical. This avoids the irreproducibility issue in the model summarization approach where “compression results differ each time.”

    Frame layout density directly determines cost-effectiveness. Visual models have a limit on their ability to read images—when the resolution is too high or the text is too dense, the recognition rate drops. The engineering approach is to typeset the text in a compact but still readable font size, usually using monospace fonts and multi-column layouts to increase the number of characters carried per token. The information density advantage of dense frames can only be realized within the readability limit.

    Text index as a fallback. For the model, an image is “visual information,” and accurately copying its content is not always reliable. Therefore, each compression result must be accompanied by an exact text index. It does not duplicate the entire content of the frame but records structural information (e.g., “Frame 3 contains the complete diff of a certain file,” “The end of Frame 2 has the return code of a certain tool call”). When the model needs exact content, it can locate it based on the index, and if necessary, the application layer can retrieve the text from the original storage.

    4. Suitable and Unsuitable Scenarios

    Any compression method has its boundaries, and this approach is no exception.

    Suitable scenarios:

    • Long-running agent tasks: debugging agents, repository-level automated refactoring, multi-step data analysis, etc., where the session lasts for hours and requires frequent backtracking to early tool outputs.
    • Middle content dominated by “dense but low-structured” text: logs, command outputs, long document excerpts—these types of content yield the greatest information density advantage when rendered into frames.

    Unsuitable scenarios:

    • Short chat compression: The summarization cost for a few turns of conversation is already low, and the fixed overhead of rendering images is not worth the trade-off.
    • Minimalist tool mode: If the runtime environment intentionally keeps only minimal capabilities in the toolset (e.g., no file read/write), the mechanism of relying on the text index to query original content cannot be implemented, and forcing its use would violate the design intent of this mode.

    5. Engineering Points for Implementation

    If you want to implement this type of compression in a self-built agent framework, pay attention to the following points:

    1. Compression trigger timing. In addition to the token threshold, it can also be actively triggered at key checkpoints (e.g., task phase transitions) to align frame boundaries with semantic boundaries, improving the model’s efficiency in utilizing information within frames.

    2. Multiple presets. Different running modes have different context requirements. You can provide multiple presets such as standard, tool-call-oriented (retaining more tool output details), and creativity-oriented (retaining more conversational tone and context flow), allowing users to choose based on the session type.

    3. Offload and fallback compatibility. Once the compression artifact is generated, it should remain in the session in the form of a “normal summary”—even if the compression module is removed, the historical session can still be opened normally, just losing the index query capability. This ensures state migrability under a plugin architecture.

    4. Installation and distribution. If implemented as a plugin, it should support various distribution methods such as in-app plugin markets, local package installations, and private source distributions. It should also declare dependencies on runtime versions (e.g., newer Node.js versions) to avoid silent failures caused by environment incompatibilities.

    6. Conclusion

    Replacing text summaries with images essentially finds a low-cost, high-fidelity balance between “area-based billing of visual models” and “deterministic local rendering.” It does not aim to be optimal in all scenarios but provides a better trade-off than traditional summaries in the niche scenario of “long sessions, heavy backtracking, and high-density text.” For developers, the more valuable insight is: context management does not necessarily require a more powerful model; sometimes, a clever representation transformation can simultaneously improve both cost and information retention.