How TokenJuice Compression Reduces Token Usage by Up to 80% in OpenHuman

TokenJuice compression embeds a three-stage pipeline—classification, rule-matching, and aggressive reduction—directly into OpenHuman's tool-execution layer, stripping redundant markup and noise to shrink inputs by up to 80% before they reach the LLM.

OpenHuman integrates the TokenJuice engine to solve the context-window bottleneck inherent in tool-augmented LLM workflows. By preprocessing every byte of raw tool output through a specialized compression pipeline, the system delivers semantically dense payloads that preserve critical information while dramatically reducing token consumption. This process is orchestrated through a tightly coupled Rust implementation located in src/openhuman/tokenjuice/.

The Three-Stage Compression Pipeline

TokenJuice operates through a deterministic pipeline that analyzes, categorizes, and minifies content before it ever hits the model's context window.

Stage 1: Content Classification

Every tool output first passes through the classifier implemented in [src/openhuman/tokenjuice/classify.rs](https://github.com/tinyhumansai/openhuman/blob/main/src/openhuman/tokenjuice/classify.rs). This module analyzes the raw byte stream to detect the content type—whether it is an HTML page, a shell command transcript, a Git log, a JSON blob, or structured logs. The classifier assigns a type signature that dictates which optimization strategy to apply, ensuring that HTML pages undergo tag stripping while log files undergo deduplication and colour-code removal.

Stage 2: Rule-Based Pattern Matching

Once classified, the engine selects a specialized rule set from the src/openhuman/tokenjuice/vendor/rules/ directory. These JSON-encoded rule files define pattern-action pairs specific to each content type. For example, an HTML page triggers rules that target <a href="..."> tags, <script> blocks, and verbose CSS attributes, while a shell output triggers rules targeting ANSI escape sequences and prompt strings.

Stage 3: Semantic Reduction

The final stage, implemented in [src/openhuman/tokenjuice/reduce.rs](https://github.com/tinyhumansai/openhuman/blob/main/src/openhuman/tokenjuice/reduce.rs), applies the matched rules to execute semantic reduction. This process transforms verbose source material into concise, semantically equivalent representations through specific operations:

  • HTML Minification: Converts HTML structures to Markdown (<a href="...">text</a> becomes [text](url)), removes script and style tags, and collapses redundant whitespace.
  • URL Compression: Shortens long URLs to their hostname or a cached alias, eliminating high-token-count query parameters and paths.
  • Character Set Cleaning: Strips non-ASCII characters, emoji, and localized symbols that consume multiple bytes per token.
  • Noise Removal: Eliminates ANSI colour codes, debug prefixes, timestamps, and repetitive log headers.
  • Deduplication: Collapses consecutive duplicate lines and summarizes repetitive log sections into count-based summaries.

Technical Implementation and Key Files

The TokenJuice engine exposes a unified API through [src/openhuman/tokenjuice/mod.rs](https://github.com/tinyhumansai/openhuman/blob/main/src/openhuman/tokenjuice/mod.rs), which serves as the entry point for the rest of the OpenHuman system.

Component Path Responsibility
Public API [mod.rs](https://github.com/tinyhumansai/openhuman/blob/main/src/openhuman/tokenjuice/mod.rs) Exposes TokenJuice::compress() and manages pipeline orchestration
Classifier [classify.rs](https://github.com/tinyhumansai/openhuman/blob/main/src/openhuman/tokenjuice/classify.rs) Detects output type using heuristic and signature analysis
Reduction Engine [reduce.rs](https://github.com/tinyhumansai/openhuman/blob/main/src/openhuman/tokenjuice/reduce.rs) Executes transformation logic against matched rules
Rule Definitions vendor/rules/ JSON files containing type-specific compression patterns
Documentation [gitbooks/features/token-compression.md](https://github.com/tinyhumansai/openhuman/blob/main/gitbooks/features/token-compression.md) High-level feature specification and token-saving benchmarks

Using TokenJuice in Practice

Tool outputs are compressed automatically when passed through the TokenJuice interface before LLM submission. The following pattern demonstrates how a scraper or log reader integrates the compression layer:

use openhuman::tokenjuice::TokenJuice;

// Raw output from a tool (e.g., HTML page, Git diff, log stream)
let raw_output: String = fetch_tool_output();

// Execute the compression pipeline
let compressed = TokenJuice::compress(&raw_output)
    .expect("TokenJuice compression failed");

// Result is token-optimized and ready for LLM context injection
let llm_response = openai_client
    .with_context(compressed)
    .complete();

Internally, the compress method invokes the classification heuristic, loads the appropriate JSON rule file from vendor/rules/, and streams the content through the reduction engine. This integration ensures that token-heavy artifacts—such as verbose web pages or stack traces—are reduced to their essential semantic content without manual preprocessing.

Why TokenJuice Achieves 80% Reduction

The 80% token reduction benchmark cited in the project documentation stems from the engine's ability to remove structural overhead that humans need for readability but LLMs process as noise. By converting HTML to Markdown, the system eliminates closing tags and attribute quotes that consume tokens without adding semantic value. By collapsing repetitive log lines into [14x identical lines removed] annotations, it preserves intent while eliminating redundancy. According to the [token-compression.md](https://github.com/tinyhumansai/openhuman/blob/main/gitbooks/features/token-compression.md) specification, this "smart token compression" approach maintains the full signal required for model reasoning while minimizing the context-window footprint.

Summary

  • TokenJuice embeds directly into OpenHuman's tool pipeline via src/openhuman/tokenjuice/mod.rs, processing outputs before LLM consumption.
  • The engine uses a three-stage pipeline: classification (classify.rs), rule-matching (vendor/rules/), and reduction (reduce.rs).
  • Specific optimizations include HTML-to-Markdown conversion, URL shortening, ANSI code stripping, non-ASCII removal, and log deduplication.
  • The system achieves up to 80% token reduction by eliminating structural markup and repetitive noise while preserving semantic content.
  • Developers interact with the engine through a single TokenJuice::compress() call that returns a context-optimized string.

Frequently Asked Questions

What types of content can TokenJuice compress?

TokenJuice handles any text-based tool output, including HTML pages, JSON APIs, Git diffs, shell logs, and structured data streams. The classifier in classify.rs automatically detects the format and selects the appropriate rule set from the vendor/rules/ directory, ensuring specialized handling for each content type without manual configuration.

How does TokenJuice handle HTML documents specifically?

For HTML content, the engine applies rules that convert tags to Markdown syntax, strip <script> and <style> blocks, remove class and id attributes, and collapse whitespace. This transformation preserves the semantic structure and link destinations while eliminating token-heavy markup syntax that provides no value to the LLM's reasoning process.

Is TokenJuice compression lossless?

The compression is semantically lossless but syntactically lossy. The engine removes formatting, styling, and repetitive elements that do not alter the meaning of the content. Critical data such as URLs, error messages, and structural hierarchies are preserved, though their representation is minimized to reduce token count.

Where are the compression rules defined in OpenHuman?

Rules are stored as JSON files in src/openhuman/tokenjuice/vendor/rules/. Each file corresponds to a specific content type (e.g., html__page.json, shell__output.json) and defines regex patterns and replacement strings that the reduction engine applies during the final compression stage.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →