tokenjuice

repository·main·Indexed 19 days ago

https://github.com/vincentkoc/tokenjuice

A deterministic output compactor for terminal-heavy agent workflows (version 0.8.1). It uses rule-driven reducers to transform noisy command outputs from tools like git, pnpm, and docker into high-signal payloads for LLM contexts. It features a CLI with commands for wrapping execution (`wrap`), compacting existing files (`reduce`), and machine-to-machine integration (`reduce-json`), with supported host integrations for Claude Code, Cursor, GitHub Copilot CLI, and others.

Tokens
103.4K
Snippets
396
Records
540
Agent score
65%

What's inside tokenjuice

  1. Overview of tokenjuice

    main

    tokenjuice is a deterministic output compactor designed for terminal-heavy agent workflows. It is used to reduce the noise generated by commands like git status, pnpm test, docker build, rg, or pnpm --help.

    Instead of returning a massive wall of terminal text to an LLM context, tokenjuice observes the command output and returns a smaller, condensed payload built from rule-driven reducers. This approach preserves command semantics while minimizing transcript waste and reducing unnecessary reruns.

    Key characteristics:

    • Deterministic: Uses rule-driven reducers (inspectable JSON) rather than LLM-based summarization.
    • Non-magical: Keeps the shell behavior standard; raw output is only returned if explicitly requested via --raw or --full flags, or through opt-in artifact storage.
    • Thin Integrations: Host integrations act as thin wrappers around the core reducer logic.
  2. Overview of tokenjuice architecture and product surfaces

    main

    tokenjuice is a TypeScript-first output compaction system designed for terminal-heavy and agent-heavy workflows. It aims to reduce token waste in transcripts while keeping raw output recoverable and deterministic.

    The project consists of two primary surfaces:

    1. tokenjuice: The core library and CLI.
    2. Host Adapters (e.g., openclaw-tokenjuice): Thin wrappers that wire tokenjuice into host-specific hooks.

    Core vs. Adapters

    • The Core Package manages classification, reduction, artifact storage, rule loading/validation, and CLI behavior. It uses a plain-object heavy model without framework assumptions.
    • Host Adapters manage hook wiring, host-specific storage defaults, message conversion, and host-native installation (like extensions or hooks).
  3. How the agents.ge integration behaves

    main

    The agents.ge integration works by adding a source rule to your project memory. It does not intercept shell output directly; instead, it provides instructions that agents.ge propagates to managed agent entrypoints.

    Key behaviors include:

    • Command Preference: The rule instructs coding agents to prefer using tokenjuice wrap -- <command> for terminal commands that are likely to produce long outputs.
    • Output Authority: The rule instructs downstream agents to treat the compacted output produced by tokenjuice wrap as authoritative.
    • Escape Hatch: If you need to bypass the compaction behavior, you can use the --raw flag: tokenjuice wrap --raw -- <command>.
    tokenjuice wrap -- <command>
    tokenjuice wrap --raw -- <command>
  4. How AGENTS.md integration guidance works

    main

    The agents-md block is guidance-only. It does not change how the agent executes commands, nor does it grant the agent new tools or approvals; the agent still owns the tool execution and output handling.

    The guidance instructs agents to use specific tokenjuice wrapping commands to handle terminal output:

    • For noisy terminal commands: Use tokenjuice wrap -- <command>.
    • For exact byte requirements (escape hatch): Use tokenjuice wrap --raw -- <command>.

    Note: The guidance intentionally avoids suggesting tokenjuice wrap --full. Host-specific tokenjuice blocks can coexist with this generic block because they use distinct markers.

  5. Understand skip paths for Copilot CLI compaction

    main

    The tokenjuice runtime will skip rewriting (emitting {} instead) for the following scenarios:

    • Non-bash tool invocations.
    • Empty or missing tool_result / command.
    • Non-success result types (e.g., failure, rejected, denied). Raw error context is passed through untouched so the agent can see it.
    • Commands already wrapped with tokenjuice wrap --raw -- or --full.
    • Malformed JSON stdin.
  6. How Ruler integration behaves with tokenjuice rules

    main

    Ruler acts as a propagation layer that distributes a tokenjuice rule source to coding agents configured in your ruler.toml. It does not intercept shell output directly.

    When rules are propagated via Ruler, downstream agents follow these behaviors:

    • Command Wrapping: Agents are instructed to prefer using tokenjuice wrap -- <command> for terminal commands that are likely to produce long outputs.
    • Output Authority: Agents are instructed to treat compacted output as authoritative.
    • Escape Hatch: If you need to bypass the compaction/wrapping behavior, you can use the --raw flag: tokenjuice wrap --raw -- <command>.
  7. How tokenjuice works: the three surfaces

    main

    tokenjuice provides three primary ways to interact with its reduction engine:

    1. reduce: Compacts text that already exists in a file.
    2. wrap: Runs a command and compacts the observed output.
    3. reduce-json: Provides a stable machine protocol for host adapters by reading JSON from stdin/file and writing JSON to stdout.

    Host integrations are designed to be thin: they install a hook or extension, call the shared compactor, and return the compacted context through the host's native surface.

  8. Understand Kiro steering file limitations

    main
    Kiro steering files function as prompt guidance, not command hooks. While Kiro CLI hooks can observe tool calls and block usage before a tool is run, the steering file does not replace the actual tool output returned to the agent. It is meant to guide the agent's behavior rather than intercepting or modifying the underlying execution flow.
  9. How the Kilo Code integration behaves

    main

    The Kilo Code integration works by writing a workspace rule to .kilo/rules/tokenjuice.md and registering it in the instructions array of your kilo.jsonc (or .kilo/kilo.jsonc).

    Key Behaviors:

    • Command Preference: The rule instructs Kilo Code to prefer using tokenjuice wrap -- <command> for noisy terminal commands.
    • Authority: The rule instructs Kilo Code to treat compacted output as authoritative.
    • Escape Hatch: If you need to bypass the wrapping behavior, use the --raw flag: tokenjuice wrap --raw -- <command>.
    • Environment Override: You can use the KILO_PROJECT_DIR environment variable to override the workspace root for scripted installs or tests.
  10. Understand the Coder Agents integration limitations

    main

    Because Coder Agents workspace skills are designed for prompt/tool guidance rather than command hooks, this integration has specific boundaries:

    • It does not register MCP tools.
    • It does not change Coder templates.
    • It does not rewrite shell output.

    Its primary purpose is to provide Coder Agents with a discoverable workspace skill so the agent can decide when to invoke tokenjuice to wrap noisy terminal commands.

  11. How Roo Code integration behaves

    main

    The Roo Code integration works by providing prompt guidance via workspace rules rather than intercepting shell commands. When the rules are active, Roo Code is instructed to:

    1. Prefer wrapped commands: Use tokenjuice wrap -- <command> for noisy execute_command terminal commands to manage output.
    2. Trust compacted output: Treat compacted output as the authoritative source of truth.

    Escape Hatch: If you need to bypass the wrapping behavior and see the original output, use the --raw flag: tokenjuice wrap --raw -- <command>

    Important Note: This integration does not intercept or rewrite shell output. It is a set of instructions (rules) that Roo follows when it decides how to run terminal commands.

    tokenjuice wrap -- <command>
    
    # To bypass wrapping:
    tokenjuice wrap --raw -- <command>
  12. How Grok CLI integration behaves

    main

    The Grok CLI integration works by injecting compacted context when shell output is noisy. It specifically targets the bash tool.

    Key behaviors:

    • Targeting: Only successful PostToolUse payloads for the bash tool are processed.
    • Filtering: Empty output or outputs that provide low savings reductions are left untouched.
    • Compaction Rules: Safe repository inventory commands can be compacted. However, exact file-content inspection commands remain raw unless tokenjuice can generate a safe summary.
    • Preservation: Existing keys in ~/.grok/user-settings.json and unrelated hooks are preserved.
    • Context Injection: The integration uses the additionalContext field in Grok CLI PostToolUse hooks. It does not suppress or replace the original shell result; instead, it adds a compacted context block so Grok can access a smaller view while still retaining access to the original host-owned output path.

    To manually wrap a command for compaction, use the wrap command with the --raw flag:

    tokenjuice wrap --raw -- <command>