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/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/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/value_object.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/unknown_pricing.rb,
lib/llm_cost_tracker/parsers/registry.rb,
lib/llm_cost_tracker/report_formatter.rb,
lib/llm_cost_tracker/storage/backends.rb,
lib/llm_cost_tracker/parsers/anthropic.rb,
lib/llm_cost_tracker/middleware/faraday.rb,
lib/llm_cost_tracker/storage/log_backend.rb,
lib/llm_cost_tracker/parsers/openai_usage.rb,
lib/llm_cost_tracker/storage/custom_backend.rb,
lib/llm_cost_tracker/parsers/openai_compatible.rb,
lib/llm_cost_tracker/storage/active_record_store.rb,
lib/llm_cost_tracker/storage/active_record_backend.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: EventMetadata, Generators, Logging, Middleware, Parsers, PriceRegistry, Pricing, Storage, TagAccessors, TagQuery, TagsColumn, ValueObject Classes: Budget, BudgetExceededError, Configuration, Error, LlmApiCall, ParsedUsage, Railtie, Report, ReportFormatter, StorageError, Tracker, UnknownPricing, UnknownPricingError

Constant Summary collapse

CONFIGURATION_MUTEX =
Mutex.new
Cost =
ValueObject.define(
  :input_cost,
  :cached_input_cost,
  :cache_read_input_cost,
  :cache_creation_input_cost,
  :output_cost,
  :total_cost,
  :currency
)
Event =
ValueObject.define(
  :provider,
  :model,
  :input_tokens,
  :output_tokens,
  :total_tokens,
  :cost,
  :tags,
  :latency_ms,
  :tracked_at
) do
  def to_h
    super.merge(
      cost: cost&.to_h,
      tags: tags ? tags.to_h : {}
    )
  end
end
VERSION =
"0.1.4"
TopCall =
ValueObject.define(:provider, :model, :total_cost)
ReportData =
ValueObject.define(
  :days,
  :from_time,
  :to_time,
  :total_cost,
  :requests_count,
  :average_latency_ms,
  :unknown_pricing_count,
  :cost_by_provider,
  :cost_by_model,
  :cost_by_tags,
  :top_calls
)

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject



42
43
44
# File 'lib/llm_cost_tracker.rb', line 42

def configuration
  @configuration || CONFIGURATION_MUTEX.synchronize { @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:



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

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

.reset_configuration!Object



56
57
58
# File 'lib/llm_cost_tracker.rb', line 56

def reset_configuration!
  CONFIGURATION_MUTEX.synchronize { @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:



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

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