Note
Optical context compression: OCR-heavy PDFs as image bundles for vision LLMs
Why text as an image can be cheaper, how the pipeline with Mistral OCR and a sizing model is built, and what a 22-page test document shows: up to 85 % less context.
- Published
- Christopher Böbel, Fullstack AI Engineer
- Christopher Böbel, Fullstack AI Engineer
- 5 min read
Feed a scanned PDF to a vision LLM and you pay per page: every page goes into the context as its own image, and with longer documents the window is full before the model can answer the first question. Plain text extraction is no answer either, because tables, drawings and layout are exactly the part that gets lost.
Optical context compression is my attempt to keep both – the image and an affordable context. The idea is simple: several pages are combined into one densely packed image, and a small model decides how dense that may get before the text becomes unreadable.
The result up front: a 22-page paper costs 11,284 tokens as text, 6,160 as one image per page – and 1,680 as six packed image bundles. That is 85 % less context for the same document, with the layout intact. A document that did not fit into the window before now does, and every question about it costs a fraction.

The rest of this note explains why that works, how the pipeline is built and where the limits are.
Why text as an image can be cheaper at all
The starting point is the paper DeepSeek-OCR: Contexts Optical Compression (Wei, Sun, Li, 2025). The authors show that a vision encoder can encode text far more compactly than a text tokenizer: as long as the number of text tokens stays within ten times the number of vision tokens, the model reads the text back with an OCR precision of around 97 %. At 20× compression it is still about 60 %.
That yields a simple rule: a page is worth turning into an image when its text costs more tokens than the image does. And it pays off more the denser the image is packed – up to the point where the model can no longer read the text reliably.
The pipeline
The published package optical-context-mcp implements this as an MCP server that any MCP-capable client can plug in. The flow:
- OCR and page images. The PDF runs through Mistral OCR; the layout is captured and rendered per page.
- Sizing model. How densely pages can be packed depends on the document: a table in 6-point type tolerates less than running text. So the decision is not a fixed threshold but a MobileNetV3 I trained on 9,076 reviewed images. Across five independent document holdouts its accuracy ranges from 0.700 to 0.923.
- Packing. Pages are combined into image bundles – in the test document, 22 pages become six images.
- Hand-over. The bundles go to the vision LLM as a whole; the server exposes three tools for that.

The package ships to PyPI through GitHub Actions with trusted publishing, five releases so far.
What the measurement shows
As the test document I used the DeepSeek-OCR paper itself: 22 pages, a mix of running text, formulas, figures and a long reference list. Vision tokens were counted with Gemini via OpenRouter at the low media resolution, where every image costs 280 tokens.
| Variant | Images | Tokens in context | Share |
|---|---|---|---|
| Text tokens (original) | – | 11,284 | 100 % |
| One image per page (baseline) | 22 | 6,160 | 55 % |
| Optical compression | 6 | 1,680 | 15 % |
That is 6.72× fewer tokens than the text and a further factor of roughly 3.5 over the naive image-per-page variant. Put differently: up to 85 % less context for the same document.
Not every page is worth it
The distribution is more interesting than the average. Because an image costs a flat 280 tokens, the break-even sits at 280 text tokens per page. In the test document 20 of 22 pages were above it; the two exceptions were two almost empty pages at the end of the document, at 85 and 243 text tokens respectively. For the densest page – 2,148 text tokens – the image saved 1,868 tokens. As a rule of thumb: from roughly 420 text tokens per page the gain is reliably above 1.5×.

So the server does this arithmetic per page and only packs what pays off – almost empty pages stay text.
Where the limits are
Three things to know before using this in production:
- Accuracy drops with density. The figures from the DeepSeek paper fall from 98.5 % at 6–7× compression to 89.8 % at 11–12×. That is exactly why the sizing model exists – it is meant to pick the density the document tolerates, not the maximum.
- The 85 % is a context reduction, not a quality benchmark. What is measured is tokens, not the quality of the model's answers to questions about the document. That is the next step.
- One document is one data point. The numbers hold for this paper with this model at this resolution. Poor-quality scans or very small type need their own measurement.
Try it
pip install optical-context-mcp
The server's source is on GitHub, the measurements and charts in the research repository. The project's case study summarises architecture and results on one page.
Read the case studyOptical Context MCP

