ragleap-optimizer
A measure-first layer that reduces the token use of LLM requests. Alpha. Not independently audited: see docs/threat-model.md for exactly what is and is not defended.
The idea: "same quality with fewer tokens" is something one application can measure, not something a library can promise. So every step reports what it changed and whether it can lose information, and evaluate compares quality before and after on your own cases, including where it gets worse. Design: issue #1164.
What is in 0.1.0
Optimizer: runs steps over a request (a dict with a list ofmessages, and optionallytools,systemand other fields) and returns the new request plus aReportwith the tokens before and after, per step. A step whose result is longer than its input is discarded. The input is never changed.- Steps (
ragleap_optimizer.steps):dedupe_context(): removes a long message that is an exact copy of a later one (system messages and short messages are never touched). Nothing is lost.minify_whitespace(): three or more newlines become two; a message that is entirely JSON is written compactly. Only spacing changes.stable_prefix(): system messages first and tool JSON in sorted key order, so a provider's prompt cache can recognise the start of the request.trim_tool_output(max_tokens): keeps the start and end of an oversized tool message with an explicit marker. This loses the middle, so the report sayslossy=Truewhenever it cuts.
ExactCache: an in-memory cache that returns an answer only for the same scope and the same request (model, messages, tools and every other field are part of the key). Needs aScopefrom ragleap-security; entries expire; erase per scope.retryandwith_fallback: bounded retries with doubling waits and a deadline, and trying several callables in order.evaluate: runs your cases with and without the optimizer, scores both answers with your judge, and reports input tokens saved, the quality change with a 95% interval, and every case that got worse. With fewer than 30 cases it gives no savings figure.
Not in 0.1.0: model routing, prompt compression, a semantic cache, secret redaction and framework adapters (planned, see #1164).
Example
from ragleap_optimizer import Optimizer, ExactCache, evaluate, steps
from ragleap_security import Scope
opt = Optimizer([steps.dedupe_context(), steps.minify_whitespace(), steps.trim_tool_output(max_tokens=800)])
out = opt.run(request)
print(out.report.tokens_before, out.report.tokens_after, out.report.lossy)
cache = ExactCache(ttl_seconds=3600)
scope = Scope("acme", subject="alice")
hit, answer = cache.get(scope, out.request)
if not hit:
answer = my_llm_call(out.request)
cache.put(scope, out.request, answer)
table = evaluate(cases, run=my_llm_call, optimizer=opt, judge=my_judge)
print(table.summary())
Limits you should know
- Token counts are estimates (one token per 3 UTF-8 bytes, which over-counts English). Pass
counter=to theOptimizerto use a real tokenizer.evaluatecounts input tokens only, not answers. - The cache lives in one process and stores whatever you put in it. It does not know whether an answer was good: a wrong answer is served until it expires. Use a short
ttl_secondsfor anything that changes, and never cache answers that depend on the current time or on private context that is not in the request. evaluateis only as good as your judge and your cases. A small or unrepresentative test set can hide a regression.- No speed numbers have been measured yet.
Verifying it yourself
pip install -e ../ragleap-security -e ".[test]"
pytest
python scripts/mutation_check.py # breaks one protection at a time; every mutation must be killed
Metadata
Release files for ragleap-optimizer 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 | Provenance | Actions |
|---|---|---|---|---|
| ragleap_optimizer-0.1.0.tar.gz | 25.0 kB | With provenance | Details for ragleap_optimizer-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ragleap_optimizer-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for ragleap_optimizer-0.1.0-py3-none-any.whl |
Total release size: 38.8 kB
Release files / ragleap_optimizer-0.1.0.tar.gz
| Download URL | ragleap_optimizer-0.1.0.tar.gz |
|---|---|
| Size | 25.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d3aeb9710de70dc6dfb47bffdebe7848f7ac9412f033cf2ebda3989d24a6240f
|
|
BLAKE2b-256 checksum How to use checksums |
23f430ee49d1f4b1e2306a71dec1d092d18f50d9c95b4f9f850954d9f69f6a87
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 5, 2026.
Transparency logRelease files / ragleap_optimizer-0.1.0-py3-none-any.whl
| Download URL | ragleap_optimizer-0.1.0-py3-none-any.whl |
|---|---|
| Size | 13.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a96f4b632d63c483e1ecd1389e86d616e3a390d4dbdf6f228db35579ee042c6e
|
|
BLAKE2b-256 checksum How to use checksums |
947ac2f65eb2de9c8bd7dfc347beb990559ce1a530a4ae474fd04c746a641627
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 5, 2026.
Transparency logSecurity for ragleap-optimizer 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.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for ragleap_optimizer-0.1.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 5, 2026.
Transparency logA 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.