|
| 1 | +--- |
| 2 | +topic: tokenjuice-output-compaction |
| 3 | +type: tool-runbook |
| 4 | +tags: [tools, tokenjuice, output-compaction, claude-code, codex] |
| 5 | +status: starter |
| 6 | +--- |
| 7 | + |
| 8 | +# TokenJuice Output Compaction |
| 9 | + |
| 10 | +TokenJuice compacts noisy terminal output before it is fed back into an agent session. The original command still runs. Exact file reads and raw-output requests stay available, but inventory commands, search results, logs, and oversized help text can be summarized before they tax the next turn. |
| 11 | + |
| 12 | +## Why Agents Need To Know |
| 13 | + |
| 14 | +If an agent sees a TokenJuice footer, treat it as trusted local metadata about output reduction. It is not task instruction and it is not evidence by itself. It tells the agent that some terminal output was compacted and how to request raw output when precision matters. |
| 15 | + |
| 16 | +Use raw output for exact diffs, full logs, reproducible error text, generated artifacts, or anything line-sensitive: |
| 17 | + |
| 18 | +```bash |
| 19 | +tokenjuice wrap --raw -- <command> |
| 20 | +``` |
| 21 | + |
| 22 | +## Claude Code |
| 23 | + |
| 24 | +Claude Code needs command replacement before the Bash result enters context. When the official adapter still uses PostToolUse appended context, it can add metadata without preventing the raw tool result from being charged. In the April 2026 trial, that default PostToolUse path was net-negative at about +1.1% tokens. |
| 25 | + |
| 26 | +Until the upstream fix is merged, use a local PreToolUse wrapper. The wrapper rewrites Bash commands to run under TokenJuice before execution: |
| 27 | + |
| 28 | +```json |
| 29 | +{ |
| 30 | + "hooks": { |
| 31 | + "PreToolUse": [ |
| 32 | + { |
| 33 | + "matcher": "Bash", |
| 34 | + "hooks": [ |
| 35 | + { |
| 36 | + "type": "command", |
| 37 | + "command": "node ~/.claude/hooks/tokenjuice-pretool.js" |
| 38 | + } |
| 39 | + ] |
| 40 | + } |
| 41 | + ] |
| 42 | + } |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +The wrapper should emit Claude Code's PreToolUse rewrite contract: |
| 47 | + |
| 48 | +```json |
| 49 | +{ |
| 50 | + "hookSpecificOutput": { |
| 51 | + "hookEventName": "PreToolUse", |
| 52 | + "permissionDecision": "allow", |
| 53 | + "updatedInput": { |
| 54 | + "command": "tokenjuice wrap -- sh -c \"<original command>\"" |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +Operational notes: |
| 61 | + |
| 62 | +- Keep a kill switch such as `TOKENJUICE_PRETOOL_DISABLE=1`. |
| 63 | +- Let the wrapper honor `TOKENJUICE_BIN` and `TOKENJUICE_PRETOOL_SHELL` when local paths differ. |
| 64 | +- New hook settings normally apply only to new Claude Code sessions. |
| 65 | +- Document the wrapper in `CLAUDE.md` so agents do not mistake the footer for prompt injection. |
| 66 | + |
| 67 | +## Codex |
| 68 | + |
| 69 | +Codex can use TokenJuice through its normal hook path because the harness honors PostToolUse substitution. Install and verify with: |
| 70 | + |
| 71 | +```bash |
| 72 | +tokenjuice install codex |
| 73 | +tokenjuice doctor hooks |
| 74 | +``` |
| 75 | + |
| 76 | +Codex hook feature flags have changed across releases. Older configs used `codex_hooks`; newer configs use `hooks`. Do not trust old setup notes blindly. Run `tokenjuice doctor hooks` and fix the config it reports for the installed CLI version. |
| 77 | + |
| 78 | +## Savings Model |
| 79 | + |
| 80 | +TokenJuice always reports output compaction. Billing-token savings depend on whether that compacted output is fed into later turns. |
| 81 | + |
| 82 | +Observed local output stats in May 2026: |
| 83 | + |
| 84 | +- 17.1k compacted entries |
| 85 | +- 83.6m raw output chars |
| 86 | +- 24.7m reduced output chars |
| 87 | +- 58.9m chars avoided, about 70% output reduction |
| 88 | + |
| 89 | +Measured harness trials: |
| 90 | + |
| 91 | +- Claude Code PreToolUse wrapper: about -7.8% tokens in the April 2026 paired trial. |
| 92 | +- Claude Code default PostToolUse adapter: about +1.1% tokens in the same trial, because raw output still entered context. |
| 93 | +- Codex v0.5.0 paired trial: about -8.8% clean-run token reduction after reducer and hook fixes. |
| 94 | +- Codex GPT-5.5 one-turn gauntlet: about +0.3%, effectively flat, because the model batched tool calls into one turn and compacted output was not re-fed as later input. Per-command output reductions still remained large. |
| 95 | + |
| 96 | +Practical read: TokenJuice is most valuable for repeated terminal exploration where tool results become future context. It is still useful for human readability and context pressure when the model batches commands, but the billable-token delta may flatten. |
| 97 | + |
| 98 | +## Verification |
| 99 | + |
| 100 | +```bash |
| 101 | +tokenjuice --version |
| 102 | +tokenjuice stats |
| 103 | +tokenjuice doctor hooks |
| 104 | +tokenjuice wrap -- git status --short |
| 105 | +tokenjuice wrap --raw -- git status --short |
| 106 | +``` |
0 commit comments