Skip to main content

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 read + cumulative real agent reads
compact-code install-hook install a Claude Code hook that counts real savings on every agent read (--project for project-level)

Install compact-code[exact] for exact token counts (tiktoken).

The live savings counter

compact-code install-hook    # one-time, hooks into Claude Code
# ... let your agent work ...
compact-code stats
# Agent savings so far: 142 reads of compacted files, 87,310 tokens saved (~$0.26).

The hook fires on every Read Claude Code performs; if the file is in this project's compaction manifest, the per-file saving (prorated for partial reads) is credited to a local ledger. No network, no telemetry — everything stays in .compact-code.json.

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_base as a proxy for your model's tokenizer; ratios are indicative.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

compact_code-0.2.1.tar.gz (54.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

compact_code-0.2.1-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file compact_code-0.2.1.tar.gz.

File metadata

  • Download URL: compact_code-0.2.1.tar.gz
  • Upload date:
  • Size: 54.8 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

Hashes for compact_code-0.2.1.tar.gz
Algorithm Hash digest
SHA256 dbaf64d5b2e83cc2a5f28abf989775d5b948d748ae423d85473fe9f57f979472
MD5 1c8171864f111d1437582538da6f721a
BLAKE2b-256 889f96807b32c108126c252504bb2248a3391c44c612c4fb4ea232b0cba4bbb3

See more details on using hashes here.

File details

Details for the file compact_code-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: compact_code-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 11.5 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

Hashes for compact_code-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3e48115220340243e72589c9167da9a15e75ac02dd092b6065fea150b7ad96d4
MD5 d476eed6aaaae1dfd0ca475a21a325e9
BLAKE2b-256 17f965780b6160c6d22deeb4f4aa1d0f15c5b006cbb2f770f37c85a0a9754724

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page