1use anyhow::Result;
2use serde::Serialize;
3
4#[derive(Serialize, Debug, clickhouse::Row)]
5pub struct LlmUsageEventRow {
6 pub time: i64,
7 pub user_id: i32,
8 pub is_staff: bool,
9 pub plan: String,
10 pub model: String,
11 pub provider: String,
12 pub input_token_count: u64,
13 pub output_token_count: u64,
14 pub requests_this_minute: u64,
15 pub tokens_this_minute: u64,
16 pub tokens_this_day: u64,
17 pub input_tokens_this_month: u64,
18 pub output_tokens_this_month: u64,
19 pub spending_this_month: u64,
20}
21
22pub async fn report_llm_usage(client: &clickhouse::Client, row: LlmUsageEventRow) -> Result<()> {
23 let mut insert = client.insert("llm_usage_events")?;
24 insert.write(&row).await?;
25 insert.end().await?;
26 Ok(())
27}