Skip to main content

objectiveai_api/chat/completions/usage_handler/
log_usage_handler.rs

1//! Simple logging usage handler for development.
2
3use crate::ctx;
4use std::sync::Arc;
5
6/// Usage handler that logs completion costs to stdout.
7pub struct LogUsageHandler;
8
9#[async_trait::async_trait]
10impl<CTXEXT> super::UsageHandler<CTXEXT> for LogUsageHandler
11where
12    CTXEXT: Send + Sync + 'static,
13{
14    async fn handle_usage(
15        &self,
16        _ctx: ctx::Context<CTXEXT>,
17        _request: Option<Arc<objectiveai::chat::completions::request::ChatCompletionCreateParams>>,
18        response: objectiveai::chat::completions::response::unary::ChatCompletion,
19    ) {
20        println!(
21            "[{}] cost: {}",
22            response.id.as_str(),
23            response.usage.total_cost,
24        );
25    }
26}