Skip to main content

TDF — Token-Dense Format

License: MIT Python 3.10+ Tests

A document format and converter designed specifically for LLMs, with 100% distinct-content recall. Savings scale with how structured the document is: 28–66% below Markdown on table-heavy and boilerplate-heavy documents (PDFs, spreadsheets, HTML), dropping into the low teens on prose-heavy documents where Markdown was already near-optimal — see Benchmarks. It includes a skeleton mode to map documents for ~99% fewer tokens, and robust parsing backed by property-based fuzzing.

pdf docx xlsx pptx html md csv txt  ──►  TDF  ──►  your LLM

tdf stats demo

The Problem

Every LLM tool accepts document uploads. Under the hood, they convert the file to Markdown and paste it into the context window. That conversion is where the money and context window goes. Markdown was designed in 2004 for human readability, not token efficiency.

Pipes in tables (|), repeating header cells, and standard formatting drastically inflate token counts. For table-heavy documents, formatting overhead is almost half the total token cost.

Why TDF?

TDF solves this by introducing a line-oriented, token-optimized, plain-text format specifically engineered for BPE tokenizers.

  • 100% Distinct-Content Recall: Unlike prompt compression tools (like LLMLingua) that destructively discard entities and numbers, TDF's default mode achieves its compression through format optimization and dictionary coding. Every meaning-bearing term survives the round-trip.
  • Instant Conversion: Runs instantly without requiring a GPU or a local LLM inference step.
  • Advanced Table Handling: Borderless PDF table detection, rectangular grid enforcement, and columnar dictionary coding (Parquet/ORC concepts applied to LLM tokens).
  • Addressable Elision: TDF can identify low-density structural boilerplate (like giant website nav trees) and replace it with a token-cheap !E marker. The LLM can retrieve the original if needed.
  • Strict Validation: A formal grammar and validator guarantee TDF outputs are structurally perfect. Malformed inputs result in safe fallbacks, never data loss.

Benchmarks

We measured TDF against standard Markdown, MarkItDown (Microsoft's standard converter), and LLMLingua (a popular prompt compression model).

Token Compression vs Markdown:

Document TDF Saving vs MD TDF Saving vs MarkItDown Recall (Fidelity)
operating_review.pdf 65.7% 65.7% 100.0%
services_agreement.docx 47.0% 47.7% 100.0%
orders.csv 45.7% 45.7% 100.0%
quarterly_deck.pptx 37.3% 38.2% 100.0%
handbook.html 43.2% 43.2% 100.0%
runbook.md 27.8% 27.8% 100.0%

These are structured/table-heavy documents, where TDF's gains are largest. On prose-heavy, out-of-training documents savings are smaller — sec_filing.html (42.6%), kubernetes_docs.html (22.9%), attention.pdf (13.3%) — because there's less table/boilerplate overhead for TDF to strip out and Markdown is already close to token-optimal for plain prose. Full numbers: bench/results_samples_real.md.

TDF vs LLMLingua (Content-Preserving vs Lossy):

TDF's numbers below use --no-legend mode (the fairest match to LLMLingua's raw compressed output, which has no self-describing header either); with the default legend-on output, handbook.html saves 43.2% instead of 47.9% (see table above).

Document Markdown Tokens TDF Tokens (Savings, no-legend) LLMLingua Tokens (Savings) LLMLingua CPU Time
sec_filing.html 26,587 15,032 (43.5%) 14,728 (44.6%) 14.33s
handbook.html 4,732 2,464 (47.9%) 3,084 (34.8%) 1.94s

LLMLingua destroys table structure, strips out rows randomly, merges columns, and drops critical values to achieve its compression. TDF achieves similar or better compression (43-47%) natively during conversion with 100% distinct-content recall.

Installation

pip install tdf-converter

Or install from source:

git clone https://github.com/meashumishra/TDF-Token-Dense-Format-.git
cd TDF-Token-Dense-Format-
pip install .

Usage

Use the CLI to convert your documents:

# Convert a document to TDF
tdf convert document.pdf --to tdf -o output.tdf

# Get a high-level summary skeleton (~99% smaller)
tdf convert large_report.docx --to skeleton

# Validate a TDF file against structural invariants
tdf validate output.tdf

# Print tokens and format stats for a file
tdf stats orders.csv

How It Works

For a deep dive into the research, algorithmic decisions (Token-cost-weighted Re-Pair, Addressable Elision, Sentence Density tiering), and the exhaustive bug-hunting campaigns that hardened TDF, read the Architecture & Research Notes.

Known limitations

  • Small documents can come out larger. prose_only.pdf is 222 Markdown tokens but 252 in TDF, because the ~130-token self-describing legend dominates. Below roughly 500 tokens, use --no-legend.
  • Scanned/image PDFs are not handled — there is no OCR step. Pair with olmOCR or Chandra first.
  • Dictionary substitution hurts raw prose readability. ...at the start of §3 1. expands losslessly but reads badly to humans.
  • Fidelity metric is blind to word order. "100% distinct-content recall" is a 'bag-of-words' metric. Reversing a sentence (e.g., "A > B" to "B > A") or swapping table columns still yields a 100% score despite destroying meaning.
  • Elision is intentionally lossy. Addressable Elision (!E) deliberately drops content to shrink context; it does not preserve the original text.
  • LLM QA accuracy is insufficiently validated. While TDF compresses structurally, we have not yet run an end-to-end LLM benchmark (e.g., GSM8K or RAG eval) to empirically prove that models can accurately reason over heavily compressed !D dictionaries without degradation.
  • Tokenizer assumptions: We have verified compression consistency (44-72% savings on structured docs) across OpenAI's o200k_base and cl100k_base tokenizers, but it remains untested on SentencePiece or Llama tokenizers.

License

MIT License. See LICENSE for details.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tdf_converter-0.1.1.tar.gz (47.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tdf_converter-0.1.1-py3-none-any.whl (43.4 kB view details)

Uploaded Python 3

File details

Details for the file tdf_converter-0.1.1.tar.gz.

File metadata

  • Download URL: tdf_converter-0.1.1.tar.gz
  • Upload date:
  • Size: 47.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.13

File hashes

Hashes for tdf_converter-0.1.1.tar.gz
Algorithm Hash digest
SHA256 a023ec2b015006412054fa371f2f48a8ecd05937993c5c77a31117b287ac79f3
MD5 fed3d275df5e6866482ea3fc6538f572
BLAKE2b-256 056a9c8437631f4fdc982e8020924d1f4f3f3a68394655f63b56cd8724871d02

See more details on using hashes here.

File details

Details for the file tdf_converter-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: tdf_converter-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 43.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.13

File hashes

Hashes for tdf_converter-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7ec25916ef612ba9dad1132b824da1ce4d2b0010efd0dfd6efe01b9481c65a8e
MD5 f3324ecea31eaba0f8d0c5c7f43a7764
BLAKE2b-256 41d4dd00ce5ff6bd64a36c3d6f716d7db8a58d88b0babaf22c9c3a091af6d4c2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page