npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@codragraph/compress

v2.1.0

Published

Lossless semantic compression for LLM contexts — strip grammar, keep facts, save tokens.

Readme

Codragraph Compression

Lossless semantic compression for LLM contexts

License: MIT Python 3.8+

Quick StartExamplesBenchmarksSpec


What is this?

Strip grammar. Keep facts. Save tokens.

- "In order to optimize the database query performance, we should consider implementing an index on the frequently accessed columns..." (70 tokens)
+ "Need fast queries. Check which columns used most. Add index to those columns..." (50 tokens)

= 29% reduction

How It Works

How Codragraph Compression Works

LLMs excel at filling linguistic gaps. They predict missing grammar, connectives, and structure.

Key insight: We remove only what LLMs can reliably reconstruct.

What we remove (predictable):

  • Grammar: "a", "the", "is", "are"
  • Connectives: "therefore", "however", "because"
  • Passive constructions: "is calculated by"
  • Filler words: "very", "quite", "essentially"

What we keep (unpredictable):

  • Facts: numbers, names, dates
  • Technical terms: "O(log n)", "binary search"
  • Constraints: "medium-large", "frequently accessed"
  • Specifics: "Stockholm", "99.9% uptime"
Compressed: "Company medium-large. Location Stockholm."
Decompressed: "at a medium-large company based in Stockholm"
           ↑ grammar added, facts unchanged ↑

Quick Start

Installation

LLM-based (best compression, requires OpenAI API):

python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt

# Set up API key
cp .env.example .env
# Edit .env and add your OpenAI API key

NLP-based (free, offline, multilingual):

python3 -m venv venv
source venv/bin/activate
pip install -r requirements-nlp.txt
python -m spacy download en_core_web_sm  # or other language models

MLM-based (free, offline, predictability-aware):

python3 -m venv venv
source venv/bin/activate
pip install -r requirements-mlm.txt
python -m spacy download en_core_web_sm

Usage

NLP-based compression (most stable, 15-30% reduction, free, offline):

python nlp.py compress "Your verbose text here"
python nlp.py compress -f input.txt -o output.txt
python nlp.py compress -f input.txt -l es  # specify language

MLM-based compression (20-30% reduction, free, offline, predictability-aware):

python mlm.py compress "Your verbose text here"
python mlm.py compress -f input.txt -o output.txt
python mlm.py compress -f input.txt -k 30  # adjust compression level

LLM-based compression (40-58% reduction, requires API key):

python codragraph_compress.py compress "Your verbose text here"
python codragraph_compress.py compress -f input.txt -o output.txt

Decompress:

python codragraph_compress.py decompress "Codragraph text here"

Examples

Resume

I am John Smith, a 32-year-old Senior Software Engineer at a large enterprise software company based in San Francisco, California. I have over 8 years of experience in backend development, distributed systems, and database optimization. Throughout my career, I have successfully designed and implemented scalable microservices...

John Smith. 32 years old. Senior Software Engineer. Large enterprise software company. San Francisco, California. 8 years experience. Backend development, distributed systems, database optimization. Designed scalable microservices. 50 million requests daily...

System Prompt

You are a helpful AI assistant designed to provide accurate and concise responses to user queries. When answering questions, you should always prioritize clarity and correctness over speed. If you are uncertain about any information, you must explicitly state your uncertainty...

Helpful AI assistant. Provide accurate, concise responses. Prioritize clarity, correctness. If uncertain, state uncertainty. Break complex problems into smaller steps. Explain reasoning clearly...

API Documentation

To authenticate with our API, you need to include your API key in the Authorization header of every request. The API key should be prefixed with the word "Bearer" followed by a space. If authentication fails, the server will return a 401 Unauthorized status code...

Authenticate API. Include API key in Authorization header every request. Prefix API key with "Bearer" space. Authentication fail, server return 401 Unauthorized status code, error message explain fail...


Benchmarks

Factual Preservation

Automated benchmark verifying that specific facts are preserved and retrievable after compression:

# LLM-based compression
python benchmark/factual_preservation/llm.py

# NLP-based compression
python benchmark/factual_preservation/nlp.py

Results: 13/13 facts preserved (100%) with 12-25% compression ratio.

See benchmark/factual_preservation/ for details.

Example Reductions

| Test Case | Original | Compressed | Reduction | |-----------|----------|------------|-----------| | System prompt | 171 tokens | 72 tokens | 58% | | API documentation | 137 tokens | 79 tokens | 42% | | Resume | 201 tokens | 156 tokens | 22% | | Average | 170 | 102 | 40% |

All examples validated with GPT-4o. See examples/ for full text.


Core Principles

  1. Strip connectives - Remove: therefore, however, because, in order to
  2. 2-5 words per sentence - One atomic thought per sentence
  3. Action verbs - Prefer: do, make, fix, check vs facilitate, optimize
  4. Be concrete - "test five, test six" not "test values 5-6"
  5. Active voice - "calculate value" not "value is calculated"
  6. Keep meaningful info - Numbers, sizes, names, constraints stay

See SPEC.md for full rules.


Use Cases

RAG Knowledge Base (199→118 tokens, 41%)

Original:

A network router is a device that forwards data packets between computer networks. Routers perform the traffic directing functions on the Internet. When a data packet arrives at a router, the router examines the destination IP address...

Compressed:

Network router forwards data packets. Routers direct Internet traffic. Packet arrives router. Router examines destination IP address. Router determines best path. Router uses routing table...

Why it works: Store compressed docs in vector DB. Agent receives compressed RAG results directly. No decompression needed—agent understands codragraph format. Fits 2-3x more context.

Agent Internal Reasoning (196→102 tokens, 48%)

Original:

First, I need to understand what the user is asking for. They want to calculate the optimal route between two cities considering both distance and traffic conditions. Let me break this down into steps. Step one: I should identify the starting city...

Compressed:

Need understand user request. User wants optimal route between cities. Consider distance, traffic. Step one: Identify starting city, destination city. Step two: Retrieve current traffic data for routes...

Why it works: Agent thinks in codragraph format during problem-solving. Chain-of-thought uses 50% fewer tokens. More reasoning steps fit in context window.


Compression Methods

LLM-based (codragraph_compress.py)

  • Reduction: 40-58%
  • Cost: Requires OpenAI API key
  • Quality: Best compression, context-aware
  • Speed: ~2s per request
  • Use when: Maximum token savings needed

MLM-based (mlm.py)

  • Reduction: 20-30%
  • Cost: Free
  • Quality: Excellent compression, predictability-aware using RoBERTa
  • Speed: ~1-5s per document (local model)
  • Method: Removes top-k most predictable tokens based on masked language model probabilities
  • Use when: Need better compression than NLP without API costs, can tolerate model download (~500MB) and initial loading time

NLP-based (nlp.py)

  • Reduction: 15-30%
  • Cost: Free
  • Quality: Good compression, rule-based
  • Speed: <100ms
  • Languages: 15+ supported (en, es, de, fr, it, pt, nl, el, nb, lt, ja, zh, pl, ro, ru, and more)
  • Use when: Working offline, processing large volumes, no API budget, or need multilingual support

When to Use

Good for:

  • LLM reasoning/thinking blocks
  • Token-constrained contexts
  • Internal documentation
  • Step-by-step instructions

Avoid for:

  • User-facing content
  • Marketing copy
  • Legal documents
  • Emotional communication

Documentation


Contributing

Contributions welcome. Submit issues or PRs.


License

MIT


Author

William Peltomäki


Inspired by TOON and the token-optimization movement.