Project
cortex
Description
When SubagentExecutor::run_subagent() requests a mandatory summary turn (lines 444-501 in executor.rs), it merges the summary turn's token counts into turn_ctx.tokens at lines 493-497:
// Update token counts
turn_ctx.tokens.input_tokens += summary_turn_ctx.tokens.input_tokens;
turn_ctx.tokens.output_tokens += summary_turn_ctx.tokens.output_tokens;
turn_ctx.tokens.cached_tokens += summary_turn_ctx.tokens.cached_tokens;
turn_ctx.tokens.reasoning_tokens += summary_turn_ctx.tokens.reasoning_tokens;
However, turn_ctx.tokens.total_tokens is not updated. The TokenUsage struct (defined in agent/mod.rs:234-247) stores total_tokens as a separate field — it is NOT computed from input_tokens + output_tokens. The proper way to update it is via TurnContext::add_tokens() (orchestrator.rs:85-88) which does self.tokens.total_tokens += input + output, but the manual addition at lines 493-497 skips this field entirely.
Later at line 533, session.record_turn(...) uses turn_ctx.tokens.total_tokens which now excludes the summary turn's tokens:
session.record_turn(
turn_ctx.tool_results.len() as u32,
turn_ctx.tokens.total_tokens as u64, // Missing summary tokens
);
Error Message
No error — the token count is silently wrong.
Debug Logs
No response
System Information
Cortex version: v0.0.7
OS: Linux
Affected file: src/cortex-engine/src/tools/handlers/subagent/executor.rs (lines 493-497, 533)
Related: src/cortex-engine/src/agent/mod.rs (lines 234-247 — TokenUsage struct)
Related: src/cortex-engine/src/agent/orchestrator.rs (lines 85-88 — add_tokens method)
Steps to Reproduce
- Spawn a subagent that completes its task but does NOT include summary markers (e.g.,
"## Summary for Orchestrator") in its response.
- The executor detects missing summary at line 447:
!has_summary_output(&result.response).
- A summary turn is executed (lines 454-469).
- If the summary succeeds, token counts are merged at lines 493-497 — but
total_tokens is skipped.
session.record_turn() at line 533 records turn_ctx.tokens.total_tokens which excludes the summary turn's tokens.
SubagentSession.tokens_used and SubagentResult.token_usage under-report actual token consumption.
Expected Behavior
turn_ctx.tokens.total_tokens should be updated alongside the other token fields when merging summary turn tokens. The missing line should be:
turn_ctx.tokens.total_tokens += summary_turn_ctx.tokens.total_tokens;
Or equivalently, use turn_ctx.add_tokens(summary_turn_ctx.tokens.input_tokens, summary_turn_ctx.tokens.output_tokens) which correctly updates all three fields.
Actual Behavior
total_tokens is not updated with summary turn tokens, causing session.tokens_used to under-report. This affects billing/quota tracking and user-facing token usage reports in SubagentResult.token_usage and ProgressEvent::Completed.total_tokens.
Additional Context
The TokenUsage struct stores total_tokens independently:
pub struct TokenUsage {
pub input_tokens: u32,
pub output_tokens: u32,
pub total_tokens: u32, // NOT computed — must be updated manually
pub cached_tokens: u32,
pub reasoning_tokens: u32,
}
The add_tokens method correctly maintains the invariant total_tokens = input + output, but the manual field-by-field addition at lines 493-497 breaks it by omitting total_tokens.
Project
cortex
Description
When
SubagentExecutor::run_subagent()requests a mandatory summary turn (lines 444-501 inexecutor.rs), it merges the summary turn's token counts intoturn_ctx.tokensat lines 493-497:However,
turn_ctx.tokens.total_tokensis not updated. TheTokenUsagestruct (defined inagent/mod.rs:234-247) storestotal_tokensas a separate field — it is NOT computed frominput_tokens + output_tokens. The proper way to update it is viaTurnContext::add_tokens()(orchestrator.rs:85-88) which doesself.tokens.total_tokens += input + output, but the manual addition at lines 493-497 skips this field entirely.Later at line 533,
session.record_turn(...)usesturn_ctx.tokens.total_tokenswhich now excludes the summary turn's tokens:Error Message
No error — the token count is silently wrong.
Debug Logs
No response
System Information
Cortex version: v0.0.7
OS: Linux
Affected file:
src/cortex-engine/src/tools/handlers/subagent/executor.rs(lines 493-497, 533)Related:
src/cortex-engine/src/agent/mod.rs(lines 234-247 —TokenUsagestruct)Related:
src/cortex-engine/src/agent/orchestrator.rs(lines 85-88 —add_tokensmethod)Steps to Reproduce
"## Summary for Orchestrator") in its response.!has_summary_output(&result.response).total_tokensis skipped.session.record_turn()at line 533 recordsturn_ctx.tokens.total_tokenswhich excludes the summary turn's tokens.SubagentSession.tokens_usedandSubagentResult.token_usageunder-report actual token consumption.Expected Behavior
turn_ctx.tokens.total_tokensshould be updated alongside the other token fields when merging summary turn tokens. The missing line should be:Or equivalently, use
turn_ctx.add_tokens(summary_turn_ctx.tokens.input_tokens, summary_turn_ctx.tokens.output_tokens)which correctly updates all three fields.Actual Behavior
total_tokensis not updated with summary turn tokens, causingsession.tokens_usedto under-report. This affects billing/quota tracking and user-facing token usage reports inSubagentResult.token_usageandProgressEvent::Completed.total_tokens.Additional Context
The
TokenUsagestruct storestotal_tokensindependently:The
add_tokensmethod correctly maintains the invarianttotal_tokens = input + output, but the manual field-by-field addition at lines 493-497 breaks it by omittingtotal_tokens.