Reduce AI token usage by up to 70% β locally, in your browser. 100% Private. 0% Data Leaks.
AI Token Optimizer is an offline-capable Chrome (and beta Firefox) extension that intercepts file uploads and large pastes (.pdf, .docx, .pptx, .xlsx, .csv, .md, .json, .html, .txt, .log, and images) on popular AI chat platforms β Claude.ai, ChatGPT, Gemini, Perplexity, plus Grok, DeepSeek, Mistral, Copilot, AI Studio and Poe in beta β converts documents locally into optimized Markdown, downscales oversized images, compacts pasted JSON and logs, and sends the lean version instead.
The result: massive context-window savings, lower API costs, faster AI reasoning, and no more platform-specific file size or file type friction.
- Why AI Token Optimizer?
- Features
- Supported Formats
- Supported Platforms
- Installation
- Usage
- How It Works
- Project Structure
- Privacy & Security
- Troubleshooting
- Contributing
- Roadmap
- License
Raw documents are extremely token-inefficient. A .docx or .pdf file carries formatting metadata, redundant whitespace, empty spreadsheet cells, and structural bloat that AI platforms tokenize anyway β burning through your context window and your usage limits.
| Without Optimizer | With Optimizer |
|---|---|
| Raw PDF/DOCX/XLSX uploaded as-is | Clean, structured Markdown uploaded instead |
| Bloated tokens from formatting noise | Up to 70% fewer tokens |
| Slower responses on large documents | Faster reasoning over compact input |
| File type & size limits get in the way | Markdown sails through every platform |
| Full-resolution screenshots billed by pixel count | Images downscaled to what vision models actually use |
| Pasted pretty-printed JSON and noisy logs | Minified JSON, repeated log lines folded |
| Files uploaded to third-party servers for parsing | Everything parsed locally in your browser |
- π 100% Local & Private β All parsing (documents, images, pastes) happens entirely inside your browser sandbox. No file data ever leaves your device.
- π Up to 70% Token Savings β Smart compaction removes redundant whitespace, empty cells, duplicated slide content, and bloated document structure.
- β‘ Seamless Interception β Automatically catches
<input type="file">change events and drag-and-drop triggers on supported chat platforms using capture-phase event hooking. Zero extra clicks. - πΌοΈ Image Downscaling β PNG/JPG/WebP uploads are resized to a 1568px long edge (configurable) before upload. Vision tokens scale with pixel count, so a 12MP screenshot drops from ~16K to ~2.5K estimated tokens.
- π Paste Compaction β Pastes over 1,500 characters get JSON minified, ANSI colour codes stripped and repeated lines folded, but only when that saves 15%+. Code indentation is never touched, and Ctrl+Z undoes it.
- π Context-Window Fit β After each conversion the on-page HUD shows the estimated token count and how much of the site's context window it uses (e.g. "β24,310 tokens Β· 12% of 200K window").
- ποΈ Output Tuning β Choose Markdown or CSV for spreadsheet tables, pick the image size limit, and turn paste compaction on or off.
- π OCR for Scanned Documents β Built-in Tesseract.js OCR (bundled locally) extracts text from scanned/image-based PDF pages.
- π° Two-Column PDF Support β Structural coordinate extraction correctly reconstructs multi-column academic papers and reports.
- π§ͺ Popup Playground β Open the extension dashboard, drag in any document, and instantly view the structured Markdown preview, copy it to your clipboard, or tweak options.
- π Savings Dashboard β Live-track total files processed, original vs. optimized sizes, and estimated cumulative tokens saved.
- βοΈ Custom Instructions Template β Automatically prepend a custom system prompt to every optimized document (e.g., "Here is the optimized markdown version of my sheet. Tidy up the figures and wait for my instructionsβ¦").
| Format | Library & Strategy | Output | Optimization Pass |
|---|---|---|---|
PDF (.pdf) |
PDF.js with structural coordinate extraction + Tesseract.js OCR fallback |
Structured .md |
Column reconstruction, list formatting, sub/superscript mapping, slide-deck deduplication |
Word (.docx) |
Mammoth.js HTML tag compilation |
Semantic .md |
Semantic headers, bold, italics, tables, and list extraction |
PowerPoint (.pptx) |
JSZip + DOMParser over slide XML |
Structured .md |
Slide titles, bullets, tables and speaker notes |
Excel (.xlsx) |
SheetJS workbook cell mapper |
Table .md (or CSV) |
Blank rows/columns stripped; optional CSV output skips per-cell pipe padding |
CSV (.csv) |
SheetJS parsing | Table .md (or CSV) |
Layout-compressed tables |
Markdown (.md) |
Native text pass-through | Optimized .md |
Whitespace compaction, multi-newline trimming |
JSON (.json) |
Native JSON.parse |
Fenced .md |
Minified (indentation is pure token overhead); JSON Lines falls back to the text pass |
HTML (.html / .htm) |
Inert DOMParser + the DOCX Markdown walker |
Semantic .md |
Scripts, styles, SVG and markup stripped |
Text / Logs (.txt / .log) |
Native text decoder | .md |
ANSI colour codes stripped, consecutive duplicate lines folded (line [x400]) |
Images (.png / .jpg / .webp) |
OffscreenCanvas |
Same format | Downscaled to a 1568px long edge by default (1024 / 2048 selectable); billed tokens scale with pixel count |
| Platform | Status |
|---|---|
| Claude.ai | β |
| ChatGPT | β |
| Gemini | β |
| Perplexity | β |
| Grok, DeepSeek, Mistral Le Chat, Copilot, Google AI Studio, Poe | π§ͺ Beta (interception not yet verified on every site) |
This extension is installed as an unpacked developer extension β it takes less than a minute.
Click the "Fork" button at the top-right of this page to create your own copy of the repository under your GitHub account.
π‘ Forking first is the recommended workflow β it lets you pull updates cleanly and contribute changes back via pull requests. If you only want to try the extension without a GitHub account, you can use Code β Download ZIP instead and skip to Step 3.
git clone https://github.com/<your-username>/Token-Optimizer.git
cd Token-Optimizer- Open Google Chrome and navigate to
chrome://extensions/ - Toggle the "Developer mode" switch ON (top-right corner)
- Click the "Load unpacked" button (top-left corner)
- Select the cloned
Token-Optimizerfolder - Pin AI Token Optimizer from your extensions bar for easy access β
- Open
about:debugging#/runtime/this-firefox - Click Load Temporary Add-on⦠and select
manifest.jsonin the cloned folder - Temporary add-ons are removed when Firefox restarts; reload it the same way
Firefox support is new and not yet verified end-to-end. Please report issues.
To get the latest version, pull the newest changes and reload:
git pullThen click the β» Reload button on the extension card in chrome://extensions/.
- Visit any supported AI platform.
- Upload or drag-and-drop a supported document or image exactly as you normally would.
- Choose Optimize in the confirmation popup (or tick Remember my choice to skip it next time).
- The extension converts the file locally and attaches the optimized version instead. The HUD shows tokens saved and how much of the context window the file uses.
Paste a large JSON blob or log into the chat box. If compaction saves 15% or more, the compact version is inserted and a short notice confirms it. Press Ctrl+Z to restore the original, or turn it off under Settings β Output Tuning.
- Click the AI Token Optimizer icon in your extensions bar.
- Drag any supported document into the popup playground.
- Instantly preview the structured Markdown output, copy it to your clipboard, or adjust conversion options.
- Check the Savings Dashboard to see your cumulative token savings.
The extension injects a lightweight, isolated content script (content.js) into supported AI domains, plus a MAIN-world script (inject.js) for deep page-level hooks. Listeners are registered on the change event of <input type="file"> elements and the drop event of drag-and-drop zones during the capture phase (useCapture = true). This allows the extension to:
- Halt the platform's own upload handlers (e.g., React file handlers on Claude/ChatGPT) before they fire.
- Parse and convert the file locally.
- Programmatically build a new optimized file using the
DataTransferAPI. - Re-dispatch the event so the platform uploads the optimized Markdown seamlessly.
The core engine (converter.js) leverages high-performance, sandboxed browser APIs and Web Workers to read file buffers:
- PDF Parse β Reconstructs paragraphs, matches font baselines to capture mathematical sub/superscripts, filters out visual-only components (images, diagram labels), reconstructs multi-column layouts, and falls back to local OCR for scanned pages.
- Word Parse β Compiles Mammoth's HTML output into lightweight semantic Markdown.
- Excel/CSV Parse β Cleans spreadsheet tables, drops completely empty rows/columns, formats dates, and generates GFM pipe tables or fenced CSV.
- PowerPoint Parse β Unzips the deck and reads slide XML for titles, bullets, tables and speaker notes.
- JSON / HTML / Text β Minifies JSON, converts HTML through an inert
DOMParser(no scripts run), and folds duplicate log lines. - Images β Decodes with
createImageBitmap, resizes on anOffscreenCanvas, and re-encodes in the original format.
content.js listens for paste in the capture phase. For large text it computes a lossless compaction, cancels the original paste and dispatches a new one carrying the compact text. Rich editors such as ProseMirror (Claude, ChatGPT) handle it natively. Plain textareas fall back to insertText, which keeps native undo working.
Savings are estimated with a tokenizer-shaped heuristic (words, 3-digit number groups, CJK characters, merged whitespace) rather than a raw character count, so whitespace removal isn't over-reported. It is an estimate, not an exact count for any one model.
Token-Optimizer/
βββ manifest.json # Chrome Extension Manifest (V3)
βββ background.js # Service worker (lifecycle & messaging)
βββ content.js # Content script β upload interception on AI platforms
βββ inject.js # MAIN-world script for page-level interception
βββ converter.js # Core engine β document β optimized Markdown
βββ styles.css # Injected on-page UI styles
βββ popup/ # Extension dashboard (playground, stats, settings)
βββ tests/ # Unit tests: node --test tests/converter.test.js
βββ lib/ # Bundled third-party libraries (PDF.js, Mammoth,
# SheetJS, JSZip, Tesseract.js β no remote loading)
We believe your data is yours alone. AI Token Optimizer requires no internet-access permissions (other than matching chat sites for script injection) and contacts zero external APIs or servers.
- π’ No telemetry or analytics tracking
- π’ No remote script loading β all libraries (including OCR models) are fully bundled locally
- π’ No file logging or third-party cookies
- π’ Minimal permissions β only
storage(for your settings and savings stats) and content-script access to the supported chat domains
You can verify every claim above yourself β the entire source code is in this repository, unminified where authored by this project, with no build step.
| Problem | Solution |
|---|---|
| Uploads aren't being intercepted | Refresh the AI platform tab after installing/reloading the extension (content scripts inject at page load). |
| Changes to the code aren't taking effect | Click β» Reload on the extension card in chrome://extensions/, then refresh the platform tab. |
| A specific document parses incorrectly | Open the popup playground, drag the file in, and inspect the preview. Please open an issue with a sanitized sample file. |
| A paste wasn't compacted | Only pastes over 1,500 characters that shrink by 15%+ are changed. Check Settings β Output Tuning β Compact Large Pastes. |
| I want the original paste back | Press Ctrl+Z right after pasting. |
| An image wasn't resized | Images already within the size limit are uploaded unchanged. GIFs are never modified. |
| Scanned PDF returns little/no text | OCR runs locally and can take longer on large scans β give it a moment. Very low-resolution scans may extract poorly. |
Contributions are welcome and appreciated! Whether it's a bug fix, a new file format, a new platform, or better docs β we'd love your help.
Please read our Contributing Guide to get started. It covers:
- π§ Setting up your development environment (fork β clone β load unpacked)
- πΏ Branch naming and commit message conventions
- π The pull request process and template
- π§βπ» Coding guidelines (privacy-first, no remote dependencies, Manifest V3)
- π§ͺ How to test your changes
Found a bug or have a feature idea? Open an issue β clear reproduction steps and sample files make fixes much faster.
AI usage has shifted: models take images natively, context windows are 200Kβ1M tokens, people paste logs and JSON as often as PDFs, and more chat apps compete for attention. The plan follows that shift.
Phase 1 β Broader inputs (v1.2, shipped)
- Image downscaling to the resolution vision models use (largest real token saving for screenshots/photos)
- JSON minification, HTMLβMarkdown, log de-duplication
- Grok, DeepSeek, Mistral, Copilot, AI Studio, Poe (beta)
- Tokenizer-aware estimate (whitespace runs no longer counted as savings)
- Untrusted file names / HTML handled without
innerHTML(XSS fix) - Disabled file types no longer trigger the confirm prompt
- Unit tests:
node --test tests/converter.test.js
Phase 2 β Smarter compression (v1.3, shipped)
- Paste compaction: pastes over 1,500 chars get JSON minified, ANSI codes stripped and repeated lines folded β only when it saves 15%+, never touching code indentation (Ctrl+Z undoes)
- CSV output option for spreadsheets (no per-cell pipe padding)
- Context-window fit in the HUD (e.g. "β24,310 tokens Β· 12% of 200K window")
- Output Tuning settings: paste toggle, table format, image max size (1024 / 1568 / 2048px)
Phase 3 β Reach
- Firefox 128+ manifest support (
browser_specific_settings, backgroundscripts) β load viaabout:debuggingβ Load Temporary Add-on βmanifest.json; not yet verified end-to-end - Chrome Web Store / Firefox AMO listings (needs store assets and the owner's developer accounts)
- Optional on-device summarisation via Chrome's built-in AI (Gemini Nano) for documents too large for any context window β deferred: lossy, so it needs its own opt-in UX
Have a suggestion? Open a feature request!
This project is licensed under the MIT License by Vinit Girdhar. Feel free to fork, modify, and distribute as you see fit.
If this project saves you tokens, consider giving it a β β it helps others discover it!
Made with β€οΈ for the AI community