Module: LlmCostTracker::Integrations::Anthropic

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

Defined Under Namespace

Modules: MessagesPatch

Class Method Summary collapse

Methods included from Base

active?, constant, elapsed_ms, enforce_budget!, install, minimum_version, patch_target, patch_targets, record_safely, request_params, status, version_constant

Class Method Details

.finish_stream(collector, errored:) ⇒ Object



87
88
89
# File 'lib/llm_cost_tracker/integrations/anthropic.rb', line 87

def finish_stream(collector, errored:)
  record_safely { collector.finish!(errored: errored) }
end

.hidden_output_tokens(usage) ⇒ Object



62
63
64
65
66
67
# File 'lib/llm_cost_tracker/integrations/anthropic.rb', line 62

def hidden_output_tokens(usage)
  ObjectReader.integer(
    ObjectReader.first(usage, :thinking_tokens, :thinking_output_tokens) ||
    ObjectReader.nested(usage, :output_tokens_details, :reasoning_tokens)
  )
end

.integration_nameObject



12
# File 'lib/llm_cost_tracker/integrations/anthropic.rb', line 12

def integration_name = :anthropic

.minimum_versionObject



14
# File 'lib/llm_cost_tracker/integrations/anthropic.rb', line 14

def minimum_version = "1.36.0"

.patch_targetsObject



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/llm_cost_tracker/integrations/anthropic.rb', line 18

def patch_targets
  [
    patch_target("Anthropic::Resources::Messages", with: MessagesPatch, methods: %i[create stream stream_raw]),
    patch_target(
      "Anthropic::Resources::Beta::Messages",
      with: MessagesPatch,
      methods: %i[create stream stream_raw],
      optional: true
    )
  ]
end

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



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/llm_cost_tracker/integrations/anthropic.rb', line 30

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

  record_safely do
    usage = ObjectReader.first(message, :usage)
    next unless usage

    input_tokens = ObjectReader.first(usage, :input_tokens)
    output_tokens = ObjectReader.first(usage, :output_tokens)
    next if input_tokens.nil? && output_tokens.nil?

    LlmCostTracker::Tracker.record(
      provider: "anthropic",
      model: ObjectReader.first(message, :model) || request[:model],
      input_tokens: ObjectReader.integer(input_tokens),
      output_tokens: ObjectReader.integer(output_tokens),
      latency_ms: latency_ms,
      usage_source: :sdk_response,
      provider_response_id: ObjectReader.first(message, :id),
      metadata: (usage)
    )
  end
end

.stream_collector(request) ⇒ Object



80
81
82
83
84
85
# File 'lib/llm_cost_tracker/integrations/anthropic.rb', line 80

def stream_collector(request)
  LlmCostTracker::StreamCollector.new(
    provider: "anthropic",
    model: request[:model] || request["model"]
  )
end

.track_stream(stream, collector:) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/llm_cost_tracker/integrations/anthropic.rb', line 69

def track_stream(stream, collector:)
  return stream unless active?

  StreamTracker.wrap(
    stream,
    collector: collector,
    active: -> { active? },
    finish: ->(errored:) { finish_stream(collector, errored: errored) }
  )
end

.usage_metadata(usage) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/llm_cost_tracker/integrations/anthropic.rb', line 54

def (usage)
  {
    cache_read_input_tokens: ObjectReader.integer(ObjectReader.first(usage, :cache_read_input_tokens)),
    cache_write_input_tokens: ObjectReader.integer(ObjectReader.first(usage, :cache_creation_input_tokens)),
    hidden_output_tokens: hidden_output_tokens(usage)
  }
end

.version_constantObject



16
# File 'lib/llm_cost_tracker/integrations/anthropic.rb', line 16

def version_constant = "Anthropic::VERSION"