Add tokens consumed and context percentage to per-tool timing footer

The per-tool timing line now shows:
- Tokens delta (tokens added to context by this tool call)
- Context window usage percentage

Example: └─ ️ 1ms  523tk | 49% ctx

Changes:
- Updated UiWriter trait print_tool_timing signature
- Track tokens before/after adding tool messages to calculate delta
- Updated ConsoleUiWriter, MachineUiWriter, PlannerUiWriter, and test mocks
This commit is contained in:
Dhanji R. Prasanna
2025-12-24 15:44:19 +11:00
parent fd22ce9890
commit cd64ebbf87
6 changed files with 16 additions and 8 deletions

View File

@@ -64,8 +64,10 @@ impl UiWriter for MachineUiWriter {
println!("TOOL_OUTPUT_LINES: {}", count);
}
fn print_tool_timing(&self, duration_str: &str) {
fn print_tool_timing(&self, duration_str: &str, tokens_delta: u32, context_percentage: f32) {
println!("TOOL_DURATION: {}", duration_str);
println!("TOKENS_DELTA: {}", tokens_delta);
println!("CONTEXT_PERCENTAGE: {:.0}", context_percentage);
println!("END_TOOL_OUTPUT");
println!();
}

View File

@@ -209,7 +209,7 @@ impl UiWriter for ConsoleUiWriter {
);
}
fn print_tool_timing(&self, duration_str: &str) {
fn print_tool_timing(&self, duration_str: &str, tokens_delta: u32, context_percentage: f32) {
// Parse the duration string to determine color
// Format is like "1.5s", "500ms", "2m 30.0s"
let color_code = if duration_str.ends_with("ms") {
@@ -251,7 +251,7 @@ impl UiWriter for ConsoleUiWriter {
""
};
println!("└─ ⚡️ {}{}\x1b[0m", color_code, duration_str);
println!("└─ ⚡️ {}{}\x1b[0m \x1b[2m{}tk | {:.0}% ctx\x1b[0m", color_code, duration_str, tokens_delta, context_percentage);
println!();
// Clear the stored tool info
*self.current_tool_name.lock().unwrap() = None;

View File

@@ -4072,6 +4072,9 @@ impl<W: UiWriter> Agent<W> {
}
};
// Track tokens before adding messages
let tokens_before = self.context_window.used_tokens;
self.context_window.add_message(tool_message);
self.context_window.add_message(result_message);
@@ -4107,8 +4110,11 @@ impl<W: UiWriter> Agent<W> {
// Closure marker with timing
if tool_call.tool != "final_output" {
let tokens_delta = self.context_window.used_tokens.saturating_sub(tokens_before);
self.ui_writer
.print_tool_timing(&Self::format_duration(exec_duration));
.print_tool_timing(&Self::format_duration(exec_duration),
tokens_delta,
self.context_window.percentage_used());
self.ui_writer.print_agent_prompt();
}

View File

@@ -39,7 +39,7 @@ pub trait UiWriter: Send + Sync {
fn print_tool_output_summary(&self, hidden_count: usize);
/// Print tool execution timing
fn print_tool_timing(&self, duration_str: &str);
fn print_tool_timing(&self, duration_str: &str, tokens_delta: u32, context_percentage: f32);
/// Print the agent prompt indicator
fn print_agent_prompt(&self);
@@ -99,7 +99,7 @@ impl UiWriter for NullUiWriter {
fn update_tool_output_line(&self, _line: &str) {}
fn print_tool_output_line(&self, _line: &str) {}
fn print_tool_output_summary(&self, _hidden_count: usize) {}
fn print_tool_timing(&self, _duration_str: &str) {}
fn print_tool_timing(&self, _duration_str: &str, _tokens_delta: u32, _context_percentage: f32) {}
fn print_agent_prompt(&self) {}
fn print_agent_response(&self, _content: &str) {}
fn notify_sse_received(&self) {}

View File

@@ -59,7 +59,7 @@ impl UiWriter for MockUiWriter {
fn update_tool_output_line(&self, _line: &str) {}
fn print_tool_output_line(&self, _line: &str) {}
fn print_tool_output_summary(&self, _hidden_count: usize) {}
fn print_tool_timing(&self, _duration_str: &str) {}
fn print_tool_timing(&self, _duration_str: &str, _tokens_delta: u32, _context_percentage: f32) {}
fn print_agent_prompt(&self) {}
fn print_agent_response(&self, _content: &str) {}
fn notify_sse_received(&self) {}

View File

@@ -265,7 +265,7 @@ impl g3_core::ui_writer::UiWriter for PlannerUiWriter {
fn update_tool_output_line(&self, _line: &str) {}
fn print_tool_output_line(&self, _line: &str) {}
fn print_tool_output_summary(&self, _hidden_count: usize) {}
fn print_tool_timing(&self, _duration_str: &str) {}
fn print_tool_timing(&self, _duration_str: &str, _tokens_delta: u32, _context_percentage: f32) {}
fn print_agent_prompt(&self) {
// No-op - don't add extra blank lines