Context Compression with Images: A New Approach to Carrying Conversation History

    This article explores an alternative to text-based summarization for long AI coding sessions: rendering the middle portion of a conversation history into images. It explains why context compression becomes necessary, the limitations of traditional model-generated summaries, and how a deterministic local pipeline can preserve more detail at lower cost. The article covers the reference architecture, the design of the compression artifact (image frames, exact token index, and file map), optional presets and their boundaries, engineering considerations such as rollback, versioning, observability, and privacy, as well as common pitfalls to avoid.

    1. Why Long Sessions Require Context Compression

    In AI coding assistants and conversational development environments, a single session can last for hours or even days. As the number of turns grows, historical messages, tool call results, file snippets, and logs keep accumulating and quickly hit the model’s context window limit. Once that limit is exceeded, the system either refuses to continue or is forced to discard earlier information, causing the model to “forget” the original constraints, confirmed interface contracts, and error leads that were previously investigated.

    The traditional approach is to hand the middle portion of the history to the model for a text summary. This path has three inherent problems. First, the summary itself consumes a model call, adding both latency and cost. Second, summarization is lossy: the model tends to keep “conclusions” while dropping “evidence,” such as specific error codes, field names, and line numbers. Third, summary quality is uncontrollable; compressing the same history at different times can produce very different results, undermining reproducibility.

    What is needed is a compression method that satisfies three constraints: the compression process itself should rely on the model as little as possible, the result should retain more detail, and the output should include an index that supports precise lookback.

    2. Core Idea: Render the Middle History as Images

    The key insight comes from a fact about billing and perception: vision models are billed mainly by image area (resolution or number of tiles), not by how many characters the image contains. Within the limit of what remains readable, a single information-dense tall image carries far more information per unit of cost than plain text. Rendering the middle content that would otherwise be summarized into text as an image preserves much more of the original detail under the same budget.

    It is worth emphasizing that rendering happens entirely locally as deterministic image processing and requires no model involvement. This yields two direct benefits: zero model call overhead during compression, and the same input always produces the same image, making results reproducible and verifiable.

    At the same time, images alone introduce new problems: they cannot be precisely searched, copied and pasted, or consumed by tools. A mature approach therefore does not “keep only images” but keeps images and structured text side by side.

    3. Reference Architecture: A Staged Deterministic Pipeline

    A practical implementation can be abstracted into the following stages, where the output of each stage is the input of the next and no stage introduces randomness:

    1. Conversation history
    2. │
    3. ├─ 1. Safe zone selection: determine head and tail retention boundaries
    4. │
    5. ├─ 2. Deterministic serialization: head/tail length limits, log elision rules, exact token count markers
    6. │
    7. ├─ 3. Rendering and convergence: image rendering -> structural convergence -> soft compression, degrading step by step
    8. │
    9. └─ 4. Single checkpoint artifact: exact head/tail text + image frames + text index + file map

    Responsibilities of each stage:

    1. Safe zone selection. The system must decide what content must never be turned into an image, typically the task definition and system constraints at the start of the session, plus the most recent turns of real interaction. Only the middle portion is a compression target.
    2. Deterministic serialization. Messages, tool outputs, and code snippets are converted into a stable text layout, with explicit line or token limits for head and tail retention. Overly long logs are handled with “head and tail excerpts plus middle elision” and an elision marker is left behind. All counts use exact token markers rather than estimates.
    3. Step-by-step convergence. Image rendering is attempted first; if a single frame exceeds the readability limit, structural convergence is applied (for example, a very large tool output keeps only head and tail excerpts and the middle is dropped); if that still does not fit, the pipeline falls back to soft compression (generating a shorter text placeholder). The whole chain is “preserve as much as possible, concede step by step” rather than a one-time discard.
    4. Single checkpoint. The final output is a single compression result, avoiding context pollution from multiple coexisting summaries.

    4. Artifact Design: Images + Exact Text Index + File Map

    The compression artifact should consist of four parts, none of which can be omitted:

    • Exact head/tail text: kept as is, with no rewriting, ensuring zero loss of the task goal and recent context.
    • Image frames: carrying the middle history, split into frames according to the readability limit, with frame order matching time order.
    • Exact token index: recording the original range and token count corresponding to each frame, making budget accounting verifiable.
    • File map: summarizing the file paths that appeared in the session and their roles (read/modified/created), so that both the model and the user can quantify “which files the current workspace involves.”

    The text index and file map must be copyable plain text. This is the key to the usability of the whole approach: the model can reference paths and field names in them on demand, and the user can copy them with one click for verification, avoiding the awkwardness of images that are “visible but unusable.”

    5. Presets and Applicability Boundaries

    Different sessions have different preferences for context strategy, so compression should be offered as optional presets rather than forced globally:

    • Standard preset: suitable for regular development sessions, with a balanced ratio of head/tail retention and image frames.
    • Tool-chain enhanced preset: aimed at tool-call-heavy scenarios that need to retain more structured output.
    • Exploratory preset: aimed at open-ended creation and solution exploration, more inclined to preserve details from the divergent process.

    The non-applicable scenarios must also be made clear. Minimal sessions deliberately omit read/write and editing tools; their design goal is to stay lightweight, so introducing image-based compression conflicts with that intent and is a negative optimization. Short conversations should not be compressed either: the fixed cost of compression exceeds the benefit and it breaks the coherence of a short session. The criterion is simple: image-based compression only pays off when the history length is already close to the window limit and there is a recurring need to look back at details.

    6. Engineering Implementation Notes

    • Uninstallable and rollback-capable. The compression capability should be integrated in a plugin-like manner and leave no side effects after removal; already-generated compression results remain in the session as ordinary summaries without breaking history integrity.
    • Version and runtime constraints. Image rendering depends on a specific runtime version, so the host environment version requirements must be checked before installation to avoid rendering failures caused by runtime differences.
    • Diverse distribution methods. In addition to one-click in-app installation, installation from the command line according to a user profile should be supported, along with offline installation paths such as locally packaged artifacts and package addresses, which is convenient for intranet and isolated environments. Example:
    1. # Install a locally packaged artifact by the specified profile
    2. tool plugin --profile <profile-name> add "file:$PWD/compact-plugin-0.7.2.tgz"
    • Observability. Each compression should output structured metrics: token counts before and after compression, number of frames, number of elided ranges, and the degradation level triggered. Without metrics, tuning is impossible.
    • Privacy and compliance. Rendering locally means the history content never leaves the machine, an important advantage over cloud-based summarization; if an enterprise archives compression artifacts to object storage or a logging service, clear redaction and access control policies are required.

    7. Common Pitfalls

    1. Treating image rendering as “screenshot archiving.” The real value lies in pairing images with an exact index; an image without an index is just an unsearchable burden.
    2. Ignoring the degradation path. Implementing only image rendering without structural convergence and soft compression will fail outright on an oversized single tool output.
    3. Using estimated tokens instead of exact counts. Once budget accounting is distorted, the compression strategy cannot run stably.
    4. Forcing it globally. Compression should be disableable and selectable per session, avoiding unnecessary overhead in short and minimal sessions.

    8. Summary

    The essence of image-based context compression is replacing “summaries generated by the model” with “a high-density visual carrier plus an exact text index generated by a local deterministic process.” It optimizes three metrics at once: zero model calls during compression, more information carried per unit of cost, and precise lookback to details. In practice, four points matter: safe zone boundaries, deterministic serialization, step-by-step degradation, and a complete index and file map. In practice with AI coding and agent applications on platforms such as Baidu AI Cloud, this kind of context governance approach can serve as a design reference for foundational capabilities in long-running task sessions.