Module: LlmCostTracker::Integrations::Anthropic

Extended by:
Base
Defined in:
lib/llm_cost_tracker/integrations/anthropic.rb

Defined Under Namespace

Modules: BatchesPatch, MessagesPatch Classes: BatchResultsCapture

Class Method Summary collapse

Methods included from Base

active?, enforce_budget!, gem_version, install, integration_name, minimum_version, patch_target, patch_targets, provider, record_safely, request_params, status, stream_collector, stream_pricing_mode, track_stream, wrap_blocking, wrap_stream

Class Method Details

.patch_targetsObject



15
16
17
18
19
20
21
22
# File 'lib/llm_cost_tracker/integrations/anthropic.rb', line 15

def patch_targets
  [
    patch_target("Anthropic::Resources::Messages", with: MessagesPatch),
    patch_target("Anthropic::Resources::Beta::Messages", with: MessagesPatch, optional: true),
    patch_target("Anthropic::Resources::Messages::Batches", with: BatchesPatch, optional: true),
    patch_target("Anthropic::Resources::Beta::Messages::Batches", with: BatchesPatch, optional: true)
  ]
end

.record_batch_result(response) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/llm_cost_tracker/integrations/anthropic.rb', line 47

def record_batch_result(response)
  return unless active?
  return unless response.respond_to?(:result) && response.result

  result = response.result
  return unless result.respond_to?(:type) && result.type.to_s == "succeeded"

  message = result.respond_to?(:message) ? result.message : nil
  return unless message
  return if LlmCostTracker::Call.already_recorded?(provider: "anthropic", provider_response_id: message.id)

  record_safely do
    usage = message.usage
    next unless usage
    next if usage.input_tokens.nil? && usage.output_tokens.nil?

    usage_hash = usage.deep_to_h
    LlmCostTracker::Tracker.record(
      event: Providers::Anthropic::ResponseParser.event_from_usage(
        usage: usage_hash,
        model: message.model,
        provider_response_id: message.id,
        usage_source: Usage::Source::SDK_BATCH_RESULT,
        pricing_mode: "batch"
      )
    )
  end
end

.record_message(message, request:, latency_ms:) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/llm_cost_tracker/integrations/anthropic.rb', line 24

def record_message(message, request:, latency_ms:)
  return unless active?

  record_safely do
    usage = message.usage
    next unless usage
    next if usage.input_tokens.nil? && usage.output_tokens.nil?

    usage_hash = usage.deep_to_h

    LlmCostTracker::Tracker.record(
      event: Providers::Anthropic::ResponseParser.event_from_usage(
        usage: usage_hash,
        model: message.model || request[:model],
        provider_response_id: message.id,
        usage_source: Usage::Source::SDK_RESPONSE,
        request: request
      ),
      latency_ms: latency_ms
    )
  end
end

.stream_pricing_mode(request) ⇒ Object



76
77
78
# File 'lib/llm_cost_tracker/integrations/anthropic.rb', line 76

def stream_pricing_mode(request)
  Providers::Anthropic::UsageExtractor.pricing_mode(request: request || {}, usage: nil)
end