llm_cost_tracker 0.3.0 → 0.3.2

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 (59) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +23 -0
  3. data/CODE_OF_CONDUCT.md +23 -0
  4. data/README.md +86 -8
  5. data/SECURITY.md +36 -0
  6. data/app/assets/llm_cost_tracker/application.css +1 -4
  7. data/app/controllers/llm_cost_tracker/assets_controller.rb +0 -2
  8. data/app/controllers/llm_cost_tracker/calls_controller.rb +9 -13
  9. data/app/controllers/llm_cost_tracker/dashboard_controller.rb +8 -19
  10. data/app/controllers/llm_cost_tracker/data_quality_controller.rb +1 -2
  11. data/app/controllers/llm_cost_tracker/models_controller.rb +5 -2
  12. data/app/controllers/llm_cost_tracker/tags_controller.rb +2 -4
  13. data/app/helpers/llm_cost_tracker/dashboard_filter_options_helper.rb +1 -7
  14. data/app/helpers/llm_cost_tracker/dashboard_query_helper.rb +5 -9
  15. data/app/services/llm_cost_tracker/dashboard/data_quality.rb +10 -10
  16. data/app/services/llm_cost_tracker/dashboard/filter.rb +6 -26
  17. data/app/services/llm_cost_tracker/dashboard/provider_breakdown.rb +0 -3
  18. data/app/services/llm_cost_tracker/dashboard/tag_breakdown.rb +0 -2
  19. data/app/services/llm_cost_tracker/pagination.rb +1 -9
  20. data/app/views/layouts/llm_cost_tracker/application.html.erb +1 -16
  21. data/app/views/llm_cost_tracker/calls/index.html.erb +13 -13
  22. data/app/views/llm_cost_tracker/calls/show.html.erb +8 -3
  23. data/app/views/llm_cost_tracker/dashboard/index.html.erb +1 -1
  24. data/app/views/llm_cost_tracker/data_quality/index.html.erb +36 -14
  25. data/app/views/llm_cost_tracker/models/index.html.erb +10 -9
  26. data/app/views/llm_cost_tracker/shared/_spend_chart.html.erb +0 -1
  27. data/app/views/llm_cost_tracker/shared/_tag_chips.html.erb +0 -1
  28. data/app/views/llm_cost_tracker/tags/index.html.erb +1 -1
  29. data/app/views/llm_cost_tracker/tags/show.html.erb +1 -1
  30. data/lib/llm_cost_tracker/configuration.rb +0 -1
  31. data/lib/llm_cost_tracker/event.rb +1 -0
  32. data/lib/llm_cost_tracker/event_metadata.rb +1 -0
  33. data/lib/llm_cost_tracker/generators/llm_cost_tracker/add_provider_response_id_generator.rb +29 -0
  34. data/lib/llm_cost_tracker/generators/llm_cost_tracker/templates/add_provider_response_id_to_llm_api_calls.rb.erb +15 -0
  35. data/lib/llm_cost_tracker/generators/llm_cost_tracker/templates/create_llm_api_calls.rb.erb +2 -0
  36. data/lib/llm_cost_tracker/llm_api_call.rb +6 -2
  37. data/lib/llm_cost_tracker/middleware/faraday.rb +1 -0
  38. data/lib/llm_cost_tracker/parameter_hash.rb +33 -0
  39. data/lib/llm_cost_tracker/parsed_usage.rb +14 -3
  40. data/lib/llm_cost_tracker/parsers/anthropic.rb +47 -28
  41. data/lib/llm_cost_tracker/parsers/gemini.rb +28 -4
  42. data/lib/llm_cost_tracker/parsers/openai_compatible.rb +5 -6
  43. data/lib/llm_cost_tracker/parsers/openai_usage.rb +14 -0
  44. data/lib/llm_cost_tracker/price_registry.rb +22 -7
  45. data/lib/llm_cost_tracker/price_sync/refresh_plan_builder.rb +162 -0
  46. data/lib/llm_cost_tracker/price_sync/registry_loader.rb +55 -0
  47. data/lib/llm_cost_tracker/price_sync/registry_writer.rb +25 -0
  48. data/lib/llm_cost_tracker/price_sync.rb +16 -184
  49. data/lib/llm_cost_tracker/pricing.rb +0 -11
  50. data/lib/llm_cost_tracker/railtie.rb +2 -1
  51. data/lib/llm_cost_tracker/report.rb +0 -5
  52. data/lib/llm_cost_tracker/storage/active_record_store.rb +10 -11
  53. data/lib/llm_cost_tracker/stream_collector.rb +17 -13
  54. data/lib/llm_cost_tracker/tags_column.rb +4 -0
  55. data/lib/llm_cost_tracker/tracker.rb +10 -2
  56. data/lib/llm_cost_tracker/version.rb +1 -1
  57. data/lib/llm_cost_tracker.rb +6 -14
  58. data/llm_cost_tracker.gemspec +3 -1
  59. metadata +37 -1
@@ -4,9 +4,6 @@ module LlmCostTracker
4
4
  module Dashboard
5
5
  ProviderRow = Data.define(:provider, :calls, :total_cost, :share_percent)
6
6
 
7
- # Aggregates cost and call counts per provider for a given scope.
8
- # Sorted by total cost descending; providers with zero cost fall to the bottom
9
- # but are still returned so users can see calls without pricing.
10
7
  class ProviderBreakdown
11
8
  def self.call(scope: LlmCostTracker::LlmApiCall.all)
12
9
  new(scope: scope).rows
@@ -9,8 +9,6 @@ module LlmCostTracker
9
9
  :average_cost_per_call
10
10
  )
11
11
 
12
- # Aggregates calls grouped by the distinct values of a single tag key.
13
- # Invalid keys raise InvalidFilterError so controllers can return HTTP 400.
14
12
  class TagBreakdown
15
13
  class << self
16
14
  def call(key:, scope: LlmCostTracker::LlmApiCall.all)
@@ -9,21 +9,13 @@ module LlmCostTracker
9
9
  attr_reader :page, :per
10
10
 
11
11
  def self.call(params)
12
- params = normalize_params(params)
12
+ params = LlmCostTracker::ParameterHash.with_indifferent_access(params)
13
13
  new(
14
14
  page: integer_param(params, :page, default: MIN_PAGE, min: MIN_PAGE),
15
15
  per: integer_param(params, :per, default: DEFAULT_PER, min: 1, max: MAX_PER)
16
16
  )
17
17
  end
18
18
 
19
- def self.normalize_params(params)
20
- return {}.with_indifferent_access if params.nil?
21
-
22
- raw = params.respond_to?(:to_unsafe_h) ? params.to_unsafe_h : params.to_h
23
- raw.with_indifferent_access
24
- end
25
- private_class_method :normalize_params
26
-
27
19
  def self.integer_param(params, key, default:, min:, max: nil)
28
20
  value = Integer(params[key], 10)
29
21
  value = [value, min].max
@@ -6,7 +6,7 @@
6
6
  <title>LLM Cost Tracker</title>
7
7
  <%= stylesheet_link_tag stylesheet_path %>
8
8
  </head>
9
- <body>
9
+ <body class="lct-body">
10
10
  <div class="lct-app">
11
11
  <main class="lct-shell">
12
12
  <header class="lct-header">
@@ -25,20 +25,5 @@
25
25
  <%= yield %>
26
26
  </main>
27
27
  </div>
28
- <script>
29
- document.addEventListener("keydown", function(event) {
30
- if (event.key !== "/" || event.defaultPrevented || event.metaKey || event.ctrlKey || event.altKey) return;
31
-
32
- var target = event.target;
33
- if (target && (target.isContentEditable || /^(INPUT|TEXTAREA|SELECT)$/.test(target.tagName))) return;
34
-
35
- var input = document.querySelector("[data-lct-filter-input]");
36
- if (!input) return;
37
-
38
- event.preventDefault();
39
- input.focus();
40
- if (typeof input.select === "function") input.select();
41
- });
42
- </script>
43
28
  </body>
44
29
  </html>
@@ -10,7 +10,7 @@
10
10
  <div class="lct-filter-row lct-filter-row-with-sort">
11
11
  <div class="lct-field">
12
12
  <label for="lct-from">From</label>
13
- <input id="lct-from" data-lct-filter-input type="date" name="from" value="<%= params[:from] %>">
13
+ <input id="lct-from" type="date" name="from" value="<%= params[:from] %>">
14
14
  </div>
15
15
 
16
16
  <div class="lct-field">
@@ -46,16 +46,17 @@
46
46
 
47
47
  <div class="lct-field">
48
48
  <label for="lct-sort">Sort</label>
49
- <select id="lct-sort" name="sort">
50
- <option value="" <%= "selected" if @sort.blank? %>>Recent first</option>
51
- <option value="expensive" <%= "selected" if @sort == "expensive" %>>Most expensive</option>
52
- <option value="input" <%= "selected" if @sort == "input" %>>Largest input</option>
53
- <option value="output" <%= "selected" if @sort == "output" %>>Largest output</option>
54
- <% if @latency_available %>
55
- <option value="slow" <%= "selected" if @sort == "slow" %>>Slowest</option>
56
- <% end %>
57
- <option value="unknown_pricing" <%= "selected" if @sort == "unknown_pricing" %>>Unknown pricing only</option>
58
- </select>
49
+ <%= select_tag :sort,
50
+ options_for_select(
51
+ [["Recent first", ""],
52
+ ["Most expensive", "expensive"],
53
+ ["Largest input", "input"],
54
+ ["Largest output", "output"]] +
55
+ (@latency_available ? [["Slowest", "slow"]] : []) +
56
+ [["Unknown pricing only", "unknown_pricing"]],
57
+ @sort
58
+ ),
59
+ id: "lct-sort" %>
59
60
  </div>
60
61
 
61
62
  <div class="lct-filter-actions">
@@ -112,7 +113,6 @@
112
113
  </thead>
113
114
  <tbody>
114
115
  <% @calls.each do |call| %>
115
- <% tags = call.parsed_tags %>
116
116
  <tr>
117
117
  <td class="lct-nowrap"><%= format_date(call.tracked_at) %></td>
118
118
  <td><%= call.provider %></td>
@@ -124,7 +124,7 @@
124
124
  <% if @latency_available %>
125
125
  <td class="lct-num<%= ' lct-num-muted' if call.latency_ms.nil? %>"><%= call.latency_ms ? "#{number(call.latency_ms)}ms" : "n/a" %></td>
126
126
  <% end %>
127
- <td><%= render "llm_cost_tracker/shared/tag_chips", tags: tags %></td>
127
+ <td><%= render "llm_cost_tracker/shared/tag_chips", tags: call.parsed_tags %></td>
128
128
  <td><%= link_to "Details", call_path(call), class: "lct-button lct-button-secondary lct-button-compact" %></td>
129
129
  </tr>
130
130
  <% end %>
@@ -73,6 +73,11 @@
73
73
  <dt>Pricing Status</dt>
74
74
  <dd><%= pricing_status(@call) %></dd>
75
75
 
76
+ <% if LlmCostTracker::LlmApiCall.provider_response_id_column? %>
77
+ <dt>Provider Response ID</dt>
78
+ <dd><%= @call.provider_response_id.presence || "n/a" %></dd>
79
+ <% end %>
80
+
76
81
  <% if @call.has_attribute?("created_at") %>
77
82
  <dt>Created At</dt>
78
83
  <dd><%= format_date(@call.created_at) %></dd>
@@ -113,12 +118,12 @@
113
118
 
114
119
  <section class="lct-panel">
115
120
  <h2 class="lct-section-title">Tags</h2>
116
- <pre class="lct-pre"><%= safe_json(@tags) %></pre>
121
+ <pre class="lct-pre"><%= safe_json(@call.parsed_tags) %></pre>
117
122
  </section>
118
123
 
119
- <% if @metadata_available %>
124
+ <% if @call.has_attribute?("metadata") %>
120
125
  <section class="lct-panel">
121
126
  <h2 class="lct-section-title">Metadata</h2>
122
- <pre class="lct-pre"><%= safe_json(@metadata) %></pre>
127
+ <pre class="lct-pre"><%= safe_json(@call.read_attribute("metadata")) %></pre>
123
128
  </section>
124
129
  <% end %>
@@ -5,7 +5,7 @@
5
5
  <div class="lct-filter-row lct-filter-row-basic">
6
6
  <div class="lct-field">
7
7
  <label for="lct-overview-from">From</label>
8
- <input id="lct-overview-from" data-lct-filter-input type="date" name="from" value="<%= params[:from] || @from_date.iso8601 %>">
8
+ <input id="lct-overview-from" type="date" name="from" value="<%= params[:from] || @from_date.iso8601 %>">
9
9
  </div>
10
10
 
11
11
  <div class="lct-field">
@@ -1,10 +1,7 @@
1
1
  <% total = @stats.total_calls %>
2
- <% known_pricing_calls = total - @stats.unknown_pricing_count %>
3
- <% tagged_calls = total - @stats.untagged_calls_count %>
4
- <% latency_calls = @stats.latency_column_present ? total - @stats.missing_latency_count : nil %>
5
2
  <% streaming_count = @stats.streaming_count %>
6
3
  <% streaming_missing_usage = @stats.streaming_missing_usage_count %>
7
- <% streams_with_usage = streaming_count && streaming_missing_usage ? streaming_count - streaming_missing_usage : nil %>
4
+ <% calls_with_provider_response_id = @stats.provider_response_id_column_present ? total - @stats.missing_provider_response_id_count : nil %>
8
5
 
9
6
  <section class="lct-panel lct-toolbar">
10
7
  <div class="lct-toolbar-head">
@@ -15,7 +12,7 @@
15
12
  <div class="lct-filter-row lct-filter-row-basic">
16
13
  <div class="lct-field">
17
14
  <label for="lct-quality-from">From</label>
18
- <input id="lct-quality-from" data-lct-filter-input type="date" name="from" value="<%= params[:from] %>">
15
+ <input id="lct-quality-from" type="date" name="from" value="<%= params[:from] %>">
19
16
  </div>
20
17
 
21
18
  <div class="lct-field">
@@ -113,6 +110,14 @@
113
110
  </article>
114
111
  <% end %>
115
112
  <% end %>
113
+
114
+ <% if @stats.provider_response_id_column_present %>
115
+ <article class="lct-stat">
116
+ <p class="lct-stat-label">Calls with provider response ID</p>
117
+ <p class="lct-stat-value"><%= number(calls_with_provider_response_id) %></p>
118
+ <p class="lct-stat-sub"><%= percent(coverage_percent(calls_with_provider_response_id, total)) %> of calls</p>
119
+ </article>
120
+ <% end %>
116
121
  </div>
117
122
  </div>
118
123
  </section>
@@ -136,41 +141,51 @@
136
141
  </tr>
137
142
  </thead>
138
143
  <tbody>
139
- <% cost_coverage = coverage_percent(known_pricing_calls, total) %>
144
+ <% cost_coverage = coverage_percent(total - @stats.unknown_pricing_count, total) %>
140
145
  <tr>
141
146
  <td>Cost (pricing known)</td>
142
147
  <td class="lct-num"><%= percent(cost_coverage) %></td>
143
- <td class="lct-num"><%= number(known_pricing_calls) %></td>
148
+ <td class="lct-num"><%= number(total - @stats.unknown_pricing_count) %></td>
144
149
  <td><%= render "llm_cost_tracker/shared/bar", value: cost_coverage, max: 100.0 %></td>
145
150
  </tr>
146
151
 
147
- <% tag_coverage = coverage_percent(tagged_calls, total) %>
152
+ <% tag_coverage = coverage_percent(total - @stats.untagged_calls_count, total) %>
148
153
  <tr>
149
154
  <td>Tags (at least one tag)</td>
150
155
  <td class="lct-num"><%= percent(tag_coverage) %></td>
151
- <td class="lct-num"><%= number(tagged_calls) %></td>
156
+ <td class="lct-num"><%= number(total - @stats.untagged_calls_count) %></td>
152
157
  <td><%= render "llm_cost_tracker/shared/bar", value: tag_coverage, max: 100.0 %></td>
153
158
  </tr>
154
159
 
155
160
  <% if @stats.latency_column_present %>
156
- <% latency_coverage = coverage_percent(latency_calls, total) %>
161
+ <% latency_coverage = coverage_percent(total - @stats.missing_latency_count, total) %>
157
162
  <tr>
158
163
  <td>Latency</td>
159
164
  <td class="lct-num"><%= percent(latency_coverage) %></td>
160
- <td class="lct-num"><%= number(latency_calls) %></td>
165
+ <td class="lct-num"><%= number(total - @stats.missing_latency_count) %></td>
161
166
  <td><%= render "llm_cost_tracker/shared/bar", value: latency_coverage, max: 100.0 %></td>
162
167
  </tr>
163
168
  <% end %>
164
169
 
165
- <% if @stats.stream_column_present && streams_with_usage && streaming_count.to_i.positive? %>
166
- <% stream_coverage = coverage_percent(streams_with_usage, streaming_count) %>
170
+ <% if @stats.stream_column_present && streaming_count.to_i.positive? && streaming_missing_usage %>
171
+ <% stream_coverage = coverage_percent(streaming_count - streaming_missing_usage, streaming_count) %>
167
172
  <tr>
168
173
  <td>Streaming usage captured</td>
169
174
  <td class="lct-num"><%= percent(stream_coverage) %></td>
170
- <td class="lct-num"><%= number(streams_with_usage) %> / <%= number(streaming_count) %></td>
175
+ <td class="lct-num"><%= number(streaming_count - streaming_missing_usage) %> / <%= number(streaming_count) %></td>
171
176
  <td><%= render "llm_cost_tracker/shared/bar", value: stream_coverage, max: 100.0 %></td>
172
177
  </tr>
173
178
  <% end %>
179
+
180
+ <% if @stats.provider_response_id_column_present %>
181
+ <% provider_response_id_coverage = coverage_percent(calls_with_provider_response_id, total) %>
182
+ <tr>
183
+ <td>Provider response ID</td>
184
+ <td class="lct-num"><%= percent(provider_response_id_coverage) %></td>
185
+ <td class="lct-num"><%= number(calls_with_provider_response_id) %></td>
186
+ <td><%= render "llm_cost_tracker/shared/bar", value: provider_response_id_coverage, max: 100.0 %></td>
187
+ </tr>
188
+ <% end %>
174
189
  </tbody>
175
190
  </table>
176
191
  </section>
@@ -216,6 +231,13 @@
216
231
  <td>Send OpenAI requests with <code class="lct-code">stream_options: { include_usage: true }</code>, or wrap custom clients with <code class="lct-code">LlmCostTracker.track_stream</code>.</td>
217
232
  </tr>
218
233
  <% end %>
234
+ <% if @stats.provider_response_id_column_present && @stats.missing_provider_response_id_count.to_i.positive? %>
235
+ <tr>
236
+ <td>Missing provider response IDs</td>
237
+ <td>Proof of provider-issued responses is weaker when calls cannot be tied back to provider objects.</td>
238
+ <td>Upgrade to the latest parser coverage and pass <code class="lct-code">provider_response_id:</code> for custom clients when the provider exposes one.</td>
239
+ </tr>
240
+ <% end %>
219
241
  </tbody>
220
242
  </table>
221
243
  </section>
@@ -7,7 +7,7 @@
7
7
  <div class="lct-filter-row lct-filter-row-with-sort">
8
8
  <div class="lct-field">
9
9
  <label for="lct-models-from">From</label>
10
- <input id="lct-models-from" data-lct-filter-input type="date" name="from" value="<%= params[:from] %>">
10
+ <input id="lct-models-from" type="date" name="from" value="<%= params[:from] %>">
11
11
  </div>
12
12
 
13
13
  <div class="lct-field">
@@ -33,14 +33,15 @@
33
33
 
34
34
  <div class="lct-field">
35
35
  <label for="lct-models-sort">Sort</label>
36
- <select id="lct-models-sort" name="sort">
37
- <option value="cost" <%= "selected" if @sort.blank? || @sort == "cost" %>>Total spend</option>
38
- <option value="calls" <%= "selected" if @sort == "calls" %>>Call volume</option>
39
- <option value="avg_cost" <%= "selected" if @sort == "avg_cost" %>>Avg cost / call</option>
40
- <% if @latency_available %>
41
- <option value="latency" <%= "selected" if @sort == "latency" %>>Avg latency</option>
42
- <% end %>
43
- </select>
36
+ <%= select_tag :sort,
37
+ options_for_select(
38
+ [["Total spend", "cost"],
39
+ ["Call volume", "calls"],
40
+ ["Avg cost / call", "avg_cost"]] +
41
+ (@latency_available ? [["Avg latency", "latency"]] : []),
42
+ @sort.presence || "cost"
43
+ ),
44
+ id: "lct-models-sort" %>
44
45
  </div>
45
46
 
46
47
  <div class="lct-filter-actions">
@@ -1,4 +1,3 @@
1
- <%# locals: series: Array[{ label:, cost: }], comparison_series: nil %>
2
1
  <% if series.blank? %>
3
2
  <div class="lct-chart-empty">No spend in this range.</div>
4
3
  <% else %>
@@ -1,4 +1,3 @@
1
- <%# locals: tags: Hash, limit: Integer (optional) %>
2
1
  <% entries = tag_chip_entries(tags, limit: local_assigns.fetch(:limit, 3)) %>
3
2
  <% if entries.empty? %>
4
3
  <span class="lct-tag-empty">(untagged)</span>
@@ -7,7 +7,7 @@
7
7
  <div class="lct-filter-row lct-filter-row-basic">
8
8
  <div class="lct-field">
9
9
  <label for="lct-tags-from">From</label>
10
- <input id="lct-tags-from" data-lct-filter-input type="date" name="from" value="<%= params[:from] %>">
10
+ <input id="lct-tags-from" type="date" name="from" value="<%= params[:from] %>">
11
11
  </div>
12
12
 
13
13
  <div class="lct-field">
@@ -12,7 +12,7 @@
12
12
  <div class="lct-filter-row lct-filter-row-basic">
13
13
  <div class="lct-field">
14
14
  <label for="lct-tag-show-from">From</label>
15
- <input id="lct-tag-show-from" data-lct-filter-input type="date" name="from" value="<%= params[:from] %>">
15
+ <input id="lct-tag-show-from" type="date" name="from" value="<%= params[:from] %>">
16
16
  </div>
17
17
 
18
18
  <div class="lct-field">
@@ -5,7 +5,6 @@ require_relative "value_helpers"
5
5
 
6
6
  module LlmCostTracker
7
7
  class Configuration
8
- # Hostname => provider name for OpenAI-compatible APIs.
9
8
  OPENAI_COMPATIBLE_PROVIDERS = {
10
9
  "openrouter.ai" => "openrouter",
11
10
  "api.deepseek.com" => "deepseek"
@@ -12,6 +12,7 @@ module LlmCostTracker
12
12
  :latency_ms,
13
13
  :stream,
14
14
  :usage_source,
15
+ :provider_response_id,
15
16
  :tracked_at
16
17
  ) do
17
18
  def to_h
@@ -10,6 +10,7 @@ module LlmCostTracker
10
10
  cached_input_tokens
11
11
  input_tokens
12
12
  output_tokens
13
+ provider_response_id
13
14
  reasoning_tokens
14
15
  total_tokens
15
16
  ].freeze
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/generators"
4
+ require "rails/generators/active_record"
5
+
6
+ module LlmCostTracker
7
+ module Generators
8
+ class AddProviderResponseIdGenerator < Rails::Generators::Base
9
+ include ActiveRecord::Generators::Migration
10
+
11
+ source_root File.expand_path("templates", __dir__)
12
+
13
+ desc "Creates a migration to add llm_api_calls.provider_response_id"
14
+
15
+ def create_migration_file
16
+ migration_template(
17
+ "add_provider_response_id_to_llm_api_calls.rb.erb",
18
+ "db/migrate/add_provider_response_id_to_llm_api_calls.rb"
19
+ )
20
+ end
21
+
22
+ private
23
+
24
+ def migration_version
25
+ "[#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}]"
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,15 @@
1
+ class AddProviderResponseIdToLlmApiCalls < ActiveRecord::Migration<%= migration_version %>
2
+ def up
3
+ return if column_exists?(:llm_api_calls, :provider_response_id)
4
+
5
+ add_column :llm_api_calls, :provider_response_id, :string
6
+ add_index :llm_api_calls, :provider_response_id
7
+ end
8
+
9
+ def down
10
+ return unless column_exists?(:llm_api_calls, :provider_response_id)
11
+
12
+ remove_index :llm_api_calls, :provider_response_id if index_exists?(:llm_api_calls, :provider_response_id)
13
+ remove_column :llm_api_calls, :provider_response_id
14
+ end
15
+ end
@@ -12,6 +12,7 @@ class CreateLlmApiCalls < ActiveRecord::Migration<%= migration_version %>
12
12
  t.integer :latency_ms
13
13
  t.boolean :stream, null: false, default: false
14
14
  t.string :usage_source
15
+ t.string :provider_response_id
15
16
  if postgresql?
16
17
  t.jsonb :tags, null: false, default: {}
17
18
  else
@@ -28,6 +29,7 @@ class CreateLlmApiCalls < ActiveRecord::Migration<%= migration_version %>
28
29
  add_index :llm_api_calls, [:provider, :tracked_at]
29
30
  add_index :llm_api_calls, :stream
30
31
  add_index :llm_api_calls, :usage_source
32
+ add_index :llm_api_calls, :provider_response_id
31
33
  add_index :llm_api_calls, :tags, using: :gin if postgresql?
32
34
  end
33
35
 
@@ -16,7 +16,6 @@ module LlmCostTracker
16
16
 
17
17
  self.table_name = "llm_api_calls"
18
18
 
19
- # Scopes for querying
20
19
  scope :with_cost, -> { where.not(total_cost: nil) }
21
20
  scope :without_cost, -> { where(total_cost: nil) }
22
21
  scope :unknown_pricing, -> { without_cost }
@@ -24,6 +23,12 @@ module LlmCostTracker
24
23
  scope :streaming, -> { stream_column? ? where(stream: true) : none }
25
24
  scope :non_streaming, -> { stream_column? ? where(stream: [false, nil]) : all }
26
25
  scope :by_usage_source, ->(source) { usage_source_column? ? where(usage_source: source.to_s) : none }
26
+ scope :with_provider_response_id, lambda {
27
+ provider_response_id_column? ? where.not(provider_response_id: [nil, ""]) : none
28
+ }
29
+ scope :missing_provider_response_id, lambda {
30
+ provider_response_id_column? ? where(provider_response_id: [nil, ""]) : none
31
+ }
27
32
  scope :streaming_missing_usage, lambda {
28
33
  return none unless stream_column? && usage_source_column?
29
34
 
@@ -51,7 +56,6 @@ module LlmCostTracker
51
56
  TagQuery.apply(self, tags)
52
57
  end
53
58
 
54
- # Aggregations
55
59
  def self.total_cost
56
60
  sum(:total_cost).to_f
57
61
  end
@@ -61,6 +61,7 @@ module LlmCostTracker
61
61
  latency_ms: latency_ms,
62
62
  stream: parsed.stream,
63
63
  usage_source: parsed.usage_source,
64
+ provider_response_id: parsed.provider_response_id,
64
65
  metadata: resolved_tags(request_env).merge(parsed.metadata)
65
66
  )
66
67
  rescue LlmCostTracker::Error
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LlmCostTracker
4
+ module ParameterHash
5
+ class << self
6
+ def hash_like?(value)
7
+ value.is_a?(Hash) || action_controller_parameters?(value)
8
+ end
9
+
10
+ def to_hash(value)
11
+ return {} if value.nil?
12
+ return value.to_unsafe_h if action_controller_parameters?(value)
13
+ return value.to_h if value.is_a?(Hash)
14
+ return {} unless value.respond_to?(:to_h)
15
+
16
+ hash = value.to_h
17
+ hash.is_a?(Hash) ? hash : {}
18
+ rescue ArgumentError, TypeError
19
+ {}
20
+ end
21
+
22
+ def with_indifferent_access(value)
23
+ to_hash(value).with_indifferent_access
24
+ end
25
+
26
+ private
27
+
28
+ def action_controller_parameters?(value)
29
+ defined?(ActionController::Parameters) && value.is_a?(ActionController::Parameters)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -12,11 +12,21 @@ module LlmCostTracker
12
12
  :cache_creation_input_tokens,
13
13
  :reasoning_tokens,
14
14
  :stream,
15
- :usage_source
15
+ :usage_source,
16
+ :provider_response_id
16
17
  )
17
18
 
18
19
  class ParsedUsage
19
- TRACKING_KEYS = %i[provider model input_tokens output_tokens total_tokens stream usage_source].freeze
20
+ TRACKING_KEYS = %i[
21
+ provider
22
+ model
23
+ input_tokens
24
+ output_tokens
25
+ total_tokens
26
+ stream
27
+ usage_source
28
+ provider_response_id
29
+ ].freeze
20
30
 
21
31
  def self.build(**attributes)
22
32
  new(
@@ -30,7 +40,8 @@ module LlmCostTracker
30
40
  cache_creation_input_tokens: attributes[:cache_creation_input_tokens],
31
41
  reasoning_tokens: attributes[:reasoning_tokens],
32
42
  stream: attributes[:stream] || false,
33
- usage_source: attributes[:usage_source]
43
+ usage_source: attributes[:usage_source],
44
+ provider_response_id: attributes[:provider_response_id]
34
45
  )
35
46
  end
36
47
 
@@ -31,6 +31,7 @@ module LlmCostTracker
31
31
 
32
32
  ParsedUsage.build(
33
33
  provider: "anthropic",
34
+ provider_response_id: response["id"],
34
35
  model: response["model"] || request["model"],
35
36
  input_tokens: usage["input_tokens"].to_i,
36
37
  output_tokens: usage["output_tokens"].to_i,
@@ -48,35 +49,9 @@ module LlmCostTracker
48
49
  request = safe_json_parse(request_body)
49
50
  model = stream_model(events) || request["model"]
50
51
  usage = stream_usage(events)
52
+ response_id = stream_response_id(events)
51
53
 
52
- if usage
53
- input = usage["input_tokens"].to_i
54
- output = usage["output_tokens"].to_i
55
- cache_read = usage["cache_read_input_tokens"].to_i
56
- cache_creation = usage["cache_creation_input_tokens"].to_i
57
-
58
- ParsedUsage.build(
59
- provider: "anthropic",
60
- model: model,
61
- input_tokens: input,
62
- output_tokens: output,
63
- total_tokens: input + output + cache_read + cache_creation,
64
- cache_read_input_tokens: usage["cache_read_input_tokens"],
65
- cache_creation_input_tokens: usage["cache_creation_input_tokens"],
66
- stream: true,
67
- usage_source: :stream_final
68
- )
69
- else
70
- ParsedUsage.build(
71
- provider: "anthropic",
72
- model: model,
73
- input_tokens: 0,
74
- output_tokens: 0,
75
- total_tokens: 0,
76
- stream: true,
77
- usage_source: :unknown
78
- )
79
- end
54
+ usage ? build_stream_result(model, usage, response_id) : build_unknown_stream_result(model, response_id)
80
55
  end
81
56
 
82
57
  private
@@ -114,6 +89,50 @@ module LlmCostTracker
114
89
  end
115
90
  nil
116
91
  end
92
+
93
+ def stream_response_id(events)
94
+ events.each do |event|
95
+ data = event[:data]
96
+ next unless data.is_a?(Hash)
97
+
98
+ id = data.dig("message", "id") || data["id"]
99
+ return id if id && !id.to_s.empty?
100
+ end
101
+ nil
102
+ end
103
+
104
+ def build_stream_result(model, usage, response_id)
105
+ input = usage["input_tokens"].to_i
106
+ output = usage["output_tokens"].to_i
107
+ cache_read = usage["cache_read_input_tokens"].to_i
108
+ cache_creation = usage["cache_creation_input_tokens"].to_i
109
+
110
+ ParsedUsage.build(
111
+ provider: "anthropic",
112
+ provider_response_id: response_id,
113
+ model: model,
114
+ input_tokens: input,
115
+ output_tokens: output,
116
+ total_tokens: input + output + cache_read + cache_creation,
117
+ cache_read_input_tokens: usage["cache_read_input_tokens"],
118
+ cache_creation_input_tokens: usage["cache_creation_input_tokens"],
119
+ stream: true,
120
+ usage_source: :stream_final
121
+ )
122
+ end
123
+
124
+ def build_unknown_stream_result(model, response_id)
125
+ ParsedUsage.build(
126
+ provider: "anthropic",
127
+ provider_response_id: response_id,
128
+ model: model,
129
+ input_tokens: 0,
130
+ output_tokens: 0,
131
+ total_tokens: 0,
132
+ stream: true,
133
+ usage_source: :unknown
134
+ )
135
+ end
117
136
  end
118
137
  end
119
138
  end