Skip to content

hooks/hermes: add transform_tool_result output filter for high-token tools#2261

Open
cfournel wants to merge 1 commit into
rtk-ai:developfrom
cfournel:feat/hermes-transform-tool-result-v2
Open

hooks/hermes: add transform_tool_result output filter for high-token tools#2261
cfournel wants to merge 1 commit into
rtk-ai:developfrom
cfournel:feat/hermes-transform-tool-result-v2

Conversation

@cfournel

@cfournel cfournel commented Jun 4, 2026

Copy link
Copy Markdown

Problem

The rtk-rewrite plugin currently only reduces tokens on the input side — rewriting terminal commands before execution via pre_tool_call. Tool outputs (what gets injected back into the conversation context) are untouched.

Analysis of 388 real Hermes sessions (~2,900 tool calls) shows four tools dominate output token consumption:

Tool Calls Est. output tokens Avg tok/call
browser_navigate 281 330,175 1,175
execute_code 410 344,030 839
read_file 130 132,701 1,020
terminal ~235 (observed)

terminal was identified as the largest single contributor in a real kanban session (55% of total context tokens — 23,502 chars across 25 calls), yet was previously unfiltered. Its output is a JSON wrapper {"output": "..."} containing raw shell output with blank lines, repeated lines, and long listings.

The browser_navigate output is a JSON blob containing an accessibility tree snapshot heavily padded with LayoutTable*, LayoutTableRow, LayoutTableCell, LayoutInline*, LayoutBlock* structural nodes that carry no semantic value for the model.

Solution

Add a transform_tool_result hook (supported by Hermes since model_tools.py:1116) that filters these tools before their output enters the context:

  • browser_navigate — parse JSON, strip Layout* accessibility-tree noise lines, deduplicate, drop stealth_warning/stealth_features metadata fields, truncate snapshot to 120 lines
  • terminal — parse JSON wrapper, apply line dedup + blank-line strip + truncate to 200 lines on the output field, repack JSON
  • read_file / execute_code — strip blank lines, deduplicate repeated lines, truncate to 200 lines

All filters fail open: returning None leaves the original result untouched. No existing behaviour is changed.

Benchmark

Tested live on https://news.ycombinator.com:

rtk: filtered browser_navigate: 8712 -> 6826 chars (21% reduction)

Observed on a real kanban session (t_c094df87, 42 messages, 1h32m):

Tool Calls Chars (unfiltered) Now filtered
terminal 25 23,502
execute_code 3 18,773
kanban_show 1 53

Changes

  • plugin.yaml — declare transform_tool_result hook so Hermes loads the plugin for it
  • __init__.py — add _transform_tool_result, _filter_browser, _filter_terminal, _filter_text; pre_tool_call path is untouched

🤖 Generated with Claude Code

@cfournel

cfournel commented Jun 4, 2026

Copy link
Copy Markdown
Author

This allows also to compact tools output in Hermes kanban so all cards are optimized too .

@cfournel cfournel force-pushed the feat/hermes-transform-tool-result-v2 branch from 20edd53 to e06c0a8 Compare June 5, 2026 07:39
… tools

Add a transform_tool_result hook filtering tool outputs before they enter
the conversation context. Covers 6 tools:

- terminal: JSON unwrap + line dedup + truncate 100L (~55% context reduction
  observed in kanban sessions)
- browser_navigate: strip Layout* accessibility-tree noise, dedup,
  truncate 120L (~21% reduction on real pages)
- read_file / execute_code: dedup + truncate 100L
- write_file: replace full diff with one-line summary (path + lines added/removed)
  — 40+ calls × 100-300L diffs were the main context killer in observed sessions
- search_files: truncate to 50 results
- kanban_show / kanban_list: keep last 10 events, drop older history

All filters fail open: None return leaves the original result untouched.
plugin.yaml updated to declare the transform_tool_result hook.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@cfournel cfournel force-pushed the feat/hermes-transform-tool-result-v2 branch from 8ba9760 to 2bf3707 Compare June 7, 2026 21:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant