Token counting reliability + summarization integration. (#3721)
This commit is contained in:
@@ -217,6 +217,34 @@ impl ProviderUsage {
|
||||
pub fn new(model: String, usage: Usage) -> Self {
|
||||
Self { model, usage }
|
||||
}
|
||||
|
||||
/// Ensures this ProviderUsage has token counts, estimating them if necessary
|
||||
pub async fn ensure_tokens(
|
||||
&mut self,
|
||||
system_prompt: &str,
|
||||
request_messages: &[Message],
|
||||
response: &Message,
|
||||
tools: &[Tool],
|
||||
) -> Result<(), ProviderError> {
|
||||
crate::providers::usage_estimator::ensure_usage_tokens(
|
||||
self,
|
||||
system_prompt,
|
||||
request_messages,
|
||||
response,
|
||||
tools,
|
||||
)
|
||||
.await
|
||||
.map_err(|e| ProviderError::ExecutionError(format!("Failed to ensure usage tokens: {}", e)))
|
||||
}
|
||||
|
||||
/// Combine this ProviderUsage with another, adding their token counts
|
||||
/// Uses the model from this ProviderUsage
|
||||
pub fn combine_with(&self, other: &ProviderUsage) -> ProviderUsage {
|
||||
ProviderUsage {
|
||||
model: self.model.clone(),
|
||||
usage: self.usage + other.usage,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Default, Copy)]
|
||||
|
||||
Reference in New Issue
Block a user