Skip to content

Commit aa912ef

Browse files
ryoppippiDarcikes
andauthored
fix(statusline): apply pricing overrides (#1660)
Pass config pricingOverrides through statusline SharedArgs so third-party model costs use the configured rates. Cover defaults and command-level precedence. Co-authored-by: Darcikes <[email protected]>
1 parent 2176815 commit aa912ef

6 files changed

Lines changed: 258 additions & 5 deletions

File tree

‎apps/ccusage/config-schema.json‎

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,6 +1435,58 @@
14351435
"markdownDescription": "Use cached pricing data where supported.",
14361436
"type": "boolean"
14371437
},
1438+
"pricingOverrides": {
1439+
"additionalProperties": {
1440+
"additionalProperties": false,
1441+
"properties": {
1442+
"cacheCreationInputTokenCost": {
1443+
"format": "double",
1444+
"type": "number"
1445+
},
1446+
"cacheCreationInputTokenCostAbove200kTokens": {
1447+
"format": "double",
1448+
"type": "number"
1449+
},
1450+
"cacheReadInputTokenCost": {
1451+
"format": "double",
1452+
"type": "number"
1453+
},
1454+
"cacheReadInputTokenCostAbove200kTokens": {
1455+
"format": "double",
1456+
"type": "number"
1457+
},
1458+
"fastMultiplier": {
1459+
"format": "double",
1460+
"type": "number"
1461+
},
1462+
"inputCostPerToken": {
1463+
"format": "double",
1464+
"type": "number"
1465+
},
1466+
"inputCostPerTokenAbove200kTokens": {
1467+
"format": "double",
1468+
"type": "number"
1469+
},
1470+
"maxInputTokens": {
1471+
"format": "uint64",
1472+
"minimum": 0.0,
1473+
"type": "integer"
1474+
},
1475+
"outputCostPerToken": {
1476+
"format": "double",
1477+
"type": "number"
1478+
},
1479+
"outputCostPerTokenAbove200kTokens": {
1480+
"format": "double",
1481+
"type": "number"
1482+
}
1483+
},
1484+
"type": "object"
1485+
},
1486+
"description": "Runtime pricing overrides keyed by raw model name.",
1487+
"markdownDescription": "Runtime pricing overrides keyed by raw model name.",
1488+
"type": "object"
1489+
},
14381490
"refreshInterval": {
14391491
"default": 1,
14401492
"description": "Statusline refresh interval in seconds.",
@@ -3954,6 +4006,58 @@
39544006
"markdownDescription": "Use cached pricing data where supported.",
39554007
"type": "boolean"
39564008
},
4009+
"pricingOverrides": {
4010+
"additionalProperties": {
4011+
"additionalProperties": false,
4012+
"properties": {
4013+
"cacheCreationInputTokenCost": {
4014+
"format": "double",
4015+
"type": "number"
4016+
},
4017+
"cacheCreationInputTokenCostAbove200kTokens": {
4018+
"format": "double",
4019+
"type": "number"
4020+
},
4021+
"cacheReadInputTokenCost": {
4022+
"format": "double",
4023+
"type": "number"
4024+
},
4025+
"cacheReadInputTokenCostAbove200kTokens": {
4026+
"format": "double",
4027+
"type": "number"
4028+
},
4029+
"fastMultiplier": {
4030+
"format": "double",
4031+
"type": "number"
4032+
},
4033+
"inputCostPerToken": {
4034+
"format": "double",
4035+
"type": "number"
4036+
},
4037+
"inputCostPerTokenAbove200kTokens": {
4038+
"format": "double",
4039+
"type": "number"
4040+
},
4041+
"maxInputTokens": {
4042+
"format": "uint64",
4043+
"minimum": 0.0,
4044+
"type": "integer"
4045+
},
4046+
"outputCostPerToken": {
4047+
"format": "double",
4048+
"type": "number"
4049+
},
4050+
"outputCostPerTokenAbove200kTokens": {
4051+
"format": "double",
4052+
"type": "number"
4053+
}
4054+
},
4055+
"type": "object"
4056+
},
4057+
"description": "Runtime pricing overrides keyed by raw model name.",
4058+
"markdownDescription": "Runtime pricing overrides keyed by raw model name.",
4059+
"type": "object"
4060+
},
39574061
"refreshInterval": {
39584062
"default": 1,
39594063
"description": "Statusline refresh interval in seconds.",

‎rust/crates/ccusage-cli/src/types.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ pub struct StatuslineArgs {
163163
pub config: Option<PathBuf>,
164164
pub debug: bool,
165165
pub model_label_aliases: HashMap<String, String>,
166+
pub pricing_overrides: BTreeMap<String, PricingOverride>,
166167
}
167168

168169
#[derive(Clone)]
@@ -227,6 +228,7 @@ impl Default for StatuslineArgs {
227228
config: None,
228229
debug: false,
229230
model_label_aliases: HashMap::new(),
231+
pricing_overrides: BTreeMap::new(),
230232
}
231233
}
232234
}

‎rust/crates/ccusage-config/src/config.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,9 @@ fn apply_config_to_statusline_args(args: &mut StatuslineArgs, config: &ConfigCon
498498
if let Some(aliases) = options.model_label_aliases {
499499
args.model_label_aliases = aliases;
500500
}
501+
if let Some(pricing_overrides) = options.pricing_overrides {
502+
merge_pricing_overrides(&mut args.pricing_overrides, pricing_overrides);
503+
}
501504
}
502505
}
503506

‎rust/crates/ccusage-config/src/config_schema.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,8 @@ pub struct StatuslineSpecificOptions {
471471
pub debug: Option<bool>,
472472
/// Map model identifiers to short display labels.
473473
pub model_label_aliases: Option<HashMap<String, String>>,
474+
/// Runtime pricing overrides keyed by raw model name.
475+
pub pricing_overrides: Option<BTreeMap<String, ConfigPricingOverride>>,
474476
}
475477

476478
#[derive(Debug, Default, Deserialize, JsonSchema)]
@@ -638,6 +640,7 @@ impl StatuslineSpecificOptions {
638640
timezone: string_option(map, "timezone"),
639641
debug: bool_option(map, "debug"),
640642
model_label_aliases: hashmap_option(map, "modelLabelAliases"),
643+
pricing_overrides: pricing_override_map_option(map, "pricingOverrides"),
641644
}
642645
}
643646
}
@@ -1071,6 +1074,7 @@ mod tests {
10711074
"noCache",
10721075
"noOffline",
10731076
"offline",
1077+
"pricingOverrides",
10741078
"refreshInterval",
10751079
"timezone",
10761080
"visualBurnRate",

‎rust/crates/ccusage-config/src/snapshots/ccusage_config__config_schema__tests__snapshots_schema_agent_specific_option_edges.snap‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,58 @@ expression: "json!({\n \"rootRef\": schema[\"$ref\"], \"rootProperties\":\n
7272
"markdownDescription": "Use cached pricing data where supported.",
7373
"type": "boolean"
7474
},
75+
"pricingOverrides": {
76+
"additionalProperties": {
77+
"additionalProperties": false,
78+
"properties": {
79+
"cacheCreationInputTokenCost": {
80+
"format": "double",
81+
"type": "number"
82+
},
83+
"cacheCreationInputTokenCostAbove200kTokens": {
84+
"format": "double",
85+
"type": "number"
86+
},
87+
"cacheReadInputTokenCost": {
88+
"format": "double",
89+
"type": "number"
90+
},
91+
"cacheReadInputTokenCostAbove200kTokens": {
92+
"format": "double",
93+
"type": "number"
94+
},
95+
"fastMultiplier": {
96+
"format": "double",
97+
"type": "number"
98+
},
99+
"inputCostPerToken": {
100+
"format": "double",
101+
"type": "number"
102+
},
103+
"inputCostPerTokenAbove200kTokens": {
104+
"format": "double",
105+
"type": "number"
106+
},
107+
"maxInputTokens": {
108+
"format": "uint64",
109+
"minimum": 0.0,
110+
"type": "integer"
111+
},
112+
"outputCostPerToken": {
113+
"format": "double",
114+
"type": "number"
115+
},
116+
"outputCostPerTokenAbove200kTokens": {
117+
"format": "double",
118+
"type": "number"
119+
}
120+
},
121+
"type": "object"
122+
},
123+
"description": "Runtime pricing overrides keyed by raw model name.",
124+
"markdownDescription": "Runtime pricing overrides keyed by raw model name.",
125+
"type": "object"
126+
},
75127
"refreshInterval": {
76128
"default": 1,
77129
"description": "Statusline refresh interval in seconds.",

‎rust/crates/ccusage/src/commands/mod.rs‎

Lines changed: 93 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -331,10 +331,7 @@ pub(crate) fn run_statusline(args: StatuslineArgs) -> Result<()> {
331331

332332
let hook: StatuslineHook =
333333
serde_json::from_str(stdin.trim()).context("Invalid input format")?;
334-
let shared = SharedArgs {
335-
offline: args.offline && !args.no_offline,
336-
..SharedArgs::default()
337-
};
334+
let shared = statusline_shared(&args);
338335
let cache_enabled = args.cache && !args.no_cache;
339336
let cache_path = statusline_cache_path(&hook.session_id);
340337
let transcript_path = Path::new(&hook.transcript_path);
@@ -386,6 +383,14 @@ pub(crate) fn run_statusline(args: StatuslineArgs) -> Result<()> {
386383
Ok(())
387384
}
388385

386+
fn statusline_shared(args: &StatuslineArgs) -> SharedArgs {
387+
SharedArgs {
388+
offline: args.offline && !args.no_offline,
389+
pricing_overrides: args.pricing_overrides.clone(),
390+
..SharedArgs::default()
391+
}
392+
}
393+
389394
/// Resolve the model label shown in the statusline.
390395
///
391396
/// Looks up the model's `display_name` in the user-configured alias map and
@@ -842,9 +847,92 @@ struct HookContext {
842847

843848
#[cfg(test)]
844849
mod tests {
845-
use ccusage_test_support::fs_fixture;
850+
use std::ffi::OsString;
851+
852+
use ccusage_config::ConfigContext;
853+
use ccusage_test_support::{EnvVarGuard, fs_fixture};
846854

847855
use super::*;
856+
use crate::cli::{Cli, Command};
857+
858+
#[test]
859+
fn statusline_invocation_passes_config_pricing_overrides_to_cost_loading() {
860+
let fixture = fs_fixture!({
861+
"ccusage.json": r#"{
862+
"defaults": {
863+
"pricingOverrides": {
864+
"third-party-model": {
865+
"inputCostPerToken": 0.000001,
866+
"outputCostPerToken": 0.000002
867+
}
868+
}
869+
},
870+
"commands": {
871+
"statusline": {
872+
"pricingOverrides": {
873+
"third-party-model": {
874+
"outputCostPerToken": 0.000003
875+
}
876+
}
877+
}
878+
}
879+
}"#,
880+
"projects/project/session.jsonl": r#"{"timestamp":"2020-01-01T00:00:00.000Z","sessionId":"session","message":{"id":"message","model":"third-party-model","usage":{"input_tokens":100000,"output_tokens":100000}}}"#,
881+
});
882+
let _env = EnvVarGuard::set("CLAUDE_CONFIG_DIR", fixture.root());
883+
let config_path = fixture.path("ccusage.json");
884+
let config_path = config_path.to_string_lossy().into_owned();
885+
let config_args = vec![
886+
"statusline".to_string(),
887+
"--config".to_string(),
888+
config_path.clone(),
889+
];
890+
let config = ConfigContext::from_args(&config_args);
891+
let cli = Cli::parse_from_with_config(
892+
[
893+
"ccusage",
894+
"statusline",
895+
"--config",
896+
config_path.as_str(),
897+
"--cost-source",
898+
"ccusage",
899+
]
900+
.into_iter()
901+
.map(OsString::from),
902+
&config,
903+
DEFAULT_SESSION_DURATION_HOURS,
904+
env!("CCUSAGE_VERSION"),
905+
)
906+
.unwrap();
907+
let Some(Command::Statusline(args)) = cli.command else {
908+
panic!("expected statusline command");
909+
};
910+
911+
let shared = statusline_shared(&args);
912+
let pricing = &shared.pricing_overrides["third-party-model"];
913+
assert_eq!(pricing.input_cost_per_token, Some(0.000001));
914+
assert_eq!(pricing.output_cost_per_token, Some(0.000003));
915+
916+
let hook = StatuslineHook {
917+
session_id: "session".to_string(),
918+
transcript_path: fixture
919+
.path("transcript.jsonl")
920+
.to_string_lossy()
921+
.into_owned(),
922+
model: HookModel {
923+
id: Some("third-party-model".to_string()),
924+
display_name: "Third-party model".to_string(),
925+
},
926+
cost: None,
927+
context_window: Some(HookContext {
928+
total_input_tokens: 100_000,
929+
context_window_size: 200_000,
930+
}),
931+
effort: None,
932+
};
933+
let output = render_statusline(&hook, &args, &shared).unwrap();
934+
assert!(output.contains("💰 $0.40 session"), "{output}");
935+
}
848936

849937
#[test]
850938
fn calculates_context_tokens_from_latest_assistant_transcript_line() {

0 commit comments

Comments
 (0)