Merged
Conversation
Track cumulative input/output/cache tokens, request count, and turn count across the session. Display via: - /tokens command — show session totals at any time - Exit summary — printed at all exit points (interactive, --prompt, SIGINT) - Per-turn inline display unchanged (📊 line) New state fields: totalInputTokens, totalOutputTokens, totalCacheReadTokens, totalRequests, totalTurns Accumulation happens in the assistant.usage event handler. Turn count incremented on each onUserPromptSubmitted. Signed-off-by: Simon Davies <simongdavies@users.noreply.github.com>
Add guidance to system message encouraging the LLM to use register_module when it identifies missing capabilities rather than just describing gaps. Modules persist across sessions. Signed-off-by: Simon Davies <simongdavies@users.noreply.github.com>
…rnings When register_module saves a user module, it now writes the sourceHash to the .json metadata. Previously only loadModuleAsync set this field, so re-registering a module with updated code would leave a stale hash that the validator flagged as a mismatch warning. Signed-off-by: Simon Davies <simongdavies@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Adds session-level token usage tracking to the agent and exposes it via a new /tokens command, with an automatic summary printed on exit.
Changes:
- Track cumulative input/output/cache tokens plus request/turn counts in
AgentStateand update them fromassistant.usageevents. - Add
/tokensslash command and an exit-time token summary renderer. - Improve module metadata persistence by keeping
sourceHashin sync; expand system message guidance around reusable modules.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| src/agent/system-message.ts | Adds guidance encouraging creation/import of reusable ha:* modules. |
| src/agent/state.ts | Extends AgentState with token/request/turn counters and initializes them in the factory. |
| src/agent/slash-commands.ts | Adds the /tokens command to print the session token summary. |
| src/agent/module-store.ts | Ensures module JSON metadata includes an updated/truncated sourceHash on save. |
| src/agent/llm-output.ts | Introduces formatTokenSummary() used by /tokens and exit-time reporting. |
| src/agent/index.ts | Prints token summary on exit paths and increments turn count on prompt submission. |
| src/agent/event-handler.ts | Accumulates token/request totals from assistant.usage SDK events. |
| src/agent/commands.ts | Registers /tokens in the help/tab-completion command registry. |
1. event-handler.ts: guard against undefined token values before accumulating 2. llm-output.ts: format large token numbers with toLocaleString, fix alignment 3. index.ts: use guard function for exit summary (no tokens = no summary) 4. state.ts: add doc comment clarifying totalRequests tracks premium requests 5. module-store.ts: use computeTruncatedHash instead of manual slice Signed-off-by: Simon Davies <simongdavies@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request introduces comprehensive session-level token usage tracking and reporting to the agent. It adds a new
/tokenscommand to display a summary of cumulative input, output, and cache tokens, as well as request and turn counts for the current session. These statistics are now tracked throughout the session and displayed automatically on exit, improving transparency around LLM usage. Additionally, there are minor improvements to module handling and system messaging.