Module: LlmCostTracker::Integrations::Openai::PatchBuilder

Defined in:
lib/llm_cost_tracker/integrations/openai.rb

Class Method Summary collapse

Class Method Details

.build(record_method:, methods:) ⇒ Object



424
425
426
427
428
# File 'lib/llm_cost_tracker/integrations/openai.rb', line 424

def build(record_method:, methods:)
  Module.new.tap do |mod|
    methods.each { |method_name| define_wrapped_method(mod, method_name, record_method) }
  end
end

.define_wrapped_method(mod, method_name, record_method) ⇒ Object



430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
# File 'lib/llm_cost_tracker/integrations/openai.rb', line 430

def define_wrapped_method(mod, method_name, record_method)
  mod.define_method(method_name) do |*args, **kwargs, &block|
    integration = LlmCostTracker::Integrations::Openai
    request = integration.request_params(args, kwargs)
    integration.enforce_budget!(request: request)
    started_at = LlmCostTracker::Timing.now_monotonic
    response = super(*integration.normalize_sdk_args(args, kwargs), &block)
    integration.public_send(
      record_method, response,
      request: request,
      latency_ms: LlmCostTracker::Timing.elapsed_ms(started_at),
      host: integration.client_host_for(self)
    )
    response
  end
end