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
|
# File 'lib/llm_cost_tracker/tracker.rb', line 23
def record(event:, latency_ms: nil, pricing_mode: nil, metadata: {}, context_tags: nil)
return unless LlmCostTracker.configuration.enabled
pricing_mode = Pricing.normalize_mode(pricing_mode) || event.pricing_mode
cost_data, pricing_snapshot, priced_line_items = Pricing.calculate(
provider: event.provider,
model: event.model,
tokens: event.token_usage,
line_items: event.line_items,
pricing_mode: pricing_mode
)
if cost_data.nil? && event.token_usage.total_tokens.positive? && priced_line_items.none?(&:priced?)
Pricing::Unknown.process(event.model)
end
event = build_event(
event: event,
pricing_mode: pricing_mode,
cost_data: cost_data,
pricing_snapshot: pricing_snapshot,
line_items: priced_line_items,
metadata: metadata,
latency_ms: latency_ms,
context_tags: context_tags
)
if Ingestion.async?
Ingestion::Inbox.save(event)
Ingestion::Worker.ensure_started
else
Ledger::Store.insert(event, skip_existence_check: true)
end
yield if block_given?
notify_subscribers(event)
Budget.check!(event)
event
end
|