Module: LlmCostTracker

Defined in:
lib/llm_cost_tracker.rb,
lib/llm_cost_tracker/event.rb,
lib/llm_cost_tracker/assets.rb,
lib/llm_cost_tracker/budget.rb,
lib/llm_cost_tracker/doctor.rb,
lib/llm_cost_tracker/engine.rb,
lib/llm_cost_tracker/errors.rb,
lib/llm_cost_tracker/report.rb,
lib/llm_cost_tracker/timing.rb,
lib/llm_cost_tracker/logging.rb,
lib/llm_cost_tracker/parsers.rb,
lib/llm_cost_tracker/pricing.rb,
lib/llm_cost_tracker/railtie.rb,
lib/llm_cost_tracker/tracker.rb,
lib/llm_cost_tracker/version.rb,
lib/llm_cost_tracker/tags/key.rb,
lib/llm_cost_tracker/ingestion.rb,
lib/llm_cost_tracker/retention.rb,
app/models/llm_cost_tracker/call.rb,
lib/llm_cost_tracker/parsers/sse.rb,
lib/llm_cost_tracker/report/data.rb,
lib/llm_cost_tracker/token_usage.rb,
lib/llm_cost_tracker/doctor/check.rb,
lib/llm_cost_tracker/doctor/probe.rb,
lib/llm_cost_tracker/integrations.rb,
lib/llm_cost_tracker/ledger/store.rb,
lib/llm_cost_tracker/parsers/base.rb,
lib/llm_cost_tracker/pricing/sync.rb,
lib/llm_cost_tracker/tags/context.rb,
lib/llm_cost_tracker/configuration.rb,
lib/llm_cost_tracker/ledger/period.rb,
lib/llm_cost_tracker/usage_capture.rb,
lib/llm_cost_tracker/capture/stream.rb,
lib/llm_cost_tracker/ledger/rollups.rb,
lib/llm_cost_tracker/parsers/gemini.rb,
lib/llm_cost_tracker/parsers/openai.rb,
lib/llm_cost_tracker/pricing/lookup.rb,
lib/llm_cost_tracker/tags/sanitizer.rb,
app/models/llm_cost_tracker/call_tag.rb,
lib/llm_cost_tracker/ingestion/batch.rb,
lib/llm_cost_tracker/ingestion/inbox.rb,
lib/llm_cost_tracker/ledger/tags/sql.rb,
lib/llm_cost_tracker/pricing/unknown.rb,
lib/llm_cost_tracker/ingestion/worker.rb,
lib/llm_cost_tracker/pricing/registry.rb,
lib/llm_cost_tracker/report/formatter.rb,
lib/llm_cost_tracker/billing/line_item.rb,
lib/llm_cost_tracker/integrations/base.rb,
lib/llm_cost_tracker/ledger/tags/query.rb,
lib/llm_cost_tracker/parsers/anthropic.rb,
lib/llm_cost_tracker/pricing/explainer.rb,
app/models/llm_cost_tracker/call_rollup.rb,
lib/llm_cost_tracker/billing/components.rb,
lib/llm_cost_tracker/doctor/price_check.rb,
lib/llm_cost_tracker/middleware/faraday.rb,
lib/llm_cost_tracker/billing/cost_status.rb,
lib/llm_cost_tracker/doctor/schema_check.rb,
lib/llm_cost_tracker/integrations/openai.rb,
lib/llm_cost_tracker/ledger/schema/calls.rb,
app/helpers/llm_cost_tracker/chart_helper.rb,
lib/llm_cost_tracker/ledger/period/totals.rb,
lib/llm_cost_tracker/parsers/openai_usage.rb,
lib/llm_cost_tracker/pricing/sync/fetcher.rb,
app/models/llm_cost_tracker/call_line_item.rb,
lib/llm_cost_tracker/ingestion/lease_claim.rb,
lib/llm_cost_tracker/integrations/ruby_llm.rb,
lib/llm_cost_tracker/ledger/schema/adapter.rb,
app/models/llm_cost_tracker/ingestion/lease.rb,
lib/llm_cost_tracker/capture/stream_tracker.rb,
lib/llm_cost_tracker/doctor/ingestion_check.rb,
lib/llm_cost_tracker/integrations/anthropic.rb,
app/models/llm_cost_tracker/provider_invoice.rb,
lib/llm_cost_tracker/doctor/capture_verifier.rb,
lib/llm_cost_tracker/doctor/cost_drift_check.rb,
lib/llm_cost_tracker/ledger/schema/call_tags.rb,
lib/llm_cost_tracker/pricing/service_charges.rb,
lib/llm_cost_tracker/capture/stream_collector.rb,
lib/llm_cost_tracker/pricing/effective_prices.rb,
app/helpers/llm_cost_tracker/pagination_helper.rb,
app/services/llm_cost_tracker/dashboard/filter.rb,
app/services/llm_cost_tracker/dashboard/params.rb,
lib/llm_cost_tracker/doctor/legacy_audit_check.rb,
lib/llm_cost_tracker/ledger/rollups/upsert_sql.rb,
lib/llm_cost_tracker/parsers/openai_compatible.rb,
app/helpers/llm_cost_tracker/application_helper.rb,
app/helpers/llm_cost_tracker/token_usage_helper.rb,
lib/llm_cost_tracker/ledger/schema/call_rollups.rb,
lib/llm_cost_tracker/pricing/sync/registry_diff.rb,
app/controllers/llm_cost_tracker/tags_controller.rb,
lib/llm_cost_tracker/pricing/sync_change_printer.rb,
app/controllers/llm_cost_tracker/calls_controller.rb,
app/models/llm_cost_tracker/ingestion/inbox_entry.rb,
lib/llm_cost_tracker/pricing/sync/registry_writer.rb,
app/controllers/llm_cost_tracker/assets_controller.rb,
app/controllers/llm_cost_tracker/models_controller.rb,
app/services/llm_cost_tracker/dashboard/date_range.rb,
app/services/llm_cost_tracker/dashboard/pagination.rb,
app/services/llm_cost_tracker/dashboard/top_models.rb,
lib/llm_cost_tracker/ledger/schema/call_line_items.rb,
app/helpers/llm_cost_tracker/dashboard_query_helper.rb,
app/services/llm_cost_tracker/dashboard/time_series.rb,
lib/llm_cost_tracker/parsers/openai_service_charges.rb,
app/helpers/llm_cost_tracker/dashboard_filter_helper.rb,
app/services/llm_cost_tracker/dashboard/data_quality.rb,
lib/llm_cost_tracker/ledger/schema/provider_invoices.rb,
app/controllers/llm_cost_tracker/dashboard_controller.rb,
app/services/llm_cost_tracker/dashboard/spend_anomaly.rb,
app/services/llm_cost_tracker/dashboard/tag_breakdown.rb,
app/services/llm_cost_tracker/dashboard/overview_stats.rb,
app/controllers/llm_cost_tracker/application_controller.rb,
lib/llm_cost_tracker/doctor/legacy_billing_status_check.rb,
app/controllers/llm_cost_tracker/data_quality_controller.rb,
app/services/llm_cost_tracker/dashboard/tag_key_explorer.rb,
lib/llm_cost_tracker/doctor/pricing_snapshot_drift_check.rb,
app/services/llm_cost_tracker/dashboard/provider_breakdown.rb,
app/helpers/llm_cost_tracker/dashboard_filter_options_helper.rb,
lib/llm_cost_tracker/generators/llm_cost_tracker/prices_generator.rb,
lib/llm_cost_tracker/generators/llm_cost_tracker/install_generator.rb

Defined Under Namespace

Modules: ApplicationHelper, Assets, Billing, Capture, ChartHelper, Dashboard, DashboardFilterHelper, DashboardFilterOptionsHelper, DashboardQueryHelper, Generators, Ingestion, Integrations, Ledger, Logging, Middleware, PaginationHelper, Parsers, Pricing, Retention, Tags, Timing, TokenUsageHelper Classes: ApplicationController, AssetsController, Budget, BudgetExceededError, Call, CallLineItem, CallRollup, CallTag, CallsController, Configuration, DashboardController, DataQualityController, Doctor, Engine, Error, Event, InvalidFilterError, ModelsController, ProviderInvoice, Railtie, Report, TagsController, TokenUsage, Tracker, UnknownPricingError, UsageCapture

Constant Summary collapse

VERSION =
"0.8.0"
KNOWN_TOKEN_KEYS =
(
  Billing::Components::TOKEN_PRICED.map(&:key) + %i[total hidden_output]
).freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject (readonly)

Returns the value of attribute configuration.



51
52
53
# File 'lib/llm_cost_tracker.rb', line 51

def configuration
  @configuration
end

Class Method Details

.configure {|config| ... } ⇒ Object

Yields:

  • (config)

Raises:



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/llm_cost_tracker.rb', line 57

def configure
  config = configuration
  raise Error, "LlmCostTracker is already configured" if config.finalized?

  yield(config)
  config.finalize!
  Pricing::Lookup.reset!
  Pricing::Registry.reset!
  Pricing::ServiceCharges.reset!
  Integrations.install!
  config
end

.reset_configuration!Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/llm_cost_tracker.rb', line 70

def reset_configuration!
  Ingestion::Worker.shutdown!(drain: false)
  @configuration = Configuration.new
  Pricing::Lookup.reset!
  Pricing::Registry.reset!
  Pricing::ServiceCharges.reset!
  Pricing::Unknown.reset!
  Ingestion::Worker.reset!
  Tags::Context.clear!
end

.table_name_prefixObject



53
54
55
# File 'lib/llm_cost_tracker.rb', line 53

def table_name_prefix
  "llm_cost_tracker_"
end

.track(provider:, tokens:, model: nil, tags: {}, latency_ms: nil, stream: false, usage_source: :manual, enforce_budget: false, provider_response_id: nil, provider_project_id: nil, provider_api_key_id: nil, provider_workspace_id: nil, batch: nil, pricing_mode: nil, service_line_items: []) ⇒ Object



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
# File 'lib/llm_cost_tracker.rb', line 85

def track(provider:, tokens:, model: nil, tags: {}, latency_ms: nil, stream: false,
          usage_source: :manual, enforce_budget: false,
          provider_response_id: nil, provider_project_id: nil, provider_api_key_id: nil,
          provider_workspace_id: nil, batch: nil, pricing_mode: nil, service_line_items: [])
  Tracker.enforce_budget! if enforce_budget

  Tracker.record(
    capture: UsageCapture.build(
      provider: provider,
      model: model,
      token_usage: TokenUsage.build_from_tokens(tokens),
      stream: stream,
      usage_source: usage_source,
      provider_response_id: provider_response_id,
      provider_project_id: provider_project_id,
      provider_api_key_id: provider_api_key_id,
      provider_workspace_id: provider_workspace_id,
      batch: batch,
      pricing_mode: pricing_mode,
      service_line_items: service_line_items
    ),
    latency_ms: latency_ms,
    pricing_mode: pricing_mode,
    metadata: tags
  )
end

.track_stream(provider:, model: nil, tags: {}, latency_ms: nil, enforce_budget: false, provider_response_id: nil, provider_project_id: nil, provider_api_key_id: nil, provider_workspace_id: nil, batch: nil, pricing_mode: nil) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/llm_cost_tracker.rb', line 112

def track_stream(provider:, model: nil, tags: {}, latency_ms: nil, enforce_budget: false,
                 provider_response_id: nil, provider_project_id: nil, provider_api_key_id: nil,
                 provider_workspace_id: nil, batch: nil, pricing_mode: nil)
  require_relative "llm_cost_tracker/capture/stream_collector"
  Tracker.enforce_budget! if enforce_budget
  collector = Capture::StreamCollector.new(
    provider: provider.to_s,
    model: model,
    latency_ms: latency_ms,
    provider_response_id: provider_response_id,
    provider_project_id: provider_project_id,
    provider_api_key_id: provider_api_key_id,
    provider_workspace_id: provider_workspace_id,
    batch: batch,
    pricing_mode: pricing_mode,
    metadata: tags
  )
  yield collector
  collector.finish!
rescue StandardError
  collector&.finish!(errored: true)
  raise
end

.with_tags(tags = nil, **kwargs) ⇒ Object



81
82
83
# File 'lib/llm_cost_tracker.rb', line 81

def with_tags(tags = nil, **kwargs, &)
  Tags::Context.with((tags || {}).merge(kwargs), &)
end