slimctx 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- slimctx-0.1.0/LICENSE +202 -0
- slimctx-0.1.0/PKG-INFO +151 -0
- slimctx-0.1.0/README.md +134 -0
- slimctx-0.1.0/pyproject.toml +27 -0
- slimctx-0.1.0/setup.cfg +4 -0
- slimctx-0.1.0/slimctx/__init__.py +30 -0
- slimctx-0.1.0/slimctx/code_compressor.py +196 -0
- slimctx-0.1.0/slimctx/json_compressor.py +234 -0
- slimctx-0.1.0/slimctx/log_compressor.py +132 -0
- slimctx-0.1.0/slimctx/mcp_server.py +181 -0
- slimctx-0.1.0/slimctx/pipeline.py +206 -0
- slimctx-0.1.0/slimctx/relevance.py +108 -0
- slimctx-0.1.0/slimctx/router.py +30 -0
- slimctx-0.1.0/slimctx/store.py +189 -0
- slimctx-0.1.0/slimctx/text_compressor.py +66 -0
- slimctx-0.1.0/slimctx/tokens.py +32 -0
- slimctx-0.1.0/slimctx.egg-info/PKG-INFO +151 -0
- slimctx-0.1.0/slimctx.egg-info/SOURCES.txt +21 -0
- slimctx-0.1.0/slimctx.egg-info/dependency_links.txt +1 -0
- slimctx-0.1.0/slimctx.egg-info/entry_points.txt +2 -0
- slimctx-0.1.0/slimctx.egg-info/requires.txt +3 -0
- slimctx-0.1.0/slimctx.egg-info/top_level.txt +1 -0
- slimctx-0.1.0/tests/test_slimctx.py +329 -0
slimctx-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
slimctx-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: slimctx
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Zero-dependency, fully-reversible context compression for AI agents
|
|
5
|
+
Author: Omkar Shukla
|
|
6
|
+
License: Apache-2.0
|
|
7
|
+
Keywords: llm,context,compression,tokens,agents
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.9
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Provides-Extra: dev
|
|
15
|
+
Requires-Dist: pytest>=7; extra == "dev"
|
|
16
|
+
Dynamic: license-file
|
|
17
|
+
|
|
18
|
+
# slimctx — the token optimizer for AI agents
|
|
19
|
+
|
|
20
|
+
<!-- mcp-name: io.github.omkar9854/token_optimizer -->
|
|
21
|
+
|
|
22
|
+
[](https://github.com/omkar9854/token_optimizer/actions/workflows/ci.yml)
|
|
23
|
+
[](LICENSE)
|
|
24
|
+
[](pyproject.toml)
|
|
25
|
+
[](pyproject.toml)
|
|
26
|
+
|
|
27
|
+
**Zero-dependency, fully-reversible context compression for AI agents.**
|
|
28
|
+
|
|
29
|
+
slimctx compresses what your agent reads — tool outputs, logs, JSON, source
|
|
30
|
+
files, prose — before it reaches the LLM. Same answers, fraction of the
|
|
31
|
+
tokens. Pure Python stdlib: no ML models, no downloads, no network calls,
|
|
32
|
+
ever. Auditable end to end in ~1,600 lines.
|
|
33
|
+
|
|
34
|
+
<p align="center">
|
|
35
|
+
<img src="demo.gif" alt="slimctx demo: 61,700 tokens compressed to 298 in 29ms, FATAL lines preserved, byte-exact retrieval" width="820">
|
|
36
|
+
<br><sub>Live output of <code>python3 benchmarks/demo.py</code> — run it yourself, nothing is staged.</sub>
|
|
37
|
+
</p>
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
from slimctx import Pipeline, Config
|
|
41
|
+
|
|
42
|
+
pipe = Pipeline(Config(target_tokens=32_000))
|
|
43
|
+
result = pipe.compress(messages) # OpenAI/Anthropic-style dicts
|
|
44
|
+
print(result.savings_ratio) # e.g. 0.82
|
|
45
|
+
|
|
46
|
+
original = pipe.retrieve("a1b2c3d4...") # byte-exact original, any time
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Results (synthetic workloads modeled on real agent traffic)
|
|
50
|
+
|
|
51
|
+
| Workload | Before | After | Savings | Key facts kept |
|
|
52
|
+
|----------------------------|-------:|-------:|--------:|:--------------:|
|
|
53
|
+
| Code search (100 results) | 5,557 | 916 | **84%** | ✓ |
|
|
54
|
+
| SRE incident debugging | 61,699 | 298 | **100%** | ✓ |
|
|
55
|
+
| GitHub issue triage | 12,836 | 975 | **92%** | ✓ |
|
|
56
|
+
| Codebase exploration | 5,734 | 2,760 | **52%** | ✓ |
|
|
57
|
+
|
|
58
|
+
Every run also verifies that each planted "needle" (the FIXME, the OOMKill,
|
|
59
|
+
the outlier) survives compression, and that every lossy transform is
|
|
60
|
+
byte-exact reversible. Reproduce with `python3 benchmarks/bench.py`.
|
|
61
|
+
|
|
62
|
+
## How it works
|
|
63
|
+
|
|
64
|
+
```
|
|
65
|
+
messages ──► ContentRouter ──► one of:
|
|
66
|
+
├─ JSON : lossless tabularization (repeated keys → header,
|
|
67
|
+
│ constant columns → legend), then relevance-ranked
|
|
68
|
+
│ row selection only if still over budget
|
|
69
|
+
├─ LOG : Drain-style template mining — repeated lines
|
|
70
|
+
│ collapse to `pattern [x1432]`; errors verbatim
|
|
71
|
+
├─ CODE : AST skeleton — signatures + docstrings kept,
|
|
72
|
+
│ bodies elided EXCEPT those relevant to the query
|
|
73
|
+
└─ TEXT : extractive sentence selection (BM25 + salience
|
|
74
|
+
+ position), verbatim, never paraphrased
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### The four guarantees
|
|
78
|
+
|
|
79
|
+
1. **Universal reversibility.** Before *any* lossy transform, the original
|
|
80
|
+
goes into a content-addressed store (memory / SQLite / bring-your-own
|
|
81
|
+
cipher) and the output carries a `[slimctx-ref <hash> ...]` marker. The
|
|
82
|
+
model — or you — can always get the byte-exact original back.
|
|
83
|
+
2. **Errors are never dropped.** Every compressor pins error/warning
|
|
84
|
+
content: log errors pass verbatim, salient JSON rows are kept, salient
|
|
85
|
+
sentences outrank filler.
|
|
86
|
+
3. **Deterministic output.** Same input → byte-identical output, across
|
|
87
|
+
runs and processes. Compressed prefixes stay stable, so provider
|
|
88
|
+
prompt-caches (Anthropic/OpenAI) keep hitting.
|
|
89
|
+
4. **Net gain or no-op.** If a transform doesn't save enough tokens to pay
|
|
90
|
+
for its marker, the original is kept untouched. The live zone (system
|
|
91
|
+
prompt + last N messages) is never modified at all.
|
|
92
|
+
|
|
93
|
+
## Why not just use Headroom?
|
|
94
|
+
|
|
95
|
+
[Headroom](https://github.com/headroomlabs-ai/headroom) is the established
|
|
96
|
+
project in this space and is more featureful today (provider proxy with SSE
|
|
97
|
+
streaming, agent wrappers, cross-agent memory, an ML compression model).
|
|
98
|
+
slimctx makes a different set of trade-offs, aimed at locked-down /
|
|
99
|
+
client-site deployments:
|
|
100
|
+
|
|
101
|
+
| | Headroom | slimctx |
|
|
102
|
+
|---|---|---|
|
|
103
|
+
| Reversibility | JSON only (CCR); dropped text is gone | **every** lossy transform |
|
|
104
|
+
| Log handling | generic text scoring | **template mining** (`[x1432]` collapse) |
|
|
105
|
+
| Code handling | AST skeleton | AST skeleton **+ query-relevant bodies kept** |
|
|
106
|
+
| Dependencies | Rust core, ONNX runtime, 261MB HF model | **stdlib only** |
|
|
107
|
+
| Network egress | HuggingFace pull on first run | **none, ever** |
|
|
108
|
+
| Store encryption | none (plaintext SQLite) | **cipher hook** (bring your own) |
|
|
109
|
+
| Determinism | cache-aligner component | **by construction** (pure functions + memo) |
|
|
110
|
+
| Audit surface | ~10s of KLOC across 3 languages | **~1,200 lines of Python** |
|
|
111
|
+
|
|
112
|
+
If you need the proxy/wrap ecosystem, use Headroom. If you need something
|
|
113
|
+
you can read in an afternoon, run air-gapped, and certify for a client
|
|
114
|
+
environment, use slimctx.
|
|
115
|
+
|
|
116
|
+
## Install / test
|
|
117
|
+
|
|
118
|
+
```bash
|
|
119
|
+
pip install -e . # or just vendor the slimctx/ directory
|
|
120
|
+
python -m pytest tests/ -q # 18 tests: invariants, not examples
|
|
121
|
+
python3 benchmarks/bench.py # reproduce the numbers above
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## Integration sketches
|
|
125
|
+
|
|
126
|
+
**As a library (any framework):** call `pipe.compress(messages)` right
|
|
127
|
+
before your provider SDK call; expose `pipe.retrieve` as a tool named
|
|
128
|
+
`retrieve` so the model can pull originals.
|
|
129
|
+
|
|
130
|
+
**As an MCP server (GitHub Copilot, Claude Code, Cursor, ...):** ships
|
|
131
|
+
built in, stdlib-only:
|
|
132
|
+
|
|
133
|
+
```bash
|
|
134
|
+
python3 -m slimctx.mcp_server --db ~/.slimctx/store.db
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
See [USAGE.md](USAGE.md) for the GitHub Copilot (`.vscode/mcp.json`) setup
|
|
138
|
+
and a security deployment checklist.
|
|
139
|
+
|
|
140
|
+
**Encrypted store:**
|
|
141
|
+
|
|
142
|
+
```python
|
|
143
|
+
from cryptography.fernet import Fernet # optional, your choice
|
|
144
|
+
f = Fernet(key)
|
|
145
|
+
store = SqliteStore("ccr.db", cipher=(f.encrypt, f.decrypt))
|
|
146
|
+
pipe = Pipeline(store=store)
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
## License
|
|
150
|
+
|
|
151
|
+
Apache-2.0. Original implementation — no code derived from Headroom.
|
slimctx-0.1.0/README.md
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
# slimctx — the token optimizer for AI agents
|
|
2
|
+
|
|
3
|
+
<!-- mcp-name: io.github.omkar9854/token_optimizer -->
|
|
4
|
+
|
|
5
|
+
[](https://github.com/omkar9854/token_optimizer/actions/workflows/ci.yml)
|
|
6
|
+
[](LICENSE)
|
|
7
|
+
[](pyproject.toml)
|
|
8
|
+
[](pyproject.toml)
|
|
9
|
+
|
|
10
|
+
**Zero-dependency, fully-reversible context compression for AI agents.**
|
|
11
|
+
|
|
12
|
+
slimctx compresses what your agent reads — tool outputs, logs, JSON, source
|
|
13
|
+
files, prose — before it reaches the LLM. Same answers, fraction of the
|
|
14
|
+
tokens. Pure Python stdlib: no ML models, no downloads, no network calls,
|
|
15
|
+
ever. Auditable end to end in ~1,600 lines.
|
|
16
|
+
|
|
17
|
+
<p align="center">
|
|
18
|
+
<img src="demo.gif" alt="slimctx demo: 61,700 tokens compressed to 298 in 29ms, FATAL lines preserved, byte-exact retrieval" width="820">
|
|
19
|
+
<br><sub>Live output of <code>python3 benchmarks/demo.py</code> — run it yourself, nothing is staged.</sub>
|
|
20
|
+
</p>
|
|
21
|
+
|
|
22
|
+
```python
|
|
23
|
+
from slimctx import Pipeline, Config
|
|
24
|
+
|
|
25
|
+
pipe = Pipeline(Config(target_tokens=32_000))
|
|
26
|
+
result = pipe.compress(messages) # OpenAI/Anthropic-style dicts
|
|
27
|
+
print(result.savings_ratio) # e.g. 0.82
|
|
28
|
+
|
|
29
|
+
original = pipe.retrieve("a1b2c3d4...") # byte-exact original, any time
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Results (synthetic workloads modeled on real agent traffic)
|
|
33
|
+
|
|
34
|
+
| Workload | Before | After | Savings | Key facts kept |
|
|
35
|
+
|----------------------------|-------:|-------:|--------:|:--------------:|
|
|
36
|
+
| Code search (100 results) | 5,557 | 916 | **84%** | ✓ |
|
|
37
|
+
| SRE incident debugging | 61,699 | 298 | **100%** | ✓ |
|
|
38
|
+
| GitHub issue triage | 12,836 | 975 | **92%** | ✓ |
|
|
39
|
+
| Codebase exploration | 5,734 | 2,760 | **52%** | ✓ |
|
|
40
|
+
|
|
41
|
+
Every run also verifies that each planted "needle" (the FIXME, the OOMKill,
|
|
42
|
+
the outlier) survives compression, and that every lossy transform is
|
|
43
|
+
byte-exact reversible. Reproduce with `python3 benchmarks/bench.py`.
|
|
44
|
+
|
|
45
|
+
## How it works
|
|
46
|
+
|
|
47
|
+
```
|
|
48
|
+
messages ──► ContentRouter ──► one of:
|
|
49
|
+
├─ JSON : lossless tabularization (repeated keys → header,
|
|
50
|
+
│ constant columns → legend), then relevance-ranked
|
|
51
|
+
│ row selection only if still over budget
|
|
52
|
+
├─ LOG : Drain-style template mining — repeated lines
|
|
53
|
+
│ collapse to `pattern [x1432]`; errors verbatim
|
|
54
|
+
├─ CODE : AST skeleton — signatures + docstrings kept,
|
|
55
|
+
│ bodies elided EXCEPT those relevant to the query
|
|
56
|
+
└─ TEXT : extractive sentence selection (BM25 + salience
|
|
57
|
+
+ position), verbatim, never paraphrased
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### The four guarantees
|
|
61
|
+
|
|
62
|
+
1. **Universal reversibility.** Before *any* lossy transform, the original
|
|
63
|
+
goes into a content-addressed store (memory / SQLite / bring-your-own
|
|
64
|
+
cipher) and the output carries a `[slimctx-ref <hash> ...]` marker. The
|
|
65
|
+
model — or you — can always get the byte-exact original back.
|
|
66
|
+
2. **Errors are never dropped.** Every compressor pins error/warning
|
|
67
|
+
content: log errors pass verbatim, salient JSON rows are kept, salient
|
|
68
|
+
sentences outrank filler.
|
|
69
|
+
3. **Deterministic output.** Same input → byte-identical output, across
|
|
70
|
+
runs and processes. Compressed prefixes stay stable, so provider
|
|
71
|
+
prompt-caches (Anthropic/OpenAI) keep hitting.
|
|
72
|
+
4. **Net gain or no-op.** If a transform doesn't save enough tokens to pay
|
|
73
|
+
for its marker, the original is kept untouched. The live zone (system
|
|
74
|
+
prompt + last N messages) is never modified at all.
|
|
75
|
+
|
|
76
|
+
## Why not just use Headroom?
|
|
77
|
+
|
|
78
|
+
[Headroom](https://github.com/headroomlabs-ai/headroom) is the established
|
|
79
|
+
project in this space and is more featureful today (provider proxy with SSE
|
|
80
|
+
streaming, agent wrappers, cross-agent memory, an ML compression model).
|
|
81
|
+
slimctx makes a different set of trade-offs, aimed at locked-down /
|
|
82
|
+
client-site deployments:
|
|
83
|
+
|
|
84
|
+
| | Headroom | slimctx |
|
|
85
|
+
|---|---|---|
|
|
86
|
+
| Reversibility | JSON only (CCR); dropped text is gone | **every** lossy transform |
|
|
87
|
+
| Log handling | generic text scoring | **template mining** (`[x1432]` collapse) |
|
|
88
|
+
| Code handling | AST skeleton | AST skeleton **+ query-relevant bodies kept** |
|
|
89
|
+
| Dependencies | Rust core, ONNX runtime, 261MB HF model | **stdlib only** |
|
|
90
|
+
| Network egress | HuggingFace pull on first run | **none, ever** |
|
|
91
|
+
| Store encryption | none (plaintext SQLite) | **cipher hook** (bring your own) |
|
|
92
|
+
| Determinism | cache-aligner component | **by construction** (pure functions + memo) |
|
|
93
|
+
| Audit surface | ~10s of KLOC across 3 languages | **~1,200 lines of Python** |
|
|
94
|
+
|
|
95
|
+
If you need the proxy/wrap ecosystem, use Headroom. If you need something
|
|
96
|
+
you can read in an afternoon, run air-gapped, and certify for a client
|
|
97
|
+
environment, use slimctx.
|
|
98
|
+
|
|
99
|
+
## Install / test
|
|
100
|
+
|
|
101
|
+
```bash
|
|
102
|
+
pip install -e . # or just vendor the slimctx/ directory
|
|
103
|
+
python -m pytest tests/ -q # 18 tests: invariants, not examples
|
|
104
|
+
python3 benchmarks/bench.py # reproduce the numbers above
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## Integration sketches
|
|
108
|
+
|
|
109
|
+
**As a library (any framework):** call `pipe.compress(messages)` right
|
|
110
|
+
before your provider SDK call; expose `pipe.retrieve` as a tool named
|
|
111
|
+
`retrieve` so the model can pull originals.
|
|
112
|
+
|
|
113
|
+
**As an MCP server (GitHub Copilot, Claude Code, Cursor, ...):** ships
|
|
114
|
+
built in, stdlib-only:
|
|
115
|
+
|
|
116
|
+
```bash
|
|
117
|
+
python3 -m slimctx.mcp_server --db ~/.slimctx/store.db
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
See [USAGE.md](USAGE.md) for the GitHub Copilot (`.vscode/mcp.json`) setup
|
|
121
|
+
and a security deployment checklist.
|
|
122
|
+
|
|
123
|
+
**Encrypted store:**
|
|
124
|
+
|
|
125
|
+
```python
|
|
126
|
+
from cryptography.fernet import Fernet # optional, your choice
|
|
127
|
+
f = Fernet(key)
|
|
128
|
+
store = SqliteStore("ccr.db", cipher=(f.encrypt, f.decrypt))
|
|
129
|
+
pipe = Pipeline(store=store)
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
## License
|
|
133
|
+
|
|
134
|
+
Apache-2.0. Original implementation — no code derived from Headroom.
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "slimctx"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Zero-dependency, fully-reversible context compression for AI agents"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
license = { text = "Apache-2.0" }
|
|
12
|
+
authors = [{ name = "Omkar Shukla" }]
|
|
13
|
+
keywords = ["llm", "context", "compression", "tokens", "agents"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Programming Language :: Python :: 3",
|
|
16
|
+
"License :: OSI Approved :: Apache Software License",
|
|
17
|
+
"Operating System :: OS Independent",
|
|
18
|
+
]
|
|
19
|
+
|
|
20
|
+
[project.optional-dependencies]
|
|
21
|
+
dev = ["pytest>=7"]
|
|
22
|
+
|
|
23
|
+
[project.scripts]
|
|
24
|
+
slimctx-mcp = "slimctx.mcp_server:main"
|
|
25
|
+
|
|
26
|
+
[tool.setuptools]
|
|
27
|
+
packages = ["slimctx"]
|
slimctx-0.1.0/setup.cfg
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"""slimctx — a zero-dependency, fully-reversible context compression layer
|
|
2
|
+
for AI agents.
|
|
3
|
+
|
|
4
|
+
from slimctx import Pipeline, Config
|
|
5
|
+
|
|
6
|
+
pipe = Pipeline(Config(target_tokens=32_000))
|
|
7
|
+
result = pipe.compress(messages) # OpenAI/Anthropic style dicts
|
|
8
|
+
result.savings_ratio # e.g. 0.72
|
|
9
|
+
pipe.retrieve("a1b2c3...") # byte-exact original back
|
|
10
|
+
|
|
11
|
+
Guarantees:
|
|
12
|
+
* pure Python stdlib — no models, no downloads, no network, ever
|
|
13
|
+
* every lossy transform stores the original first (always reversible)
|
|
14
|
+
* deterministic + memoized -> stable prefixes -> provider caches hit
|
|
15
|
+
* error/warning content is never dropped by any compressor
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
from .pipeline import Config, Pipeline, Result
|
|
19
|
+
from .store import MemoryStore, SqliteStore
|
|
20
|
+
from .tokens import estimate_tokens
|
|
21
|
+
|
|
22
|
+
__version__ = "0.1.0"
|
|
23
|
+
__all__ = [
|
|
24
|
+
"Pipeline",
|
|
25
|
+
"Config",
|
|
26
|
+
"Result",
|
|
27
|
+
"MemoryStore",
|
|
28
|
+
"SqliteStore",
|
|
29
|
+
"estimate_tokens",
|
|
30
|
+
]
|