llm_cost_tracker 0.8.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (150) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +136 -0
  3. data/README.md +14 -6
  4. data/app/assets/llm_cost_tracker/application.css +65 -5
  5. data/app/controllers/llm_cost_tracker/application_controller.rb +25 -33
  6. data/app/controllers/llm_cost_tracker/assets_controller.rb +1 -1
  7. data/app/controllers/llm_cost_tracker/calls_controller.rb +21 -11
  8. data/app/controllers/llm_cost_tracker/data_quality_controller.rb +4 -0
  9. data/app/controllers/llm_cost_tracker/reconciliation_controller.rb +106 -0
  10. data/app/controllers/llm_cost_tracker/tags_controller.rb +15 -1
  11. data/app/helpers/llm_cost_tracker/application_helper.rb +11 -1
  12. data/app/helpers/llm_cost_tracker/inline_style_helper.rb +28 -0
  13. data/app/helpers/llm_cost_tracker/reconciliation_helper.rb +13 -0
  14. data/app/helpers/llm_cost_tracker/token_usage_helper.rb +5 -1
  15. data/app/models/llm_cost_tracker/call.rb +0 -3
  16. data/app/models/llm_cost_tracker/call_line_item.rb +1 -5
  17. data/app/models/llm_cost_tracker/call_rollup.rb +0 -3
  18. data/app/models/llm_cost_tracker/call_tag.rb +0 -4
  19. data/app/models/llm_cost_tracker/ingestion/inbox_entry.rb +0 -4
  20. data/app/models/llm_cost_tracker/ingestion/lease.rb +0 -3
  21. data/app/models/llm_cost_tracker/provider_invoice.rb +7 -3
  22. data/app/models/llm_cost_tracker/provider_invoice_import.rb +29 -0
  23. data/app/services/llm_cost_tracker/dashboard/data_quality.rb +33 -4
  24. data/app/services/llm_cost_tracker/dashboard/filter.rb +6 -4
  25. data/app/services/llm_cost_tracker/dashboard/setup_state.rb +110 -0
  26. data/app/views/layouts/llm_cost_tracker/application.html.erb +6 -1
  27. data/app/views/llm_cost_tracker/calls/show.html.erb +26 -41
  28. data/app/views/llm_cost_tracker/dashboard/index.html.erb +9 -9
  29. data/app/views/llm_cost_tracker/data_quality/index.html.erb +92 -53
  30. data/app/views/llm_cost_tracker/reconciliation/index.html.erb +183 -0
  31. data/app/views/llm_cost_tracker/shared/_bar.html.erb +1 -1
  32. data/app/views/llm_cost_tracker/shared/_filters.html.erb +3 -0
  33. data/app/views/llm_cost_tracker/shared/_metric_stack.html.erb +1 -1
  34. data/app/views/llm_cost_tracker/tags/show.html.erb +60 -0
  35. data/config/routes.rb +3 -2
  36. data/lib/llm_cost_tracker/billing/components.rb +45 -3
  37. data/lib/llm_cost_tracker/billing/components.yml +71 -0
  38. data/lib/llm_cost_tracker/billing/cost_status.rb +21 -25
  39. data/lib/llm_cost_tracker/billing/line_item.rb +16 -50
  40. data/lib/llm_cost_tracker/budget.rb +31 -7
  41. data/lib/llm_cost_tracker/capture/stream_collector.rb +113 -34
  42. data/lib/llm_cost_tracker/capture/stream_tracker.rb +40 -5
  43. data/lib/llm_cost_tracker/configuration.rb +72 -17
  44. data/lib/llm_cost_tracker/doctor/capture_verifier.rb +1 -1
  45. data/lib/llm_cost_tracker/doctor/cost_drift_check.rb +2 -0
  46. data/lib/llm_cost_tracker/doctor/ingestion_check.rb +30 -4
  47. data/lib/llm_cost_tracker/doctor/invoice_reconciliation_check.rb +164 -0
  48. data/lib/llm_cost_tracker/doctor/legacy_audit_check.rb +0 -2
  49. data/lib/llm_cost_tracker/doctor/legacy_billing_status_check.rb +0 -2
  50. data/lib/llm_cost_tracker/doctor/schema_check.rb +5 -2
  51. data/lib/llm_cost_tracker/doctor.rb +72 -14
  52. data/lib/llm_cost_tracker/engine.rb +8 -0
  53. data/lib/llm_cost_tracker/errors.rb +3 -2
  54. data/lib/llm_cost_tracker/event.rb +48 -1
  55. data/lib/llm_cost_tracker/generators/llm_cost_tracker/async_ingestion_generator.rb +43 -0
  56. data/lib/llm_cost_tracker/generators/llm_cost_tracker/call_rollups_generator.rb +43 -0
  57. data/lib/llm_cost_tracker/generators/llm_cost_tracker/install_generator.rb +17 -26
  58. data/lib/llm_cost_tracker/generators/llm_cost_tracker/reconciliation_generator.rb +34 -0
  59. data/lib/llm_cost_tracker/generators/llm_cost_tracker/templates/create_llm_cost_tracker_async_ingestion.rb.erb +29 -0
  60. data/lib/llm_cost_tracker/generators/llm_cost_tracker/templates/create_llm_cost_tracker_call_rollups.rb.erb +15 -0
  61. data/lib/llm_cost_tracker/generators/llm_cost_tracker/templates/create_llm_cost_tracker_calls.rb.erb +5 -58
  62. data/lib/llm_cost_tracker/generators/llm_cost_tracker/templates/create_llm_cost_tracker_reconciliation.rb.erb +60 -0
  63. data/lib/llm_cost_tracker/generators/llm_cost_tracker/templates/initializer.rb.erb +35 -25
  64. data/lib/llm_cost_tracker/generators/llm_cost_tracker/templates/upgrade_call_rollups_provider.rb.erb +35 -0
  65. data/lib/llm_cost_tracker/generators/llm_cost_tracker/templates/upgrade_call_tags_key_value_index.rb.erb +32 -0
  66. data/lib/llm_cost_tracker/generators/llm_cost_tracker/templates/upgrade_image_tokens.rb.erb +18 -0
  67. data/lib/llm_cost_tracker/generators/llm_cost_tracker/templates/upgrade_provider_invoice_imports_provider.rb.erb +32 -0
  68. data/lib/llm_cost_tracker/generators/llm_cost_tracker/templates/upgrade_provider_invoices_metadata_index.rb.erb +25 -0
  69. data/lib/llm_cost_tracker/generators/llm_cost_tracker/upgrade_call_rollups_provider_generator.rb +29 -0
  70. data/lib/llm_cost_tracker/generators/llm_cost_tracker/upgrade_call_tags_key_value_index_generator.rb +30 -0
  71. data/lib/llm_cost_tracker/generators/llm_cost_tracker/upgrade_image_tokens_generator.rb +29 -0
  72. data/lib/llm_cost_tracker/generators/llm_cost_tracker/upgrade_provider_invoice_imports_provider_generator.rb +31 -0
  73. data/lib/llm_cost_tracker/generators/llm_cost_tracker/upgrade_provider_invoices_metadata_index_generator.rb +31 -0
  74. data/lib/llm_cost_tracker/ingestion/batch.rb +5 -2
  75. data/lib/llm_cost_tracker/ingestion/inbox.rb +3 -25
  76. data/lib/llm_cost_tracker/ingestion/pool.rb +44 -0
  77. data/lib/llm_cost_tracker/ingestion/worker.rb +28 -34
  78. data/lib/llm_cost_tracker/ingestion.rb +48 -11
  79. data/lib/llm_cost_tracker/integrations/anthropic.rb +31 -26
  80. data/lib/llm_cost_tracker/integrations/base.rb +35 -15
  81. data/lib/llm_cost_tracker/integrations/openai.rb +345 -84
  82. data/lib/llm_cost_tracker/integrations/ruby_llm.rb +111 -14
  83. data/lib/llm_cost_tracker/integrations.rb +33 -14
  84. data/lib/llm_cost_tracker/ledger/period/totals.rb +25 -7
  85. data/lib/llm_cost_tracker/ledger/rollups.rb +22 -17
  86. data/lib/llm_cost_tracker/ledger/schema/call_line_items.rb +41 -1
  87. data/lib/llm_cost_tracker/ledger/schema/call_rollups.rb +16 -6
  88. data/lib/llm_cost_tracker/ledger/schema/call_tags.rb +28 -2
  89. data/lib/llm_cost_tracker/ledger/schema/calls.rb +2 -4
  90. data/lib/llm_cost_tracker/ledger/schema/ingestion_inbox_entries.rb +57 -0
  91. data/lib/llm_cost_tracker/ledger/schema/ingestion_leases.rb +52 -0
  92. data/lib/llm_cost_tracker/ledger/schema/provider_invoice_imports.rb +56 -0
  93. data/lib/llm_cost_tracker/ledger/schema/provider_invoices.rb +28 -13
  94. data/lib/llm_cost_tracker/ledger/store.rb +34 -31
  95. data/lib/llm_cost_tracker/ledger/tags/encoding.rb +37 -0
  96. data/lib/llm_cost_tracker/ledger/tags/query.rb +2 -2
  97. data/lib/llm_cost_tracker/ledger.rb +2 -1
  98. data/lib/llm_cost_tracker/logging.rb +0 -4
  99. data/lib/llm_cost_tracker/masking.rb +39 -0
  100. data/lib/llm_cost_tracker/middleware/faraday.rb +120 -33
  101. data/lib/llm_cost_tracker/parsers/anthropic.rb +36 -28
  102. data/lib/llm_cost_tracker/parsers/azure.rb +46 -0
  103. data/lib/llm_cost_tracker/parsers/base.rb +53 -43
  104. data/lib/llm_cost_tracker/parsers/gemini.rb +24 -22
  105. data/lib/llm_cost_tracker/parsers/openai.rb +20 -38
  106. data/lib/llm_cost_tracker/parsers/openai_compatible.rb +26 -39
  107. data/lib/llm_cost_tracker/parsers/openai_service_charges.rb +81 -13
  108. data/lib/llm_cost_tracker/parsers/openai_usage.rb +126 -59
  109. data/lib/llm_cost_tracker/parsers.rb +31 -4
  110. data/lib/llm_cost_tracker/prices.json +572 -493
  111. data/lib/llm_cost_tracker/pricing/backfill.rb +140 -0
  112. data/lib/llm_cost_tracker/pricing/effective_prices.rb +7 -40
  113. data/lib/llm_cost_tracker/pricing/estimator.rb +33 -0
  114. data/lib/llm_cost_tracker/pricing/explainer.rb +4 -1
  115. data/lib/llm_cost_tracker/pricing/lookup.rb +73 -5
  116. data/lib/llm_cost_tracker/pricing/mode.rb +76 -0
  117. data/lib/llm_cost_tracker/pricing/registry.rb +3 -8
  118. data/lib/llm_cost_tracker/pricing/service_charges.rb +14 -12
  119. data/lib/llm_cost_tracker/pricing/{sync_change_printer.rb → sync/change_printer.rb} +3 -3
  120. data/lib/llm_cost_tracker/pricing/sync/registry_writer.rb +62 -1
  121. data/lib/llm_cost_tracker/pricing/sync.rb +4 -10
  122. data/lib/llm_cost_tracker/pricing/unknown.rb +5 -2
  123. data/lib/llm_cost_tracker/pricing.rb +117 -44
  124. data/lib/llm_cost_tracker/providers/anthropic/tier_classification.rb +22 -0
  125. data/lib/llm_cost_tracker/providers/azure/hosts.rb +17 -0
  126. data/lib/llm_cost_tracker/providers/gemini/model_families.rb +17 -0
  127. data/lib/llm_cost_tracker/providers/openai/hosts.rb +35 -0
  128. data/lib/llm_cost_tracker/providers/openai/model_families.rb +51 -0
  129. data/lib/llm_cost_tracker/railtie.rb +8 -0
  130. data/lib/llm_cost_tracker/reconcile_tasks.rb +134 -0
  131. data/lib/llm_cost_tracker/reconciliation/diff.rb +409 -0
  132. data/lib/llm_cost_tracker/reconciliation/diff_result.rb +44 -0
  133. data/lib/llm_cost_tracker/reconciliation/import_result.rb +19 -0
  134. data/lib/llm_cost_tracker/reconciliation/importer.rb +254 -0
  135. data/lib/llm_cost_tracker/reconciliation/sources/anthropic_usage.rb +172 -0
  136. data/lib/llm_cost_tracker/reconciliation/sources/fingerprint.rb +20 -0
  137. data/lib/llm_cost_tracker/reconciliation/sources/openai_usage.rb +142 -0
  138. data/lib/llm_cost_tracker/reconciliation.rb +118 -0
  139. data/lib/llm_cost_tracker/report/data.rb +4 -1
  140. data/lib/llm_cost_tracker/report.rb +0 -4
  141. data/lib/llm_cost_tracker/retention.rb +31 -6
  142. data/lib/llm_cost_tracker/tags/context.rb +3 -4
  143. data/lib/llm_cost_tracker/tags/sanitizer.rb +73 -21
  144. data/lib/llm_cost_tracker/token_usage.rb +14 -2
  145. data/lib/llm_cost_tracker/tracker.rb +41 -55
  146. data/lib/llm_cost_tracker/version.rb +1 -1
  147. data/lib/llm_cost_tracker.rb +19 -14
  148. data/lib/tasks/llm_cost_tracker.rake +41 -4
  149. metadata +49 -3
  150. data/lib/llm_cost_tracker/usage_capture.rb +0 -58
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 357a557efba70db48baf47dde01b0aec7ee8f17787c09276a608572037e1b405
4
- data.tar.gz: dd91244e99ecbc531d592572ea419a6567d51980f1f95f9eedb0a929f52ffb71
3
+ metadata.gz: e84eab476358c65154ce99e1c1b95d91406094a1221e3724253b3a2efb471ed5
4
+ data.tar.gz: 8793c30b7cdbd161cc9ea4b242969f8c3ba61e164aa5779c84e33e4d5ac38db5
5
5
  SHA512:
6
- metadata.gz: a9ef38be029273bb2df21e9837e92d166ccb3c931c859437a3e5ace57daebbfaf78db182b93a87aedd9be1df18ef26937cae47b3382a61489fe1e4b1cdd0b669
7
- data.tar.gz: 852695ad7b07962ad540b16669e285324b1af5059e975ca645aa2670f8ed5069640e06207a06f170edcdfc19a22e9aacdab684caf831feaa7c334a3d52f41a67
6
+ metadata.gz: b377e608cf26ae425ac6eda48213878b14d108aa559c56035279e83f0414370db21fc05f8115d27094a92beaa380959b50a21c46730f7cc4ee8b63f8db7f2ad8
7
+ data.tar.gz: b53b1587f4080f74140cc73854edd1d1916b66a6e8e4b520a33b4897a0d5b428798c9cd6f248e189628d5fd6f71460727306523de0ce7268ced2c0a183b9b5bd
data/CHANGELOG.md CHANGED
@@ -2,6 +2,142 @@
2
2
 
3
3
  Format: [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). Versioning: [SemVer](https://semver.org/spec/v2.0.0.html).
4
4
 
5
+ ## [0.10.0] - 2026-05-17
6
+
7
+ ### Added
8
+
9
+ - `budget_exceeded_behavior = :block_requests` now also blocks before send when an estimate of the call's input cost plus prior spend would cross the daily / monthly limit, or when the estimate alone crosses `per_call_budget`. `BudgetExceededError` and `on_budget_exceeded` payloads gain a `stage` field (`:pre_send` or `:post_spend`). Calls to models with no pricing match skip the pre-send check. See [Budgets](docs/budgets.md).
10
+ - `bin/rails llm_cost_tracker:backfill_unknown_pricing` rake task recomputes cost, pricing snapshot, line-item costs, and rollup buckets for calls that landed with no pricing (e.g. a new model recorded before the next scraper refresh added its rates). The Data Quality dashboard's "Unknown pricing by model" panel points at this task. Idempotent — re-running only touches calls still missing a cost.
11
+ - Azure OpenAI Service is captured out of the box across both Azure OpenAI (`*.openai.azure.com`) and Microsoft Foundry (`*.services.ai.azure.com`) hostnames, and on both the classic `/openai/deployments/{name}/{operation}` path and the v1 `/openai/v1/{operation}` path. OpenAI Ruby SDK calls made with either base URL also tag as `provider: "azure_openai"`. Pricing resolves to the matching `openai/<model>` entry; regional / Data Zone uplifts are configurable via `config.pricing_overrides` with the `azure_openai/<model>` prefix. See [Configuration → Azure OpenAI Service](docs/configuration.md#azure-openai-service).
12
+ - `bin/rails generate llm_cost_tracker:upgrade_provider_invoice_imports_provider` writes the migration so two reconciliation importers sharing a `source` (e.g. `csv/openai` and `csv/anthropic`) no longer cross-pollute resume cursors.
13
+ - `bin/rails generate llm_cost_tracker:upgrade_provider_invoices_metadata_index` writes a migration adding a GIN index on `llm_cost_tracker_provider_invoices.metadata` (PostgreSQL only) so reconciliation metadata lookups don't seq-scan on large invoice sets. No-op on MySQL.
14
+ - A `prices_file` with `metadata.currency: "EUR"` (or any non-USD code) now flows through to the call's `pricing_snapshot.currency`, the `call_rollups.currency` bucket, every `call_line_items.currency` row (token and service-charge alike), and the header `cost.currency` instead of being hardcoded to USD. The bundled price snapshot and `pricing_overrides` still default to USD; mixed-currency line items continue to drop from the header total with a warning.
15
+
16
+ ### Fixed
17
+
18
+ - OpenAI Chat Completions calls to the specialized search models (`gpt-4o-search-preview`, `gpt-4o-mini-search-preview`, `gpt-5-search-api`) record the per-call web-search fee as a line item at OpenAI's "Web search preview" rate (non-reasoning $25/1k, reasoning $10/1k). These models always search before responding, so the fee applies on every non-streaming call.
19
+ - Async ingestion writes through its own dedicated connection pool instead of competing with request-handling threads, so tracking an LLM call from inside a caller transaction no longer deadlocks under burst load and the inbox row still persists when the caller transaction rolls back. Tune via `config.ingestion_pool_size` if your PG / PgBouncer budget is tight.
20
+ - `mount LlmCostTracker::Engine => "/llm-costs"` works without adding `require "llm_cost_tracker/engine"` to `config/application.rb` — the engine is autoloaded.
21
+ - Upgrade migrations for the call_rollups and call_tags indexes build concurrently on PostgreSQL, so the upgrade no longer takes a long table-write lock on installs with millions of rows. The rollups upgrade keeps pre-upgrade aggregates (bucketed under empty provider) instead of wiping them.
22
+ - CSV exports stream in 500-row batches so peak memory stays flat at large export sizes while the user-selected sort order is preserved (`?sort=expensive`, `?sort=slow`, etc.).
23
+ - `bin/rails generate llm_cost_tracker:install` skips `config/initializers/llm_cost_tracker.rb` when it already exists instead of prompting Thor's "overwrite?" dialog, so re-running the generator in CI no longer hangs waiting on stdin.
24
+ - Async inbox entries no longer truncate on MySQL — large pricing snapshots and stack traces stay intact (MEDIUMTEXT instead of TEXT cap at 64 KB). PostgreSQL is unaffected.
25
+ - Logs no longer warn `unknown pricing for model X` when the call has no token pricing but at least one service charge was successfully priced. Misleading false-positive is gone for Anthropic web-search-style calls on models missing from the token-pricing table.
26
+ - `llm_cost_tracker:prune` rake task also prunes async inbox entries and finished provider invoice imports past the `DAYS` cutoff, so pending or quarantined rows no longer flush retroactively and old import-cursor rows don't linger forever.
27
+ - Azure OpenAI deployments using `audio/speech`, `images/edits`, `images/variations`, `moderations`, or `responses` endpoints are now captured by the Faraday parser; previously only `chat/completions`, `completions`, `embeddings`, `audio/transcriptions`, `audio/translations`, and `images/generations` matched.
28
+ - `prices:refresh` drops service-charge keys when a scraper stops emitting them, so stale charges no longer linger in the local price snapshot. Scrapers that don't parse a charges section (groq, gemini) preserve existing entries.
29
+
30
+ ### Changed
31
+
32
+ - Schema: `llm_cost_tracker_provider_invoice_imports` gains a `provider` column and replaces the `(source, started_at)` index with `(source, provider, started_at)`. Existing installs run `bin/rails generate llm_cost_tracker:upgrade_provider_invoice_imports_provider && bin/rails db:migrate`.
33
+ - BREAKING: `config.durable_ingestion = true/false` is replaced by `config.ingestion = :inline | :async` (default `:inline`). `config.durable_ingestion_pool_size` is renamed to `config.ingestion_pool_size`, and the install generator is now `bin/rails generate llm_cost_tracker:async_ingestion`. Update `config/initializers/llm_cost_tracker.rb` accordingly.
34
+
35
+ ## [0.9.0] - 2026-05-12
36
+
37
+ 0.9 leans the default install: only `calls`, `call_line_items`, and `call_tags`
38
+ are mandatory. Durable ingestion, rollup-cached budget reads, and provider
39
+ invoice reconciliation are opt-in behind config flags and dedicated generators.
40
+ Plus expanded SDK capture (OpenAI embeddings/audio/images/moderation, RubyLLM
41
+ paint/moderate), correct handling of Anthropic data residency and Priority
42
+ Tier, and a security-hardened dashboard. Existing installs need a migration —
43
+ see [Upgrading](docs/upgrading.md).
44
+
45
+ ### Added
46
+
47
+ - **Experimental:** opt-in provider invoice reconciliation. Set `config.reconciliation_enabled = true` and run `bin/rails generate llm_cost_tracker:reconciliation`. Public surface: `LlmCostTracker::Reconciliation.import / .diff`, `config.register_reconciliation_importer(:source) { … }`, rake tasks `llm_cost_tracker:reconcile:import` and `:reconcile:diff`. Doctor warns when drift exceeds 5% or imports go stale past 14 days. See [Configuration](docs/configuration.md#reconciliation-experimental-opt-in).
48
+ - Dashboard Data Quality page now shows a "Streaming health by provider" breakdown (streams, with-usage, unknown, unknown share) so a misconfigured OpenAI-compatible host shipping streams without `stream_options.include_usage` is visible at a glance.
49
+ - Dashboard tag detail page drills into a single value via `?tag_value=…` with total cost, call count, average per call, and a daily spend timeseries.
50
+ - Bundled rates for OpenAI embeddings (`text-embedding-3-small` / `-3-large` / `-ada-002`, including 50% batch discount) and token-priced transcription (`gpt-4o-transcribe`, `gpt-4o-mini-transcribe`). Token-priced transcription splits audio and text inputs at their separate rates. DALL-E and Whisper still record as zero-token visibility events until their per-image / per-minute pricing components land.
51
+ - OpenAI `gpt-image-1` / `gpt-image-1-mini` / `gpt-image-1.5` / `gpt-image-2` priced per image-token at their published standard rates, with `batch_*` shadow rates for the 50% batch tier. (Earlier preview snapshots stored only the batch rates, which silently halved image-generation costs.) The SDK integration extracts `usage.input_tokens_details.image_tokens` for image-as-input flows (edits / variations) and treats `usage.output_tokens` as image output. Requires the new `bin/rails generate llm_cost_tracker:upgrade_image_tokens` migration on v0.8 → v0.9 upgrades.
52
+ - OpenAI `tts-1` / `tts-1-hd` priced per character (request `input.length`). `gpt-4o-mini-tts` is left as a zero-cost visibility event because its tokens are not exposed to the client.
53
+ - OpenAI SDK integration now also patches `Embeddings#create`, `Images#generate` / `#edit` / `#create_variation`, `Audio::Transcriptions#create`, `Audio::Speech#create`, `Moderations#create`, and `Chat::Completions#stream`. Calls without provider-reported usage record as zero-token visibility events.
54
+ - RubyLLM SDK integration also records `Provider#paint` and `Provider#moderate`.
55
+ - `bin/rails generate llm_cost_tracker:upgrade_call_rollups_provider` writes the v0.8 → v0.9 migration that adds the `provider` column and swaps the unique index. Re-runs are no-ops.
56
+ - [EU AI Act record-keeping guide](docs/eu_ai_act.md) — maps the ledger fields and `llm_cost_tracker:prune` retention to Article 26(6) deployer obligations (≥ 6-month retention, traceability, attribution tags). Not legal advice.
57
+
58
+ ### Fixed
59
+
60
+ - Subscriber failures during `Tracker.record` no longer lose the event — the ledger write happens first; subscriber errors are caught and logged.
61
+ - Header `total_cost` no longer mixes currencies. Mismatched service-line costs keep their per-line currency and are excluded from the header total (with a warning).
62
+ - Budget reads aggregate across all rollup currencies instead of being silently scoped to USD only.
63
+ - `bin/rails llm_cost_tracker:setup` no longer fails with `Missing Thor class for invoke llm_cost_tracker:prices`, is idempotent on re-runs, and surfaces a friendly error when the database is unreachable.
64
+ - Stream events are no longer lost when finalization raises. The collector retries on the next `finish!`. Abandoned streams (wrapped but never iterated) emit a usage event instead of disappearing.
65
+ - Faraday streaming overflow keeps the buffer accumulated up to the limit (matching the SDK collector) instead of dropping all events.
66
+ - Edits to `config.prices_file` are picked up without a gem reload — the lookup cache invalidates on file mtime changes.
67
+ - Models flagged with `_source: "manual"` in the local prices file are preserved through `prices:refresh` when the remote registry does not claim the same key.
68
+ - Anthropic Priority Tier no longer falls to `cost_status: unknown`. It's a throughput commitment, not a per-token surcharge — both the SDK integration and the Faraday parser treat `service_tier: "priority"` as standard pricing.
69
+ - Anthropic `data_residency` mode triggers on `inference_geo: "us"` only — the documented +1.1x uplift tier. Earlier preview ranges that listed `"eu"` were incorrect; EU data residency runs through Bedrock Frankfurt or Vertex Belgium with separate pricing, not the Anthropic API.
70
+ - Anthropic `web_fetch_request` is recorded with a `$0` rate (Anthropic bills web fetch via standard tokens, not per fetch). The scraper picks up the "no additional charges" wording so `prices:refresh` keeps it accurate.
71
+ - OpenAI `web_search_call` is now priced model-aware. The legacy `web_search_preview` tool routes to `web_search_preview_request_reasoning` ($10/1k for gpt-5/o-series) or `web_search_preview_request_non_reasoning` ($25/1k for everything else), matching OpenAI's three published web-search billing paths. `gpt-5-chat-latest` and dotted variants (`gpt-5.1-chat-latest`, `gpt-5.2-chat-latest`, …) are classified as non-reasoning despite the `gpt-5` prefix.
72
+ - Anthropic Cost API reconciliation now ingests rows against live admin payloads (preview builds expected obsolete field names and produced zero rows). `service_tier: "batch"` and `inference_geo: "us"` are the only dimensions that promote a row's `pricing_mode`.
73
+ - Reconciliation diff windows are anchored in UTC; non-UTC servers no longer skew the window.
74
+ - Reconciliation provider totals sum only invoices fully contained in the diff window. Partially overlapping invoices no longer count at their full `billed_amount`.
75
+ - Reconciliation diff window upper bound is now exclusive of midnight on the day after `period_end`. Calls tracked at exactly `00:00:00.000` of the next month no longer get counted in both periods.
76
+ - `Reconciliation.import` / `Reconciliation.diff` accept (and require, for unmapped sources) an explicit `provider:`. Built-in mappings cover `openai`, `openai_usage`, `anthropic`, `anthropic_usage`, `gemini`. CSV and other custom sources must pass `provider:` (or be derivable from a prior import's metadata) — the previous silent fall-through summed local calls across every provider.
77
+ - Reconciliation import errors no longer echo the exception verbatim into the dashboard flash. The full trace goes to logs; the alert shows the exception class only.
78
+ - `Reconciliation::Importer` works on MySQL/Trilogy installs (adapter-aware `upsert_all`).
79
+ - Reconciliation `external_id` is namespaced by `source/provider` for sources that carry multiple providers (e.g. `csv/openai:row-1` vs `csv/anthropic:row-1`). The same CSV row id imported under two providers no longer collides on the unique index. Native sources keep their `openai:` / `anthropic:` / `gemini:` prefix.
80
+ - Reconciliation dashboard groups latest-period and drill-down by source, provider, and currency. A second provider importing under the same source no longer hides its drift in the first provider's row.
81
+ - OpenAI Cost API reconciliation tags the organization id under `provider_workspace_id` so org-level scope filters work.
82
+ - Reconciliation diff drill-down is capped at the top 100 unmatched rows by amount with totals counted separately, so the dashboard stays responsive on large monthly reconciliations. Pass `DRILLDOWN_LIMIT=all` to `rake llm_cost_tracker:reconcile:diff` to see every row.
83
+ - Period totals fall back to live aggregation from `llm_cost_tracker_calls` when `cache_rollups = true` but the rollups table has no row for the period. Budget reads and dashboard totals no longer read zero during a rollup rebuild window after the v0.9 upgrade migration.
84
+ - OpenAI hosted-tool service line items (`web_search_call`, `file_search_call`, `code_interpreter_call`, `mcp_call`) are recorded when the SDK returns the type as a Symbol. Previously these line items were silently dropped on SDK-shaped responses.
85
+ - Image generation streams (`gpt-image-1.5`, `gpt-image-2`) and audio streams no longer overflow on a single base64 chunk; the final usage event is captured and tokens get priced.
86
+ - Interrupted Anthropic and Gemini streams record the right provider name instead of `provider: "unknown"`.
87
+ - Tag sanitizer redacts secrets before truncating, so the leading bytes of a secret can't survive a small `max_tag_value_bytesize`. Nested `[REDACTED]` markers stay whole regardless of the byte budget.
88
+ - `Pricing::Registry` rejects non-finite price values (`Infinity` / `NaN`) alongside negatives.
89
+ - Reconciliation `ProviderInvoiceImport.started_at` is the wall-clock import time. Backfills with a historical `imported_at` no longer invert `resume_cursor_for` ordering.
90
+ - Reconciliation install migration is re-runnable on installs that already carry the v0.8 placeholder tables.
91
+ - Pre-release v0.9 deployers who imported reconciliation rows before these fixes need `LlmCostTracker::ProviderInvoice.delete_all` and a re-import — the `external_id` prefix and the OpenAI organization-id field both changed shape.
92
+ - Budget reads survive the v0.9 upgrade migration's rollup truncation — a partial rollup row no longer hides historical pre-migration spend in the same period.
93
+ - Streaming requests that hit `unknown_pricing_behavior = :raise` after the response is received raise without recording a synthetic zero-token event.
94
+ - Reconciliation doctor checks each `source / provider / currency` combination separately; a stale Anthropic CSV import no longer hides behind a fresh OpenAI one on the same source.
95
+ - Reconciliation imports normalise `currency` to upper case so `usd` and `USD` no longer split the diff.
96
+ - Reconciliation dashboard and CLI render `n/a` for invoice rows imported with no `billed_amount` instead of `$0.00`.
97
+ - Reconciliation diff drill-down shows the actual unmatched rows even when most invoices match — small-amount unmatched rows are no longer hidden by a wall of matched big-amount rows.
98
+ - OpenAI SDK Responses calls bill image and text tokens separately for `gpt-image-*` models, matching the Faraday parser.
99
+ - OpenAI SDK integration captures the request when the caller passes a typed request object (anything that responds to `to_h`) instead of dropping it.
100
+ - Custom prices files with `Infinity` / `NaN` service-charge rates fail to load with a clear error instead of silently corrupting cost math.
101
+ - High-cardinality tag filters (`Call.by_tag(:tenant_id, …)`) now hit a composite index instead of scanning. Existing installs run `bin/rails generate llm_cost_tracker:upgrade_call_tags_key_value_index && bin/rails db:migrate`.
102
+ - Reconciliation diff over a large invoice set uses an index scan on the new `(source, currency, period_start)` composite.
103
+ - Doctor warns when provider invoice rows are stored with non-uppercase currency and points at the one-line backfill SQL, instead of the dashboard silently zeroing out diffs against legacy lowercase data.
104
+ - A request-level `pricing_mode` no longer overrides what the provider reports back on a streamed response. Provider-reported standard wins over a request that asked for priority.
105
+ - The new generators (`call_rollups`, `durable_ingestion`, `reconciliation`, `upgrade_call_rollups_provider`) are reachable through `bin/rails generate llm_cost_tracker:<name>`.
106
+ - Faraday streaming captures no longer silently degrade to `usage_source: :unknown`.
107
+ - Dashboard filters apply the default 30-day range when `from`/`to` params are missing.
108
+ - `provider_api_key_id` and `provider_workspace_id` are masked on the call detail page and CSV export. Host apps that added a `metadata` column written as a JSON string now flow through the same masking instead of rendering the raw column.
109
+ - Faraday parser tracks OpenAI `/v1/images/*` and `/v1/audio/transcriptions`/`/v1/audio/translations` so raw-Faraday image generations and transcriptions land in the ledger. `/v1/audio/speech` and `/v1/moderations` are also matched so `Tracker.enforce_budget!` gates them; they do not record a row because OpenAI does not return token usage for those endpoints.
110
+ - OpenAI SDK `Audio::Translations#create` is now patched alongside `Audio::Transcriptions#create`.
111
+ - OpenAI SDK `Images#generate` / `#edit` / `#create_variation` no longer double-counts cached input tokens.
112
+ - OpenAI SDK `Responses.create` and Faraday parser both route output to `image_output_tokens` for `gpt-image-*` models even when the response omits `output_tokens_details.image_tokens`.
113
+ - OpenAI SDK `Images#generate` / `#edit` / `#create_variation` no longer drops the text-output remainder when `output_tokens_details` reports only `image_tokens`. The remainder lands as `output_tokens`, matching the Faraday parser.
114
+ - RubyLLM `Provider#paint` for `gpt-image-*` models records image output tokens under `image_output_tokens` so image rates apply.
115
+ - RubyLLM integration treats Anthropic `service_tier: "priority"` as standard pricing (Priority Tier is committed throughput, not a surcharge). Previously these calls fell to `cost_status: unknown` because the literal `"priority"` was passed through as `pricing_mode`.
116
+ - Reconciliation diff falls back to live `llm_cost_tracker_call_line_items` aggregation when the rollup fast path finds no row for the period. Without the fallback, past-month diffs after the v0.9 `upgrade_call_rollups_provider` migration (which truncates rollups) would report `local_total = $0` until events repopulate the new schema.
117
+ - Provider-invoice reconciliation falls back to `match_basis: "model"` (was `period_only`) when an invoice carries only a model identifier.
118
+ - `prices:refresh` bootstraps a missing local pricing file instead of failing with `Errno::ENOENT`.
119
+ - Doctor's durable-inbox verification no longer leaves a synthetic inbox row behind when `Tracker.track` raises `BudgetExceededError`.
120
+ - Install-generator snippet in [Upgrading](docs/upgrading.md) for the reconciliation table now matches the shipped index (`(source, currency, period_start)`).
121
+ - Doctor catches schema drift on required columns, required indexes, and the foreign key on `call_line_items` before the first row is inserted.
122
+ - Service-charge rows render `n/a` instead of `$0.00` when `cost_status` is `unknown`, so unpriced charges don't masquerade as zero-cost.
123
+ - Enabling `:ruby_llm` together with `:openai` / `:anthropic` logs a warning at install — RubyLLM routes through HTTP, so calls would otherwise be double-counted. Pick one path per provider.
124
+
125
+ ### Changed
126
+
127
+ - BREAKING: `bin/rails generate llm_cost_tracker:install --dashboard` no longer writes the `mount LlmCostTracker::Engine` line into `config/routes.rb`. The CLI prints the snippet wrapped in your auth instead — leaving the dashboard auto-mounted would expose spend, tags, and provider IDs to anyone who can reach the host. Add the route under your authentication block.
128
+ - BREAKING: `config.durable_ingestion` defaults to `false`. Tracking writes go directly to the ledger from the request thread; the durable inbox + worker + leases tables are no longer created by the install generator. Existing installs keep their tables — set `config.durable_ingestion = true` to keep the inbox path. Fresh installs that need durability run `bin/rails generate llm_cost_tracker:durable_ingestion` and flip the flag.
129
+ - BREAKING: `config.cache_rollups` defaults to `false`. Budget reads aggregate live from `llm_cost_tracker_calls`; the rollup table is no longer created by the install generator. Existing installs keep their table — set `config.cache_rollups = true` to keep the rollup fast path. Fresh installs run `bin/rails generate llm_cost_tracker:call_rollups` and flip the flag.
130
+ - BREAKING: `llm_cost_tracker_call_rollups` gains a `provider` column; unique index moves from `(period, period_start, currency)` to `(period, period_start, currency, provider)`. See [Upgrading](docs/upgrading.md).
131
+ - BREAKING: `llm_cost_tracker_calls` gains `image_input_tokens` and `image_output_tokens` columns (default 0) so OpenAI `gpt-image-*` models can bill image-token rates separately from text. Run `bin/rails generate llm_cost_tracker:upgrade_image_tokens && bin/rails db:migrate`. CSV exports include the new columns between `audio_input_tokens` / `output_tokens` and `audio_output_tokens` / `total_tokens` respectively — downstream consumers indexing by header name keep working; positional consumers shift by two.
132
+ - BREAKING: `LlmCostTracker::Call.by_tag(key, value)` encodes Hash and Array values with `JSON.generate` to match how `Ledger::Store` writes them. The previous `value.to_s` path produced `"{:k=>v}"`-shaped strings that never matched stored JSON, so filtering by nested attribution silently returned zero rows.
133
+ - Faraday middleware auto-injects `stream_options: { include_usage: true }` on OpenAI and OpenAI-compatible chat-completions streaming requests when the caller hasn't set it. Disable with `config.auto_enable_stream_usage = false`.
134
+ - Header `total_cost` and per-line-item rates can no longer disagree on the context-tier boundary or the resolved pricing mode.
135
+ - OpenAI-compatible chat-completions streams without a final usage chunk log a warning instead of recording silently as `usage_source: "unknown"`.
136
+ - `Tags::Sanitizer` redacts tag values matching known secret patterns (OpenAI/Anthropic, GitHub, AWS, JWT, Slack, Stripe, Google API key, `Bearer …`) regardless of tag key, recurses into nested Hash/Array leaves, and on tag-count overflow keeps the most recently added tags. `Tags::Context` sanitises at block entry so raw values never reach notification subscribers, the Faraday request env, or in-flight stream collectors.
137
+ - Engine dashboard adds CSRF protection on the reconciliation import endpoint, sets `Cache-Control: no-store` on CSV exports, registers `tag` / `tag_value` in `config.filter_parameters`, and emits `X-Frame-Options: DENY` / `Referrer-Policy: same-origin` / a baseline `Content-Security-Policy` on every dashboard response. CSV export is capped at 10,000 rows and respects the requested sort.
138
+ - Dashboard schema drift check runs once per process instead of on every request, cutting per-request DB metadata load. Code reloads in development still trigger a re-check.
139
+ - Dashboard dynamic widths (progress bars, budget fills, stack segments) render via a per-request CSP-nonced `<style>` block instead of inline `style="…"` attributes. Strict `style-src 'self' 'nonce-…'` no longer collapses the visualisations.
140
+
5
141
  ## [0.8.0] - 2026-05-07
6
142
 
7
143
  0.8 is a storage rebuild. Tokens and tool/runtime charges share one shape
data/README.md CHANGED
@@ -29,8 +29,7 @@ gem "openai"
29
29
  bin/rails llm_cost_tracker:setup
30
30
  ```
31
31
 
32
- That runs the install generator with the dashboard and pricing snapshot,
33
- migrates the database, then verifies via `llm_cost_tracker:doctor`.
32
+ Runs the install generator, drops a price snapshot, migrates the database, and verifies via `llm_cost_tracker:doctor`.
34
33
 
35
34
  ```ruby
36
35
  # config/initializers/llm_cost_tracker.rb
@@ -40,7 +39,7 @@ LlmCostTracker.configure do |config|
40
39
  end
41
40
  ```
42
41
 
43
- Tag your calls that's how you find out who burned the money:
42
+ Tag your calls to attribute spend:
44
43
 
45
44
  ```ruby
46
45
  LlmCostTracker.with_tags(user_id: Current.user&.id, feature: "chat") do
@@ -49,8 +48,15 @@ LlmCostTracker.with_tags(user_id: Current.user&.id, feature: "chat") do
49
48
  end
50
49
  ```
51
50
 
52
- Mount the dashboard at `/llm-costs` and put it behind your app's auth — it
53
- ships without one.
51
+ Mount the dashboard in `config/routes.rb`, behind your auth:
52
+
53
+ ```ruby
54
+ authenticate :admin do
55
+ mount LlmCostTracker::Engine => "/llm-costs"
56
+ end
57
+ ```
58
+
59
+ The engine ships without authentication on purpose.
54
60
 
55
61
  ## What lands in the ledger
56
62
 
@@ -68,6 +74,7 @@ ships without one.
68
74
  | --- | --- |
69
75
  | OpenAI | Official SDK or Faraday |
70
76
  | Anthropic | Official SDK or Faraday |
77
+ | Azure OpenAI | Faraday or official SDK (auto-detected on `*.openai.azure.com` and Foundry `*.services.ai.azure.com`, both deployments and `/openai/v1/...`) |
71
78
  | Google Gemini | Faraday |
72
79
  | RubyLLM | Provider layer |
73
80
  | `ruby-openai` | Faraday |
@@ -81,7 +88,7 @@ need `stream_options: { include_usage: true }`.
81
88
 
82
89
  - No proxy. Direct calls only.
83
90
  - No prompts. Token counts and metadata only.
84
- - Not invoice-grade. Provider response IDs are stored for reconciliation.
91
+ - No traces, evals, or prompt management. Different product, different gem.
85
92
  - Not multi-service. Built for a Rails monolith.
86
93
 
87
94
  ## Manual tracking
@@ -110,6 +117,7 @@ LlmCostTracker.track(
110
117
  - [Extending](docs/extending.md)
111
118
  - [Operations](docs/operations.md)
112
119
  - [Architecture](docs/architecture.md)
120
+ - [EU AI Act record-keeping](docs/eu_ai_act.md)
113
121
  - [Upgrading](docs/upgrading.md)
114
122
  - [Changelog](CHANGELOG.md)
115
123
 
@@ -163,6 +163,31 @@
163
163
 
164
164
  .lct-panel + .lct-panel,
165
165
  .lct-stat-grid + .lct-panel { margin-top: 16px; }
166
+ .lct-grid > .lct-panel + .lct-panel,
167
+ .lct-grid > .lct-stat-grid + .lct-panel { margin-top: 0; }
168
+
169
+ .lct-breadcrumb {
170
+ align-items: center;
171
+ color: var(--lct-muted);
172
+ display: flex;
173
+ font-size: var(--fs-sm);
174
+ gap: 8px;
175
+ margin-bottom: 14px;
176
+ }
177
+ .lct-breadcrumb-link {
178
+ color: var(--lct-muted);
179
+ text-decoration: none;
180
+ transition: color 0.15s ease;
181
+ }
182
+ .lct-breadcrumb-link:hover { color: var(--lct-text); }
183
+ .lct-breadcrumb-link:focus-visible {
184
+ color: var(--lct-text);
185
+ outline: 2px solid var(--lct-accent-soft);
186
+ outline-offset: 2px;
187
+ border-radius: 2px;
188
+ }
189
+ .lct-breadcrumb-sep { color: var(--lct-border-strong); }
190
+ .lct-breadcrumb-current { color: var(--lct-text); font-weight: 500; }
166
191
 
167
192
  .lct-banner {
168
193
  align-items: center;
@@ -229,7 +254,7 @@
229
254
 
230
255
  .lct-hero-side .lct-stat-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
231
256
  .lct-stat-grid-spaced { margin-bottom: 16px; }
232
- .lct-two-col { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
257
+ .lct-two-col { align-items: start; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
233
258
 
234
259
  .lct-stat {
235
260
  align-items: flex-start;
@@ -256,7 +281,6 @@
256
281
 
257
282
  .lct-stat-label,
258
283
  .lct-field label,
259
- .lct-dl dt,
260
284
  .lct-call-summary-label,
261
285
  .lct-call-breakdown-title,
262
286
  .lct-chip-label {
@@ -301,6 +325,21 @@
301
325
  .lct-table-compact th,
302
326
  .lct-table-compact td { padding: 10px 12px; }
303
327
 
328
+ .lct-badge {
329
+ align-items: center;
330
+ border-radius: 999px;
331
+ display: inline-flex;
332
+ font-size: var(--fs-xs);
333
+ font-weight: 600;
334
+ letter-spacing: 0.02em;
335
+ line-height: 1;
336
+ padding: 4px 10px;
337
+ text-transform: uppercase;
338
+ vertical-align: middle;
339
+ }
340
+ .lct-badge-ok { background: var(--lct-success-soft); color: var(--lct-success); }
341
+ .lct-badge-warn { background: var(--lct-warning-soft); color: var(--lct-warning-strong); }
342
+
304
343
  .lct-delta-badge {
305
344
  align-items: center;
306
345
  border-radius: 999px;
@@ -387,8 +426,10 @@
387
426
  .lct-stack-fill-cache-write { background: #f59e0b; }
388
427
  .lct-stack-fill-cache-write-extended { background: #a855f7; }
389
428
  .lct-stack-fill-audio-input { background: #ec4899; }
429
+ .lct-stack-fill-image-input { background: #f97316; }
390
430
  .lct-stack-fill-output { background: #0ea5e9; }
391
431
  .lct-stack-fill-audio-output { background: #14b8a6; }
432
+ .lct-stack-fill-image-output { background: #84cc16; }
392
433
 
393
434
  .lct-budget { display: grid; gap: 10px; }
394
435
  .lct-budget-head { align-items: baseline; display: flex; gap: 10px; justify-content: space-between; }
@@ -491,6 +532,20 @@
491
532
  }
492
533
  .lct-state-title { font-size: var(--fs-lg); font-weight: 600; margin: 0 0 6px; letter-spacing: -0.005em; }
493
534
  .lct-state-copy { color: var(--lct-muted); margin: 0 auto; max-width: 480px; font-size: var(--fs-sm); line-height: 1.55; }
535
+ .lct-state-pre {
536
+ background: var(--lct-bg);
537
+ border: 1px solid var(--lct-border);
538
+ border-radius: 6px;
539
+ color: var(--lct-text);
540
+ font-family: var(--mono);
541
+ font-size: var(--fs-sm);
542
+ line-height: 1.55;
543
+ margin: 12px auto 0;
544
+ max-width: 560px;
545
+ overflow: auto;
546
+ padding: 12px 14px;
547
+ text-align: left;
548
+ }
494
549
  .lct-state-actions { display: flex; gap: 8px; justify-content: center; margin-top: 20px; }
495
550
 
496
551
  .lct-toolbar {
@@ -832,16 +887,21 @@
832
887
 
833
888
  .lct-nowrap { white-space: nowrap; }
834
889
 
835
- .lct-detail-grid { display: grid; gap: 16px; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
890
+ .lct-detail-grid { align-items: start; display: grid; gap: 16px; grid-template-columns: repeat(3, minmax(0, 1fr)); }
836
891
 
837
892
  .lct-dl {
838
893
  display: grid;
839
894
  gap: 12px;
840
- grid-template-columns: minmax(120px, 0.4fr) minmax(0, 1fr);
895
+ grid-template-columns: minmax(120px, 0.45fr) minmax(0, 1fr);
841
896
  margin: 0;
842
897
  }
843
898
 
844
- .lct-dl dd { margin: 0; min-width: 0; overflow-wrap: anywhere; word-break: break-word; }
899
+ .lct-dl dt {
900
+ color: var(--lct-muted);
901
+ font-size: var(--fs-sm);
902
+ font-weight: 500;
903
+ }
904
+ .lct-dl dd { color: var(--lct-text); font-size: var(--fs-sm); margin: 0; min-width: 0; overflow-wrap: anywhere; word-break: break-word; }
845
905
 
846
906
  .lct-pre {
847
907
  background: #0f172a;
@@ -1,53 +1,33 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "securerandom"
4
+
3
5
  module LlmCostTracker
4
6
  class ApplicationController < ActionController::Base
5
7
  layout "llm_cost_tracker/application"
6
8
 
9
+ protect_from_forgery with: :exception
10
+
11
+ before_action :set_dashboard_security_headers
7
12
  before_action :ensure_current_schema
8
13
 
14
+ helper_method :dashboard_csp_nonce
15
+
9
16
  rescue_from ActiveRecord::ConnectionNotEstablished, with: :render_database_error
17
+ rescue_from ActiveRecord::AdapterNotSpecified, with: :render_database_error
10
18
  rescue_from ActiveRecord::RecordNotFound, with: :render_not_found
11
19
  rescue_from ActiveRecord::StatementInvalid, with: :render_database_error
12
20
  rescue_from LlmCostTracker::InvalidFilterError, with: :render_invalid_filter
13
21
 
14
- SCHEMA_CHECKS = [
15
- [
16
- LlmCostTracker::Ledger::Schema::Calls,
17
- "The llm_cost_tracker_calls table does not match the current LLM Cost Tracker schema."
18
- ],
19
- [
20
- LlmCostTracker::Ledger::Schema::CallRollups,
21
- "The llm_cost_tracker_call_rollups table does not match the current LLM Cost Tracker schema."
22
- ],
23
- [
24
- LlmCostTracker::Ledger::Schema::CallLineItems,
25
- "The llm_cost_tracker_call_line_items table does not match the current LLM Cost Tracker schema."
26
- ],
27
- [
28
- LlmCostTracker::Ledger::Schema::CallTags,
29
- "The llm_cost_tracker_call_tags table does not match the current LLM Cost Tracker schema."
30
- ]
31
- ].freeze
32
-
33
- private_constant :SCHEMA_CHECKS
34
-
35
22
  private
36
23
 
37
24
  def ensure_current_schema
38
- unless LlmCostTracker::Call.table_exists?
39
- @setup_message = "The llm_cost_tracker_calls table is not available yet."
40
- return render template: "llm_cost_tracker/shared/setup_required"
41
- end
25
+ drift = LlmCostTracker::Dashboard::SetupState.current
26
+ return unless drift
42
27
 
43
- SCHEMA_CHECKS.each do |schema, message|
44
- errors = schema.current_schema_errors
45
- next if errors.empty?
46
-
47
- @setup_message = message
48
- @setup_details = errors + ["See docs/upgrading.md for the migration path."]
49
- return render template: "llm_cost_tracker/shared/setup_required"
50
- end
28
+ @setup_message = drift.message
29
+ @setup_details = drift.details
30
+ render template: "llm_cost_tracker/shared/setup_required"
51
31
  end
52
32
 
53
33
  def render_database_error(error)
@@ -63,5 +43,17 @@ module LlmCostTracker
63
43
  def render_not_found
64
44
  render "llm_cost_tracker/errors/not_found", status: :not_found
65
45
  end
46
+
47
+ def set_dashboard_security_headers
48
+ nonce = dashboard_csp_nonce
49
+ response.headers["X-Frame-Options"] = "DENY"
50
+ response.headers["Referrer-Policy"] = "same-origin"
51
+ response.headers["Content-Security-Policy"] =
52
+ "default-src 'self'; style-src 'self' 'nonce-#{nonce}'; img-src 'self' data:; frame-ancestors 'none'"
53
+ end
54
+
55
+ def dashboard_csp_nonce
56
+ request.env["llm_cost_tracker.csp_nonce"] ||= SecureRandom.base64(16)
57
+ end
66
58
  end
67
59
  end
@@ -3,7 +3,7 @@
3
3
  module LlmCostTracker
4
4
  class AssetsController < ActionController::Base
5
5
  def stylesheet
6
- response.set_header("Cache-Control", cache_control_header)
6
+ response.headers["Cache-Control"] = cache_control_header
7
7
  send_file LlmCostTracker::Assets::STYLESHEET_PATH, type: "text/css", disposition: "inline"
8
8
  end
9
9
 
@@ -6,6 +6,7 @@ require "json"
6
6
  module LlmCostTracker
7
7
  class CallsController < ApplicationController
8
8
  CSV_EXPORT_LIMIT = 10_000
9
+ CSV_EXPORT_BATCH_SIZE = 500
9
10
  CSV_FORMULA_PREFIXES = ["=", "+", "-", "@", "\t", "\r"].freeze
10
11
  DEFAULT_ORDER = "tracked_at DESC, id DESC"
11
12
 
@@ -22,7 +23,8 @@ module LlmCostTracker
22
23
  @calls = ordered_scope.includes(:tag_records).limit(@page.limit).offset(@page.offset).to_a
23
24
  end
24
25
  format.csv do
25
- send_data render_csv(ordered_scope.limit(CSV_EXPORT_LIMIT)),
26
+ response.headers["Cache-Control"] = "no-store"
27
+ send_data render_csv(ordered_scope),
26
28
  type: "text/csv",
27
29
  disposition: %(attachment; filename="llm_calls_#{Time.now.utc.strftime('%Y%m%d_%H%M%S')}.csv")
28
30
  end
@@ -30,7 +32,7 @@ module LlmCostTracker
30
32
  end
31
33
 
32
34
  def show
33
- @call = LlmCostTracker::Call.find(params[:id])
35
+ @call = LlmCostTracker::Call.includes(:line_items, :tag_records).find(params[:id])
34
36
  end
35
37
 
36
38
  private
@@ -54,13 +56,24 @@ module LlmCostTracker
54
56
  fields = csv_fields
55
57
  CSV.generate do |csv|
56
58
  csv << fields.map(&:to_s)
57
-
58
- relation.includes(:tag_records).each do |call|
59
+ each_export_batch(relation) do |call|
59
60
  csv << fields.map { |field| csv_value(field, call) }
60
61
  end
61
62
  end
62
63
  end
63
64
 
65
+ def each_export_batch(relation, &)
66
+ offset = 0
67
+ while offset < CSV_EXPORT_LIMIT
68
+ batch_size = [CSV_EXPORT_BATCH_SIZE, CSV_EXPORT_LIMIT - offset].min
69
+ batch = relation.limit(batch_size).offset(offset).preload(:tag_records).to_a
70
+ break if batch.empty?
71
+
72
+ batch.each(&)
73
+ offset += batch.size
74
+ end
75
+ end
76
+
64
77
  def csv_fields
65
78
  %i[tracked_at provider model] +
66
79
  TokenUsage.members +
@@ -74,8 +87,9 @@ module LlmCostTracker
74
87
  case field
75
88
  when :tracked_at
76
89
  call.tracked_at&.utc&.iso8601
77
- when :provider, :model, :provider_response_id, :provider_project_id, :provider_api_key_id,
78
- :provider_workspace_id, :cost_status
90
+ when :provider_api_key_id, :provider_workspace_id, :provider_project_id
91
+ csv_safe(LlmCostTracker::Masking.mask_value(field, call[field]))
92
+ when :provider, :model, :provider_response_id, :cost_status
79
93
  csv_safe(call[field])
80
94
  when :pricing_snapshot
81
95
  csv_safe(csv_json(call.pricing_snapshot))
@@ -87,11 +101,7 @@ module LlmCostTracker
87
101
  end
88
102
 
89
103
  def csv_json(value)
90
- return value.transform_keys(&:to_s).to_json if value.is_a?(Hash)
91
-
92
- JSON.parse(value || "{}").to_json
93
- rescue JSON::ParserError
94
- "{}"
104
+ Hash(value).deep_stringify_keys.to_json
95
105
  end
96
106
 
97
107
  def csv_safe(value)
@@ -16,6 +16,10 @@ module LlmCostTracker
16
16
  total_calls: @summary.total
17
17
  )
18
18
  @service_charge_rows = Dashboard::DataQuality.service_charge_rows(scope).to_a
19
+ @streaming_health_rows = Dashboard::DataQuality.streaming_health_rows(
20
+ scope,
21
+ total_streaming: @summary.streaming_count
22
+ )
19
23
  end
20
24
  end
21
25
  end
@@ -0,0 +1,106 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LlmCostTracker
4
+ class ReconciliationController < ApplicationController
5
+ def index
6
+ @reconciliation_enabled = LlmCostTracker::Reconciliation.enabled?
7
+ @reconciliation_installed = LlmCostTracker::ProviderInvoice.table_exists?
8
+ if @reconciliation_enabled && @reconciliation_installed
9
+ @scopes = invoice_scopes
10
+ @sources = @scopes.map { |scope| scope[:source] }.uniq
11
+ @diffs = @scopes.filter_map { |scope| diff_for(scope) }
12
+ @last_imported_at = LlmCostTracker::ProviderInvoice.maximum(:imported_at)
13
+ else
14
+ @scopes = []
15
+ @sources = []
16
+ @diffs = []
17
+ @last_imported_at = nil
18
+ end
19
+ @threshold = LlmCostTracker::Reconciliation::DEFAULT_THRESHOLD_PERCENT
20
+ @configured_importers = @reconciliation_enabled ? configured_importers : {}
21
+ end
22
+
23
+ def trigger_import
24
+ unless LlmCostTracker::Reconciliation.enabled?
25
+ return redirect_to reconciliation_path, alert: "Reconciliation is disabled"
26
+ end
27
+
28
+ source = params[:source].to_s
29
+ importer = configured_importers[source.to_sym]
30
+ return redirect_to reconciliation_path, alert: "No importer configured for #{source}" if importer.nil?
31
+
32
+ result = importer.call
33
+ if result.respond_to?(:errors) && result.errors.any?
34
+ LlmCostTracker::Logging.warn(
35
+ "Reconciliation import for #{source} returned #{result.errors.size} row error(s)"
36
+ )
37
+ return redirect_to(
38
+ reconciliation_path,
39
+ alert: "Imported #{result.respond_to?(:total_imported) ? result.total_imported : 0} " \
40
+ "#{source} rows with #{result.errors.size} row error(s); see Rails logs for details."
41
+ )
42
+ end
43
+ message = if result.respond_to?(:total_imported)
44
+ "Imported #{result.total_imported} #{source} rows"
45
+ else
46
+ "Triggered #{source} importer"
47
+ end
48
+ redirect_to reconciliation_path, notice: message
49
+ rescue StandardError => e
50
+ LlmCostTracker::Logging.warn("Reconciliation import failed for #{source}: #{e.class}: #{e.message}")
51
+ redirect_to reconciliation_path,
52
+ alert: "Import failed (#{e.class.name}); see Rails logs for details."
53
+ end
54
+
55
+ private
56
+
57
+ def configured_importers
58
+ LlmCostTracker.configuration.reconciliation_importers
59
+ end
60
+
61
+ def invoice_scopes
62
+ connection = LlmCostTracker::ProviderInvoice.connection
63
+ provider_expr =
64
+ if LlmCostTracker::Ledger::Schema::Adapter.postgresql?(connection)
65
+ Arel.sql("metadata->>'provider'")
66
+ else
67
+ Arel.sql("JSON_UNQUOTE(JSON_EXTRACT(metadata, '$.provider'))")
68
+ end
69
+ LlmCostTracker::ProviderInvoice
70
+ .group(:source, provider_expr, :currency)
71
+ .order(:source, :currency)
72
+ .pluck(:source, provider_expr, :currency)
73
+ .map { |source, provider, currency| { source: source, provider: provider, currency: currency.upcase } }
74
+ end
75
+
76
+ def diff_for(scope)
77
+ window = scope_invoices(scope)
78
+ .order(period_end: :desc, period_start: :desc)
79
+ .limit(1)
80
+ .pick(:period_start, :period_end)
81
+ return nil unless window
82
+
83
+ LlmCostTracker::Reconciliation.diff(
84
+ source: scope[:source], provider: scope[:provider], currency: scope[:currency],
85
+ period_start: window[0], period_end: window[1]
86
+ )
87
+ rescue ArgumentError => e
88
+ LlmCostTracker::Logging.warn("Reconciliation diff skipped for #{scope.inspect}: #{e.message}")
89
+ nil
90
+ end
91
+
92
+ def scope_invoices(scope)
93
+ relation = LlmCostTracker::ProviderInvoice
94
+ .where(source: scope[:source], currency: scope[:currency])
95
+ connection = LlmCostTracker::ProviderInvoice.connection
96
+ provider = scope[:provider]
97
+ return relation if provider.nil? || provider.empty?
98
+
99
+ if LlmCostTracker::Ledger::Schema::Adapter.postgresql?(connection)
100
+ relation.where("metadata->>'provider' = ?", provider)
101
+ else
102
+ relation.where("JSON_UNQUOTE(JSON_EXTRACT(metadata, '$.provider')) = ?", provider)
103
+ end
104
+ end
105
+ end
106
+ end