DEV Community

iwan rustiawan
iwan rustiawan

Posted on

Tips on Handling Files in LLMs

Tips on Handling Files in LLMs

You might want to pull information (especially from doc/text documents) out of a file and then interact with an LLM about its contents. The easiest approach is to let a multimodal LLM extract the file and feed it straight into the context for discussion.

That's not wrong — but picture the token waste: your context gets dominated by image tokens, because every page is rendered as an image that burns far more tokens than its text would. For multi-page files, the cost climbs fast.

For cases like this, it's better to handle the file inside your own backend, where you have far more control. You can extract the text separately and assemble the LLM context more deliberately — for example, only injecting the parts you actually consider necessary.

Here's a quick comparison from my own experience:

OCR / Text-Extraction Pipeline Direct LLM/Vision
Stability Winner. OCR is consistent — the same input tends to produce the same text. The LLM only reasons over the extracted text, so there are fewer sources of variance and the failure modes are more bounded and well understood. ⚠️ Vision extraction varies run-to-run; it can silently drop rows on dense tables or hallucinate digits.
Cost Winner at scale. Open-source engines (Tesseract/PaddleOCR) are free to self-host, or you can use a proven managed OCR with predictable per-page pricing. ❌ Image tokens dominate — every page rendered as an image costs far more tokens than its text (unless you downscale aggressively, which then sacrifices digit accuracy).
Numeric Accuracy Winner. If accuracy on numbers is the goal, you can use OCR with a custom model purpose-built for tabular digits. ❌ Weaker on dense financial tables; digit-level errors are exactly the kind of failure you can't tolerate.
Document Classification ⚠️ OCR strips out visual cues (logos, stamps, layout). But usually the text alone is enough. Winner here. Vision sees logos/layout/scanned docs/handwriting that OCR struggles with.

One important caveat: this isn't about "vision being bad." There are cases where Direct Vision is actually the better call — low-quality scans, handwriting, or highly unstructured layouts where building a clean OCR pipeline is more trouble than it's worth. The point is to match the tool to the nature of your data, not to paint with one brush.

For any system that touches numbers or finances, my rule is simple: hand numeric extraction to a layer that's deterministic and auditable — not to a model that "thinks it's probably right."

LLM #AIEngineering #SoftwareEngineering

Top comments (0)