Skip to content

Commit e2de14e

Browse files
committed
📈 Add token usage reporting from LLM responses
Extract detailed usage statistics from PromptResponse to log input, output, and total token counts during agent execution. This enables better visibility into API consumption for cost monitoring and performance analysis. Uses extended_details() to get the full response including usage metrics rather than just the output text.
1 parent 485a0f9 commit e2de14e

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

src/agents/iris.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//! and multi-turn execution using Rig. One agent to rule them all! ✨
55
66
use anyhow::Result;
7-
use rig::agent::{Agent, AgentBuilder as RigAgentBuilder};
7+
use rig::agent::{Agent, AgentBuilder as RigAgentBuilder, PromptResponse};
88
use rig::client::builder::DynClientBuilder;
99
use rig::completion::{CompletionModel, Prompt};
1010
use schemars::JsonSchema;
@@ -551,11 +551,28 @@ Guidelines:
551551
"LLM request",
552552
"Sending prompt to agent with multi_turn(50)",
553553
);
554-
let response = agent.prompt(&full_prompt).multi_turn(50).await?;
554+
let prompt_response: PromptResponse = agent
555+
.prompt(&full_prompt)
556+
.multi_turn(50)
557+
.extended_details()
558+
.await?;
555559

556560
timer.finish();
557561

558-
debug::debug_llm_response(&response, std::time::Duration::from_secs(0), None);
562+
// Extract usage stats for debug output
563+
let usage = &prompt_response.total_usage;
564+
debug::debug_context_management(
565+
"Token usage",
566+
&format!(
567+
"input: {} | output: {} | total: {}",
568+
usage.input_tokens, usage.output_tokens, usage.total_tokens
569+
),
570+
);
571+
572+
let response = &prompt_response.output;
573+
#[allow(clippy::cast_possible_truncation, clippy::as_conversions)]
574+
let total_tokens_usize = usage.total_tokens as usize;
575+
debug::debug_llm_response(response, std::time::Duration::from_secs(0), Some(total_tokens_usize));
559576

560577
// Update status - synthesis phase
561578
crate::iris_status_dynamic!(
@@ -566,7 +583,7 @@ Guidelines:
566583
);
567584

568585
// Extract and parse JSON from the response
569-
let json_str = extract_json_from_response(&response)?;
586+
let json_str = extract_json_from_response(response)?;
570587
let sanitized_json = sanitize_json_response(&json_str);
571588
let sanitized_ref = sanitized_json.as_ref();
572589

0 commit comments

Comments
 (0)