Module: LlmCostTracker

Defined in:
lib/llm_cost_tracker.rb,
lib/llm_cost_tracker/cost.rb,
lib/llm_cost_tracker/event.rb,
lib/llm_cost_tracker/budget.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/pricing.rb,
lib/llm_cost_tracker/railtie.rb,
lib/llm_cost_tracker/tag_key.rb,
lib/llm_cost_tracker/tracker.rb,
lib/llm_cost_tracker/version.rb,
lib/llm_cost_tracker/tag_query.rb,
lib/llm_cost_tracker/report_data.rb,
lib/llm_cost_tracker/tags_column.rb,
lib/llm_cost_tracker/llm_api_call.rb,
lib/llm_cost_tracker/parsed_usage.rb,
lib/llm_cost_tracker/parsers/base.rb,
lib/llm_cost_tracker/configuration.rb,
lib/llm_cost_tracker/tag_accessors.rb,
lib/llm_cost_tracker/event_metadata.rb,
lib/llm_cost_tracker/parsers/gemini.rb,
lib/llm_cost_tracker/parsers/openai.rb,
lib/llm_cost_tracker/price_registry.rb,
lib/llm_cost_tracker/period_grouping.rb,
lib/llm_cost_tracker/unknown_pricing.rb,
lib/llm_cost_tracker/parsers/registry.rb,
lib/llm_cost_tracker/report_formatter.rb,
lib/llm_cost_tracker/parsers/anthropic.rb,
lib/llm_cost_tracker/middleware/faraday.rb,
app/services/llm_cost_tracker/pagination.rb,
lib/llm_cost_tracker/engine_compatibility.rb,
lib/llm_cost_tracker/parsers/openai_usage.rb,
app/services/llm_cost_tracker/dashboard/filter.rb,
lib/llm_cost_tracker/parsers/openai_compatible.rb,
app/helpers/llm_cost_tracker/application_helper.rb,
app/controllers/llm_cost_tracker/tags_controller.rb,
lib/llm_cost_tracker/storage/active_record_store.rb,
app/controllers/llm_cost_tracker/calls_controller.rb,
app/controllers/llm_cost_tracker/models_controller.rb,
app/services/llm_cost_tracker/dashboard/top_models.rb,
app/services/llm_cost_tracker/dashboard/time_series.rb,
app/services/llm_cost_tracker/dashboard/data_quality.rb,
app/controllers/llm_cost_tracker/dashboard_controller.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,
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_latency_ms_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

Defined Under Namespace

Modules: ApplicationHelper, Dashboard, EngineCompatibility, EventMetadata, Generators, Logging, Middleware, Parsers, PeriodGrouping, PriceRegistry, Pricing, Storage, TagAccessors, TagKey, TagQuery, TagsColumn Classes: ApplicationController, Budget, BudgetExceededError, CallsController, Configuration, Cost, DashboardController, DataQualityController, Engine, Error, Event, InvalidFilterError, LlmApiCall, ModelsController, Pagination, ParsedUsage, Railtie, Report, ReportData, ReportFormatter, StorageError, TagsController, TopCall, Tracker, UnknownPricing, UnknownPricingError

Constant Summary collapse

VERSION =
"0.2.0.alpha2"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject



39
40
41
# File 'lib/llm_cost_tracker.rb', line 39

def configuration
  @configuration ||= Configuration.new
end

Class Method Details

.configure {|configuration| ... } ⇒ void

This method returns an undefined value.

Configure the gem once during application boot.

Yield Parameters:



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

def configure
  yield(configuration)
  configuration.normalize_openai_compatible_providers!
  warn_for_configuration!
end

.reset_configuration!Object



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

def reset_configuration!
  @configuration = Configuration.new
end

.track(provider:, model:, input_tokens:, output_tokens:, latency_ms: nil, **metadata) ⇒ LlmCostTracker::Event

Track an LLM request manually for non-Faraday clients.

LlmCostTracker.track(
  provider: :openai,
  model: "gpt-4o",
  input_tokens: 150,
  output_tokens: 50,
  feature: "chat",
  user_id: current_user.id
)

Parameters:

  • provider (String, Symbol)

    Provider name, such as :openai or :anthropic.

  • model (String)

    Provider model identifier.

  • input_tokens (Integer)

    Billed input token count.

  • output_tokens (Integer)

    Billed output token count.

  • latency_ms (Integer, nil) (defaults to: nil)

    Optional request latency in milliseconds.

  • metadata (Hash)

    Attribution tags and provider-specific usage metadata.

Returns:



75
76
77
78
79
80
81
82
83
84
# File 'lib/llm_cost_tracker.rb', line 75

def track(provider:, model:, input_tokens:, output_tokens:, latency_ms: nil, **)
  Tracker.record(
    provider: provider.to_s,
    model: model,
    input_tokens: input_tokens,
    output_tokens: output_tokens,
    latency_ms: latency_ms,
    metadata: 
  )
end