Skip to main content

BudgetTracker

Struct BudgetTracker 

Source
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

Source

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);
Source

pub fn with_metrics_collector( self, collector: Option<Arc<dyn MetricsCollector>>, ) -> Self

Set the metrics collector for emitting usage metrics

Source

pub fn report_tokens(&self, tokens: u64)

Report token usage

§Examples
use juncture_core::pregel::budget::{BudgetTracker, BudgetConfig};

let tracker = BudgetTracker::new(BudgetConfig::new());
tracker.report_tokens(100);
assert_eq!(tracker.current_usage().tokens_used, 100);
Source

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.

Source

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);
Source

pub fn report_step(&self)

Report a completed step

§Examples
use juncture_core::pregel::budget::{BudgetTracker, BudgetConfig};

let tracker = BudgetTracker::new(BudgetConfig::new());
tracker.report_step();
tracker.report_step();
assert_eq!(tracker.current_usage().steps_completed, 2);
Source

pub fn report_llm_call(&self)

Report an LLM call

This should be called when an LLM invocation completes successfully.

Source

pub fn report_llm_duration(&self, duration_ms: u64)

Report LLM call duration in milliseconds

This should be called when an LLM invocation completes.

Source

pub fn report_tool_call(&self)

Report tool call

This should be called when a tool invocation completes.

Source

pub fn report_tool_error(&self)

Report tool error

This should be called when a tool invocation fails.

Source

pub fn report_tool_duration(&self, duration_ms: u64)

Report tool execution duration in milliseconds

This should be called when a tool invocation completes.

Source

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);
Source

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);
Source

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());
Source

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);

Trait Implementations§

Source§

impl Debug for BudgetTracker

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more