pub struct SessionTracker {
pub model: String,
pub turns: Vec<TurnUsage>,
}Expand description
Accumulates token usage across all turns of a single agent session.
The tracker is owned by CoderAgent::run() for the duration of a task. At the end of the task it is embedded in AgentResult so the REPL can display the final totals and store them in SQLite.
§Example
let mut tracker = SessionTracker::new("gpt-4o".to_string());
tracker.record(Some(&Usage { prompt_tokens: 100, completion_tokens: 50, total_tokens: 150 }));
assert_eq!(tracker.total_prompt_tokens(), 100);
assert_eq!(tracker.total_completion_tokens(), 50);Fields§
§model: StringName of the model in use (used for cost lookup).
turns: Vec<TurnUsage>Ordered list of per-turn records (earliest first).
Implementations§
Source§impl SessionTracker
impl SessionTracker
Sourcepub fn record(&mut self, usage: Option<&Usage>)
pub fn record(&mut self, usage: Option<&Usage>)
Record the usage from one LLM call.
If the API returned no usage data (None) the turn is silently
skipped — we never panic or error on missing usage.
Sourcepub fn total_prompt_tokens(&self) -> u32
pub fn total_prompt_tokens(&self) -> u32
Sum of all prompt tokens across every turn.
Sourcepub fn total_completion_tokens(&self) -> u32
pub fn total_completion_tokens(&self) -> u32
Sum of all completion tokens across every turn.
Sourcepub fn total_tokens(&self) -> u32
pub fn total_tokens(&self) -> u32
Grand total tokens (prompt + completion).
Sourcepub fn turn_count(&self) -> usize
pub fn turn_count(&self) -> usize
Number of turns recorded.
Sourcepub fn estimated_cost_usd(&self) -> Option<f64>
pub fn estimated_cost_usd(&self) -> Option<f64>
Estimate cost in USD using the built-in price table.
Returns None when the model isn’t found in the table.
Prices are per 1,000,000 tokens (as published by the providers).
Sourcepub fn summary_line(&self) -> String
pub fn summary_line(&self) -> String
Format a short one-line summary suitable for the completion banner.
Example: 15,678 tokens (~$0.047)
If no turns were recorded (provider didn’t return usage), returns "".
Sourcepub fn detailed_report(&self) -> String
pub fn detailed_report(&self) -> String
Format a detailed multi-line report for the /tokens REPL command.
Shows per-turn breakdown plus session totals and cost estimate.
Trait Implementations§
Source§impl Clone for SessionTracker
impl Clone for SessionTracker
Source§fn clone(&self) -> SessionTracker
fn clone(&self) -> SessionTracker
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more