Class: LlmCostTracker::Configuration
- Inherits:
-
Object
- Object
- LlmCostTracker::Configuration
- Defined in:
- lib/llm_cost_tracker/configuration.rb
Constant Summary collapse
- OPENAI_COMPATIBLE_PROVIDERS =
{ "openrouter.ai" => "openrouter", "api.deepseek.com" => "deepseek", "api.groq.com" => "groq" }.freeze
- BUDGET_EXCEEDED_BEHAVIORS =
%i[notify raise block_requests].freeze
- UNKNOWN_PRICING_BEHAVIORS =
%i[ignore warn raise].freeze
- INGESTION_MODES =
%i[inline async].freeze
- SCALAR_ATTRIBUTES =
%i[enabled default_tags on_budget_exceeded monthly_budget daily_budget per_call_budget log_level prices_file max_tag_count max_tag_value_bytesize ingestion_pool_size].freeze
- ENUM_ATTRIBUTES =
{ budget_exceeded_behavior: [BUDGET_EXCEEDED_BEHAVIORS, :notify], unknown_pricing_behavior: [UNKNOWN_PRICING_BEHAVIORS, :warn], ingestion: [INGESTION_MODES, :inline] }.freeze
- DEFAULT_REDACTED_TAG_KEYS =
%w[api_key access_token authorization credential password refresh_token secret].freeze
Instance Method Summary collapse
- #auto_enable_stream_usage=(value) ⇒ Object
- #cache_rollups=(value) ⇒ Object
- #finalize! ⇒ Object
- #finalized? ⇒ Boolean
-
#initialize ⇒ Configuration
constructor
A new instance of Configuration.
- #instrument(*names) ⇒ Object
- #instrumented?(name) ⇒ Boolean
- #normalized_redacted_tag_keys ⇒ Object
- #openai_compatible_providers=(providers) ⇒ Object
- #pricing_overrides=(value) ⇒ Object
- #reconciliation_enabled=(value) ⇒ Object
- #reconciliation_importers=(importers) ⇒ Object
- #redacted_tag_keys=(value) ⇒ Object
- #register_reconciliation_importer(source, &block) ⇒ Object
- #report_tag_breakdowns=(value) ⇒ Object
Constructor Details
#initialize ⇒ Configuration
Returns a new instance of Configuration.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/llm_cost_tracker/configuration.rb', line 44 def initialize @enabled = true @default_tags = {} @on_budget_exceeded = nil @monthly_budget = nil @daily_budget = nil @per_call_budget = nil self.budget_exceeded_behavior = :notify self.unknown_pricing_behavior = :warn @log_level = :info @prices_file = nil @max_tag_count = 50 @max_tag_value_bytesize = 1024 @ingestion_pool_size = nil self.pricing_overrides = {} @instrumented_integrations = Set.new @report_tag_breakdowns = [] @redacted_tag_keys = DEFAULT_REDACTED_TAG_KEYS.dup self.openai_compatible_providers = OPENAI_COMPATIBLE_PROVIDERS @reconciliation_importers = {} @reconciliation_enabled = false @auto_enable_stream_usage = true self.ingestion = :inline @cache_rollups = false @finalized = false end |
Instance Method Details
#auto_enable_stream_usage=(value) ⇒ Object
81 82 83 84 |
# File 'lib/llm_cost_tracker/configuration.rb', line 81 def auto_enable_stream_usage=(value) ensure_mutable! @auto_enable_stream_usage = value end |
#cache_rollups=(value) ⇒ Object
71 72 73 74 |
# File 'lib/llm_cost_tracker/configuration.rb', line 71 def cache_rollups=(value) ensure_mutable! @cache_rollups = value end |
#finalize! ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/llm_cost_tracker/configuration.rb', line 153 def finalize! @default_tags = deep_freeze(@default_tags || {}) @pricing_overrides = deep_freeze(@pricing_overrides || {}) @instrumented_integrations = deep_freeze(@instrumented_integrations || Set.new) @report_tag_breakdowns = deep_freeze(Array(@report_tag_breakdowns)) @redacted_tag_keys = deep_freeze(Array(@redacted_tag_keys)) @openai_compatible_providers = deep_freeze( normalize_openai_compatible_providers(@openai_compatible_providers) ) @finalized = true end |
#finalized? ⇒ Boolean
170 171 172 |
# File 'lib/llm_cost_tracker/configuration.rb', line 170 def finalized? @finalized end |
#instrument(*names) ⇒ Object
130 131 132 133 |
# File 'lib/llm_cost_tracker/configuration.rb', line 130 def instrument(*names) ensure_mutable! @instrumented_integrations.merge(normalize_instrumentation_names(names)) end |
#instrumented?(name) ⇒ Boolean
135 136 137 |
# File 'lib/llm_cost_tracker/configuration.rb', line 135 def instrumented?(name) @instrumented_integrations.include?(name) end |
#normalized_redacted_tag_keys ⇒ Object
165 166 167 168 |
# File 'lib/llm_cost_tracker/configuration.rb', line 165 def normalized_redacted_tag_keys @normalized_redacted_tag_keys ||= Array(@redacted_tag_keys).map { |key| Tags::Sanitizer.normalized_key(key) }.freeze end |
#openai_compatible_providers=(providers) ⇒ Object
108 109 110 111 |
# File 'lib/llm_cost_tracker/configuration.rb', line 108 def openai_compatible_providers=(providers) ensure_mutable! @openai_compatible_providers = normalize_openai_compatible_providers(providers) end |
#pricing_overrides=(value) ⇒ Object
113 114 115 116 117 118 |
# File 'lib/llm_cost_tracker/configuration.rb', line 113 def pricing_overrides=(value) ensure_mutable! @pricing_overrides = Pricing::Registry.normalize_price_table(value || {}) rescue ArgumentError => e raise Error, "invalid pricing_overrides: #{e.}" end |
#reconciliation_enabled=(value) ⇒ Object
76 77 78 79 |
# File 'lib/llm_cost_tracker/configuration.rb', line 76 def reconciliation_enabled=(value) ensure_mutable! @reconciliation_enabled = value end |
#reconciliation_importers=(importers) ⇒ Object
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/llm_cost_tracker/configuration.rb', line 86 def reconciliation_importers=(importers) ensure_mutable! raise Error, RECONCILIATION_DISABLED_MESSAGE unless @reconciliation_enabled @reconciliation_importers = (importers || {}).to_h do |source, importer| raise Error, "reconciliation_importers[#{source}] must respond to call" unless importer.respond_to?(:call) [source.to_sym, importer] end end |
#redacted_tag_keys=(value) ⇒ Object
125 126 127 128 |
# File 'lib/llm_cost_tracker/configuration.rb', line 125 def redacted_tag_keys=(value) ensure_mutable! @redacted_tag_keys = Array(value).map(&:to_s) end |
#register_reconciliation_importer(source, &block) ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/llm_cost_tracker/configuration.rb', line 97 def register_reconciliation_importer(source, &block) ensure_mutable! raise Error, RECONCILIATION_DISABLED_MESSAGE unless @reconciliation_enabled raise Error, "register_reconciliation_importer requires a block" unless block @reconciliation_importers[source.to_sym] = block end |