feat(gateway): add opt-in token usage footer to responses - #11394
feat(gateway): add opt-in token usage footer to responses#11394Floris-Jan wants to merge 1 commit into
Conversation
Token data is tracked internally (session_prompt_tokens, etc.) but only visible via explicit /usage command. Users report 'token cost anxiety' — they want per-response visibility without asking. - New config: display.token_usage_footer (default: false) - Appends compact footer: '📊 1.2k in · 456 out · ~$0.0234 · claude-sonnet-4' - Gated by per-platform display config (same system as show_reasoning) - Skipped on failed responses
teknium1
left a comment
There was a problem hiding this comment.
Thanks for the focused opt-in footer contribution. The token/cost capability is not yet present on current main, but this implementation predates the merged canonical footer system and needs a rewrite rather than a direct salvage.
Problems
gateway/run.py:18818-18819populatesagent_result["input_tokens"]andagent_result["output_tokens"]from cumulative session counters. The proposed reads atgateway/run.py:4032-4033would display and price session totals after each reply, not per-response usage.- Current
gateway/runtime_footer.py:106-118is the canonical configurable footer renderer, merged ine123f4ecf(#17026). It supportsmodel,context_pct, andcwd; the proposed separatedisplay.token_usage_footerpath would bypass that system and its streaming trailing-footer handling ingateway/run.py:12065-12071. tests/gateway/test_token_usage_footer.py:6copies formatter logic into a test helper, so it does not exercise the production config, result, or delivery paths.
Suggested changes
- Extend
display.runtime_footer.fieldsandgateway/runtime_footer.pywith usage/cost fields. - Pass a per-turn usage delta to the renderer, then cover the real renderer and both streamed and non-streamed gateway delivery paths.
This is an automated hermes-sweeper review.
| _show_usage_footer = False | ||
|
|
||
| if _show_usage_footer and response and not agent_result.get("failed"): | ||
| _input_toks = agent_result.get("input_tokens", 0) or 0 |
There was a problem hiding this comment.
agent_result["input_tokens"] is populated from the cumulative AIAgent.session_prompt_tokens counter on current main (gateway/run.py:18818-18819), so this would show a growing session total on every reply rather than the claimed per-response usage. Please plumb a per-turn usage delta before rendering or pricing it.
| import pytest | ||
|
|
||
|
|
||
| def _format_usage_footer(input_tokens, output_tokens, model, cost_amount=None, cost_status="estimated"): |
There was a problem hiding this comment.
This helper duplicates production formatting rather than testing the gateway path. Please test the canonical footer renderer/delivery integration so config resolution, failed responses, and streamed trailing-footers are covered.
What does this PR do?
Token data is tracked internally (
session_prompt_tokens,session_completion_tokens) but only visible via explicit/usagecommand. Users report "token cost anxiety" — they want per-response visibility without asking.Changes:
display.token_usage_footer(default:false)📊 1.2k in · 456 out · ~$0.0234 · claude-sonnet-4show_reasoning)Type of Change
Changes Made
gateway/run.py— after reasoning display block, beforeagent:endhook: readsdisplay.token_usage_footerconfig (viaresolve_display_setting), formats compact footer with token counts + estimated cost + model name, appends to responsehermes_cli/config.py— added"token_usage_footer": Falsetodisplaydefaultstests/gateway/test_token_usage_footer.py(new) — 8 tests covering formatting (small/large counts, cost/no-cost, model name extraction) and config default verificationHow to Test
pytest tests/gateway/test_token_usage_footer.py -v— all 8 tests passdisplay.token_usage_footer: truein~/.hermes/config.yaml📊 X in · Y out · $Z · model-nameChecklist
feat(gateway): add opt-in token usage footer to responsespytest tests/gateway/test_token_usage_footer.py -v— 8 passedcli-config.yaml.example— N/A (no example file for gateway display keys)