Skip to main content

Token-saving abbreviation compressor for OpenAI LLM prompts

Project description

abbrcompress

Token-saving abbreviation compression for OpenAI-compatible LLM prompts.

abbrcompress identifies frequently repeated phrases in a prompt, replaces them with short abbreviations, and decodes the model response back to the original text — helping reduce token usage for long or repetitive prompts. Testing on real datasets shows reductions of 1-2% of tokens sending to OpenAI.

What this library does

✅ Detects repeated n-grams

✅ Computes net token savings using tiktoken

✅ Applies abbreviations only when savings are positive

✅ Decodes model responses safely

❌ Does not modify OpenAI tokenization

❌ Does not guarantee savings for short prompts


Install

pip install abbrcompress
# or
uv pip install abbrcompress

Usage

from abbrcompress.core import build_abbreviations, compress_prompt, decode_abbreviations, token_len

prompt = """your prompt text here"""

# Build abbreviation dictionary
abbr_dict, _ = build_abbreviations(prompt)

# Compress prompt
compressed_prompt = compress_prompt(prompt, abbr_dict)

# TODO: send compressed_prompt to OpenAI and get return_message as response
return_message = _  # Placeholder for OpenAI response

# Decode return from OpenAI
decoded_text = decode_abbreviations(return_message, abbr_dict)

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

abbrcompress-0.1.4.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

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

abbrcompress-0.1.4-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file abbrcompress-0.1.4.tar.gz.

File metadata

  • Download URL: abbrcompress-0.1.4.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for abbrcompress-0.1.4.tar.gz
Algorithm Hash digest
SHA256 1b144b2bafef86b7ffcd2f1fbc0c5bd199c55b42679c2d45a2366d21f4153fb7
MD5 5d4182465ae20727b3c90ed0b1192dfe
BLAKE2b-256 42818cb6351a406994af33792370f46d0f3f9b41ba0fbc012cef319e039c73ad

See more details on using hashes here.

File details

Details for the file abbrcompress-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: abbrcompress-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for abbrcompress-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 068221c327be8db7ab357e4aa6a03c1a3a219846b0facc4ab254a0811e549e33
MD5 4b8d6c650dbc7b8cc70dee772a2aabbc
BLAKE2b-256 d2afdd4d9b3c1b8e23bdacd8f3bf14e10990ae2a6da6346564e0b8d413db2d4b

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