Introduction

DeepSeek Harness (DSH) supports dynamic plugin extensions. In agent development, tool outputs such as JSON, CLI tables, or logs often directly enter the conversation context, causing Token costs to grow linearly with conversation turns. dsh-plugin-save-token is a dynamic plugin designed to reduce Token costs through structure-aware compression and deduplication strategies, without reducing model reasoning quality. All optimizations follow the principle of reversibility: any replacement can be restored with one click through tools.

Core Features

The plugin implements the following core mechanisms:

  1. Structure-Aware Compression
    For pipe-delimited high-density tables (where ≥70% of lines share the same delimiter), the plugin retains the first 60 lines and the last 40 lines, and applies stride sampling to the middle section. The version received by the model includes original line number annotations (such as L61: ...), ensuring retrieval precision.

  2. TOON-Style Lossless Encoding
    For uniform JSON arrays (such as API responses containing 300 homogeneous objects), the plugin preferentially uses lossless encoding in TOON format (such as prices[300]{model,input,output}:), and only enables lossy compression when the lossless path is unavailable.

  3. Compression-Cache-Retrieve (CCR) Mode
    Before each replacement, the original text is written to disk (via DSH’s spillStore). The compressed content exposes two retrieval paths: the dynamic tool save_token_expand (invoked by marker id) and file locators.

  4. Cross-Turn Deduplication
    For outputs that are byte-for-byte identical within a 90-second time window, the plugin replaces them with a stub referencing the previous turn’s context, preventing redundant content from accumulating.

  5. Dual-Gating Mechanism
    The mechanism only takes effect when both of the following conditions are satisfied:
    - Byte gate: compressed size ≤ 72% of the original size, and absolute savings ≥ 500B.
    - Token gate: estimated Token count is strictly less than the original count.
    If either condition is not satisfied, the original content is retained.

  6. Error Line Protection
    For compressed regions of log-like outputs, the plugin retains up to 25 lines matching keywords such as error/fatal/traceback/timeout.

  7. Compression Pressure Coupling
    The plugin checks context size at inference step boundaries. When the actual context exceeds 120k tokens, it triggers DSH’s native compaction.compactIfNeeded() method.

  8. End-to-End Metering and Dashboard
    The plugin intercepts llm/stream events and separately measures Tokens actually billed and Tokens avoided through compression. The settings page provides a dual-panel dashboard displaying KPIs, stacked charts, and leaderboards.

Usage

The plugin provides the dynamic tool save_token_expand to DSH clients for retrieving compressed content by marker id. Users can view detailed KPIs and per-request stacked charts on the settings page.

Applicable Scenarios and Limitations

  • Applicable scenarios: When large tool outputs (API JSON, CLI tables) directly enter the conversation context.
  • Non-triggering scenarios: When the agent uses a “write to file then read” pattern, the plugin does not intervene.
  • Preconditions: Reversibility is a strict prerequisite. If disk writing (spill) fails, the compression operation is abandoned.