Skip to main content

TokenEstimator

Struct TokenEstimator 

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

Estimates token counts for messages and tracks cumulative usage across turns.

§Token Estimation Strategy

Uses character-based approximation:

  • ASCII characters: 4 chars ≈ 1 token
  • Non-ASCII characters (CJK, emoji, etc.): 2 chars ≈ 1 token

This provides a reasonable estimate within ~20% of actual token counts for most common text patterns.

§Example

use talos_agent::token::{TokenEstimator, ModelPricing};
use talos_core::message::{Message, Usage};

let mut estimator = TokenEstimator::new();

// Estimate tokens for a set of messages
let messages = vec![
    Message::User { content: "Hello, world!".into() },
    Message::Assistant { content: "Hi there!".into(), tool_calls: vec![], reasoning: None },
];
let estimated = estimator.estimate(&messages);

// Track actual usage from a turn
estimator.track_usage(Usage {
    input_tokens: 100,
    output_tokens: 50,
    cache_read_tokens: 80,
    cache_write_tokens: 20,
    reasoning_tokens: 0,
});

// Get cumulative usage
let total = estimator.total_usage();
assert_eq!(total.input_tokens, 100);

// Estimate cost
let pricing = ModelPricing {
    input_per_1k: 0.003,
    output_per_1k: 0.015,
    cache_read_per_1k: 0.001,
    cache_write_per_1k: 0.002,
};
let cost = estimator.estimated_cost(&pricing);

Implementations§

Source§

impl TokenEstimator

Source

pub fn new() -> Self

Creates a new token estimator with zero cumulative usage.

Source

pub fn estimate(&self, messages: &[Message]) -> u32

Estimates the token count for a slice of messages.

Iterates over all message content (user text, assistant text, tool results) and applies character-based heuristics to approximate token count.

§Arguments
  • messages — The messages to estimate tokens for.
§Returns

The estimated total token count across all messages.

§Example
use talos_agent::token::TokenEstimator;
use talos_core::message::Message;

let estimator = TokenEstimator::new();
let messages = vec![
    Message::User { content: "Hello!".into() },
];
let tokens = estimator.estimate(&messages);
assert!(tokens > 0);
Source

pub fn estimate_text(text: &str) -> u32

Estimates the token count for a single string of text.

Uses character-based heuristics:

  • ASCII characters: 4 chars ≈ 1 token
  • Non-ASCII characters: 2 chars ≈ 1 token

Empty strings return 0 tokens.

§Arguments
  • text — The text to estimate tokens for.
§Returns

The estimated token count.

§Example
use talos_agent::token::TokenEstimator;

// English text: ~4 chars per token
let english = TokenEstimator::estimate_text("Hello, world!");
assert!(english > 0);

// CJK text: ~2 chars per token
let cjk = TokenEstimator::estimate_text("你好世界");
assert!(cjk > 0);

// Empty text: 0 tokens
let empty = TokenEstimator::estimate_text("");
assert_eq!(empty, 0);
Source

pub fn track_usage(&mut self, turn_usage: Usage)

Accumulates usage from a single turn into the cumulative total.

§Arguments
  • turn_usage — The usage statistics from a single turn.
§Example
use talos_agent::token::TokenEstimator;
use talos_core::message::Usage;

let mut estimator = TokenEstimator::new();
estimator.track_usage(Usage {
    input_tokens: 100,
    output_tokens: 50,
    cache_read_tokens: 80,
    cache_write_tokens: 20,
    reasoning_tokens: 0,
});

let total = estimator.total_usage();
assert_eq!(total.input_tokens, 100);
assert_eq!(total.output_tokens, 50);
Source

pub fn total_usage(&self) -> Usage

Returns the cumulative usage across all tracked turns.

§Returns

A Usage struct with the sum of all tracked turn usage.

§Example
use talos_agent::token::TokenEstimator;
use talos_core::message::Usage;

let mut estimator = TokenEstimator::new();
estimator.track_usage(Usage {
    input_tokens: 100,
    output_tokens: 50,
    cache_read_tokens: 0,
    cache_write_tokens: 0,
    reasoning_tokens: 0,
});
estimator.track_usage(Usage {
    input_tokens: 200,
    output_tokens: 75,
    cache_read_tokens: 100,
    cache_write_tokens: 50,
    reasoning_tokens: 0,
});

let total = estimator.total_usage();
assert_eq!(total.input_tokens, 300);
assert_eq!(total.output_tokens, 125);
assert_eq!(total.cache_read_tokens, 100);
assert_eq!(total.cache_write_tokens, 50);
Source

pub fn estimated_cost(&self, pricing: &ModelPricing) -> f64

Calculates the estimated cost based on cumulative usage and model pricing.

Uses simple multiplication: (tokens / 1000) * price_per_1k for each usage category.

§Arguments
  • pricing — The pricing information for the model.
§Returns

The estimated total cost in the currency unit of the pricing.

§Example
use talos_agent::token::{TokenEstimator, ModelPricing};
use talos_core::message::Usage;

let mut estimator = TokenEstimator::new();
estimator.track_usage(Usage {
    input_tokens: 1000,
    output_tokens: 500,
    cache_read_tokens: 800,
    cache_write_tokens: 200,
    reasoning_tokens: 0,
});

let pricing = ModelPricing {
    input_per_1k: 0.003,
    output_per_1k: 0.015,
    cache_read_per_1k: 0.001,
    cache_write_per_1k: 0.002,
};

let cost = estimator.estimated_cost(&pricing);
// (1000/1000)*0.003 + (500/1000)*0.015 + (800/1000)*0.001 + (200/1000)*0.002
// = 0.003 + 0.0075 + 0.0008 + 0.0004 = 0.0117
assert!((cost - 0.0117).abs() < 0.0001);

Trait Implementations§

Source§

impl Clone for TokenEstimator

Source§

fn clone(&self) -> TokenEstimator

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TokenEstimator

Source§

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

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

impl Default for TokenEstimator

Source§

fn default() -> TokenEstimator

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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<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