Skip to main content

made_core/value_objects/
token_usage.rs

1//! [`TokenUsage`] — prompt + completion token counts for one LLM call.
2//!
3//! A small, immutable record of what a single model call cost in tokens.
4//! Naming the two counts as one value object keeps the metric port free
5//! of bare `u32` pairs that are trivial to transpose.
6
7/// Token counts reported by a model for a single call.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
9pub struct TokenUsage {
10    prompt: u32,
11    completion: u32,
12}
13
14impl TokenUsage {
15    /// Build a usage record from prompt (input) and completion (output)
16    /// token counts.
17    #[must_use]
18    pub const fn new(prompt: u32, completion: u32) -> Self {
19        Self { prompt, completion }
20    }
21
22    /// Tokens consumed by the request (the "prompt" / "input" side).
23    #[must_use]
24    pub const fn prompt(self) -> u32 {
25        self.prompt
26    }
27
28    /// Tokens produced in the response (the "completion" / "output" side).
29    #[must_use]
30    pub const fn completion(self) -> u32 {
31        self.completion
32    }
33}
34
35#[cfg(test)]
36mod tests {
37    use super::*;
38
39    #[test]
40    fn accessors_return_constructed_counts() {
41        let usage = TokenUsage::new(120, 45);
42        assert_eq!(usage.prompt(), 120);
43        assert_eq!(usage.completion(), 45);
44    }
45
46    #[test]
47    fn default_is_zero() {
48        assert_eq!(TokenUsage::default(), TokenUsage::new(0, 0));
49    }
50}