Skip to content

feat(hooks): expose turn-end token usage - #1700

Open
Soham-o wants to merge 13 commits into
1jehuang:masterfrom
Soham-o:feat/1697-turn-end-token-usage
Open

Soham-o wants to merge 13 commits into
1jehuang:masterfrom
Soham-o:feat/1697-turn-end-token-usage

Conversation

@Soham-o

@Soham-o Soham-o commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

Summary

  • export summed turn token usage through the existing turn_end hook
  • add input/output token fields plus cache-read/cache-write fields when reported
  • sum only assistant messages from the turn's existing start_message_index
  • document provider-specific input semantics and add focused regression coverage

Fixes #1697.

Validation

  • focused unit coverage for turn-scoped assistant usage aggregation
  • cache fields remain omitted when no assistant message reports them
  • documentation updated for the new turn_end environment fields

Scope

Observer hook contract only; no changes to provider accounting or persisted session data.

@greptile-apps

greptile-apps Bot commented Oct 4, 2026 •

Copy link
Copy Markdown

RetriggerConfidence Score: 5/5

[Medium risk] Adds token usage metrics to turn-end hook events.

No outstanding issue blocks merging.

What we checked:

  • Executed the referenced sources crates/jcode-compaction-core/src/lib.rs::effective_context_tokens_from_usage and crates/jcode-app-core/src/agent/turn_execution.rs::turn_token_usage_tests to validate the streaming path that stores the derived prompt count and the turn_end emission of PROMPT_TOKENS. T-Rex
  • Ran normalization and aggregation tests, and observed that each revision passed 1 normalization test and 2 aggregation tests. T-Rex
  • Noted that the only code-change between revisions was reformatting the aggregation expression. T-Rex
  • Captured supporting artifacts documenting the run, including a shell script and two test-run logs for review. T-Rex

Summary

The PR adds prompt-token totals to the turn_end hook and documents the new token fields. No outstanding issue blocks merging.

Reviews (3) · Last reviewed commit: "fix(hooks): format prompt token aggregat..."

Comment thread crates/jcode-app-core/src/agent/turn_execution.rs Outdated
Comment thread crates/jcode-app-core/src/agent/turn_execution.rs Outdated
@greptile-apps

This comment has been minimized.

Soham-o commented Oct 4, 2026

Copy link
Copy Markdown
Contributor Author

CI is currently blocked only by the repository code-size ratchet (scripts/check_code_size_budget.py). The substantive checks for this PR are passing (module resolution, formatting, Clippy, warning budget, and Linux build/tests). The guardrail reports unrelated oversized-file regressions across the merge tree, so I’m not changing unrelated code or refreshing the baseline in this scoped hook patch. Please let me know if you want this handled as a repository-level baseline/ratchet update.

Soham-o commented Oct 4, 2026

Copy link
Copy Markdown
Contributor Author

Confirmed the guardrail failure is inherited: upstream master at 77f4f9d fails the same check_code_size_budget.py step even without #1700. I opened separate PR #1701 to synchronize the code-size baseline against current master (#692). That keeps this feature PR scoped to turn-end token usage.

@hoangdq08

Copy link
Copy Markdown

Thanks for picking this up so fast. One request: could the hook also export JCODE_HOOK_PROMPT_TOKENS (summed prompt_tokens)?

With only INPUT_TOKENS a consumer can't tell whether cache is included. In my sessions, 292 of 13,916 cached rows under a Claude session have input_tokens == prompt_tokens, so the raw input alone gives the wrong uncached count. prompt - cache_read - cache_write works for every provider.

Soham-o commented Oct 5, 2026

Copy link
Copy Markdown
Contributor Author

Implemented the requested JCODE_HOOK_PROMPT_TOKENS export in the existing turn-scoped aggregation path. It sums stored assistant prompt_tokens for the current turn, adds regression coverage, and documents the provider-reported semantics alongside the existing input/cache fields. The change stays scoped to the hook contract; the known repository code-size ratchet issue remains isolated in #1701.

@greptile-apps

greptile-apps Bot commented Oct 5, 2026

Copy link
Copy Markdown

Want your agent to iterate on Greptile's feedback? Try greploops.

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.

turn_end hook: include the turn's token usage

2 participants