pub struct BudgetTracker { /* private fields */ }Expand description
Budget tracker for execution limits
Tracks resource usage during execution and checks against configured limits.
Implementations§
Source§impl BudgetTracker
impl BudgetTracker
Sourcepub fn new(config: BudgetConfig) -> Self
pub fn new(config: BudgetConfig) -> Self
Create a new budget tracker with the given configuration
§Examples
use juncture_core::pregel::budget::{BudgetTracker, BudgetConfig};
use std::time::Duration;
let config = BudgetConfig::new()
.with_max_tokens(1000)
.with_max_duration(Duration::from_secs(60));
let tracker = BudgetTracker::new(config);Sourcepub fn with_metrics_collector(
self,
collector: Option<Arc<dyn MetricsCollector>>,
) -> Self
pub fn with_metrics_collector( self, collector: Option<Arc<dyn MetricsCollector>>, ) -> Self
Set the metrics collector for emitting usage metrics
Sourcepub fn report_tokens(&self, tokens: u64)
pub fn report_tokens(&self, tokens: u64)
Sourcepub fn report_output_tokens(&self, tokens: u64)
pub fn report_output_tokens(&self, tokens: u64)
Report token output (generated tokens)
This is called separately to distinguish between input and output tokens for metrics purposes.
Sourcepub fn report_cost(&self, cost_usd: f64)
pub fn report_cost(&self, cost_usd: f64)
Report cost in USD
Cost is stored internally in micros-USD (1/1,000,000 of a USD) to avoid floating-point arithmetic in atomic operations.
§Examples
use juncture_core::pregel::budget::{BudgetTracker, BudgetConfig};
let tracker = BudgetTracker::new(BudgetConfig::new());
tracker.report_cost(0.001); // 0.001 USD
assert_eq!(tracker.current_usage().cost_usd, 0.001);Sourcepub fn report_step(&self)
pub fn report_step(&self)
Sourcepub fn report_llm_call(&self)
pub fn report_llm_call(&self)
Report an LLM call
This should be called when an LLM invocation completes successfully.
Sourcepub fn report_llm_duration(&self, duration_ms: u64)
pub fn report_llm_duration(&self, duration_ms: u64)
Report LLM call duration in milliseconds
This should be called when an LLM invocation completes.
Sourcepub fn report_tool_call(&self)
pub fn report_tool_call(&self)
Report tool call
This should be called when a tool invocation completes.
Sourcepub fn report_tool_error(&self)
pub fn report_tool_error(&self)
Report tool error
This should be called when a tool invocation fails.
Sourcepub fn report_tool_duration(&self, duration_ms: u64)
pub fn report_tool_duration(&self, duration_ms: u64)
Report tool execution duration in milliseconds
This should be called when a tool invocation completes.
Sourcepub fn report_usage(&self, tokens: u64, cost_usd: f64)
pub fn report_usage(&self, tokens: u64, cost_usd: f64)
Report token and cost usage from a model call
§Examples
use juncture_core::pregel::budget::{BudgetTracker, BudgetConfig};
let tracker = BudgetTracker::new(BudgetConfig::new());
tracker.report_usage(1000, 0.001);
assert_eq!(tracker.current_usage().tokens_used, 1000);
assert!((tracker.current_usage().cost_usd - 0.001).abs() < 0.0001);Sourcepub fn report_model_call(&self, input_tokens: u64, output_tokens: u64)
pub fn report_model_call(&self, input_tokens: u64, output_tokens: u64)
Report token usage from a model call with separate input/output counts
Adds the sum of input and output tokens to the total token counter. This is the primary method for integrating LLM provider usage reporting with budget enforcement.
§Examples
use juncture_core::pregel::budget::{BudgetTracker, BudgetConfig};
let tracker = BudgetTracker::new(BudgetConfig::new());
tracker.report_model_call(50, 150);
assert_eq!(tracker.current_usage().tokens_used, 200);Sourcepub fn check(&self) -> Option<BudgetExceededReason>
pub fn check(&self) -> Option<BudgetExceededReason>
Check if any budget limit has been exceeded
Returns Some(BudgetExceededReason) if a limit was exceeded,
or None if all limits are within bounds.
§Examples
use juncture_core::pregel::budget::{BudgetTracker, BudgetConfig};
use std::time::Duration;
let config = BudgetConfig::new().with_max_tokens(100);
let tracker = BudgetTracker::new(config);
tracker.report_tokens(150);
assert!(tracker.check().is_some());Sourcepub fn current_usage(&self) -> BudgetUsage
pub fn current_usage(&self) -> BudgetUsage
Get current usage statistics
§Examples
use juncture_core::pregel::budget::{BudgetTracker, BudgetConfig};
let tracker = BudgetTracker::new(BudgetConfig::new());
tracker.report_tokens(100);
tracker.report_cost(0.01);
tracker.report_step();
let usage = tracker.current_usage();
assert_eq!(usage.tokens_used, 100);
assert_eq!(usage.cost_usd, 0.01);
assert_eq!(usage.steps_completed, 1);
assert!(usage.duration.as_secs() < 1);