OpenToken Token-Compression Engine

repository·main·Indexed 41 days ago

https://github.com/mrgray17/opentoken

A token-compression engine for AI coding agents that reduces token consumption by 50-80% by stripping noise from tool outputs like git diffs and npm logs. It features a pipeline architecture with secrets redaction, ANSI stripping, and an auto-escalation system that adjusts compression levels (off, lean, ultra, ceiling) based on LLM context window fill percentage. Available as a Core Library, CLI, MCP Server for IDEs, and OpenCode plugin. Version 2.0.0.

Tokens
40.1K
Snippets
138
Records
173
Agent score
51%

What's inside OpenToken

  1. How OpenToken compression works

    main

    OpenToken acts as a transparent filter between a tool's runtime and the LLM context window. It uses a pipeline architecture to reduce token usage by 50-80% without losing semantic meaning.

    The Pipeline Process

    1. Secrets Redaction: Runs first to ensure sensitive data is removed.
    2. Binary Detection: Uses a NUL byte guard to prevent corruption of binary streams.
    3. ANSI Stripping: Removes terminal color codes and control sequences.
    4. Family Detection & Routing: The engine detects the command context (e.g., git, npm, cargo) and routes the output to a specialized pipeline of 10-20 stages.
    5. Conservative Safety Filter: Every stage ends with a filter that estimates BPE token counts. If a stage increases the token count, the original output is returned. This ensures a 0-risk principle where compression never regresses quality.

    Compression Techniques

    • Noise Reduction: Pre-call rewrites (adding --quiet or -q to commands), JSON cleanup, and path shortening.
    • Structural Compression: Diff folding, log folding (collapsing identical lines), and line noise normalization (replacing timestamps/PIDs with placeholders).
    • Advanced Compression: LTSC (Lossless Token Sequence Compression), LZW token substitution, and reversible compression.
  2. Install OpenToken packages

    main

    OpenToken provides several packages depending on your use case. Bun v1.2+ is recommended, but the core package also supports Node.js >=18 via an fs-compat polyfill.

    • OpenCode plugin: Auto-loads with zero configuration.
    • MCP Server: For use with any MCP-compatible IDE.
    • CLI: For piping or wrapping commands.
    • Core Library: For programmatic integration.
    npm i -g @mrgray17/opentoken     # OpenCode plugin (auto-loads, zero config)
    npm i -g @mrgray17/opentoken-mcp # Any IDE via MCP
    bun x @mrgray17/opentoken-cli    # CLI pipe/wrap
    npm i @mrgray17/opentoken-core   # Library
  3. Integrate OpenToken with MCP-compatible IDEs

    main

    To use OpenToken in an AI-powered IDE via the Model Context Protocol (MCP), add the opentoken-mcp server to your IDE's configuration file.

    ### Cursor / Windsurf
    Add to `~/.cursor/mcp.json`:
    ```json
    { "mcpServers": { "opentoken": { "command": "opentoken-mcp" } } }

    Claude Desktop

    Add to ~/.claude/claude_desktop_config.json:

    { "mcpServers": { "opentoken": { "command": "opentoken-mcp" } } }

    VS Code Copilot

    Add to .vscode/mcp.json:

    { "servers": { "opentoken": { "type": "stdio", "command": "opentoken-mcp" } } }
  4. Manage context-aware compression with Auto-escalation

    main

    The auto-escalation system automatically adjusts text compression levels based on how much of the LLM's context window is filled. As the fillPct (fill percentage) increases, the system moves through progressively more aggressive compression modes to save space.

    Compression Levels

    • off: No compression applied.
    • lean: Drops filler words and replaces long words with short synonyms.
    • ultra: Applies lean compression, removes low-information sentences, replaces phrases with symbols (e.g., →, ≠), and compresses lists into semicolon-separated strings.
    • ceiling: The most aggressive mode. If the text exceeds 30 lines, it truncates the middle, keeping only the first 10 and last 5 lines.

    Escalation Thresholds

    • 50% fill $\rightarrow$ lean
    • 75% fill $\rightarrow$ ultra
    • 85% fill $\rightarrow$ ceiling
    // Example of how levels escalate based on context usage
    // 50% fill -> lean
    // 75% fill -> ultra
    // 85% fill -> ceiling
  5. Understand the TOON format structure

    main

    TOON (Token-Oriented Object Notation) is a format optimized for structured data reduction. It uses two primary patterns depending on the data type:

    1. Tabular Arrays (Arrays of Objects)

    When converting an array of objects, TOON uses a header-based approach to avoid repeating keys for every entry.

    Format: {first_key}[{count}]{key1,key2,...}: {val1,val2,...} {val1,val2,...}

    • first_key: The first key found in the data.
    • [count]: The total number of items in the array.
    • {key1,key2,...}: A comma-separated list of all unique keys found in the array.
    • :: The header terminator.
    • Rows: Each subsequent line contains the values for that object, separated by commas.

    2. Nested Objects and Primitives

    For standard objects, TOON uses a key-value mapping:

    • Primitives: key: value
    • Nested Objects: key: followed by indented lines.
    • Primitive Arrays: key[length]: val1,val2,...

    Example of a nested TOON structure:

    user:
      id: 1
      profile:
        name: Alice
        tags[2]: admin,staff
  6. How the deduplication engine works

    main

    The deduplication engine reduces noise by collapsing repeated information into single reference lines. It operates using several key mechanisms:

    1. Session Isolation: All state is keyed by a sessionID using a SessionStore. This ensures that deduplication logic in one session does not corrupt or interfere with another.
    2. Detection Methods:
      • Exact Match: Uses content hashing to find identical strings.
      • Fuzzy Similarity: Uses Jaccard similarity (threshold of 0.85) to detect content that is substantially similar but not identical. This is only applied to content longer than 100 characters.
    3. Adaptive Windowing: To manage memory and performance, the engine maintains a sliding window of recent calls. The window size is adaptive based on content length:
      • Small content: MIN_WINDOW (8 calls)
      • Medium content (1000-5000 chars): 32 calls
      • Large content (>5000 chars): MAX_WINDOW (64 calls)
    4. Cross-Tool Support: The engine tracks the tool name associated with each call, allowing it to provide context in the duplicate reference (e.g., [Duplicate of call #1 (tool_name) ...]).
  7. How OpenToken memory works

    main

    OpenToken uses a structured MEMORY.md file as a lightweight, cross-session fact store. This allows the system to maintain context across different interactions without requiring a heavy database.

    Key Concepts:

    • Fact Structure: Each fact is a single line in a Markdown list, typically following the pattern: - ProjectName: keywords | files.
    • Project Isolation: Facts are prefixed with the project name (derived from the directory name). When retrieving context, the system prioritizes facts belonging to the current project.
    • Cost-Efficiency: To prevent context window bloat, the system only injects memory if the estimated token savings (from not re-explaining things) outweigh the cost of the injection itself.
    • Deduplication: The system uses a Jaccard-like similarity check to ensure that nearly identical facts are not stored multiple times.
  8. Understand the Progressive Disclosure system

    main

    The progressive disclosure system manages large tool outputs to maintain efficient context windows. It operates based on two thresholds:

    • MAX_INLINE_LINES: 80 lines
    • MAX_INLINE_BYTES: 8KB

    How it works

    1. Inline Mode: If the content is below the thresholds, the full content is returned directly.
    2. Offload Mode: If the content exceeds thresholds, the system:
      • Generates a summary based on the tool type (e.g., counting errors for bash, symbols for read, or matches for grep).
      • Writes the full content to a temporary file in ~/.config/opentoken/offload/.
      • Returns a summary and a pointer (an entryId) to the user.
    3. Fallback Mode: If writing to a file fails, the system returns the summary plus the first 50 lines and the last 20 lines of the content.

    Summary Formats by Tool

    • bash: Includes error counts and file change counts.
    • read: Includes total lines and symbol counts (e.g., functions, classes, interfaces).
    • grep: Includes match counts and the number of unique files matched.
    • glob: Includes the total number of files found.
  9. How JSON sampling identifies error entries

    main

    The sampling logic uses an internal heuristic to identify "error entries" within an array to ensure they are preserved rather than sampled out. An item is considered an error entry if it is an object containing any of the following keys:

    • error
    • errors
    • message
    • status
    • code
    • statusCode

    An entry is flagged if the value of one of these keys is:

    1. A string matching the regex /error|fail|exception|invalid|unauthorized/i (case-insensitive).
    2. A number greater than or equal to 400.
  10. How OpenToken manages tool execution lifecycle

    main

    OpenToken intercepts tool calls to reduce token usage and prevent context overflow through two main hooks:

    tool.execute.before (Pre-call filtering)

    Before a tool runs, OpenToken:

    • Validates the tool name.
    • Applies filters to arguments (e.g., blocking certain grep patterns or glob patterns).
    • Can block execution if the request is deemed unsafe or inefficient, returning a [OpenToken blocked] message.

    tool.execute.after (Post-call compression)

    After a tool returns, OpenToken:

    • Validates the output size to prevent context explosion.
    • Applies tool-specific filters (e.g., applyBashFilter for bash commands, applyReadFilter for file reads, applyGrepFilter for grep results).
    • Performs deduplication to remove redundant information.
    • Estimates token savings and updates session metrics.
    • Automatically adjusts the compression level based on usage.
  11. Understand the LTSC compression format

    main

    LTSC (Lossless Token Sequence Compression) uses a specific structure designed to be easily parsed by LLMs. A compressed payload consists of two parts:

    1. Dictionary Header: A comment block at the very beginning of the string: <!--LTSC:token1=value1,token2=value2,...-->.
    2. Compressed Body: The original text where repeated sequences have been replaced by tokens like $1, $2, etc.

    Example Structure: <!--LTSC:$1=repeated_sequence--> This is a $1 and another $1.

  12. Configure OpenToken via config.json

    main

    OpenToken works with zero configuration by default. However, you can provide optional overrides in ~/.config/opentoken/config.json.

    {
      "enableMetrics": true,
      "safeReadRoot": "/home/user/projects/myapp",
      "maxOutputBytes": 1048576,
      "enableSymbolIndex": false
    }