Skip to content

[Feature]: Add TOON compression for LLM-facing data #2057

@acamq

Description

@acamq

Prerequisites

  • I will write this issue in English
  • I have searched existing issues and discussions to avoid duplicates
  • This feature request is specific to oh-my-opencode (not OpenCode core)
  • I have read the documentation and couldn't find this feature

Problem Description

LLM token costs are significant when passing structured data between agents, formatting tool outputs, and constructing prompts. Large arrays (session lists, task lists, MCP responses, file listings) consume unnecessary tokens when formatted as verbose JSON.

Proposed Solution

Integrate @toon-format/toon - a compact encoding format designed for LLM input that achieves ~40% token reduction on uniform arrays.

Repository Health: 22.8k stars, MIT license, TypeScript, zero runtime deps, actively maintained.

Key Integration Points (55 identified):

Category Points Priority Savings
Tool Output Formatting 12 HIGH ~40%
Hook Transformation 7 HIGH ~30%
LLM Prompt Construction 9 MEDIUM ~20%
Inter-Agent Transfer 8 MEDIUM ~15%
MCP Response Formatting 5 LOW ~35%

Top Targets (Phase 1):

  • session-formatter.ts - session lists (tabular, perfect for TOON)
  • task-list.ts - task arrays
  • skill-mcp/tools.ts - MCP tool/resource/prompt responses
  • glob/result-formatter.ts - file listings
  • tool-execute-after.ts hook - catch all tool outputs

Example transformation:

// Before: ~200 tokens
[{"id":"ses_1","messages":45,"agents":["sisyphus"]},{"id":"ses_2","messages":12,"agents":["oracle"]}]

// After: ~80 tokens
sessions[2]{id,messages,agents}:
  ses_1,45,[sisyphus]
  ses_2,12,[oracle]

Config schema:

{
  "features": {
    "toonCompression": {
      "enable": true,
      "threshold": 5000
    }
  }
}

Alternatives Considered

  • JSON-compact: Works for deeply nested data but TOON excels on uniform arrays
  • Custom compression: Reinventing the wheel; TOON is battle-tested (22k+ stars)
  • No compression: Current state, but token costs are significant

Feature Type

  • New MCP Integration (technically a library integration)
  • Configuration Option
  • Other (data serialization optimization)

Additional Context

Implementation effort: ~11 hours total (phased approach)

Existing patterns in codebase:

  • hash-computation.ts already uses AsyncGenerator for streaming
  • tool-output-truncator.ts handles large outputs (50k token limit)
  • dynamic-truncator.ts provides line-by-line truncation

Scope boundaries:

  • ✅ Include: Uniform arrays, tool outputs, MCP responses
  • ❌ Exclude: Config files (JSONC), deeply nested data, single objects

Contribution

  • I'm willing to submit a PR for this feature
  • I can help with testing
  • I can help with documentation

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions