Skip to content

Commit ed64503

Browse files
committed
Merge remote-tracking branch 'origin/main' into claude/add-openclaw-support-gFRkq
# Conflicts: # rust/crates/ccusage/src/cli.rs
2 parents a3b3ec5 + 2a02e16 commit ed64503

5 files changed

Lines changed: 134 additions & 92 deletions

File tree

‎rust/crates/ccusage/src/adapter/all.rs‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ fn codex_group_row(
550550
period: period.to_string(),
551551
agent: "codex",
552552
models_used: group.models.keys().cloned().collect(),
553-
input_tokens: group.input_tokens,
553+
input_tokens: codex::non_cached_input_tokens(group.input_tokens, group.cached_input_tokens),
554554
output_tokens: group.output_tokens,
555555
cache_creation_tokens: 0,
556556
cache_read_tokens: group.cached_input_tokens,
@@ -974,6 +974,7 @@ fn agent_label(agent: &str) -> &str {
974974
#[cfg(test)]
975975
mod tests {
976976
use super::*;
977+
use crate::CodexModelUsage;
977978
use std::{
978979
sync::{
979980
atomic::{AtomicUsize, Ordering},
@@ -1121,6 +1122,37 @@ mod tests {
11211122
assert_eq!(report["totals"]["totalTokens"], 130);
11221123
}
11231124

1125+
#[test]
1126+
fn uses_non_cached_codex_input_tokens_in_all_rows() {
1127+
let mut group = CodexGroup {
1128+
input_tokens: 100,
1129+
cached_input_tokens: 90,
1130+
output_tokens: 5,
1131+
total_tokens: 105,
1132+
..CodexGroup::default()
1133+
};
1134+
group.models.insert(
1135+
"gpt-5".to_string(),
1136+
CodexModelUsage {
1137+
input_tokens: 100,
1138+
cached_input_tokens: 90,
1139+
output_tokens: 5,
1140+
total_tokens: 105,
1141+
..CodexModelUsage::default()
1142+
},
1143+
);
1144+
let row = codex_group_row(
1145+
"2026-01-02",
1146+
&group,
1147+
&PricingMap::default(),
1148+
CodexSpeed::Standard,
1149+
);
1150+
1151+
assert_eq!(row.input_tokens, 10);
1152+
assert_eq!(row.cache_read_tokens, 90);
1153+
assert_eq!(row.total_tokens, 105);
1154+
}
1155+
11241156
#[test]
11251157
fn displays_total_tokens_with_cache_tokens_like_typescript_table() {
11261158
let row = AllRow {

‎rust/crates/ccusage/src/adapter/codex.rs‎

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -435,15 +435,21 @@ fn group_json(
435435
speed: CodexSpeed,
436436
) -> Value {
437437
let cost = calculate_group_cost(group, pricing, speed);
438+
let input_tokens = non_cached_input_tokens(group.input_tokens, group.cached_input_tokens);
439+
let models = group
440+
.models
441+
.iter()
442+
.map(|(model, usage)| (model.clone(), model_usage_json(usage)))
443+
.collect::<BTreeMap<_, _>>();
438444
let mut row = json!({
439445
period_key(kind): period,
440-
"inputTokens": group.input_tokens,
446+
"inputTokens": input_tokens,
441447
"cachedInputTokens": group.cached_input_tokens,
442448
"outputTokens": group.output_tokens,
443449
"reasoningOutputTokens": group.reasoning_output_tokens,
444450
"totalTokens": group.total_tokens,
445451
"costUSD": json_float(cost),
446-
"models": group.models,
452+
"models": models,
447453
});
448454
if kind == AgentReportKind::Session {
449455
row["lastActivity"] = json!(group.last_activity);
@@ -454,6 +460,21 @@ fn group_json(
454460
row
455461
}
456462

463+
pub(crate) fn non_cached_input_tokens(input_tokens: u64, cached_input_tokens: u64) -> u64 {
464+
input_tokens.saturating_sub(cached_input_tokens)
465+
}
466+
467+
fn model_usage_json(usage: &CodexModelUsage) -> Value {
468+
json!({
469+
"inputTokens": non_cached_input_tokens(usage.input_tokens, usage.cached_input_tokens),
470+
"cachedInputTokens": usage.cached_input_tokens,
471+
"outputTokens": usage.output_tokens,
472+
"reasoningOutputTokens": usage.reasoning_output_tokens,
473+
"totalTokens": usage.total_tokens,
474+
"isFallback": usage.is_fallback,
475+
})
476+
}
477+
457478
fn totals_json<'a>(
458479
groups: impl Iterator<Item = &'a CodexGroup>,
459480
pricing: &PricingMap,
@@ -466,7 +487,7 @@ fn totals_json<'a>(
466487
let mut total = 0;
467488
let mut cost = 0.0;
468489
for group in groups {
469-
input += group.input_tokens;
490+
input += non_cached_input_tokens(group.input_tokens, group.cached_input_tokens);
470491
cached += group.cached_input_tokens;
471492
output += group.output_tokens;
472493
reasoning += group.reasoning_output_tokens;
@@ -671,6 +692,41 @@ mod tests {
671692
assert_eq!(group.total_tokens, 280);
672693
}
673694

695+
#[test]
696+
fn reports_non_cached_codex_input_separately_from_cached_input() {
697+
let pricing = PricingMap::default();
698+
let report = report_json(
699+
&[CodexTokenUsageEvent {
700+
session_id: "session-1".to_string(),
701+
timestamp: "2026-01-02T00:00:00.000Z".to_string(),
702+
model: Some("gpt-5".to_string()),
703+
input_tokens: 100,
704+
cached_input_tokens: 90,
705+
output_tokens: 5,
706+
reasoning_output_tokens: 0,
707+
total_tokens: 105,
708+
is_fallback_model: false,
709+
}],
710+
AgentReportKind::Daily,
711+
Some("UTC"),
712+
&pricing,
713+
CodexSpeed::Standard,
714+
)
715+
.unwrap();
716+
717+
assert_eq!(report["daily"][0]["inputTokens"], 10);
718+
assert_eq!(report["daily"][0]["cachedInputTokens"], 90);
719+
assert_eq!(report["daily"][0]["totalTokens"], 105);
720+
assert_eq!(report["totals"]["inputTokens"], 10);
721+
assert_eq!(report["totals"]["cachedInputTokens"], 90);
722+
assert_eq!(report["totals"]["totalTokens"], 105);
723+
assert_eq!(report["daily"][0]["models"]["gpt-5"]["inputTokens"], 10);
724+
assert_eq!(
725+
report["daily"][0]["models"]["gpt-5"]["cachedInputTokens"],
726+
90
727+
);
728+
}
729+
674730
#[test]
675731
fn charges_cached_input_at_input_rate_when_codex_pricing_omits_cache_read_rate() {
676732
let mut pricing = PricingMap::default();

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

Lines changed: 35 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,43 +1463,17 @@ fn root_help_text() -> String {
14631463
" session Show all detected coding (agent) CLI usage grouped by session",
14641464
" blocks Show usage report grouped by session billing blocks",
14651465
" statusline Display compact status line for Claude Code hooks with hybrid time+file caching (Beta)",
1466-
" claude daily Show usage report grouped by date",
1467-
" claude monthly Show usage report grouped by month",
1468-
" claude weekly Show usage report grouped by week",
1469-
" claude session Show usage report grouped by conversation session",
1470-
" claude blocks Show usage report grouped by session billing blocks",
1471-
" claude statusline Display compact status line for Claude Code hooks with hybrid time+file caching (Beta)",
1472-
" codex daily Show Codex token usage grouped by day",
1473-
" codex monthly Show Codex token usage grouped by month",
1474-
" codex session Show Codex token usage grouped by session",
1475-
" opencode daily Show OpenCode token usage grouped by day",
1476-
" opencode weekly Show OpenCode token usage grouped by week",
1477-
" opencode monthly Show OpenCode token usage grouped by month",
1478-
" opencode session Show OpenCode token usage grouped by session",
1479-
" amp daily Show Amp token usage grouped by day",
1480-
" amp monthly Show Amp token usage grouped by month",
1481-
" amp session Show Amp token usage grouped by session",
1482-
" hermes daily Show Hermes usage grouped by date",
1483-
" hermes monthly Show Hermes usage grouped by month",
1484-
" hermes session Show Hermes usage grouped by session",
1485-
" pi daily Show pi-agent usage grouped by date",
1486-
" pi monthly Show pi-agent usage grouped by month",
1487-
" pi session Show pi-agent usage grouped by session",
1488-
" goose daily Show Goose usage grouped by date",
1489-
" goose monthly Show Goose usage grouped by month",
1490-
" goose session Show Goose usage grouped by session",
1491-
" kilo daily Show Kilo usage grouped by date",
1492-
" kilo monthly Show Kilo usage grouped by month",
1493-
" kilo session Show Kilo usage grouped by session",
1494-
" copilot daily Show GitHub Copilot CLI usage grouped by date",
1495-
" copilot monthly Show GitHub Copilot CLI usage grouped by month",
1496-
" copilot session Show GitHub Copilot CLI usage grouped by session",
1497-
" gemini daily Show Gemini CLI usage grouped by date",
1498-
" gemini monthly Show Gemini CLI usage grouped by month",
1499-
" gemini session Show Gemini CLI usage grouped by session",
1500-
" openclaw daily Show OpenClaw usage grouped by date",
1501-
" openclaw monthly Show OpenClaw usage grouped by month",
1502-
" openclaw session Show OpenClaw usage grouped by session",
1466+
" claude Show Claude Code usage commands",
1467+
" codex Show Codex token usage commands",
1468+
" opencode Show OpenCode token usage commands",
1469+
" amp Show Amp token usage commands",
1470+
" hermes Show Hermes usage commands",
1471+
" pi Show pi-agent usage commands",
1472+
" goose Show Goose usage commands",
1473+
" kilo Show Kilo usage commands",
1474+
" copilot Show GitHub Copilot CLI usage commands",
1475+
" gemini Show Gemini CLI usage commands",
1476+
" openclaw Show OpenClaw usage commands",
15031477
"",
15041478
"For more info, run any command with the `--help` flag:",
15051479
" ccusage daily --help",
@@ -1508,43 +1482,17 @@ fn root_help_text() -> String {
15081482
" ccusage session --help",
15091483
" ccusage blocks --help",
15101484
" ccusage statusline --help",
1511-
" ccusage claude daily --help",
1512-
" ccusage claude monthly --help",
1513-
" ccusage claude weekly --help",
1514-
" ccusage claude session --help",
1515-
" ccusage claude blocks --help",
1516-
" ccusage claude statusline --help",
1517-
" ccusage codex daily --help",
1518-
" ccusage codex monthly --help",
1519-
" ccusage codex session --help",
1520-
" ccusage opencode daily --help",
1521-
" ccusage opencode weekly --help",
1522-
" ccusage opencode monthly --help",
1523-
" ccusage opencode session --help",
1524-
" ccusage amp daily --help",
1525-
" ccusage amp monthly --help",
1526-
" ccusage amp session --help",
1527-
" ccusage hermes daily --help",
1528-
" ccusage hermes monthly --help",
1529-
" ccusage hermes session --help",
1530-
" ccusage pi daily --help",
1531-
" ccusage pi monthly --help",
1532-
" ccusage pi session --help",
1533-
" ccusage goose daily --help",
1534-
" ccusage goose monthly --help",
1535-
" ccusage goose session --help",
1536-
" ccusage kilo daily --help",
1537-
" ccusage kilo monthly --help",
1538-
" ccusage kilo session --help",
1539-
" ccusage copilot daily --help",
1540-
" ccusage copilot monthly --help",
1541-
" ccusage copilot session --help",
1542-
" ccusage gemini daily --help",
1543-
" ccusage gemini monthly --help",
1544-
" ccusage gemini session --help",
1545-
" ccusage openclaw daily --help",
1546-
" ccusage openclaw monthly --help",
1547-
" ccusage openclaw session --help",
1485+
" ccusage claude --help",
1486+
" ccusage codex --help",
1487+
" ccusage opencode --help",
1488+
" ccusage amp --help",
1489+
" ccusage hermes --help",
1490+
" ccusage pi --help",
1491+
" ccusage goose --help",
1492+
" ccusage kilo --help",
1493+
" ccusage copilot --help",
1494+
" ccusage gemini --help",
1495+
" ccusage openclaw --help",
15481496
"",
15491497
]
15501498
.map(str::to_string)
@@ -1986,25 +1934,27 @@ mod tests {
19861934
}
19871935

19881936
#[test]
1989-
fn help_lists_agent_namespace_commands() {
1937+
fn root_help_lists_agent_namespaces_without_nested_commands() {
19901938
let help = help_text();
1991-
assert!(help.contains("\n claude daily"));
1992-
assert!(help.contains("\n codex daily"));
1993-
assert!(help.contains("\n opencode daily"));
1994-
assert!(help.contains("\n amp daily"));
1995-
assert!(help.contains("\n pi daily"));
1996-
assert!(help.contains("\n copilot daily"));
1997-
assert!(help.contains("\n gemini daily"));
1998-
assert!(help.contains("\n openclaw daily"));
1939+
let agents = [
1940+
"claude", "codex", "opencode", "amp", "hermes", "pi", "goose", "kilo", "copilot",
1941+
"gemini", "openclaw",
1942+
];
1943+
1944+
for agent in agents {
1945+
assert!(help.contains(&format!("\n {agent} ")));
1946+
assert!(!help.contains(&format!("\n {agent} daily")));
1947+
}
19991948
}
20001949

20011950
#[test]
20021951
fn root_help_lists_command_descriptions_and_follow_up_help_commands() {
20031952
let help = help_text();
20041953

2005-
assert!(help.contains("codex daily Show Codex token usage grouped by day"));
1954+
assert!(help.contains("codex Show Codex token usage commands"));
20061955
assert!(help.contains("For more info, run any command with the `--help` flag:"));
2007-
assert!(help.contains("ccusage codex daily --help"));
1956+
assert!(help.contains("ccusage codex --help"));
1957+
assert!(!help.contains("ccusage codex daily --help"));
20081958
}
20091959

20101960
#[test]

‎rust/crates/ccusage/src/main.rs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,11 +668,16 @@ mod tests {
668668
.unwrap();
669669

670670
assert_eq!(report["daily"][0]["date"], "2026-01-02");
671-
assert_eq!(report["daily"][0]["inputTokens"], 100);
671+
assert_eq!(report["daily"][0]["inputTokens"], 90);
672672
assert_eq!(report["daily"][0]["cachedInputTokens"], 10);
673673
assert_eq!(report["daily"][0]["outputTokens"], 50);
674674
assert_eq!(report["daily"][0]["reasoningOutputTokens"], 0);
675675
assert_eq!(report["daily"][0]["totalTokens"], 150);
676+
assert_eq!(report["daily"][0]["models"]["gpt-5"]["inputTokens"], 90);
677+
assert_eq!(
678+
report["daily"][0]["models"]["gpt-5"]["cachedInputTokens"],
679+
10
680+
);
676681
assert_eq!(report["daily"][0]["costUSD"], json!(0.00061375));
677682
assert_eq!(report["totals"]["costUSD"], json!(0.00061375));
678683
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,7 @@ pub(crate) struct CodexTokenUsageEvent {
126126
pub(crate) is_fallback_model: bool,
127127
}
128128

129-
#[derive(Debug, Clone, Default, Serialize)]
130-
#[serde(rename_all = "camelCase")]
129+
#[derive(Debug, Clone, Default)]
131130
pub(crate) struct CodexModelUsage {
132131
pub(crate) input_tokens: u64,
133132
pub(crate) cached_input_tokens: u64,

0 commit comments

Comments
 (0)