Dense Python for AI agents, readable view for humans. Same logic, ~30-55% fewer tokens.
Project description
compact-code
Dense Python for AI agents, readable view for humans. Same logic, fewer tokens.
AI coding agents (Claude Code, Cursor, Codex...) read your code far more often than they write it. Every Read, every context refill re-pays the token weight of your code. But your code is written for humans: comments, docstrings, type annotations, blank lines, 4-space indentation — none of which the agent needs to understand the logic.
compact-code removes all of it, provably without changing the program (the AST is preserved), and gives you back a readable view on demand. Think minified JS + source maps, applied to the AI workflow.
pip install compact-code
cd your-project
compact-code compact # 3 seconds, AST-safe, reversible via git
pytest # your tests are the safety net
compact-code stats # see what you now save on every read
What it does
- removes comments, docstrings and type annotations (keeps functional class-field annotations: dataclasses, NamedTuple, pydantic)
- strips blank lines, re-indents to 1 space
- never touches string contents, public names, parameters or logic
- verifies AST equivalence on every file before writing — if the check fails, the file is skipped
Measured results
Benchmarked with twin-codebase experiments (identical prompts, blind agents, real test suites as judges — full methodology in the repo):
| Scenario | Session-token savings |
|---|---|
| Surgical one-file fix | ~0% (agent barely reads code) |
| Maintenance on a fully compacted codebase | -31% |
| Codebase exploration / audit (replicated n=3) | -56% |
Quality: across 16 paired sessions, agents on compacted code passed exactly the same test suites and evals as agents on normal code. Zero measured loss.
Codebase density gain: -19% to -41% tokens depending on the project (less if docstrings must be kept).
Commands
| Command | What it does |
|---|---|
compact-code compact [paths] |
compact **/*.py in place (skips .venv, .git, etc.) |
compact-code compact --check |
dry run: report savings without writing |
compact-code compact --keep-docstrings |
keep docstrings (use when they are functional: CLI help text, doctests, flit) |
compact-code expand <file> |
print a readable 4-space-indented view (--write to rewrite in place) |
compact-code stats |
tokens and $ saved per full codebase read |
Install compact-code[exact] for exact token counts (tiktoken).
The honest fine print
- The gain is proportional to how much code your agent actually reads. Exploration-heavy and read-heavy sessions save the most; one-file quick fixes save nothing.
- Some docstrings are functional (click help text, doctests, flit metadata). Always run your test suite after compacting; on failure, restore via git and re-run with
--keep-docstrings. - Token counts use tiktoken
o200k_baseas a proxy for your model's tokenizer; ratios are indicative.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file compact_code-0.1.0.tar.gz.
File metadata
- Download URL: compact_code-0.1.0.tar.gz
- Upload date:
- Size: 52.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6c36b458a273463e5f7851cf325fdffa2d144843ac9a9574333791c2c85e857d
|
|
| MD5 |
ce331fff3616e523274279b2bf43cf3b
|
|
| BLAKE2b-256 |
3873bddbf1506b898318422f840d8dbc8e29276290e9efb95f57444affdda124
|
File details
Details for the file compact_code-0.1.0-py3-none-any.whl.
File metadata
- Download URL: compact_code-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9369e0513f75b332a4c7de96342ed031860d30095c1264d607f8add247728774
|
|
| MD5 |
11ee80d4f546c3de8ad0005341110f62
|
|
| BLAKE2b-256 |
4b4fe26fa97e22d05f7022981038659f74c53c1d87a7294abe283c1cf5b16faf
|