ContextRay
Deterministic context optimization for LLMs.
Reduce token usage, remove redundancy, and improve reasoning quality — without breaking your prompts.
🚨 The Problem
LLM-based systems (agents, chatbots, workflows) accumulate massive context over time.
This leads to:
- 💸 High token costs
- 🐢 Slower responses
- 🧠 Worse reasoning (needle-in-a-haystack problem)
Most solutions try to "summarize" or "compress" context using ML.
That breaks:
- JSON
- code blocks
- structured prompts
✅ The Solution
ContextRay is a deterministic context optimizer.
It does NOT summarize.
It does NOT hallucinate.
It simply:
- removes duplicate content
- preserves structure
- keeps everything byte-safe
🔥 What It Does
Given a large LLM context, ContextRay:
- detects repeated chunks
- removes safe duplicates
- preserves critical instructions
- protects code blocks and structured data
- shows you exactly where tokens are being wasted
⚡ Example
Input
[
{"role": "user", "content": "Explain recursion"},
{"role": "assistant", "content": "Recursion is..."},
{"role": "assistant", "content": "Recursion is..."}
]
A ready-to-run copy lives in examples/sample_chat.json.
Run
contextray optimize input.json
Output
✔ ContextRay Optimization Complete
=== CONTEXTRAY OPTIMIZATION REPORT ===
📊 IMPACT
Original: 1200 chars
Optimized: 700 chars
Saved: 500 chars (42%)
🔥 TOP WASTE
- Hash a1b2c3d4e5f6... repeated 2 times (450 chars wasted)
🛡️ SAFETY
✓ System messages preserved
✓ Cross-role duplicates not removed
✓ Code blocks protected
✓ Small chunks skipped
-------------------------------------
Output saved to: input_optimized.json
🛡️ Safety Guarantees
- System messages are preserved
- Code blocks are protected
- Cross-role duplicates are NOT removed
- Small chunks are ignored
- No ML, no hallucination risk
📦 Installation
pip install .
or from PyPI once published:
pip install contextray
🚀 Usage
CLI
contextray optimize input.json
Optional:
--output out.json # custom output path (default: <input>_optimized<ext>)
--stdout # print JSON to stdout instead of writing a file
Python API
from contextray import optimize_context
result = optimize_context(messages)
print(result["optimized_context"])
print(result["report"])
📈 Output Structure
{
"optimized_context": [{"role": "...", "content": "..."}],
"metrics": {"total_chars_in": 0, "total_chars_out": 0, "chars_saved": 0, "reduction_percentage": 0.0},
"top_waste_blocks": [],
"report": "..."
}
⚠️ Limitations (V1)
- Only exact duplicate detection
- Basic code block detection
- Token estimation is approximate
📜 License
MIT License
Metadata
Release files for contextray 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| contextray-0.1.0.tar.gz | 20.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| contextray-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 31.8 kB
Release files / contextray-0.1.0.tar.gz
| Download URL | contextray-0.1.0.tar.gz |
|---|---|
| Size | 20.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
52780ae7b3f86c3157d44549789932611042c09a88151523fc654a0b0dbfd0cc
|
|
BLAKE2b-256 checksum How to use checksums |
a5cfcf41fed9875ee481d56a014dfeaef0d9816e75a88fa7a549c64ed8ae7635
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.5
|
Release files / contextray-0.1.0-py3-none-any.whl
| Download URL | contextray-0.1.0-py3-none-any.whl |
|---|---|
| Size | 11.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7276f0daf19725b6872a76a6bad84c6edbd071826babd933ad730e4323f22229
|
|
BLAKE2b-256 checksum How to use checksums |
99d7d156434a810bd6ddffe29a609e6b82d478976aebe7c5657cc0e12039ede9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.5
|