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.0.tar.gz (54.6 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.0-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: compact_code-0.2.0.tar.gz
  • Upload date:
  • Size: 54.6 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.0.tar.gz
Algorithm Hash digest
SHA256 4fdf4c21923998aab1de321ebe612cc6421192667a06b6603b11e154aadc6704
MD5 201d0494546c6ac803022b82afeedcec
BLAKE2b-256 d166716295be09204018eda337495a261d706b829c3dbce1a3cc921de964fe87

See more details on using hashes here.

File details

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

File metadata

  • Download URL: compact_code-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 11.4 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 712265c2d973940ef393fd1cbbedbbb85af8d591c750d6060d6b51baac829ed3
MD5 9986330dc76651cc54addc9ec08aa38d
BLAKE2b-256 79a0ab9429bf6294605154664f4b14ce2f0b4fd18e905cdded2d3a46e9e064de

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