Separate paid fallback usage in operations reports

This commit is contained in:
wuyang
2026-11-04 10:22:03 +08:00
parent 713c3cd0a2
commit 7fc9d4d843
2 changed files with 15 additions and 3 deletions
+12 -2
View File
@@ -122,7 +122,9 @@ def scheduled_codex_usage(directory, start, end):
if not directory or not Path(directory).is_dir():
return {'available': False, 'reason': '定时运营汇总目录未配置或不可用'}
totals = {k: 0 for k in ('input_tokens', 'cached_input_tokens', 'output_tokens', 'reasoning_output_tokens')}
api_totals = dict.fromkeys(totals, 0)
runs = failed = 0
api_runs = api_uncertain = api_usage_events = 0
for path in Path(directory).glob('*.summary.json'):
try:
record = json.loads(path.read_text())
@@ -132,13 +134,21 @@ def scheduled_codex_usage(directory, start, end):
continue
runs += 1
failed += record.get('status') != 'complete'
if record.get('auth') == 'api_key':
api_runs += record.get('api_fallback_used') is True
api_uncertain += record.get('api_fallback_used') is None
for usage in record.get('usage', []):
api_usage_events += 1
for k in api_totals:
api_totals[k] += max(0, int(usage.get(k, 0)))
continue
for usage in record.get('usage', []):
for k in totals:
totals[k] += max(0, int(usage.get(k, 0)))
except (ValueError, OSError, KeyError, TypeError, AttributeError):
continue
return {'available': True, 'runs': runs, 'failed_runs': failed, **totals,
'measurement_note': '来自独立定时会话的运行汇总,与交互会话分开统计;失败或中断可能没有完整用量。'}
return {'available': True, 'runs': runs, 'failed_runs': failed, 'api_runs_separate': api_runs, 'api_runs_uncertain': api_uncertain, 'api_observed_usage': api_totals if api_usage_events else None, **totals,
'measurement_note': '来自独立定时会话的运行汇总,与交互会话分开统计;API 备用调用不计入订阅 token,实际成本单独核算。失败或中断可能没有完整用量。'}
def harness_run_usage(path, start, end):