Pixels for Programs? A Cross-Provider Case Study of Input-Token Accounting for Source Code as Text and Images

· Source: cs.SE updates on arXiv.org · Field: Technology & Digital — Artificial Intelligence & Machine Learning, Software Development & Engineering, Data Science & Analytics · Depth: Expert, extended

Summary

The article presents a case study on input-token accounting for source code sent to commercial vision-language model APIs as text versus compact images. The study benchmarked requests across five programming languages, nine source lengths from 20 to 2,000 lines, and 15 model aliases exposed by Anthropic, OpenAI, and Google Vertex AI (Gemini). Across 675 complete text/image pairs, aggregate image-to-text ratios were 0.135 for Anthropic, 0.194 for OpenAI, and 0.242 for Gemini, corresponding to reported input-token reductions of 86.5%, 80.6%, and 75.8% respectively. However, these aggregates conceal varying break-even behaviors: Anthropic and OpenAI images consistently received lower counts, while Gemini images required 6.95 times more tokens at 20 lines and only crossed below text at 200 lines. A targeted audit also revealed non-monotonic Gemini image accounting across page boundaries. The study provides a reproducible protocol and artifact but does not measure semantic fidelity, task accuracy, latency, monetary cost, or coding-agent efficiency.

Key takeaway

For Machine Learning Engineers optimizing API costs for code processing, you should implement conditional routing for source code inputs. For short contexts, raw text is often more cost-effective, especially with Gemini. For longer, read-mostly contexts, visual representation can reduce reported tokens, but you must validate fidelity and monitor provider-specific break-even points and page-sensitive accounting. Always compare against textual compression and retrieval methods.

Key insights

Sending source code as compact images to LLM APIs can significantly reduce reported input tokens, but with provider-specific break-even points.

Principles

Method

The study used a two-stage transform: replacing indentation with markers, then rendering to PNG pages. It collected provider-reported input tokens for raw text and compact images across varied languages and lengths.

In practice

Topics

Code references

Best for: AI Architect, AI Engineer, CTO, AI Scientist, Machine Learning Engineer, Research Scientist

Related on AIssential

Open in AIssential →

Editorial summary, takeaway, and curation by AIssential. Original article published by cs.SE updates on arXiv.org.