systemprompt_cli/commands/analytics/costs/
mod.rs1mod breakdown;
12pub mod summary;
13mod trends;
14
15use anyhow::Result;
16use clap::{Args, Subcommand};
17use schemars::JsonSchema;
18use serde::{Deserialize, Serialize};
19
20use crate::context::CommandContext;
21use crate::shared::render_result;
22
23#[derive(Debug, Args)]
29pub struct CostsArgs {
30 #[command(subcommand)]
31 pub cmd: Option<CostsCommands>,
32
33 #[command(flatten)]
34 pub summary: summary::SummaryArgs,
35}
36
37#[derive(Debug, Subcommand)]
38pub enum CostsCommands {
39 #[command(about = "Cost summary", alias = "list")]
40 Summary(summary::SummaryArgs),
41
42 #[command(about = "Cost trends over time")]
43 Trends(trends::TrendsArgs),
44
45 #[command(about = "Cost breakdown by model/agent")]
46 Breakdown(breakdown::BreakdownArgs),
47}
48
49#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
50pub struct CostSummaryOutput {
51 pub period: String,
52 pub total_cost_microdollars: i64,
53 pub total_requests: i64,
54 pub total_tokens: i64,
55 pub avg_cost_per_request_microdollars: f64,
56 pub change_percent: Option<f64>,
57 pub auto_widened_to: Option<String>,
58}
59
60#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
61pub struct CostTrendPoint {
62 pub timestamp: String,
63 pub cost_microdollars: i64,
64 pub request_count: i64,
65 pub tokens: i64,
66}
67
68#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
69pub struct CostTrendsOutput {
70 pub period: String,
71 pub group_by: String,
72 pub points: Vec<CostTrendPoint>,
73 pub total_cost_microdollars: i64,
74}
75
76#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
77pub struct CostBreakdownItem {
78 pub name: String,
79 pub cost_microdollars: i64,
80 pub request_count: i64,
81 pub tokens: i64,
82 pub percentage: f64,
83}
84
85#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
86pub struct CostBreakdownOutput {
87 pub period: String,
88 pub breakdown_by: String,
89 pub items: Vec<CostBreakdownItem>,
90 pub total_cost_microdollars: i64,
91}
92
93pub async fn execute(args: CostsArgs, ctx: &CommandContext) -> Result<()> {
94 let db_ctx = ctx.database().await?;
95 match args.cmd.unwrap_or(CostsCommands::Summary(args.summary)) {
96 CostsCommands::Summary(args) => {
97 let result = summary::execute_with_pool(args, &db_ctx, &ctx.cli).await?;
98 render_result(&result, &ctx.cli);
99 Ok(())
100 },
101 CostsCommands::Trends(args) => {
102 let result = trends::execute_with_pool(args, &db_ctx, &ctx.cli).await?;
103 render_result(&result, &ctx.cli);
104 Ok(())
105 },
106 CostsCommands::Breakdown(args) => {
107 let result = breakdown::execute_with_pool(args, &db_ctx, &ctx.cli).await?;
108 render_result(&result, &ctx.cli);
109 Ok(())
110 },
111 }
112}