tool-output-truncate 0.1.0

Truncate tool output (file reads, command runs, search hits) before adding to LLM message history. Char-aware head/middle/tail strategies with a configurable elision marker. Zero deps.
Documentation
# tool-output-truncate

[![Crates.io](https://img.shields.io/crates/v/tool-output-truncate.svg)](https://crates.io/crates/tool-output-truncate)
[![Documentation](https://docs.rs/tool-output-truncate/badge.svg)](https://docs.rs/tool-output-truncate)
[![CI](https://github.com/MukundaKatta/tool-output-truncate/actions/workflows/ci.yml/badge.svg)](https://github.com/MukundaKatta/tool-output-truncate/actions/workflows/ci.yml)
[![License](https://img.shields.io/crates/l/tool-output-truncate.svg)](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