Module: LlmCostTracker

Defined in:
lib/llm_cost_tracker.rb,
lib/llm_cost_tracker/check.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/ledger.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/currency.rb,
lib/llm_cost_tracker/tags/key.rb,
lib/llm_cost_tracker/ingestion.rb,
lib/llm_cost_tracker/providers.rb,
lib/llm_cost_tracker/retention.rb,
app/models/llm_cost_tracker/call.rb,
lib/llm_cost_tracker/capture/sse.rb,
lib/llm_cost_tracker/report/data.rb,
lib/llm_cost_tracker/charges/cost.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/pricing/mode.rb,
lib/llm_cost_tracker/pricing/rate.rb,
lib/llm_cost_tracker/pricing/sync.rb,
lib/llm_cost_tracker/tags/context.rb,
lib/llm_cost_tracker/usage/source.rb,
lib/llm_cost_tracker/configuration.rb,
lib/llm_cost_tracker/ledger/period.rb,
lib/llm_cost_tracker/ledger/schema.rb,
lib/llm_cost_tracker/usage/catalog.rb,
lib/llm_cost_tracker/ingestion/pool.rb,
lib/llm_cost_tracker/ledger/rollups.rb,
lib/llm_cost_tracker/pricing/source.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/pricing/matcher.rb,
lib/llm_cost_tracker/pricing/unknown.rb,
lib/llm_cost_tracker/usage/dimension.rb,
lib/llm_cost_tracker/capture_verifier.rb,
lib/llm_cost_tracker/ingestion/worker.rb,
lib/llm_cost_tracker/pricing/backfill.rb,
lib/llm_cost_tracker/pricing/registry.rb,
lib/llm_cost_tracker/report/formatter.rb,
lib/llm_cost_tracker/charges/line_item.rb,
lib/llm_cost_tracker/integrations/base.rb,
lib/llm_cost_tracker/ledger/tags/query.rb,
lib/llm_cost_tracker/pricing/estimator.rb,
lib/llm_cost_tracker/pricing/price_key.rb,
lib/llm_cost_tracker/usage/token_usage.rb,
app/models/llm_cost_tracker/call_rollup.rb,
lib/llm_cost_tracker/doctor/price_check.rb,
lib/llm_cost_tracker/ledger/schema/base.rb,
lib/llm_cost_tracker/middleware/faraday.rb,
lib/llm_cost_tracker/capture/sdk_payload.rb,
lib/llm_cost_tracker/charges/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,
lib/llm_cost_tracker/pricing/calculation.rb,
app/helpers/llm_cost_tracker/chart_helper.rb,
lib/llm_cost_tracker/ledger/period/totals.rb,
lib/llm_cost_tracker/ledger/tags/encoding.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,
lib/llm_cost_tracker/ledger/tags/breakdown.rb,
lib/llm_cost_tracker/pricing/service_rates.rb,
lib/llm_cost_tracker/providers/azure/hosts.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/providers/azure/parser.rb,
lib/llm_cost_tracker/providers/openai/hosts.rb,
app/services/llm_cost_tracker/dashboard/sort.rb,
lib/llm_cost_tracker/ledger/schema/call_tags.rb,
lib/llm_cost_tracker/providers/gemini/parser.rb,
lib/llm_cost_tracker/providers/openai/parser.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,
app/helpers/llm_cost_tracker/application_helper.rb,
app/helpers/llm_cost_tracker/token_usage_helper.rb,
app/services/llm_cost_tracker/dashboard/masking.rb,
lib/llm_cost_tracker/ledger/schema/call_rollups.rb,
lib/llm_cost_tracker/pricing/sync/registry_diff.rb,
lib/llm_cost_tracker/providers/anthropic/parser.rb,
app/controllers/llm_cost_tracker/tags_controller.rb,
app/helpers/llm_cost_tracker/inline_style_helper.rb,
lib/llm_cost_tracker/integrations/openai/patches.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/helpers/llm_cost_tracker/sortable_table_helper.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/controllers/llm_cost_tracker/pricing_controller.rb,
app/helpers/llm_cost_tracker/dashboard_query_helper.rb,
app/services/llm_cost_tracker/dashboard/setup_state.rb,
app/services/llm_cost_tracker/dashboard/time_series.rb,
lib/llm_cost_tracker/ledger/schema/ingestion/leases.rb,
app/services/llm_cost_tracker/dashboard/data_quality.rb,
lib/llm_cost_tracker/providers/gemini/model_families.rb,
lib/llm_cost_tracker/providers/openai/model_families.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,
lib/llm_cost_tracker/providers/gemini/usage_extractor.rb,
lib/llm_cost_tracker/providers/openai/response_parser.rb,
lib/llm_cost_tracker/providers/openai/service_charges.rb,
lib/llm_cost_tracker/providers/openai/usage_extractor.rb,
app/services/llm_cost_tracker/dashboard/monthly_budget.rb,
app/services/llm_cost_tracker/dashboard/overview_stats.rb,
lib/llm_cost_tracker/integrations/openai/batch_capture.rb,
app/controllers/llm_cost_tracker/application_controller.rb,
lib/llm_cost_tracker/providers/openai_compatible/parser.rb,
app/controllers/llm_cost_tracker/data_quality_controller.rb,
app/services/llm_cost_tracker/dashboard/pricing_overview.rb,
app/services/llm_cost_tracker/dashboard/tag_key_explorer.rb,
lib/llm_cost_tracker/providers/anthropic/response_parser.rb,
lib/llm_cost_tracker/providers/anthropic/usage_extractor.rb,
app/services/llm_cost_tracker/dashboard/provider_breakdown.rb,
lib/llm_cost_tracker/ledger/schema/ingestion/inbox_entries.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/call_rollups_generator.rb,
lib/llm_cost_tracker/generators/llm_cost_tracker/async_ingestion_generator.rb,
lib/llm_cost_tracker/generators/llm_cost_tracker/upgrade_image_tokens_generator.rb,
lib/llm_cost_tracker/generators/llm_cost_tracker/upgrade_call_rollups_provider_generator.rb,
lib/llm_cost_tracker/generators/llm_cost_tracker/upgrade_call_tags_key_value_index_generator.rb

Defined Under Namespace

Modules: ApplicationHelper, Assets, Budget, Capture, Charges, ChartHelper, Dashboard, DashboardFilterOptionsHelper, DashboardQueryHelper, Generators, Ingestion, InlineStyleHelper, Integrations, Ledger, Logging, Middleware, PaginationHelper, Parsers, Pricing, Providers, Report, Retention, SortableTableHelper, Tags, Timing, TokenUsageHelper, Tracker, Usage Classes: ApplicationController, AssetsController, BudgetExceededError, Call, CallLineItem, CallRollup, CallTag, CallsController, CaptureVerifier, Check, Configuration, DashboardController, DataQualityController, Doctor, Engine, Error, Event, InvalidFilterError, ModelsController, PricingController, Railtie, TagsController, UnknownPricingError

Constant Summary collapse

VERSION =
"0.12.0"
DEFAULT_CURRENCY =
"USD"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject (readonly)

Returns the value of attribute configuration.



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

def configuration
  @configuration
end

Class Method Details

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

Yields:

  • (config)

Raises:



53
54
55
56
57
58
59
60
61
62
# File 'lib/llm_cost_tracker.rb', line 53

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

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

.table_name_prefixObject



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

def table_name_prefix
  "llm_cost_tracker_"
end

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



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/llm_cost_tracker.rb', line 68

def track(provider:,
          tokens:,
          model: nil,
          tags: {},
          latency_ms: nil,
          stream: false,
          usage_source: Usage::Source::MANUAL,
          enforce_budget: false,
          provider_response_id: nil,
          provider_project_id: nil,
          provider_api_key_id: nil,
          provider_workspace_id: nil,
          pricing_mode: nil,
          service_line_items: [])
  Tracker.record(
    event: Event.build(
      provider: provider,
      model: model,
      token_usage: 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,
      pricing_mode: pricing_mode,
      service_line_items: service_line_items
    ),
    latency_ms: latency_ms,
    metadata: tags,
    enforce_budget: enforce_budget
  )
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, pricing_mode: nil) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/llm_cost_tracker.rb', line 102

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,
                 pricing_mode: nil)
  require_relative "llm_cost_tracker/capture/stream_collector"
  Budget.enforce!(provider: provider, model: model, force: true) 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,
    pricing_mode: pricing_mode,
    metadata: tags
  )
  yield collector
  collector.finish!
rescue StandardError
  collector&.finish!(errored: true)
  raise
end

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



64
65
66
# File 'lib/llm_cost_tracker.rb', line 64

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