DEV Community

Uglypear Data
Uglypear Data

Posted on Originally published at uglypear.com

Document Compression, Local Compliance, and LLM Token Cost: A Practitioner's Framework

If you run document search or retrieval-augmented workflows inside a regulated institution, you are balancing three forces that pull in different directions: the need to keep data inside your own perimeter, the need to control what each role can see, and the brutal token economics of feeding large documents into a language model. This post is not a product pitch. It is a set of principles and a selection framework, drawn from verifiable industry data and from what a recent validated pipeline demonstrates about the shape of a working system.

The economic pressure is real

Start with the cost side, because it is the quiet killer of these projects. According to the IBM Cost of a Data Breach Report 2025, the global average cost of a breach hit 4.44 million US dollars, with an average of 241 days to contain it. That number is about breaches, but the lesson for builders is broader: the longer sensitive data sits in a place you do not fully control, the more expensive a mistake becomes. Containment time is a design property, not bad luck.

Now the usability side. A survey by M-Files, with Vanson Bourne, found that 96 percent of employees struggle to find the latest version of a document, and 83 percent have recreated a file from scratch. So even before you involve an LLM, the underlying document layer is already failing people. Lay a token-billing model on top of a broken document layer and you get the worst of both worlds: expensive and wrong.

The scale makes it concrete. There are more than 3 trillion PDFs in circulation worldwide. One major collaboration platform alone takes over 2 billion file uploads every day. Any system you build is operating on a firehose, not a tidy dataset.

Principle 1: Permission is a property of the query, not the file

The most common architectural mistake is attaching access control to the document or the folder. That model forces a binary: a role gets the whole file or nothing. Real institutional documents are not binary. A planning report has public summaries, role-restricted financial tables, and redactable sections. When you can only protect at the folder level, you either over-share or lock everything down until it is useless.

The alternative is to push access tiers from "public" and "restricted" down to the content-segment level, and enforce them at query time. When a query runs, the system evaluates, per section, what the asking role is authorized to see, and returns only that. Same query, different roles, genuinely different answers — not a filter applied after the fact, but a gate applied before the content is assembled. This is the core of a defensible compliance posture for any multi-role institution.

Principle 2: Do not migrate the source of truth

The second failure mode is the migration phase itself. Copying a living organization's data into a new repository is where version drift, broken references, and accidental exposures happen. The safer pattern is to connect directly to the databases the organization already runs. The original data stays in place. Nothing is copied into a competing silo that can fall out of date.

This has two effects. Operationally, you avoid a multi-month migration that nobody enjoys and that rarely finishes cleanly. Compliance-wise, you avoid creating a second copy of sensitive data that then needs its own protection, its own audit, and its own deletion policy. The knowledge layer becomes a reader over the source of truth, not a warehouse competing with it.

Principle 3: Make the system recoverable

Indexing is never a one-time event. Content changes, and the index must change with it. Incremental indexing keeps the index fresh without re-scanning everything, which matters at the scale of billions of uploads. But incremental systems need an undo. Snapshot rollback lets a mistaken or partial indexing run be reverted to a known-good state.

The safeguard I now consider mandatory is a guard against accidental mass deletion. One tired operator, one wrong scope, and a corpus can vanish. A brake that requires confirmation or limits blast radius is not bureaucracy — it is the difference between a Tuesday incident and a career incident. These three properties — incremental indexing, snapshot rollback, mass-deletion guard — are what let a system stay alive long after the launch demo is forgotten.

Principle 4: Compression is a first-class concern, not an afterthought

Here is where token economics enter directly. Language models bill by token, and public-sector documents are huge: procurement packs, filings, engineering drawings, and regulatory submissions that routinely run to hundreds of pages, full of high-resolution scans. Feed that raw into a model and the cost scales with every needless pixel.

A recent benchmark is instructive. A 443-page OFD document — a format used in certain government workflows — was compressed end-to-end in 124 seconds. More importantly for running cost, image compression in the pipeline cut LLM token cost by roughly 85.9 percent against a GPT-4o baseline. That is not a tuning improvement. That is the line between a system affordable at scale and one that gets switched off when the bill arrives.

But compression has a trap. If it destroys the elements that carry legal authority, it is worse than nothing. Seals and tables must survive. A table of figures is not decoration in a regulatory context; a seal is not a logo. A validated pipeline should preserve these key layout elements even as it shrinks the payload. If your compression throws away the seal, you have produced a plausible-but-invalid record, which is more dangerous than a large-but-correct one.

Principle 5: Sovereignty is a baseline, not a premium

For public-sector and other regulated work, residency is non-negotiable. The data cannot leave your perimeter — not to a shared tenant, not across a border someone else controls. This is the argument for private, on-premise, and where required, air-gapped, local-first operation. "We encrypt it in transit" is not a sufficient answer when the underlying data is a citizen record or a national budget line. "It runs inside our perimeter and the original never moves" is the only answer that survives legal review.

This is also why any framework should treat deployment model as a primary filter rather than a checkbox buried at the end. If a tool cannot run where your data lives, no amount of feature richness saves it.

A selection framework

The following table is a generic evaluation lens. It is meant to help you compare candidates on dimensions that matter, not to favor any single product.

Dimension Why it matters What to look for
Data residency Legal exposure grows with every copy outside your perimeter Private / on-premise / air-gapped operation; original data stays in place
Permission granularity Folder-level control forces over-share or lockout Access enforced at the content-segment level, at query time
Migration-free integration Migration is where drift and accidental exposure happen Direct connection to existing databases; no data copy
Rollback Index errors are inevitable at scale Snapshot rollback plus a guard against mass deletion
Compression efficiency Token cost scales with document size Measurable token reduction without loss of seals/tables
Compliance audit Regulators ask what happened and when Per-role access logging and recoverable state

Use it as a scorecard, not a shopping list. A candidate that scores well on compression but fails residency is a non-starter for public work. One that nails residency but cannot enforce segment-level access will quietly over-share.

What a validated end-to-end pipeline looks like

To make the principles concrete, consider the properties a recently validated pipeline demonstrated. I will keep it vendor-neutral, because the pattern is the point.

  • Restricted access tiers pushed to the content segment and enforced at query time, so different roles receive only their authorized parts of the same document.
  • A direct connection to the organization's existing databases, with the original data never migrating.
  • Incremental indexing paired with snapshot rollback and an accidental mass-deletion guard.
  • A full pipeline validated end-to-end, meaning each stage — ingestion, indexing, access control, compression, retrieval — was tested as a connected whole rather than in isolation.
  • Compression that, on a 443-page OFD document, ran end-to-end in 124 seconds and cut LLM token cost by about 85.9 percent versus a GPT-4o baseline, while preserving seals and tables.

None of these properties requires a specific brand. They are design commitments. When you evaluate a tool, ask which of them it can prove, not which it can describe.

Closing thoughts for builders

The temptation is to optimize the model and ignore the document layer. The data says otherwise: most organizations cannot even find the current version of a file, and breaches cost millions with containment measured in hundreds of days. The cheap wins are upstream — segment-level access, no-migration connection, recoverable state, sovereign deployment, and compression that respects legal layout.

Pick a framework, score candidates against it honestly, and refuse to compromise on residency and permission granularity. Everything else can be iterated. Those two cannot be bolted on later.

Top comments (0)