agentslim 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.
- agentslim-0.1.0/CONTRIBUTING.md +68 -0
- agentslim-0.1.0/LICENSE +21 -0
- agentslim-0.1.0/PKG-INFO +300 -0
- agentslim-0.1.0/README.md +267 -0
- agentslim-0.1.0/agentslim/__init__.py +27 -0
- agentslim-0.1.0/agentslim/code.py +307 -0
- agentslim-0.1.0/agentslim/compressor.py +242 -0
- agentslim-0.1.0/agentslim/memory.py +201 -0
- agentslim-0.1.0/agentslim/tools.py +176 -0
- agentslim-0.1.0/agentslim/utils.py +130 -0
- agentslim-0.1.0/examples/01_compressor.py +80 -0
- agentslim-0.1.0/examples/02_memory.py +79 -0
- agentslim-0.1.0/examples/03_tools.py +113 -0
- agentslim-0.1.0/examples/04_code_context.py +132 -0
- agentslim-0.1.0/pyproject.toml +63 -0
- agentslim-0.1.0/tests/__init__.py +1 -0
- agentslim-0.1.0/tests/test_compressor.py +105 -0
- agentslim-0.1.0/tests/test_memory.py +104 -0
- agentslim-0.1.0/tests/test_tools_and_utils.py +127 -0
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# Contributing to agentslim
|
|
2
|
+
|
|
3
|
+
Thank you for your interest in contributing! Here's how to get started.
|
|
4
|
+
|
|
5
|
+
## Setup
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
git clone https://github.com/yourusername/agentslim.git
|
|
9
|
+
cd agentslim
|
|
10
|
+
pip install -e ".[dev]"
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Running Tests
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pytest
|
|
17
|
+
# With coverage:
|
|
18
|
+
pytest --cov=agentslim --cov-report=term-missing
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Project Structure
|
|
22
|
+
|
|
23
|
+
```
|
|
24
|
+
agentslim/
|
|
25
|
+
├── agentslim/
|
|
26
|
+
│ ├── __init__.py # Public API exports
|
|
27
|
+
│ ├── compressor.py # Text / HTML / JSON compressor
|
|
28
|
+
│ ├── memory.py # Smart context window manager
|
|
29
|
+
│ ├── tools.py # Tool schema minifier
|
|
30
|
+
│ ├── code.py # Code-aware context extractor
|
|
31
|
+
│ └── utils.py # Token counting & cost estimation
|
|
32
|
+
├── examples/ # Runnable demos
|
|
33
|
+
├── tests/ # pytest test suite
|
|
34
|
+
├── pyproject.toml
|
|
35
|
+
└── README.md
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Code Style
|
|
39
|
+
|
|
40
|
+
- Follow PEP 8
|
|
41
|
+
- Use type hints everywhere
|
|
42
|
+
- Every public function must have a docstring with an `Example::` block
|
|
43
|
+
- Keep zero mandatory dependencies (optional extras only)
|
|
44
|
+
|
|
45
|
+
## Adding a New Feature
|
|
46
|
+
|
|
47
|
+
1. Add your code to the relevant module (or create a new one)
|
|
48
|
+
2. Export it in `agentslim/__init__.py`
|
|
49
|
+
3. Write tests in `tests/`
|
|
50
|
+
4. Add an example in `examples/`
|
|
51
|
+
5. Update `README.md`
|
|
52
|
+
|
|
53
|
+
## Publishing (maintainers)
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
pip install hatch
|
|
57
|
+
hatch build
|
|
58
|
+
hatch publish
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Ideas & Roadmap
|
|
62
|
+
|
|
63
|
+
- [ ] Async `AgentMemory` for async LLM summarizers
|
|
64
|
+
- [ ] LangChain `ChatMessageHistory` adapter
|
|
65
|
+
- [ ] Streaming compressor (compress tokens as they arrive)
|
|
66
|
+
- [ ] YAML ↔ JSON converter for more compact tool schemas
|
|
67
|
+
- [ ] CLI tool: `agentslim compress file.html`
|
|
68
|
+
- [ ] Integration tests with real OpenAI API (optional CI step)
|
agentslim-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 agentslim contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
agentslim-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: agentslim
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Make your AI agents leaner, faster, and cheaper — smart context management and token compression
|
|
5
|
+
Project-URL: Homepage, https://github.com/WastedSwl/agentslim
|
|
6
|
+
Project-URL: Documentation, https://github.com/WastedSwl/agentslim#readme
|
|
7
|
+
Project-URL: Issues, https://github.com/WastedSwl/agentslim/issues
|
|
8
|
+
Author: agentslim contributors
|
|
9
|
+
License: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: agents,ai,compression,context,langchain,llm,openai,tokens
|
|
12
|
+
Classifier: Development Status :: 3 - Alpha
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
21
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
22
|
+
Requires-Python: >=3.9
|
|
23
|
+
Provides-Extra: all
|
|
24
|
+
Requires-Dist: tiktoken>=0.5; extra == 'all'
|
|
25
|
+
Provides-Extra: dev
|
|
26
|
+
Requires-Dist: hatch; extra == 'dev'
|
|
27
|
+
Requires-Dist: pytest-cov; extra == 'dev'
|
|
28
|
+
Requires-Dist: pytest>=7.0; extra == 'dev'
|
|
29
|
+
Requires-Dist: tiktoken>=0.5; extra == 'dev'
|
|
30
|
+
Provides-Extra: tiktoken
|
|
31
|
+
Requires-Dist: tiktoken>=0.5; extra == 'tiktoken'
|
|
32
|
+
Description-Content-Type: text/markdown
|
|
33
|
+
|
|
34
|
+
# agentslim 🪶
|
|
35
|
+
|
|
36
|
+
**Make your AI agents leaner, faster, and cheaper.**
|
|
37
|
+
|
|
38
|
+
`agentslim` is a zero-dependency Python toolkit that reduces token consumption in LLM-powered agents — without sacrificing reasoning quality.
|
|
39
|
+
|
|
40
|
+
[](https://pypi.org/project/agentslim/)
|
|
41
|
+
[](https://pypi.org/project/agentslim/)
|
|
42
|
+
[](LICENSE)
|
|
43
|
+
|
|
44
|
+
---
|
|
45
|
+
|
|
46
|
+
## Why?
|
|
47
|
+
|
|
48
|
+
Every token counts — literally. When building agents you routinely waste tokens on:
|
|
49
|
+
|
|
50
|
+
| Problem | Typical waste |
|
|
51
|
+
|---|---|
|
|
52
|
+
| Verbose JSON tool schemas | 200–800 tokens per request |
|
|
53
|
+
| Raw HTML web scrapes fed to the LLM | 60–80% noise |
|
|
54
|
+
| Naively truncated chat history | Lost context, broken reasoning |
|
|
55
|
+
| Sending entire source files to coding agents | 10× more than needed |
|
|
56
|
+
|
|
57
|
+
`agentslim` solves all four with one clean API.
|
|
58
|
+
|
|
59
|
+
---
|
|
60
|
+
|
|
61
|
+
## Install
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
pip install agentslim
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
For accurate token counting (uses `tiktoken` under the hood):
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
pip install agentslim[tiktoken]
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
---
|
|
74
|
+
|
|
75
|
+
## Quick Start
|
|
76
|
+
|
|
77
|
+
```python
|
|
78
|
+
from agentslim import Compressor, AgentMemory, ToolMinifier, CodeContext
|
|
79
|
+
|
|
80
|
+
# 1 ── Compress any content before sending to LLM
|
|
81
|
+
c = Compressor()
|
|
82
|
+
slim = c.compress(raw_html_or_json_or_text) # auto-detects format
|
|
83
|
+
|
|
84
|
+
# 2 ── Smart context window with auto-summarization
|
|
85
|
+
mem = AgentMemory(max_tokens=6000)
|
|
86
|
+
mem.add("user", "Build me a FastAPI app")
|
|
87
|
+
mem.add("assistant", "Sure! Here's the plan...")
|
|
88
|
+
messages = mem.get_messages() # ready for openai.chat.completions.create()
|
|
89
|
+
|
|
90
|
+
# 3 ── Minify tool schemas
|
|
91
|
+
slim_tools = ToolMinifier.minify(my_tools) # shorter descriptions
|
|
92
|
+
hint_str = ToolMinifier.to_compact_str(my_tools) # one-liner per tool
|
|
93
|
+
|
|
94
|
+
# 4 ── Send only the relevant code chunk, not the whole file
|
|
95
|
+
snippet = CodeContext.extract_function("app.py", "handle_request")
|
|
96
|
+
outline = CodeContext.outline("app.py") # class/function map
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
---
|
|
100
|
+
|
|
101
|
+
## Modules
|
|
102
|
+
|
|
103
|
+
### 🗜️ `Compressor` — Text / HTML / JSON compressor
|
|
104
|
+
|
|
105
|
+
Strips noise from content before it hits your LLM.
|
|
106
|
+
|
|
107
|
+
```python
|
|
108
|
+
from agentslim import Compressor
|
|
109
|
+
from agentslim.compressor import CompressorConfig
|
|
110
|
+
|
|
111
|
+
# Defaults — safe for most use cases
|
|
112
|
+
c = Compressor()
|
|
113
|
+
|
|
114
|
+
# Fine-grained control
|
|
115
|
+
c = Compressor(config=CompressorConfig(
|
|
116
|
+
strip_html=True,
|
|
117
|
+
remove_decorative_html=True, # drops <script>, <style>, <nav>, etc.
|
|
118
|
+
collapse_whitespace=True,
|
|
119
|
+
remove_filler_phrases=True, # "Certainly! As an AI language model..."
|
|
120
|
+
compact_json=True,
|
|
121
|
+
remove_python_comments=False, # keep comments by default
|
|
122
|
+
))
|
|
123
|
+
|
|
124
|
+
clean = c.compress(raw_content) # auto-detects JSON / HTML / text
|
|
125
|
+
clean = c.compress_html(html_string)
|
|
126
|
+
clean = c.compress_json(json_string)
|
|
127
|
+
clean = c.compress_text(plain_text)
|
|
128
|
+
clean = c.compress_code(source, language="python") # or "js" / "ts"
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
**Savings report:**
|
|
132
|
+
|
|
133
|
+
```python
|
|
134
|
+
from agentslim.utils import tokens_saved_report
|
|
135
|
+
|
|
136
|
+
report = tokens_saved_report(original, compressed, model="gpt-4o")
|
|
137
|
+
# {
|
|
138
|
+
# 'original_tokens': 1842,
|
|
139
|
+
# 'compressed_tokens': 612,
|
|
140
|
+
# 'tokens_saved': 1230,
|
|
141
|
+
# 'percent_saved': 66.8,
|
|
142
|
+
# 'cost_saved_usd': 0.003075
|
|
143
|
+
# }
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
---
|
|
147
|
+
|
|
148
|
+
### 🧠 `AgentMemory` — Smart sliding-window context manager
|
|
149
|
+
|
|
150
|
+
Instead of naively cutting old messages (which breaks reasoning), `AgentMemory` **auto-summarizes** the oldest messages into a compact system note.
|
|
151
|
+
|
|
152
|
+
```python
|
|
153
|
+
from agentslim import AgentMemory
|
|
154
|
+
|
|
155
|
+
mem = AgentMemory(
|
|
156
|
+
max_tokens=6000, # soft limit on the active window
|
|
157
|
+
archive_ratio=0.4, # archive the oldest 40% when limit is hit
|
|
158
|
+
summarize_fn=None, # optional: plug in your LLM for better summaries
|
|
159
|
+
)
|
|
160
|
+
|
|
161
|
+
mem.add("system", "You are a helpful assistant.")
|
|
162
|
+
mem.add("user", "Hello!")
|
|
163
|
+
mem.add("assistant", "Hi! How can I help?")
|
|
164
|
+
|
|
165
|
+
messages = mem.get_messages() # list[dict] — pass directly to any OpenAI-compatible API
|
|
166
|
+
print(mem.stats())
|
|
167
|
+
# MemoryStats(active_messages=3, archived=0, active_tokens=24, ...)
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
**With a real LLM summarizer:**
|
|
171
|
+
|
|
172
|
+
```python
|
|
173
|
+
import openai
|
|
174
|
+
|
|
175
|
+
def gpt_summarize(messages):
|
|
176
|
+
history = "\n".join(f"{m.role}: {m.content}" for m in messages)
|
|
177
|
+
resp = openai.chat.completions.create(
|
|
178
|
+
model="gpt-4o-mini",
|
|
179
|
+
messages=[
|
|
180
|
+
{"role": "system", "content": "Summarize in 3 sentences."},
|
|
181
|
+
{"role": "user", "content": history},
|
|
182
|
+
],
|
|
183
|
+
)
|
|
184
|
+
return resp.choices[0].message.content
|
|
185
|
+
|
|
186
|
+
mem = AgentMemory(max_tokens=8000, summarize_fn=gpt_summarize)
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
---
|
|
190
|
+
|
|
191
|
+
### 🛠️ `ToolMinifier` — Tool schema minifier
|
|
192
|
+
|
|
193
|
+
OpenAI function schemas are JSON-heavy. `ToolMinifier` cuts them down.
|
|
194
|
+
|
|
195
|
+
```python
|
|
196
|
+
from agentslim import ToolMinifier
|
|
197
|
+
|
|
198
|
+
# Option A: minify but keep JSON format (for the API)
|
|
199
|
+
slim_tools = ToolMinifier.minify(tools, max_desc=80)
|
|
200
|
+
|
|
201
|
+
# Option B: ultra-compact one-liner hint for system prompts
|
|
202
|
+
print(ToolMinifier.to_compact_str(tools))
|
|
203
|
+
# get_weather(location:string, unit:string?) -> Any # Get current weather…
|
|
204
|
+
# send_email(to:string, subject:string, body:string) -> Any
|
|
205
|
+
|
|
206
|
+
# Option C: auto-generate schemas from Python functions
|
|
207
|
+
def search(query: str, max_results: int) -> str:
|
|
208
|
+
"""Search the web for real-time info."""
|
|
209
|
+
...
|
|
210
|
+
|
|
211
|
+
tools = ToolMinifier.from_python_functions(search)
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
| Format | Tokens (example) |
|
|
215
|
+
|---|---|
|
|
216
|
+
| Full verbose JSON | ~520 |
|
|
217
|
+
| `minify()` | ~310 |
|
|
218
|
+
| `to_compact_str()` | ~40 |
|
|
219
|
+
|
|
220
|
+
---
|
|
221
|
+
|
|
222
|
+
### 📄 `CodeContext` — Code-aware chunk extractor
|
|
223
|
+
|
|
224
|
+
Don't send 500-line files to your coding agent — send only what it needs.
|
|
225
|
+
|
|
226
|
+
```python
|
|
227
|
+
from agentslim import CodeContext
|
|
228
|
+
|
|
229
|
+
# Extract a single function (+ N lines of context)
|
|
230
|
+
snippet = CodeContext.extract_function("app.py", "process_payment", context_lines=3)
|
|
231
|
+
|
|
232
|
+
# Extract a class skeleton (signatures only)
|
|
233
|
+
skeleton = CodeContext.extract_class("service.py", "PaymentService", methods_only=True)
|
|
234
|
+
|
|
235
|
+
# Outline: class/function map of the whole file
|
|
236
|
+
outline = CodeContext.outline("app.py")
|
|
237
|
+
# ['class PaymentService (L12)', 'def charge (L28)', 'def refund (L45)']
|
|
238
|
+
|
|
239
|
+
# Folded view: function bodies replaced with '...'
|
|
240
|
+
folded = CodeContext.folded("large_module.py")
|
|
241
|
+
|
|
242
|
+
# Extract specific line range
|
|
243
|
+
chunk = CodeContext.extract_lines("app.py", start_line=120, end_line=145, context_lines=5)
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
| View | Tokens saved |
|
|
247
|
+
|---|---|
|
|
248
|
+
| Full source | 0% |
|
|
249
|
+
| Folded | ~55% |
|
|
250
|
+
| Outline only | ~85% |
|
|
251
|
+
|
|
252
|
+
---
|
|
253
|
+
|
|
254
|
+
### 📊 `utils` — Token counting & cost estimation
|
|
255
|
+
|
|
256
|
+
```python
|
|
257
|
+
from agentslim.utils import count_tokens, estimate_cost
|
|
258
|
+
|
|
259
|
+
tokens = count_tokens("Hello, world!") # uses tiktoken if available
|
|
260
|
+
|
|
261
|
+
cost = estimate_cost(input_tokens=1000, output_tokens=200, model="gpt-4o")
|
|
262
|
+
# {'input_usd': 0.0025, 'output_usd': 0.002, 'total_usd': 0.0045}
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
Supported models: `gpt-4o`, `gpt-4o-mini`, `gpt-4-turbo`, `gpt-3.5-turbo`,
|
|
266
|
+
`claude-3-5-sonnet`, `claude-3-haiku`, `gemini-1.5-pro`, `gemini-1.5-flash`.
|
|
267
|
+
|
|
268
|
+
---
|
|
269
|
+
|
|
270
|
+
## Compatibility
|
|
271
|
+
|
|
272
|
+
`agentslim` is framework-agnostic. It works with anything that accepts a list of `{"role": ..., "content": ...}` dicts:
|
|
273
|
+
|
|
274
|
+
- ✅ OpenAI Python SDK
|
|
275
|
+
- ✅ LangChain / LangGraph
|
|
276
|
+
- ✅ LlamaIndex
|
|
277
|
+
- ✅ Anthropic SDK
|
|
278
|
+
- ✅ Google Generative AI SDK
|
|
279
|
+
- ✅ Any custom agent framework
|
|
280
|
+
|
|
281
|
+
---
|
|
282
|
+
|
|
283
|
+
## Running tests
|
|
284
|
+
|
|
285
|
+
```bash
|
|
286
|
+
pip install -e ".[dev]"
|
|
287
|
+
pytest
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
---
|
|
291
|
+
|
|
292
|
+
## Contributing
|
|
293
|
+
|
|
294
|
+
PRs and issues welcome! See [CONTRIBUTING.md](CONTRIBUTING.md).
|
|
295
|
+
|
|
296
|
+
---
|
|
297
|
+
|
|
298
|
+
## License
|
|
299
|
+
|
|
300
|
+
MIT © agentslim contributors
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
# agentslim 🪶
|
|
2
|
+
|
|
3
|
+
**Make your AI agents leaner, faster, and cheaper.**
|
|
4
|
+
|
|
5
|
+
`agentslim` is a zero-dependency Python toolkit that reduces token consumption in LLM-powered agents — without sacrificing reasoning quality.
|
|
6
|
+
|
|
7
|
+
[](https://pypi.org/project/agentslim/)
|
|
8
|
+
[](https://pypi.org/project/agentslim/)
|
|
9
|
+
[](LICENSE)
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## Why?
|
|
14
|
+
|
|
15
|
+
Every token counts — literally. When building agents you routinely waste tokens on:
|
|
16
|
+
|
|
17
|
+
| Problem | Typical waste |
|
|
18
|
+
|---|---|
|
|
19
|
+
| Verbose JSON tool schemas | 200–800 tokens per request |
|
|
20
|
+
| Raw HTML web scrapes fed to the LLM | 60–80% noise |
|
|
21
|
+
| Naively truncated chat history | Lost context, broken reasoning |
|
|
22
|
+
| Sending entire source files to coding agents | 10× more than needed |
|
|
23
|
+
|
|
24
|
+
`agentslim` solves all four with one clean API.
|
|
25
|
+
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
## Install
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
pip install agentslim
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
For accurate token counting (uses `tiktoken` under the hood):
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
pip install agentslim[tiktoken]
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
---
|
|
41
|
+
|
|
42
|
+
## Quick Start
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
from agentslim import Compressor, AgentMemory, ToolMinifier, CodeContext
|
|
46
|
+
|
|
47
|
+
# 1 ── Compress any content before sending to LLM
|
|
48
|
+
c = Compressor()
|
|
49
|
+
slim = c.compress(raw_html_or_json_or_text) # auto-detects format
|
|
50
|
+
|
|
51
|
+
# 2 ── Smart context window with auto-summarization
|
|
52
|
+
mem = AgentMemory(max_tokens=6000)
|
|
53
|
+
mem.add("user", "Build me a FastAPI app")
|
|
54
|
+
mem.add("assistant", "Sure! Here's the plan...")
|
|
55
|
+
messages = mem.get_messages() # ready for openai.chat.completions.create()
|
|
56
|
+
|
|
57
|
+
# 3 ── Minify tool schemas
|
|
58
|
+
slim_tools = ToolMinifier.minify(my_tools) # shorter descriptions
|
|
59
|
+
hint_str = ToolMinifier.to_compact_str(my_tools) # one-liner per tool
|
|
60
|
+
|
|
61
|
+
# 4 ── Send only the relevant code chunk, not the whole file
|
|
62
|
+
snippet = CodeContext.extract_function("app.py", "handle_request")
|
|
63
|
+
outline = CodeContext.outline("app.py") # class/function map
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
---
|
|
67
|
+
|
|
68
|
+
## Modules
|
|
69
|
+
|
|
70
|
+
### 🗜️ `Compressor` — Text / HTML / JSON compressor
|
|
71
|
+
|
|
72
|
+
Strips noise from content before it hits your LLM.
|
|
73
|
+
|
|
74
|
+
```python
|
|
75
|
+
from agentslim import Compressor
|
|
76
|
+
from agentslim.compressor import CompressorConfig
|
|
77
|
+
|
|
78
|
+
# Defaults — safe for most use cases
|
|
79
|
+
c = Compressor()
|
|
80
|
+
|
|
81
|
+
# Fine-grained control
|
|
82
|
+
c = Compressor(config=CompressorConfig(
|
|
83
|
+
strip_html=True,
|
|
84
|
+
remove_decorative_html=True, # drops <script>, <style>, <nav>, etc.
|
|
85
|
+
collapse_whitespace=True,
|
|
86
|
+
remove_filler_phrases=True, # "Certainly! As an AI language model..."
|
|
87
|
+
compact_json=True,
|
|
88
|
+
remove_python_comments=False, # keep comments by default
|
|
89
|
+
))
|
|
90
|
+
|
|
91
|
+
clean = c.compress(raw_content) # auto-detects JSON / HTML / text
|
|
92
|
+
clean = c.compress_html(html_string)
|
|
93
|
+
clean = c.compress_json(json_string)
|
|
94
|
+
clean = c.compress_text(plain_text)
|
|
95
|
+
clean = c.compress_code(source, language="python") # or "js" / "ts"
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
**Savings report:**
|
|
99
|
+
|
|
100
|
+
```python
|
|
101
|
+
from agentslim.utils import tokens_saved_report
|
|
102
|
+
|
|
103
|
+
report = tokens_saved_report(original, compressed, model="gpt-4o")
|
|
104
|
+
# {
|
|
105
|
+
# 'original_tokens': 1842,
|
|
106
|
+
# 'compressed_tokens': 612,
|
|
107
|
+
# 'tokens_saved': 1230,
|
|
108
|
+
# 'percent_saved': 66.8,
|
|
109
|
+
# 'cost_saved_usd': 0.003075
|
|
110
|
+
# }
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
---
|
|
114
|
+
|
|
115
|
+
### 🧠 `AgentMemory` — Smart sliding-window context manager
|
|
116
|
+
|
|
117
|
+
Instead of naively cutting old messages (which breaks reasoning), `AgentMemory` **auto-summarizes** the oldest messages into a compact system note.
|
|
118
|
+
|
|
119
|
+
```python
|
|
120
|
+
from agentslim import AgentMemory
|
|
121
|
+
|
|
122
|
+
mem = AgentMemory(
|
|
123
|
+
max_tokens=6000, # soft limit on the active window
|
|
124
|
+
archive_ratio=0.4, # archive the oldest 40% when limit is hit
|
|
125
|
+
summarize_fn=None, # optional: plug in your LLM for better summaries
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
mem.add("system", "You are a helpful assistant.")
|
|
129
|
+
mem.add("user", "Hello!")
|
|
130
|
+
mem.add("assistant", "Hi! How can I help?")
|
|
131
|
+
|
|
132
|
+
messages = mem.get_messages() # list[dict] — pass directly to any OpenAI-compatible API
|
|
133
|
+
print(mem.stats())
|
|
134
|
+
# MemoryStats(active_messages=3, archived=0, active_tokens=24, ...)
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
**With a real LLM summarizer:**
|
|
138
|
+
|
|
139
|
+
```python
|
|
140
|
+
import openai
|
|
141
|
+
|
|
142
|
+
def gpt_summarize(messages):
|
|
143
|
+
history = "\n".join(f"{m.role}: {m.content}" for m in messages)
|
|
144
|
+
resp = openai.chat.completions.create(
|
|
145
|
+
model="gpt-4o-mini",
|
|
146
|
+
messages=[
|
|
147
|
+
{"role": "system", "content": "Summarize in 3 sentences."},
|
|
148
|
+
{"role": "user", "content": history},
|
|
149
|
+
],
|
|
150
|
+
)
|
|
151
|
+
return resp.choices[0].message.content
|
|
152
|
+
|
|
153
|
+
mem = AgentMemory(max_tokens=8000, summarize_fn=gpt_summarize)
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
---
|
|
157
|
+
|
|
158
|
+
### 🛠️ `ToolMinifier` — Tool schema minifier
|
|
159
|
+
|
|
160
|
+
OpenAI function schemas are JSON-heavy. `ToolMinifier` cuts them down.
|
|
161
|
+
|
|
162
|
+
```python
|
|
163
|
+
from agentslim import ToolMinifier
|
|
164
|
+
|
|
165
|
+
# Option A: minify but keep JSON format (for the API)
|
|
166
|
+
slim_tools = ToolMinifier.minify(tools, max_desc=80)
|
|
167
|
+
|
|
168
|
+
# Option B: ultra-compact one-liner hint for system prompts
|
|
169
|
+
print(ToolMinifier.to_compact_str(tools))
|
|
170
|
+
# get_weather(location:string, unit:string?) -> Any # Get current weather…
|
|
171
|
+
# send_email(to:string, subject:string, body:string) -> Any
|
|
172
|
+
|
|
173
|
+
# Option C: auto-generate schemas from Python functions
|
|
174
|
+
def search(query: str, max_results: int) -> str:
|
|
175
|
+
"""Search the web for real-time info."""
|
|
176
|
+
...
|
|
177
|
+
|
|
178
|
+
tools = ToolMinifier.from_python_functions(search)
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
| Format | Tokens (example) |
|
|
182
|
+
|---|---|
|
|
183
|
+
| Full verbose JSON | ~520 |
|
|
184
|
+
| `minify()` | ~310 |
|
|
185
|
+
| `to_compact_str()` | ~40 |
|
|
186
|
+
|
|
187
|
+
---
|
|
188
|
+
|
|
189
|
+
### 📄 `CodeContext` — Code-aware chunk extractor
|
|
190
|
+
|
|
191
|
+
Don't send 500-line files to your coding agent — send only what it needs.
|
|
192
|
+
|
|
193
|
+
```python
|
|
194
|
+
from agentslim import CodeContext
|
|
195
|
+
|
|
196
|
+
# Extract a single function (+ N lines of context)
|
|
197
|
+
snippet = CodeContext.extract_function("app.py", "process_payment", context_lines=3)
|
|
198
|
+
|
|
199
|
+
# Extract a class skeleton (signatures only)
|
|
200
|
+
skeleton = CodeContext.extract_class("service.py", "PaymentService", methods_only=True)
|
|
201
|
+
|
|
202
|
+
# Outline: class/function map of the whole file
|
|
203
|
+
outline = CodeContext.outline("app.py")
|
|
204
|
+
# ['class PaymentService (L12)', 'def charge (L28)', 'def refund (L45)']
|
|
205
|
+
|
|
206
|
+
# Folded view: function bodies replaced with '...'
|
|
207
|
+
folded = CodeContext.folded("large_module.py")
|
|
208
|
+
|
|
209
|
+
# Extract specific line range
|
|
210
|
+
chunk = CodeContext.extract_lines("app.py", start_line=120, end_line=145, context_lines=5)
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
| View | Tokens saved |
|
|
214
|
+
|---|---|
|
|
215
|
+
| Full source | 0% |
|
|
216
|
+
| Folded | ~55% |
|
|
217
|
+
| Outline only | ~85% |
|
|
218
|
+
|
|
219
|
+
---
|
|
220
|
+
|
|
221
|
+
### 📊 `utils` — Token counting & cost estimation
|
|
222
|
+
|
|
223
|
+
```python
|
|
224
|
+
from agentslim.utils import count_tokens, estimate_cost
|
|
225
|
+
|
|
226
|
+
tokens = count_tokens("Hello, world!") # uses tiktoken if available
|
|
227
|
+
|
|
228
|
+
cost = estimate_cost(input_tokens=1000, output_tokens=200, model="gpt-4o")
|
|
229
|
+
# {'input_usd': 0.0025, 'output_usd': 0.002, 'total_usd': 0.0045}
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
Supported models: `gpt-4o`, `gpt-4o-mini`, `gpt-4-turbo`, `gpt-3.5-turbo`,
|
|
233
|
+
`claude-3-5-sonnet`, `claude-3-haiku`, `gemini-1.5-pro`, `gemini-1.5-flash`.
|
|
234
|
+
|
|
235
|
+
---
|
|
236
|
+
|
|
237
|
+
## Compatibility
|
|
238
|
+
|
|
239
|
+
`agentslim` is framework-agnostic. It works with anything that accepts a list of `{"role": ..., "content": ...}` dicts:
|
|
240
|
+
|
|
241
|
+
- ✅ OpenAI Python SDK
|
|
242
|
+
- ✅ LangChain / LangGraph
|
|
243
|
+
- ✅ LlamaIndex
|
|
244
|
+
- ✅ Anthropic SDK
|
|
245
|
+
- ✅ Google Generative AI SDK
|
|
246
|
+
- ✅ Any custom agent framework
|
|
247
|
+
|
|
248
|
+
---
|
|
249
|
+
|
|
250
|
+
## Running tests
|
|
251
|
+
|
|
252
|
+
```bash
|
|
253
|
+
pip install -e ".[dev]"
|
|
254
|
+
pytest
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
---
|
|
258
|
+
|
|
259
|
+
## Contributing
|
|
260
|
+
|
|
261
|
+
PRs and issues welcome! See [CONTRIBUTING.md](CONTRIBUTING.md).
|
|
262
|
+
|
|
263
|
+
---
|
|
264
|
+
|
|
265
|
+
## License
|
|
266
|
+
|
|
267
|
+
MIT © agentslim contributors
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"""
|
|
2
|
+
agentslim — Make your AI agents leaner, faster, and cheaper.
|
|
3
|
+
|
|
4
|
+
Core modules:
|
|
5
|
+
- memory: Smart context window with auto-summarization
|
|
6
|
+
- compressor: Text / HTML / JSON compressor before sending to LLM
|
|
7
|
+
- tools: Tool/function-call schema minifier
|
|
8
|
+
- code: Code-aware context extractor (send only relevant chunks)
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from agentslim.memory import AgentMemory
|
|
12
|
+
from agentslim.compressor import Compressor
|
|
13
|
+
from agentslim.tools import ToolMinifier
|
|
14
|
+
from agentslim.code import CodeContext
|
|
15
|
+
from agentslim.utils import count_tokens, estimate_cost
|
|
16
|
+
|
|
17
|
+
__version__ = "0.1.0"
|
|
18
|
+
__author__ = "agentslim contributors"
|
|
19
|
+
|
|
20
|
+
__all__ = [
|
|
21
|
+
"AgentMemory",
|
|
22
|
+
"Compressor",
|
|
23
|
+
"ToolMinifier",
|
|
24
|
+
"CodeContext",
|
|
25
|
+
"count_tokens",
|
|
26
|
+
"estimate_cost",
|
|
27
|
+
]
|