Skip to content

PDF token estimates are computed from the compressed container, not the extracted text — chunks overfill by ~3.5x and get bisected #2903

Closed

Description

@abhay-codes07

Summary

_read_files sends a PDF through _file_to_text → extract_pdf_text, but _estimate_file_tokens reads the file with read_text and tokenises that. A PDF's bytes are not its text — every real PDF Flate-compresses its content streams — so the estimate measures a compressed binary and comes out several times too small.

_pack_chunks_by_tokens sizes chunks from that estimate, so the chunk overfills, the request blows the model's context window, and _extract_with_adaptive_retry bisects: the same content is paid for two, four or eight times before it fits.

Reproduce

One 400-line fixture, the same text, stored two ways:

uncompressed             bytes=28470  est=5559  actual=4598  actual/est=0.83x
FlateDecode (real PDFs)  bytes= 1728  est=1334  actual=4599  actual/est=3.45x

est is _estimate_file_tokens(path); actual is the tokenised output of _read_files([path], root) — the prompt that will really be sent.

The two rows hold identical text. The only difference is the container, and the estimate moves by 4x. Uncompressed PDFs are roughly right by luck; compressed ones — which is all of them in practice — undercount by 3.45x here, and the ratio scales with how well the text compresses.

Why it is easy to miss

Nothing looks broken. There is no wrong output and no error the user sees: the run just costs several times what it should and takes correspondingly longer, on a corpus of papers, which is exactly the corpus a PDF implies. The bisection machinery works as designed and silently absorbs the mis-estimate.

Suggested fix

Estimate from the same text the prompt will carry — route PDFs through _file_to_text in _estimate_file_tokens, as _read_files already does.

Worth memoising: packing probes the same file repeatedly while deciding where a chunk ends, and extraction is the expensive part. Keying on (path, size, mtime) keeps a corpus of papers to one parse per run without serving a stale estimate for a file rewritten mid-run.

The tokenizer-absent fallback deserves the same treatment: it currently uses stat().st_size, which for a PDF is the compressed size and is wrong in the same direction.

Related

Same class as #2900 — the estimate and slicing paths read the raw file while the prompt path goes through _file_to_text. That issue notes PDFs still need the converter threaded through read_slice_text; this is the independent half in token estimation, which costs money even where slicing never applies.

I have this working with tests and will open a PR shortly.

Environment

graphify v8 @ b14b52e (0.9.47)
Python 3.12, Windows 11

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions