You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
PDF token estimates are computed from the compressed container, not the extracted text — chunks overfill by ~3.5x and get bisected #2903
_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:
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.
Activity
fix(llm): estimate PDF tokens from the extracted text, not the contai…
chore: bump to 0.9.49
safishamsi commentedon Aug 24, 2026
Fixed in v0.9.49: https://github.com/Graphify-Labs/graphify/releases/tag/v0.9.49. Closing as resolved.