TokenLedgerRs 0.1.0

Token management and pricing governance CLI for AI coding agents
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
use chrono::{DateTime, Utc};
use clap::{Args, Parser, Subcommand, ValueEnum};
use serde::Serialize;
use std::path::PathBuf;

#[derive(Parser, Debug)]
#[command(name = "tokenledger")]
#[command(about = "Fast token/session usage and blended cost analytics")]
pub struct Cli {
    #[command(subcommand)]
    pub command: Command,
}

#[derive(Subcommand, Debug)]
pub enum Command {
    Monthly(MonthlyArgs),
    Daily(DailyArgs),
    Coverage(CoverageArgs),
    PricingCheck(PricingCheckArgs),
    PricingApply(PricingApplyArgs),
    PricingReconcile(PricingReconcileArgs),
    PricingLint(PricingLintArgs),
    PricingAudit(PricingAuditArgs),
    Ingest(IngestArgs),
    Bench(BenchArgs),
    Orchestrate(OrchestrateArgs),
    Benchmarks(BenchmarksArgs),
}

#[derive(Args, Debug, Clone)]
pub struct QueryArgs {
    #[arg(long = "events", required = true)]
    pub events: Vec<PathBuf>,
    #[arg(long, default_value = "pricing.example.json")]
    pub pricing: PathBuf,
    #[arg(long = "provider")]
    pub providers: Vec<String>,
    #[arg(long = "model")]
    pub models: Vec<String>,
    #[arg(long, help = "Limit rows for per-model output in table/markdown")]
    pub top_models: Option<usize>,
    #[arg(long, help = "Limit rows for per-provider output in table/markdown")]
    pub top_providers: Option<usize>,
    #[arg(long, default_value = "table")]
    pub output: OutputMode,
    #[arg(
        long,
        value_enum,
        default_value_t = OnUnpricedAction::Error,
        help = "Behavior when events reference provider/model entries missing from pricing"
    )]
    pub on_unpriced: OnUnpricedAction,
}

#[derive(Parser, Debug)]
pub struct MonthlyArgs {
    #[command(flatten)]
    pub query: QueryArgs,
    #[arg(long, help = "Month in YYYY-MM")]
    pub month: Option<String>,
}

#[derive(Parser, Debug)]
pub struct DailyArgs {
    #[command(flatten)]
    pub query: QueryArgs,
    #[arg(long, help = "Month in YYYY-MM")]
    pub month: Option<String>,
}

#[derive(Parser, Debug)]
pub struct CoverageArgs {
    #[arg(long = "events", required = true)]
    pub events: Vec<PathBuf>,
    #[arg(long, default_value = "pricing.example.json")]
    pub pricing: PathBuf,
    #[arg(long, help = "Month in YYYY-MM")]
    pub month: Option<String>,
    #[arg(long, default_value_t = false)]
    pub json_output: bool,
    #[arg(long, help = "Write suggested pricing patch JSON to this path")]
    pub write_patch: Option<PathBuf>,
    #[arg(long, help = "Write unpriced events JSONL to this path")]
    pub write_unpriced_events: Option<PathBuf>,
}

#[derive(Parser, Debug)]
pub struct PricingCheckArgs {
    #[arg(long = "events", required = true)]
    pub events: Vec<PathBuf>,
    #[arg(long, default_value = "pricing.example.json")]
    pub pricing: PathBuf,
    #[arg(long, help = "Month in YYYY-MM")]
    pub month: Option<String>,
    #[arg(
        long,
        default_value_t = false,
        help = "Return success even when unpriced events are found"
    )]
    pub allow_unpriced: bool,
    #[arg(long, help = "Write suggested pricing patch JSON to this path")]
    pub write_patch: Option<PathBuf>,
    #[arg(long, help = "Write unpriced events JSONL to this path")]
    pub write_unpriced_events: Option<PathBuf>,
}

#[derive(Parser, Debug)]
pub struct PricingApplyArgs {
    #[arg(long, required = true, help = "Pricing JSON file to update")]
    pub pricing: PathBuf,
    #[arg(long, required = true, help = "Pricing patch JSON to merge")]
    pub patch: PathBuf,
    #[arg(long, default_value_t = false)]
    pub dry_run: bool,
    #[arg(long, default_value_t = false)]
    pub write_backup: bool,
    #[arg(
        long,
        default_value_t = false,
        help = "Allow overwriting existing model rates/alias mappings"
    )]
    pub allow_overwrite_model_rates: bool,
}

#[derive(Parser, Debug)]
pub struct PricingReconcileArgs {
    #[arg(long = "events", required = true)]
    pub events: Vec<PathBuf>,
    #[arg(long, default_value = "pricing.example.json")]
    pub pricing: PathBuf,
    #[arg(long, help = "Month in YYYY-MM")]
    pub month: Option<String>,
    #[arg(long, default_value = "./benchmarks/results")]
    pub workdir: PathBuf,
    #[arg(
        long,
        default_value_t = false,
        help = "Return success even when unpriced events are found after reconcile"
    )]
    pub allow_unpriced: bool,
    #[arg(long, default_value_t = false)]
    pub dry_run: bool,
    #[arg(long, default_value_t = false)]
    pub write_backup: bool,
    #[arg(
        long,
        default_value_t = false,
        help = "Allow overwriting existing model rates/alias mappings"
    )]
    pub allow_overwrite_model_rates: bool,
}

#[derive(Parser, Debug)]
pub struct PricingLintArgs {
    #[arg(long, default_value = "pricing.example.json")]
    pub pricing: PathBuf,
    #[arg(
        long,
        default_value_t = false,
        help = "Return success even when placeholder model rates are found"
    )]
    pub allow_placeholders: bool,
}

#[derive(Parser, Debug)]
pub struct PricingAuditArgs {
    #[arg(long, default_value = "pricing.example.json")]
    pub pricing: PathBuf,
    #[arg(long, default_value_t = 30)]
    pub max_age_days: i64,
    #[arg(long, default_value_t = false)]
    pub allow_stale: bool,
    #[arg(long, default_value_t = false)]
    pub allow_missing_source: bool,
    #[arg(long, default_value_t = false)]
    pub json_output: bool,
}

#[derive(Parser, Debug)]
pub struct IngestArgs {
    #[arg(
        long = "provider",
        value_enum,
        help = "Provider adapter(s) to ingest from; repeatable. Defaults to all providers."
    )]
    pub providers: Vec<IngestProvider>,
    #[arg(long, help = "Output path for normalized JSONL")]
    pub output: PathBuf,
    #[arg(long, help = "Append to output JSONL instead of truncating file")]
    pub append: bool,
    #[arg(long, help = "Only include records at or after this RFC3339 timestamp")]
    pub since: Option<DateTime<Utc>>,
    #[arg(long, help = "Max number of normalized events to emit")]
    pub limit: Option<usize>,
    #[arg(
        long,
        help = "Checkpoint state file path (source path -> last_modified_unix)"
    )]
    pub state_file: Option<PathBuf>,
    #[arg(long, help = "Skip unchanged sources based on checkpoint state")]
    pub incremental: bool,
    #[arg(long, help = "Write structured ingest summary JSON to this path")]
    pub summary_json_path: Option<PathBuf>,
    #[arg(
        long,
        help = "Deduplicate emitted events by request key (provider, session, timestamp, model, token totals)"
    )]
    pub dedupe_by_request: bool,
}

#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, ValueEnum)]
pub enum IngestProvider {
    Claude,
    Codex,
    Proxyapi,
    Cursor,
    Droid,
}

#[derive(Parser, Debug)]
pub struct BenchArgs {
    #[arg(long = "events")]
    pub events: Vec<PathBuf>,
    #[arg(long, default_value = "pricing.example.json")]
    pub pricing: PathBuf,
    #[arg(long, default_value = "all")]
    pub scenario: BenchScenario,
    #[arg(long, help = "Month in YYYY-MM")]
    pub month: Option<String>,
    #[arg(long, default_value_t = 5)]
    pub warm_iterations: usize,
    #[arg(long, default_value_t = 10_000)]
    pub warm_tail_events: usize,
    #[arg(long, default_value_t = 2_000)]
    pub burst_batch_events: usize,
    #[arg(long, default_value_t = false)]
    pub json_output: bool,
    #[arg(long, value_enum, default_value_t = OnUnpricedAction::Error)]
    pub on_unpriced: OnUnpricedAction,
    #[arg(long, help = "Write benchmark JSON report to this path")]
    pub json_output_path: Option<PathBuf>,
    #[arg(long, help = "Load baseline benchmark JSON and include deltas")]
    pub baseline: Option<PathBuf>,
    #[arg(
        long,
        help = "Load golden benchmark JSON and validate elapsed-independent correctness fields"
    )]
    pub golden: Option<PathBuf>,
    #[arg(
        long,
        default_value_t = 0.0001,
        help = "Absolute epsilon for floating-point golden correctness comparisons"
    )]
    pub golden_epsilon: f64,
    #[arg(
        long,
        help = "Aggregate benchmark JSON reports from this directory and print trend summary"
    )]
    pub trend_dir: Option<PathBuf>,
    #[arg(
        long,
        help = "Write benchmark report to benchmarks/results/bench-<timestamp>.json and update latest-summary.json"
    )]
    pub record: bool,
    #[arg(long, help = "Optional label metadata stored in benchmark report")]
    pub label: Option<String>,
    #[arg(
        long,
        help = "In trend mode, fail if latest vs median exceeds thresholds from benchmarks/perf-gates.json"
    )]
    pub trend_fail_on_regression: bool,
}

#[derive(Parser, Debug)]
pub struct OrchestrateArgs {
    #[arg(long, default_value = "./examples/ingested.sample.jsonl")]
    pub events_out: PathBuf,
    #[arg(long)]
    pub state_file: Option<PathBuf>,
    #[arg(long)]
    pub since: Option<DateTime<Utc>>,
    #[arg(long)]
    pub limit: Option<usize>,
    #[arg(long = "providers", value_enum)]
    pub providers: Vec<IngestProvider>,
    #[arg(long, help = "Month in YYYY-MM")]
    pub month: Option<String>,
    #[arg(long, default_value = "pricing.example.json")]
    pub pricing: PathBuf,
    #[arg(long, value_enum, default_value_t = OnUnpricedAction::Error)]
    pub on_unpriced: OnUnpricedAction,
    #[arg(long)]
    pub skip_ingest: bool,
    #[arg(long)]
    pub skip_bench: bool,
    #[arg(long)]
    pub skip_gate: bool,
    #[arg(
        long,
        default_value_t = false,
        help = "Skip pricing reconcile stage (coverage -> apply -> check)"
    )]
    pub skip_pricing_reconcile: bool,
    #[arg(
        long,
        default_value = "./benchmarks/results",
        help = "Workdir for pricing reconcile artifacts"
    )]
    pub pricing_reconcile_workdir: PathBuf,
    #[arg(
        long,
        default_value_t = false,
        help = "Use reconcile workdir as-is (disable per-run timestamped artifact subdirectories)"
    )]
    pub pricing_reconcile_static_artifacts: bool,
    #[arg(
        long,
        default_value_t = false,
        help = "Run pricing reconcile in dry-run mode"
    )]
    pub pricing_reconcile_dry_run: bool,
    #[arg(
        long,
        default_value_t = false,
        help = "Write pricing backup during reconcile apply stage"
    )]
    pub pricing_reconcile_write_backup: bool,
    #[arg(
        long,
        default_value_t = false,
        help = "Allow pricing reconcile to overwrite existing model rates/aliases"
    )]
    pub pricing_reconcile_allow_overwrite_model_rates: bool,
    #[arg(
        long,
        default_value_t = false,
        help = "Allow pricing reconcile to succeed even when unpriced events remain"
    )]
    pub pricing_reconcile_allow_unpriced: bool,
    #[arg(
        long,
        default_value_t = false,
        help = "Run pricing-lint before monthly/daily/bench stages"
    )]
    pub pricing_lint: bool,
    #[arg(
        long,
        default_value_t = false,
        help = "Run pricing-audit before monthly/daily/bench stages"
    )]
    pub pricing_audit: bool,
    #[arg(
        long,
        default_value_t = 30,
        help = "Maximum allowed age of pricing metadata in days for pricing-audit"
    )]
    pub pricing_max_age_days: i64,
    #[arg(long)]
    pub summary_json_path: Option<PathBuf>,
    #[arg(
        long,
        help = "Optional deterministic ingest cache metadata path; unchanged inputs reuse existing --events-out output"
    )]
    pub ingest_cache_path: Option<PathBuf>,
    #[arg(
        long,
        help = "Optional aggregate cache path for monthly/daily outputs keyed by month/filter/pricing/events fingerprint"
    )]
    pub aggregate_cache_path: Option<PathBuf>,
    #[arg(
        long,
        help = "Write compact JSON snapshot for menu/statusbar UIs (CodexBar/OpenCode style)"
    )]
    pub ui_snapshot_path: Option<PathBuf>,
    #[arg(
        long,
        value_enum,
        default_value_t = UiSnapshotMode::Compact,
        help = "UI snapshot verbosity mode: compact (top lists) or extended (full provider/model breakdowns)"
    )]
    pub ui_snapshot_mode: UiSnapshotMode,
    #[arg(long, help = "Write orchestrate pipeline summary JSON to this path")]
    pub pipeline_summary_path: Option<PathBuf>,
}

#[derive(Copy, Clone, Debug, ValueEnum)]
pub enum BenchScenario {
    ColdBackfill,
    WarmTail,
    Burst,
    All,
}

#[derive(Copy, Clone, Debug, ValueEnum)]
pub enum OutputMode {
    Table,
    Markdown,
    Json,
}

pub use ParetoRs::OnUnpricedAction;

#[derive(Copy, Clone, Debug, Eq, PartialEq, ValueEnum, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum UiSnapshotMode {
    Compact,
    Extended,
}

// =============================================================================
// BENCHMARKS CLI
// =============================================================================

#[derive(Parser, Debug)]
pub struct BenchmarksArgs {
    #[command(subcommand)]
    pub command: BenchmarksCommand,
}

#[derive(Subcommand, Debug)]
pub enum BenchmarksCommand {
    /// Refresh benchmarks from all sources
    Refresh(RefreshBenchmarksArgs),
    /// List available benchmarks
    List(ListBenchmarksArgs),
    /// Show benchmark for a specific model
    Show(ShowBenchmarkArgs),
    /// Validate benchmark configuration
    Validate(ValidateBenchmarksArgs),
}

#[derive(Args, Debug)]
pub struct RefreshBenchmarksArgs {
    #[arg(long, help = "Artificial Analysis API key")]
    pub aa_api_key: Option<String>,

    #[arg(long, help = "OpenRouter API key")]
    pub openrouter_api_key: Option<String>,

    #[arg(long, default_value_t = false, help = "Skip API calls, use cache only")]
    pub no_fetch: bool,

    #[arg(long, help = "Output file for benchmarks JSON")]
    pub output: Option<PathBuf>,

    #[arg(long, help = "Comma-separated sources to refresh")]
    pub sources: Option<String>,
}

#[derive(Args, Debug)]
pub struct ListBenchmarksArgs {
    #[arg(long, help = "Filter by source (aa, openrouter, manual)")]
    pub source: Option<String>,

    #[arg(long, default_value_t = 20, help = "Limit number of results")]
    pub limit: usize,

    #[arg(long, default_value = "table", help = "Output format (table, json)")]
    pub output: String,

    #[arg(long, help = "Sort by field (intelligence, speed, cost)")]
    pub sort_by: Option<String>,
}

#[derive(Args, Debug)]
pub struct ShowBenchmarkArgs {
    #[arg(help = "Model ID to show")]
    pub model_id: String,

    #[arg(long, default_value = "table", help = "Output format")]
    pub output: String,
}

#[derive(Args, Debug)]
pub struct ValidateBenchmarksArgs {
    #[arg(long, help = "Config file to validate")]
    pub config: Option<PathBuf>,
}