Skip to main content

UsageTracker

Struct UsageTracker 

Source
pub struct UsageTracker { /* private fields */ }
Expand description

Tracks API usage using a sliding window algorithm for accurate quota management.

UsageTracker monitors both requests-per-minute (RPM) and tokens-per-minute (TPM) usage against configured quotas. It uses a bucket-based sliding window approach where each bucket represents a 1-second slot, providing more accurate rate limit tracking than traditional fixed-window counters.

§Sliding Window Algorithm

The tracker maintains BUCKET_COUNT buckets (default: 60), each covering a 1-second window. When checking usage or calculating backoff times, only buckets within the last BUCKETS_WINDOW_S seconds (default: 60) are considered valid.

§Example

use thryd::tracker::{UsageTracker, count_token};

let mut tracker = UsageTracker::with_quota(
    Some(100_000),  // TPM quota
    Some(60),       // RPM quota
);

// Add a request (input + output tokens)
tracker.add_request_raw(
    "Hello, world!".to_string(),
    "Hi there!".to_string(),
);

// Check remaining quota
let remaining_tpm = tracker.remaining_tpm_quota();
let remaining_rpm = tracker.remaining_rpm_quota();

// Check if we can make another request
if tracker.has_capacity() {
    println!("Can make request immediately");
} else {
    let wait_ms = tracker.need_wait_for_string("Another request".to_string());
    println!("Wait {}ms", wait_ms);
}

§Thread Safety

UsageTracker uses interior mutability and is not Sync. For multi-threaded usage, wrap in a mutex or use within a single-threaded context.

Implementations§

Source§

impl UsageTracker

Source

pub fn with_quota(tpm_quota: Option<Quota>, rpm_quota: Option<Quota>) -> Self

Creates a new UsageTracker with the specified quotas.

Pass None for a quota type to disable tracking for that dimension.

§Arguments
  • tpm_quota - Tokens-per-minute quota limit, or None to disable TPM tracking
  • rpm_quota - Requests-per-minute quota limit, or None to disable RPM tracking
§Example
use thryd::tracker::UsageTracker;

// Track both RPM and TPM
let tracker = UsageTracker::with_quota(Some(100_000), Some(60));

// Track only TPM (unlimited RPM)
let tracker = UsageTracker::with_quota(Some(100_000), None);
Source

pub fn add_request(&mut self, used_token: Quota) -> &mut Self

Records a request with a pre-counted token amount.

This increments the RPM counter by 1 and adds used_token to the TPM counter.

§Arguments
  • used_token - The number of tokens used by this request (input + output)
§Returns
  • &mut Self - Returns self for method chaining
Source

pub fn add_request_raw( &mut self, input_text: String, output_text: String, ) -> &mut Self

Records a request by automatically counting tokens in the input and output text.

This is a convenience method that calls count_token() on both strings and then calls add_request() with the sum.

§Arguments
  • input_text - The input/prompt text sent to the model
  • output_text - The model’s response text
§Returns
  • &mut Self - Returns self for method chaining
§Example
use thryd::tracker::UsageTracker;

let mut tracker = UsageTracker::with_quota(Some(100_000), Some(60));
tracker.add_request_raw(
    "What is the capital of France?".to_string(),
    "The capital of France is Paris.".to_string(),
);
Source

pub fn rpm_usage(&self) -> Option<Quota>

Returns the total number of requests in the current sliding window.

§Returns
  • Option<Quota> - Current RPM usage, or None if RPM tracking is disabled
Source

pub fn remaining_rpm_quota(&self) -> Option<Quota>

Returns the remaining request quota available in the current window.

§Returns
  • Option<Quota> - Remaining RPM quota, or None if RPM tracking is disabled
Source

pub fn tpm_usage(&self) -> Option<Quota>

Returns the total number of tokens used in the current sliding window.

§Returns
  • Option<Quota> - Current TPM usage, or None if TPM tracking is disabled
Source

pub fn remaining_tpm_quota(&self) -> Option<Quota>

Returns the remaining token quota available in the current window.

§Returns
  • Option<Quota> - Remaining TPM quota, or None if TPM tracking is disabled
Source

pub fn need_wait_for(&self, input_token: Quota) -> u64

Calculates the minimum wait time needed before a request with the given token count can be made without violating rate limits.

This considers both RPM and TPM limits, returning the maximum wait time required by either constraint.

§Arguments
  • input_token - Number of tokens in the incoming request
§Returns
  • u64 - Milliseconds to wait before the request can proceed. Returns 0 if there is sufficient capacity.
Source

pub fn need_wait_for_string(&self, input_string: String) -> u64

Calculates wait time for a request with text that will be token-counted first.

Convenience method that counts tokens in the input string and calls need_wait_for() with the result.

§Arguments
  • input_string - The input text to count tokens for
§Returns
  • u64 - Milliseconds to wait before the request can proceed
Source

pub fn has_capacity(&self) -> bool

Checks whether there is capacity to make a request without rate limiting.

This is a convenience check that verifies both RPM and TPM have remaining quota.

§Returns
  • bool - true if at least 1 RPM and 1 TPM quota remain, false otherwise. Returns true if the respective tracking is disabled (None quota).

Trait Implementations§

Source§

impl Debug for UsageTracker

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for UsageTracker

Source§

fn default() -> UsageTracker

Returns the “default value” for a type. 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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
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