Skip to main content

Token Shredder

Shrink files, folders and web pages into compact markdown that fits your LLM's token budget — locally, without AI.

For developers who paste code, documents and data into Claude, ChatGPT or another model and run out of context (or money). Token Shredder keeps the structure and the facts a model needs and drops the rest: code becomes an outline, a CSV becomes a summary of its columns, a web page becomes its article. It's lossy on purpose — and you decide how lossy, or give it a token budget and a question.

No API key and no AI model; it runs on your machine. (The one network access: on first use, the tokenizer library downloads a 1.7 MB file once.)

Status: alpha (v0.4.1). Tested on ~120 real files and on macOS, Linux and Windows; the interface may still change. See what it can't do.

Install

pip install token-shredder              # or: pipx install token-shredder
uvx token-shredder convert file.pdf     # or run it without installing

Python 3.10–3.13.

See it

A 60 KB CSV (30,085 tokens) → 751 tokens: token-shredder convert titanic.csv

Rows: 891, columns: 12
- `Survived`: number, enum: 0 ×549 (62%), 1 ×342 (38%)
- `Pclass`: number, enum: 3 ×491 (55%), 1 ×216 (24%), 2 ×184 (21%)
- `Sex`: string, enum: male ×577 (65%), female ×314 (35%)
- `Age`: number 0.42–80 (int: no), mean 29.7 | null ×177
- `Fare`: number 0–512.329 (int: no), mean 32.2
...
First 5 rows: (table)

A 1,400-line TypeScript file (38,753 tokens) → 2,300 tokens: token-shredder convert strings.ts -Q "how does truncate work"

## Declarations (1438 lines)
export function isFalsyOrWhitespace(str: string | undefined): boolean
// Helper to produce a string with a variable number of arguments.
export function format(value: string, ...args: any[]): string
...                                          ← every function: signature + first line of its doc

## Relevant to "how does truncate work"
### export function truncate(value: string, maxLength: number, ...) (lines 106-117)
```ts
export function truncate(value: string, maxLength: number, suffix = Ellipsis): string {
	if (value.length <= maxLength) {
		return value;
	...                                          ← the matching function in full
```

Use

token-shredder convert paper.pdf                  # writes paper.md, prints how many tokens it saved
token-shredder convert paper.pdf -o -             # print the markdown instead (default when piped)
token-shredder convert paper.pdf --copy           # put it on the clipboard
token-shredder convert https://example.com/post   # a web page: just the article
cat data.json | token-shredder convert -          # from stdin

token-shredder convert app.py --max-tokens 2000   # the most detail that fits in 2,000 tokens
token-shredder convert app.py -Q "how are retries handled"   # outline + the matching parts in full

token-shredder batch ./src --max-tokens 20000 --copy          # a whole folder within 20k tokens
token-shredder batch ./docs -o ./docs-md                       # one .md per file

batch reads folders recursively and skips hidden folders, dependency/build folders (node_modules, venv, dist, ...) and files named like secrets. With -o - or --copy it produces one document with a <file path="..."> block per file. Every option: docs/USAGE.md.

How it works

Detail levels. -d 1 to -d 5; the default is 3. Higher always means more content, for every format, and a summary is never larger than the full file.

Level Code Data (CSV, JSON, Excel, ...) Web pages PDF, Word, text
1 public names column/field names and types headings headings
2 + signatures + counts and ranges + paragraphs, lists + first sentence of each paragraph
3 + first line of each doc comment + examples, category counts, one line per record + tables; reference lists dropped the full text, cleaned up
4 + full doc comments all data, compacted + link URLs, references + page markers, speaker notes
5 the file itself the file itself the raw HTML the raw extracted text

"Cleaned up" means broken lines and split words are rejoined and page headers, footers and page numbers are removed. "One line per record" is a short index such as 7651: <issue title> [2 comments, 2026-11-21, by …] or react: 17.0.2. Per-format details: docs/DETAIL_LEVELS.md.

Token budget (--max-tokens N, -t N). Picks the highest level that fits. For a folder the budget is shared: small files stay whole, big ones split the rest. If nothing fits, the output is cut and marked [... truncated ...].

Question (--query "...", -Q). Splits the file(s) into functions, sections, rows or records, ranks them by how well their words match your question, and adds the best ones in full after the outline. This is plain keyword matching — fast and offline, but it won't find a part that uses different words than your question.

Credentials. Well-known API key, token and private-key formats are replaced by [REDACTED: kind], and .env values are never shown. This catches common mistakes; it is not a secret scanner, so still check what you paste. --keep-secrets turns it off.

Tokens are counted with tiktoken (cl100k_base), which is close to but not the same as Claude's tokenizer.

Formats

PDF, Word (.docx), PowerPoint (.pptx), Excel (.xlsx), HTML, Markdown, text, CSV/TSV, JSON, JSONL, JSON5, YAML, TOML, INI, XML, SVG, SQL, GraphQL, Protobuf, CSS, .env, logs, Dockerfile, Makefile, .gitignore, shell scripts, and code: Python, JavaScript/TypeScript, Java, Go, Rust, C/C++. Not supported: scanned PDFs and images (no OCR), audio/video, .doc/.xls/.ppt. Details: docs/FILE_FORMATS.md.

How well it works

Default level, tokens counted with tiktoken. "Plain text" is what you'd otherwise paste: the extracted text of a PDF/Word file, the visible text of a web page, the file itself for code and data.

File Plain text MarkItDown Token Shredder
arXiv paper (PDF, 15 pages) 10,168 12,175 9,640
Wikipedia article (HTML) 27,535 71,099 9,684
requests/sessions.py (Python) 7,336 — 1,288
10 GitHub issues (JSON) 15,234 — 1,115
86,343-row CSV, 8.8 MB 3,841,232 — 887

PDFs and Word files only get slightly smaller at the default level (they are cleaned, not summarised); use -d 2 or a budget for more. Code and data shrink a lot because the output is an outline or a summary.

Do the answers survive? In a small self-run test, a model (Claude Sonnet) answered 60 questions about 12 files twice: once from MarkItDown's output and once from Token Shredder's, without knowing which was which. A second model graded the answers blind.

  • Both got 52 of 60 right. Token Shredder used 7× fewer tokens (166k vs 1.2M).
  • One file decided the tie: a lockfile MarkItDown makes 831k tokens long, too big to give the model at all. Without it, MarkItDown did better: 52/55 against 47/55.
  • Token Shredder's misses were "how does this function work inside?" questions; with -Q set to the question it got 5 of those 6. MarkItDown's misses were exact counts in a large table.

Full tables, caveats and how to reproduce them: docs/BENCHMARKS.md.

When not to use it

  • You need every word or every row. Use MarkItDown, which converts faithfully, or -d 5.
  • Scanned PDFs, images, audio, complex PDF tables. Use MarkItDown or Docling (OCR, layout models).
  • Packing a whole repository with exact parsers for every language. Use Repomix --compress or Aider's repo map.

Limitations

The ones that matter most:

  • Code outlines drop function bodies. Use -Q "your question" for the relevant functions, or -d 5 for the whole file.
  • Outlines for languages other than Python are pattern-based, not parsed, and can miss unusual declarations.
  • Data summaries drop most individual values. The per-record index covers a few hundred records; -d 4 gives all the data.
  • No OCR, and PDF tables come out as plain lines of text.
  • -Q is keyword matching, not semantic search.
  • Token counts are estimates; keep 10–20% margin near a hard limit.

Everything else (web page edge cases, encodings, caching, very large folders, ...): docs/LIMITATIONS.md.

Python API

from token_shredder import convert_file, batch_convert

r = convert_file("paper.pdf", max_tokens=4000, query="training setup",
                 write_output=False, show_metrics=False)
r.markdown        # the output
r.output_tokens   # its size (and r.source_tokens: the plain-text size)
r.detail_level    # the level that was used
r.warnings        # notes: truncation, redactions, query matches, ...

batch_convert(folder, max_tokens=..., query=..., write_output=False) returns one result per file. Reference: docs/API.md. There is also an optional HTTP API: pip install "token-shredder[api]", then uvicorn token_shredder.api:app (no authentication; keep it on localhost).

Development

pip install -e ".[dev]"
python -m pytest
python benchmarks/robustness.py <folder>     # run every file through all levels and check the invariants

CHANGELOG.md · CONTRIBUTING.md · MIT licence (LICENSE)

Metadata

Release files for token-shredder 0.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for token-shredder 0.4.1
File Size Uploaded Provenance Actions
token_shredder-0.4.1.tar.gz 116.2 kB Without provenance Details for token_shredder-0.4.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for token-shredder 0.4.1
File Interpreter ABI Platform Actions
token_shredder-0.4.1-py3-none-any.whl Python 3 none any Details for token_shredder-0.4.1-py3-none-any.whl

Total release size: 237.3 kB

Release files / token_shredder-0.4.1.tar.gz

Download URL token_shredder-0.4.1.tar.gz
Size 116.2 kB
Tags Source
SHA-256 checksum
How to use checksums
65d74f4ffd4e9be605c484c25fd2ae284e975172457abc94ca6b33f1cb50db5e
BLAKE2b-256 checksum
How to use checksums
96e885705899d11bc747042734e80d85815fca64d110c0b146a49c3688577a5f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.16

Release files / token_shredder-0.4.1-py3-none-any.whl

Download URL token_shredder-0.4.1-py3-none-any.whl
Size 121.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f72cb90f30eed33baa4c059901d1d7db8af41a9290aa092a54e9e6589af0f66b
BLAKE2b-256 checksum
How to use checksums
b0466a308e0dafd228adc4c22bbf23ad6b83e3ae3ff6a2c185c9552c1d3e4f6b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.16

Security for token-shredder 0.4.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.4.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page