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/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,
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/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,
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/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/ledger/call.rb,
lib/llm_cost_tracker/doctor/price_check.rb,
lib/llm_cost_tracker/middleware/faraday.rb,
lib/llm_cost_tracker/pricing/components.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/ledger/rollups/batch.rb,
lib/llm_cost_tracker/parsers/openai_usage.rb,
lib/llm_cost_tracker/pricing/sync/fetcher.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/event.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,
lib/llm_cost_tracker/doctor/capture_verifier.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/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,
app/models/llm_cost_tracker/ledger/call_metrics.rb,
app/models/llm_cost_tracker/ledger/period/total.rb,
lib/llm_cost_tracker/pricing/sync/registry_diff.rb,
app/controllers/llm_cost_tracker/tags_controller.rb,
lib/llm_cost_tracker/ledger/schema/period_totals.rb,
app/controllers/llm_cost_tracker/calls_controller.rb,
app/models/llm_cost_tracker/ledger/tags/accessors.rb,
lib/llm_cost_tracker/pricing/sync/registry_loader.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/models/llm_cost_tracker/ledger/period/grouping.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/configuration/instrumentation.rb,
app/helpers/llm_cost_tracker/dashboard_query_helper.rb,
app/services/llm_cost_tracker/dashboard/time_series.rb,
app/helpers/llm_cost_tracker/dashboard_filter_helper.rb,
app/services/llm_cost_tracker/dashboard/data_quality.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,
app/controllers/llm_cost_tracker/data_quality_controller.rb,
app/services/llm_cost_tracker/dashboard/tag_key_explorer.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,
lib/llm_cost_tracker/generators/llm_cost_tracker/add_ingestion_generator.rb,
lib/llm_cost_tracker/generators/llm_cost_tracker/add_streaming_generator.rb,
lib/llm_cost_tracker/generators/llm_cost_tracker/add_latency_ms_generator.rb,
lib/llm_cost_tracker/generators/llm_cost_tracker/add_token_usage_generator.rb,
lib/llm_cost_tracker/generators/llm_cost_tracker/add_period_totals_generator.rb,
lib/llm_cost_tracker/generators/llm_cost_tracker/upgrade_tags_to_jsonb_generator.rb,
lib/llm_cost_tracker/generators/llm_cost_tracker/upgrade_cost_precision_generator.rb,
lib/llm_cost_tracker/generators/llm_cost_tracker/add_provider_response_id_generator.rb

Defined Under Namespace

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

Constant Summary collapse

VERSION =
"0.7.1"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject (readonly)

Returns the value of attribute configuration.



48
49
50
# File 'lib/llm_cost_tracker.rb', line 48

def configuration
  @configuration
end

Class Method Details

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

Yields:

  • (config)

Raises:



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/llm_cost_tracker.rb', line 50

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

  yield(config)
  config.openai_compatible_providers = config.openai_compatible_providers.dup
  config.finalize!
  Pricing::Lookup.reset!
  Integrations.install!
  config
end

.enforce_budget!Object



87
88
89
# File 'lib/llm_cost_tracker.rb', line 87

def enforce_budget!
  Tracker.enforce_budget!
end

.flush!(timeout: nil) ⇒ Object



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

def flush!(timeout: nil)
  if timeout
    Ingestion::Worker.flush!(timeout: timeout)
  else
    Ingestion::Worker.flush!
  end
end

.reset_configuration!Object



62
63
64
65
66
67
68
69
# File 'lib/llm_cost_tracker.rb', line 62

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

.shutdown!(timeout: nil, drain: true) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/llm_cost_tracker.rb', line 79

def shutdown!(timeout: nil, drain: true)
  if timeout
    Ingestion::Worker.shutdown!(timeout: timeout, drain: drain)
  else
    Ingestion::Worker.shutdown!(drain: drain)
  end
end

.track(provider:, input_tokens:, output_tokens:, model: nil, latency_ms: nil, stream: false, usage_source: :manual, enforce_budget: false, provider_response_id: nil, pricing_mode: nil, **metadata) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/llm_cost_tracker.rb', line 96

def track(provider:, input_tokens:, output_tokens:, model: nil, latency_ms: nil, stream: false,
          usage_source: :manual, enforce_budget: false, provider_response_id: nil, pricing_mode: nil, **)
  enforce_budget! if enforce_budget
  token_usage = TokenUsage.from_hash(.merge(input_tokens: input_tokens, output_tokens: output_tokens))

  Tracker.record(
    capture: UsageCapture.build(
      provider: provider,
      model: model,
      token_usage: token_usage,
      stream: stream,
      usage_source: usage_source,
      provider_response_id: provider_response_id
    ),
    latency_ms: latency_ms,
    pricing_mode: pricing_mode,
    metadata: 
  )
end

.track_stream(provider:, model: nil, latency_ms: nil, enforce_budget: false, provider_response_id: nil, pricing_mode: nil, **metadata) ⇒ Object



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

def track_stream(provider:, model: nil, latency_ms: nil, enforce_budget: false, provider_response_id: nil,
                 pricing_mode: nil, **)
  require_relative "llm_cost_tracker/capture/stream_collector"
  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,
    pricing_mode: pricing_mode,
    metadata: 
  )
  yield collector
  collector.finish!
rescue StandardError
  collector&.finish!(errored: true)
  raise
end

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



91
92
93
94
# File 'lib/llm_cost_tracker.rb', line 91

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