Skip to main content

LLMSlim v0.3.1

LLMSlim is a Python library for extractive, rewrite, and hybrid LLM-context compression. It uses offline TF-IDF and sentence-ranking signals to reduce redundant context while preserving high-value content.

v0.3.1 security hardening: LLMSlim mitigates compression-induced instruction elevation by preventing untrusted RAG/tool/assistant content from gaining protected priority through imperative or safety-critical wording. This is a compression safety boundary, not complete prompt-injection prevention.

What's new in v0.3.1

  • Adds provenance-aware ContextRole priority handling.
  • Caps untrusted rag, tool, and assistant content at Tier 2 and prevents it from becoming must_keep.
  • Protects inline code spans and adds CJK sentence boundaries (, , ).
  • Uses nonce-tagged rewrite fences when a payload contains fence-like text, without changing the payload.
  • Exposes CompressionResult.token_counter_used and warns once when the heuristic token fallback is active.
  • Makes the benchmark runner report observed pytest results instead of hardcoded counts.
  • Removes unused selection helpers.

Verified release gate

Check Result
Python test suite 432 passed, 0 failed
Branch coverage 92.57%
Ruff Passed
Benchmark runner 432 passed, 0 failed; reliability 100/100

The bundled benchmark corpus has 43 samples across its checked-in datasets. Benchmark and latency results depend on input, machine, tokenizer availability, and selected strategy; LLMSlim does not promise a universal latency or retention percentage.

Install

pip install llmslim

# Optional exact OpenAI-compatible token counts
pip install "llmslim[fast-tokens]"

Python 3.8+ is supported.

Quick start

from llmslim import ContextRole, compress

result = compress(
    "Long context goes here...",
    target_ratio=0.5,
    strategy="extractive",
    context_role=ContextRole.GENERAL,
)

print(result.compressed_text)
print(result.original_tokens, result.compressed_tokens)
print(result.token_counter_used)  # "tiktoken" or "heuristic"

LLMSlim supports:

  • extractive (default): local sentence selection.
  • rewrite: provider-backed rewrite with validation.
  • hybrid: extractive pre-compression followed by rewrite.

rewrite and hybrid require a caller-supplied provider.

Provenance and compression safety

ContextRole describes who authored the input:

Role Treatment
system, developer Trusted instructions; protected priority is available.
user Semi-trusted; imperative wording can be high priority but is not safety-tier locked solely by wording.
assistant, tool, rag Untrusted; capped at Tier 2 and never must_keep.
general Legacy default behavior for compress(text).
from llmslim import ContextRole, compress_documents

# Retrieved documents are untrusted by default.
documents = compress_documents(
    ["Retrieved document text..."],
    query="What is the account status?",
    target_ratio=0.4,
)

# Use an explicit label only when the caller knows the provenance.
trusted = compress(
    "Developer-authored instruction...",
    context_role=ContextRole.DEVELOPER,
)

LLMSlim trusts caller-provided labels and cannot authenticate provenance. Untrusted text may still be selected by relevance; it simply cannot gain the protected budget through imperative-looking wording. Applications still need normal defense in depth.

Chat pipeline behavior

from llmslim import compress_chat_messages

messages = [
    {"role": "system", "content": "Return JSON."},
    {"role": "user", "content": "Long request context..."},
    {"role": "assistant", "content": "Long prior response..."},
]

compressed = compress_chat_messages(messages, target_ratio=0.5)

compress_documents() now defaults to RAG provenance. compress_chat_messages() propagates each message role (system, developer, user, assistant, tool); system messages remain uncompressed by default.

CLI

llmslim input.txt --ratio 0.5 --stats
llmslim input.txt --strategy hybrid --ratio 0.5

Development

pip install -e ".[dev]"
pytest tests
pytest --cov=llmslim --cov-branch --cov-report=term-missing
ruff check llmslim benchmarks/benchmark.py tests
python benchmark.py

Availability and roadmap

The released runtime is Python. There is no published @llmslim/core npm package, Rust engine, or WASM runtime in v0.3.1. Those may be explored in future releases but are not current product capabilities.

See CHANGELOG.md, release notes, and Phase 1 release gate for details.

Security reporting

Report vulnerabilities privately according to SECURITY.md.

License

MIT. See LICENSE.

Download files

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

Source Distribution

llmslim-0.3.1.tar.gz (1.2 MB view details)

Uploaded Source

Built Distribution

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

llmslim-0.3.1-py3-none-any.whl (64.5 kB view details)

Uploaded Python 3

File details

Details for the file llmslim-0.3.1.tar.gz.

File metadata

  • Download URL: llmslim-0.3.1.tar.gz
  • Upload date:
  • Size: 1.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for llmslim-0.3.1.tar.gz
Algorithm Hash digest
SHA256 3c0f7623e6411dafb4f25d90ca156cdd40def9c5b6ee4258edc2dfe84e4f3eec
MD5 f92ee6ea79aacb28bb4ee0922b6514f6
BLAKE2b-256 01ef04b7720cc066dac46549778d4dde40c21a97595ea70cd087931fe90fef80

See more details on using hashes here.

Provenance

The following attestation bundles were made for llmslim-0.3.1.tar.gz:

Publisher: release.yml on Thanatos9404/llmslim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file llmslim-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: llmslim-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 64.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for llmslim-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b709f149c67b9d997f0f831ef0542dffef130cace028c3008fcdb975b1d22e5d
MD5 55655b95815770dd4e1960494c0ac15f
BLAKE2b-256 de7aa0e75ebe4bc81ce4800268c9bc3986b721172ecade85b9f318f984809096

See more details on using hashes here.

Provenance

The following attestation bundles were made for llmslim-0.3.1-py3-none-any.whl:

Publisher: release.yml on Thanatos9404/llmslim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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