Skip to main content

Invaria — Universal LLM Context Compiler

Compress less. Preserve more. Prove everything.

PyPI version Python versions License: MIT

Invaria is a deterministic, rule-based context compiler for Large Language Models.

Instead of blindly dropping tokens based on probabilistic attention scores (which often leads to hallucinated numbers, broken URLs, or lost named entities), Invaria treats your prompt as source code. It compiles the context into an intermediate representation (IR), generates compression candidates, verifies that strict preservation contracts are met, and emits the cheapest safe representation.


Why Invaria?

Most prompt compressors use smaller ML models to guess which words are "unimportant." This is dangerous for enterprise use cases where missing a "NOT", dropping a crucial ID, or altering a currency amount is catastrophic.

Invaria introduces a Verification Engine. It mathematically guarantees that:

  • ✅ Numerical Invariants (e.g., $5000, 99%, 0.001) are preserved.
  • ✅ Lexical Entities (Proper nouns, CamelCase, Acronyms) are untouched.
  • ✅ Structured Identifiers (URLs, Email addresses, API Keys, UUIDs) are safely retained.
  • ✅ Formatting (Markdown tables, JSON structures, code blocks) remains syntactically valid.

If a compressed candidate drops a required invariant, Invaria rejects it.


Installation

Install via pip. Invaria v0.1.0 is extremely lightweight and relies primarily on pydantic.

pip install invaria

Note: Optional dependencies for ML-based semantic compression (openai, anthropic, google) will be utilized in upcoming releases.


Python API Quickstart

Invaria is designed to be easily integrated into your existing LLM pipelines, RAG systems, or agent frameworks.

from invaria import Invaria

# Initialize the compiler
compiler = Invaria(mode="invariant")

text = """
User Alice transferred $5,000 to Bob on Monday... 
Please do not delete the backup files!!! They are stored in Amazon S3.
"""

# Compress the context
result = compiler.compress(text)

# The compressed text
print(result.text)
# Output: "User Alice transferred $5,000 to Bob on Monday. Please do not delete the backup files! They are stored in Amazon S3."

# Access detailed metrics
print(f"Original Tokens:   {result.original_tokens}")
print(f"Compressed Tokens: {result.compressed_tokens}")
print(f"Token Reduction:   {result.token_reduction}")
print(f"Compression Ratio: {result.compression_ratio:.2f}x")

# Cryptographic proof of preservation
print(f"Verified Safe:     {result.proof.verified}")
print(f"Preserved Entities:{result.proof.preserved}")

Inspecting Extracted Invariants

You can also use Invaria just to parse and inspect the semantic constraints within a prompt without compressing it:

ir = compiler.compile("Contact Support at help@example.com if Stripe webhooks fail.")

for node_id, node in ir.nodes.items():
    print(f"Type: {node.type} | Value: {node.value}")
# Type: entity | Value: Contact
# Type: entity | Value: Support
# Type: entity | Value: help@example.com
# Type: entity | Value: Stripe

CLI Usage

Invaria comes with a built-in Command Line Interface for quickly compressing files or verifying external compressions in your CI/CD pipelines.

Compress a File

invaria compress prompt.txt --mode invariant

Outputs the compression metrics, the cryptographic proof status, and the compressed text.

Verify a Compression

If you have an original context and a compressed version, Invaria can verify if the compressed version safely retained all necessary invariants from the original:

invaria verify original.txt compressed.txt

Outputs Verification Passed: True or False.


Compiler Architecture

Invaria operates similarly to a traditional programming language compiler:

graph LR
    A[Text Input] --> B(COMPILE)
    B -->|Semantic IR| C(OPTIMIZE)
    C -->|Generate Candidates| D(VERIFY)
    D -->|Check Constraints| E(EMIT)
    E --> F[Verified Compressed Text]
  1. Compile (Extract Invariants): Scans the text to build a SemanticGraph (IR) identifying all Entities, Numerical Invariants, Dates, and Instructions.
  2. Optimize (Generate Candidates): Applies active compressors (like LexicalCompressor for whitespace/punctuation normalization).
  3. Verify (Preservation Contract): The VerificationEngine checks the candidate string against the Semantic IR to ensure zero critical tokens were lost or mutated.
  4. Emit: Computes SHA-256 hashes for the proof, calculates token reductions, and returns a CompressionResult.

Roadmap

  • v0.1.x (Current): Deterministic Lexical Compression, Entity/Numerical Invariant Extraction, Core Verification Engine, CLI.
  • v0.2.x: Syntactic Compression (safe removal of filler phrases, stop words) and Tiktoken integration for exact token pricing.
  • v0.3.x: Abstractive Semantic Compression using LLM providers (openai, anthropic, google) backed by Invaria's deterministic VerificationEngine to prevent LLM hallucinations during summarization.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Copyright (c) 2026 Supratim Ghosh & Invaria Contributors.

Metadata

Release files for invaria 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for invaria 0.1.0
File Size Uploaded Provenance
invaria-0.1.0.tar.gz 10.1 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for invaria 0.1.0
File Interpreter ABI Platform
invaria-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 23.9 kB

Release files / invaria-0.1.0.tar.gz

Download URL invaria-0.1.0.tar.gz
Size 10.1 kB
Tags Source
SHA-256 checksum
How to use checksums
c072a0a8049298c3c534d29d62b7fa9a0e2e437583df72f4f25794b49503ad54
BLAKE2b-256 checksum
How to use checksums
ee4419677f9874a4724eb8c33f9e7d0fb19cc763200d3610b2464d709ff428f3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.3 {"installer":{"name":"uv","version":"0.13.3","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}

Release files / invaria-0.1.0-py3-none-any.whl

Download URL invaria-0.1.0-py3-none-any.whl
Size 13.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2f80eb9ac3bc758963c61a0c306abdd4603c36c50de77ac291957776e61a0126
BLAKE2b-256 checksum
How to use checksums
18b21b8f1d5e7279c44227fc2238909ec906825c5fbcd75b37905356a79ddafa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.3 {"installer":{"name":"uv","version":"0.13.3","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}

Security for invaria 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page