Skip to main content

systemprompt_cli/commands/analytics/costs/
mod.rs

1//! Cost analytics: spend summary, trends over time, and breakdown by model or
2//! agent.
3//!
4//! Defines the [`CostsCommands`] subcommand tree and the typed output shapes
5//! ([`CostSummaryOutput`], [`CostTrendsOutput`], [`CostBreakdownOutput`])
6//! rendered by the `analytics costs` commands.
7//!
8//! Copyright (c) systemprompt.io — Business Source License 1.1.
9//! See <https://systemprompt.io> for licensing details.
10
11mod breakdown;
12mod 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/// `analytics costs` with no subcommand runs the summary.
24///
25/// That keeps the bare command runnable rather than a usage error. The
26/// summary's own arguments are flattened here, which leaves their clap defaults
27/// the single source of truth.
28#[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}
58
59#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
60pub struct CostTrendPoint {
61    pub timestamp: String,
62    pub cost_microdollars: i64,
63    pub request_count: i64,
64    pub tokens: i64,
65}
66
67#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
68pub struct CostTrendsOutput {
69    pub period: String,
70    pub group_by: String,
71    pub points: Vec<CostTrendPoint>,
72    pub total_cost_microdollars: i64,
73}
74
75#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
76pub struct CostBreakdownItem {
77    pub name: String,
78    pub cost_microdollars: i64,
79    pub request_count: i64,
80    pub tokens: i64,
81    pub percentage: f64,
82}
83
84#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
85pub struct CostBreakdownOutput {
86    pub period: String,
87    pub breakdown_by: String,
88    pub items: Vec<CostBreakdownItem>,
89    pub total_cost_microdollars: i64,
90}
91
92pub async fn execute(args: CostsArgs, ctx: &CommandContext) -> Result<()> {
93    let db_ctx = ctx.database().await?;
94    match args.cmd.unwrap_or(CostsCommands::Summary(args.summary)) {
95        CostsCommands::Summary(args) => {
96            let result = summary::execute_with_pool(args, &db_ctx, &ctx.cli).await?;
97            render_result(&result, &ctx.cli);
98            Ok(())
99        },
100        CostsCommands::Trends(args) => {
101            let result = trends::execute_with_pool(args, &db_ctx, &ctx.cli).await?;
102            render_result(&result, &ctx.cli);
103            Ok(())
104        },
105        CostsCommands::Breakdown(args) => {
106            let result = breakdown::execute_with_pool(args, &db_ctx, &ctx.cli).await?;
107            render_result(&result, &ctx.cli);
108            Ok(())
109        },
110    }
111}