feat(oneshot): add --usage-file JSON usage report to hermes -z (#59615)

* feat(oneshot): add --usage-file JSON usage report to hermes -z

Pipelines driving hermes -z (batch reviewers, cron scripts, eval
harnesses) had no way to account for per-invocation spend: the agent
computes estimated_cost_usd and full token counts internally, but
oneshot mode discards everything except the final response text.

- hermes -z PROMPT --usage-file PATH writes a JSON report after the
  run: estimated_cost_usd, cost_status/source, input/output/cache/
  reasoning/total tokens, api_calls, model, provider, session_id,
  completed, failed.
- Written even when the run fails (with a failure field) so callers
  can always account for spend; the write itself is best-effort and
  never masks the run's own outcome.
- Flag registered in both the full parser and the Termux fast path;
  added to both value-flag scan sets so profile detection stays
  correct.

Validation: 6 unit tests + live E2E (real -z run produced a report
with real OpenRouter cost + token counts).

* test: include usage_file kwarg in oneshot dispatch assertions

The two dispatch tests assert the exact kwargs dict passed to
run_oneshot; the new usage_file kwarg must appear there.
This commit is contained in:
Teknium 2026-07-06 11:10:51 -07:00 committed by GitHub
parent 7426c09bee
commit 7dfd5077ce
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 133 additions and 0 deletions

View file

@ -112,6 +112,17 @@ def build_top_level_parser():
"auto-bypassed. Intended for scripts / pipes."
),
)
parser.add_argument(
"--usage-file",
metavar="PATH",
default=None,
help=(
"One-shot mode only: after the run, write a JSON usage report "
"(estimated cost, token counts, model, api_calls) to PATH. "
"The report is written even when the run fails, so pipelines "
"can always account for spend. No effect outside -z/--oneshot."
),
)
# --model / --provider are accepted at the top level so they can pair
# with -z without needing the `chat` subcommand. If neither -z nor a
# subcommand consumes them, they fall through harmlessly as None.

View file

@ -399,6 +399,7 @@ def _apply_profile_override() -> None:
"-t", "--toolsets",
"-r", "--resume",
"-s", "--skills",
"--usage-file",
}
optional_value_flags = {"-c", "--continue"}
i = 0
@ -12240,6 +12241,7 @@ _TOP_LEVEL_VALUE_FLAGS = frozenset(
"-t", "--toolsets",
"-r", "--resume",
"-s", "--skills",
"--usage-file",
# ``-c / --continue`` is nargs='?' (optional value). Treat it as
# value-taking: if the next token is a subcommand-looking word
# the user almost certainly meant it as the session name, and
@ -12463,6 +12465,7 @@ def _try_termux_fast_cli_launch() -> bool:
model=getattr(args, "model", None),
provider=getattr(args, "provider", None),
toolsets=getattr(args, "toolsets", None),
usage_file=getattr(args, "usage_file", None),
)
)
@ -14112,6 +14115,7 @@ def main():
model=getattr(args, "model", None),
provider=getattr(args, "provider", None),
toolsets=getattr(args, "toolsets", None),
usage_file=getattr(args, "usage_file", None),
)
)

View file

@ -25,6 +25,7 @@ import logging
import os
import sys
from contextlib import redirect_stderr, redirect_stdout
from pathlib import Path
from typing import Optional
from hermes_cli.fallback_config import get_fallback_chain
@ -122,11 +123,49 @@ def _validate_explicit_toolsets(toolsets: object = None) -> tuple[list[str] | No
return valid, None
def _write_usage_file(path: Optional[str], result: dict, failure: Optional[str] = None) -> None:
"""Best-effort JSON usage report for pipelines (``-z --usage-file``).
Written even on failure so callers can always account for spend. Never
raises a broken usage write must not mask the run's own outcome.
"""
if not path:
return
try:
import json
report = {
"estimated_cost_usd": result.get("estimated_cost_usd"),
"cost_status": result.get("cost_status"),
"cost_source": result.get("cost_source"),
"input_tokens": result.get("input_tokens"),
"output_tokens": result.get("output_tokens"),
"cache_read_tokens": result.get("cache_read_tokens"),
"cache_write_tokens": result.get("cache_write_tokens"),
"reasoning_tokens": result.get("reasoning_tokens"),
"total_tokens": result.get("total_tokens"),
"api_calls": result.get("api_calls"),
"model": result.get("model"),
"provider": result.get("provider"),
"session_id": result.get("session_id"),
"completed": result.get("completed"),
"failed": bool(result.get("failed")) or failure is not None,
}
if failure is not None:
report["failure"] = failure
out = Path(path).expanduser()
out.parent.mkdir(parents=True, exist_ok=True)
out.write_text(json.dumps(report, indent=2) + "\n", encoding="utf-8")
except Exception:
pass
def run_oneshot(
prompt: str,
model: Optional[str] = None,
provider: Optional[str] = None,
toolsets: object = None,
usage_file: Optional[str] = None,
) -> int:
"""Execute a single prompt and print only the final content block.
@ -137,6 +176,10 @@ def run_oneshot(
provider: Optional provider override. Falls back to config.yaml's
model.provider, then "auto".
toolsets: Optional comma-separated string or iterable of toolsets.
usage_file: Optional path; when set, a JSON usage report (estimated
cost, token counts, model, api_calls) is written there after the
run even when the run fails so pipelines can account for
spend per invocation.
Returns the exit code. Caller should sys.exit() with the return.
"""
@ -209,11 +252,15 @@ def run_oneshot(
# Re-raise control-flow exceptions so the parent handles them as usual
# (Ctrl-C / explicit sys.exit() inside the agent).
if isinstance(failure, (KeyboardInterrupt, SystemExit)):
_write_usage_file(usage_file, result, failure=repr(failure))
raise failure
_write_usage_file(usage_file, result, failure=str(failure))
real_stderr.write(f"hermes -z: agent failed: {failure}\n")
real_stderr.flush()
return 1
_write_usage_file(usage_file, result)
if response:
real_stdout.write(response)
if not response.endswith("\n"):