Skip to content

feat: per-step trajectory token metrics + list-price cost estimate on subscription#738

Merged
ericleepi314 merged 1 commit into
mainfrom
feat/trajectory-perstep-metrics-and-est-cost
Jul 22, 2026
Merged

feat: per-step trajectory token metrics + list-price cost estimate on subscription#738
ericleepi314 merged 1 commit into
mainfrom
feat/trajectory-perstep-metrics-and-est-cost

Conversation

@ericleepi314

Copy link
Copy Markdown
Collaborator

Summary

Closes the two documented trajectory-parity gaps vs harbor's built-in claude-code agent (surfaced by the chess-best-move comparison).

1. Per-step token metrics. claude-code's ATIF trajectory carries per-turn token usage on each agent step; clawcodex's didn't because the persisted conversation dropped per-message usage. AssistantMessage already holds the turn's usage (query.py sets usage=response.usage), but headless _persist dropped it and message_to_dict never serialized it. Fix (production src): thread usage through add_messagecreate_messagecreate_assistant_message, persist it in _persist, serialize it in message_to_dict (message_from_dict already loaded it). The adapter builds per-step ATIF Metrics from it.

2. Cost on subscription runs. record_api_usage zeroes billed cost under a subscription (correct for live /cost — plan allowance, not metered credits), so the cost block reported $0 while claude-code reports a list-price cost. Fix: build_cost_block also emits estimated_cost_usd — always computed via compute_cost (ignores billing_mode). Purely additive; never touches /cost or the budget gate. The adapter uses it for the cost column when billed cost is 0.

Test plan

  • Live opus-subscription trial (fix-git, reward 1.0): 11/12 steps carry per-step Metrics (user step none); session cost block total_cost_usd=0.0 (billed) + estimated_cost_usd=0.4906 (list-price) → trajectory/leaderboard cost_usd=0.4906 (was 0.0/None). Cost reconciles exactly with opus pricing.
  • Fresh headless deepseek run persists per-message usage → adapter attaches Metrics
  • New tests: usage round-trips through to_dict/from_dict, no usage key when absent; subscription→billed $0 but estimated==compute_cost, metered→estimate==billed
  • 1348 + 1046 tests pass across message/conversation/cost/headless/session/serialize/bridge
  • Critic → APPROVE (cost arithmetic independently reconciled)

🤖 Generated with Claude Code

… subscription

Closes the two documented trajectory-parity gaps vs the built-in
claude-code agent.

FOLLOWUP 1 — per-step token metrics. claude-code's trajectory carries
per-turn token usage on every agent step; clawcodex's didn't, because the
persisted conversation dropped it. AssistantMessage already holds the
turn's usage (query.py sets usage=response.usage), but:
  - headless _persist called add_message(role, content), dropping usage;
  - message_to_dict never serialized usage (message_from_dict already
    loaded it).
Fix (production src): thread usage through add_message → create_message →
create_assistant_message (which already accepts it), have _persist pass
msg.usage, and serialize usage in message_to_dict. Round-trips now. The
Harbor adapter builds per-step ATIF Metrics (prompt=input+cache_read+
cache_creation, cached=cache_read, completion=output) from it.

FOLLOWUP 2 — cost on subscription runs. record_api_usage zeroes billed
cost under a subscription (plan allowance, not metered credits — correct
for the live /cost display), so the session cost block reported $0 while
claude-code reports a list-price cost. Fix: build_cost_block now also
emits estimated_cost_usd — the always-computed list-price figure (via
compute_cost, ignoring billing_mode) — purely additive, never touching
/cost or the budget gate. The adapter uses it for the leaderboard/
trajectory cost column when billed cost is 0.

Verified: fresh headless run persists per-message usage → 4/5 steps get
Metrics (user step none, correct); subscription opus tokens → billed $0 /
estimated $1.10 (== compute_cost); metered → billed wins. 1348 tests pass
across message/conversation/cost/headless/session + new round-trip and
estimated-cost tests.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@ericleepi314
ericleepi314 merged commit 27f01cc into main Jul 22, 2026
2 checks passed
@ericleepi314
ericleepi314 deleted the feat/trajectory-perstep-metrics-and-est-cost branch July 22, 2026 08:38
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