Cut LLM Token Costs by 70%: Rendering Long Contexts as Images for Multimodal Models
AI coding assistants can rack up significant bills due to repetitive, long-context text like system prompts and tool documentation sent in every request. This article explores a cost-saving technique where voluminous reference text is rendered into PNG images and sent as image tokens, leveraging the lower pricing of image tokens in multimodal models. By implementing a local proxy, developers can achieve up to a 70% reduction in API costs without altering model outputs, while maintaining full transparency and control over the compression process.
1. The Cost Problem: Why AI Coding Assistants Are Expensive
As LLM-powered coding assistants become integrated into development workflows, a practical issue has emerged: cost. During long task executions, every request carries a massive amount of repetitive content, including:
- System Prompts: Long texts defining tool behavior, often tens of thousands of characters.
- Tool Documentation: Descriptions of available functions, parameters, and return formats.
- Conversation History: Complete records of previous interactions.
- Tool Execution Results and Logs: Command outputs, test reports, and error stacks.
For the model, these often only require a quick glance to grasp the general idea. However, for input token billing, they are charged in full every round. The longer the task and heavier the context, the faster the bill inflates. This is why many users report costs as a significant burden when heavily using coding assistants.
2. A Counterintuitive Approach: “Photographing” Text for the Model
Common cost-saving routes in the industry typically fall into two categories: optimizing prompts to trim redundant content, or using a smaller model to generate summaries before passing them to the main model. The former offers limited compression, while the latter risks information loss and incurs extra summarization costs.
Recently, a more clever technical path has emerged in the community: bypassing summarization and directly rendering long contexts into PNG images, sending them as image blocks within the request. The core logic relies on the pricing structure of multimodal LLMs. In current mainstream APIs, the unit price of image tokens (when converted) is significantly lower than text tokens, and the model’s ability to recognize text within images (OCR and layout understanding) is now sufficient to “read” formatted documents.
Developer tests show: a system prompt and tool documentation of about 48,000 characters consumes roughly 25,000 tokens as text input. When rendered into a single image page, it only requires about 2,700 image tokens—a compression ratio approaching an order of magnitude. Based on general pricing levels of current multimodal models, the end-to-end bill can be reduced by about 60% to 70%. While this figure may change with model pricing adjustments, the effectiveness of the compression approach itself has been validated.
3. Engineering Implementation: A Local Proxy Between Client and API
This solution is typically implemented as a Local Proxy, featuring a very lightweight architecture:
Coding Tool Client ──HTTP──> Local Proxy Service ──HTTP──> Model API│├─ Identify bulky text blocks├─ Render locally to PNG└─ Assemble multimodal request
Sitting in the middle, the proxy completes three tasks before sending a request:
- Detection: Scans the request body to identify text blocks with abnormal size and low information density (e.g., overly long system prompts, tool docs).
- Rendering: Uses a headless browser or drawing library to render these texts into well-formatted images, replacing the original text blocks with image content blocks.
- Pass-through: Short texts or user instructions not suitable for processing are passed through unchanged.
A typical integration requires only two steps, without modifying anything beyond the client configuration:
# Start the local proxy, listening on the loopback addressnpx some-proxy# Point the coding tool's API address to the local proxyexport ANTHROPIC_BASE_URL=http://127.0.0.1:47821
The entire proxy can be implemented in TypeScript, relying on a Node 18+ environment, open-sourced under the MIT license, with virtually zero deployment cost.
4. Visibility and Control: Necessary Design for Cost-Saving Tools
Any middleware that “alters” request content must provide users with two basic capabilities: visibility and control.
- Visibility: The proxy provides a local Dashboard page, displaying in real-time how many tokens were saved per request, which text blocks were converted to images, and a size comparison before and after conversion. This transparency allows users to determine if the conversion affected task quality.
- Control: A one-click toggle on the panel disables the compression feature, reverting to pure text mode. In scenarios where the model inaccurately recognizes image content or task success rates drop, users can immediately stop the loss.
Another crucial boundary: the proxy only processes request inputs and never touches model outputs. That is, the model’s response content remains exactly the same as if no proxy were used; the results the user receives are not “processed”.
5. What Gets “Photographed”? Clarifying a Common Misunderstanding
It must be emphasized: these tools compress “reference content” like system prompts, tool documentation, and execution logs, not the user’s input query itself. The instructions issued by the user are sent as text verbatim, ensuring the model’s understanding of the task intent is entirely unaffected.
This is based on an information theory judgment: the information density of different content within a long context varies greatly. User instructions need to be conveyed word-for-word; whereas for a tool document of thousands of lines, the model usually only needs to understand its structure and key points to complete the task—this type of content is the ideal candidate for “dimension reduction to images”.
6. Applicability and Potential Risks
This approach is not a silver bullet; several points need evaluation before implementation:
- Model Capability Dependency: Effectiveness depends on the model’s accuracy in reading text within images. If future models change their text recognition capabilities, or vendors adjust pricing rules for image tokens, the benefits will fluctuate.
- Task Type Sensitivity: For scenarios involving fine-grained modifications of long code segments requiring character-by-character comparison, image compression may introduce recognition errors. It is recommended to maintain pure text transmission for critical code blocks.
- Auditability: When used in enterprise environments, ensure the image rendering process is completed entirely locally and that sensitive code does not pass through third-party services.
- Compliance and Billing Changes: Image token pricing policies are set by individual vendors; long-term cost calculations should be periodically reviewed.
7. Conclusion
Rendering long contexts into images to feed multimodal models is essentially a clever “format arbitrage”: leveraging the price difference between text and image tokens to exchange a reduction in cost within an acceptable range of information fidelity. Its implementation barrier is low—just a local proxy and a renderer—and it naturally fits scenarios like coding assistants where “context is heavy, repetition is high, and precision requirements are tiered”.
For developers, this provides a new perspective on cost optimization: beyond prompt engineering and model distillation, changing the encoding format of information is also a viable lever to reduce token expenditure. Of course, any middleware alteration should be premised on transparency and control, maintaining the ability to “turn it off anytime and inspect everything”, to achieve a sustainable balance between cost-saving and quality.
