# tool-output-truncate
[](https://crates.io/crates/tool-output-truncate)
[](https://docs.rs/tool-output-truncate)
[](https://github.com/MukundaKatta/tool-output-truncate/actions/workflows/ci.yml)
[](https://crates.io/crates/tool-output-truncate)
**Truncate tool output before adding it to LLM message history.**
When an agent runs `cat file.log`, `ripgrep`, or a database query, the
result can be megabytes. Naively appending it to the conversation blows
the context window. The standard fix is keep head + tail and replace the
middle with an elision marker. This is that, char-aware (UTF-8 safe) and
line-aware, zero deps.
## Install
```toml
[dependencies]
tool-output-truncate = "0.1"
```
## Use
```rust
use tool_output_truncate::truncate_middle;
let big = std::fs::read_to_string("server.log")?;
let safe_to_send = truncate_middle(&big, 4000);
// "first 2000 chars...\n\n[123456 chars truncated]\n\n...last 2000 chars"
```
Four strategies:
```rust
truncate_head(text, max_chars) // keep prefix
truncate_tail(text, max_chars) // keep suffix
truncate_middle(text, max_chars) // keep both ends (default for logs)
truncate_middle_lines(text, max_lines) // line-aware version of middle
```
All four are no-ops when the input already fits.
## What it does NOT do
- No tokenization. Pass a char cap. As a rough Anthropic/OpenAI proxy,
treat `chars * 4 ≈ tokens` (so 4000 chars ≈ 1k tokens).
- No structured truncation (JSON, YAML, XML). For JSON specifically,
parse first and decide which fields to keep.
- No summarization. This is character arithmetic only.
## Why a focused crate
Most agent frameworks ship a custom truncator inline. They reinvent the
edge cases each time: UTF-8 boundaries, odd-sized budgets, line-aware
splitting that doesn't show half a line. This is the four-function lib
you grab instead.
## License
MIT OR Apache-2.0