dspy_rs/core/lm/
usage.rs

1use async_openai::types::CompletionUsage;
2use serde::{Deserialize, Serialize};
3use std::ops::Add;
4
5#[derive(Clone, Debug, Default, Serialize, Deserialize)]
6pub struct LmUsage {
7    pub prompt_tokens: u32,
8    pub completion_tokens: u32,
9    pub total_tokens: u32,
10    pub reasoning_tokens: Option<u32>,
11}
12
13impl From<CompletionUsage> for LmUsage {
14    fn from(usage: CompletionUsage) -> Self {
15        LmUsage {
16            prompt_tokens: usage.prompt_tokens,
17            completion_tokens: usage.completion_tokens,
18            total_tokens: usage.total_tokens,
19            reasoning_tokens: usage
20                .completion_tokens_details
21                .and_then(|details| details.reasoning_tokens),
22        }
23    }
24}
25
26impl Add for LmUsage {
27    type Output = LmUsage;
28
29    fn add(self, other: LmUsage) -> Self {
30        LmUsage {
31            prompt_tokens: self.prompt_tokens + other.prompt_tokens,
32            completion_tokens: self.completion_tokens + other.completion_tokens,
33            total_tokens: self.total_tokens + other.total_tokens,
34            reasoning_tokens: self.reasoning_tokens.or(other.reasoning_tokens),
35        }
36    }
37}