use super::client::{AnthropicClient, convert_anthropic_error};
use adk_anthropic::{MessageCountTokensParams, Model};
use adk_core::{AdkError, LlmRequest};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TokenCount {
pub input_tokens: u32,
}
impl AnthropicClient {
pub async fn count_tokens(&self, request: &LlmRequest) -> Result<TokenCount, AdkError> {
let params =
Self::build_message_params(&self.model, self.max_tokens, request, &self.config)?;
let mut count_params =
MessageCountTokensParams::new(params.messages, Model::Custom(self.model.clone()));
if let Some(system) = params.system {
count_params = count_params.with_system(system);
}
if let Some(tools) = params.tools {
count_params = count_params.with_tools(tools);
}
if let Some(thinking) = params.thinking {
count_params = count_params.with_thinking(thinking);
}
let result =
self.client.count_tokens(count_params).await.map_err(convert_anthropic_error)?;
Ok(TokenCount { input_tokens: result.input_tokens })
}
}