feat: vision-worthy detection and token-savings estimate#6
Merged
Conversation
Add PageFeatures (image/path counts and coverage), TokenEstimate with a saved property, the Source.VISION case, the tokens field on PageRoute, and RouteResult.tokens_saved.
Add Pdf.page_features, which counts raster image and vector path objects and their page coverage via pypdfium2 — the signals that flag charts, tables, and diagrams without rendering.
Add is_vision_worthy: route a page to a vision model when images cover much of it, vectors cover a large area, or many vector paths suggest a table or chart.
Add token estimators following each provider's documented tokenization: Claude 28px patches with resolution caps, OpenAI tile counting, and a text estimate.
route_pdf now sends visually-dominant pages to vision and attaches a token estimate to every page, so RouteResult.tokens_saved shows the cost avoided.
Test the detector on synthetic and real page features, the token formulas against documented provider examples, and routing of a table page to vision.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Completes the routing brain. Even pages with a clean text layer can lose their meaning when flattened — tables, charts, diagrams — so those go to a vision model. Every page now carries a token estimate, making the cost avoided explicit.
What
detect.py):is_vision_worthy(features)routes a page to vision when raster images cover >= 40% of it, vector paths cover >= 30%, or there are >= 25 vector paths (ruled tables, charts, diagrams).Pdf.page_features): counts raster image and vector path objects and their coverage via pypdfium2 — no rendering, no ML, and no AGPL PyMuPDF dependency.tokens.py):claude_image_tokens(28px patches, 1568/4784 caps),openai_image_tokens(tile counting),estimate_text_tokens.Source.VISIONbranch and attaches aTokenEstimateto each page;RouteResult.tokens_savedtotals the tokens avoided versus sending every page as an image.Why pypdfium2 over PyMuPDF
PyMuPDF (
get_drawings/find_tables) is AGPL, which would be imposed on this MIT package. pypdfium2 page objects give the same image/vector signals under a permissive license.Tests
On a mixed 3-page document (prose / table / scan) the router saves ~3077 tokens versus sending every page as an image.
Verified locally: ruff, ruff format, mypy (strict), pytest (37) all pass.