Class: LlmCostTracker::Capture::StreamCollector
- Inherits:
-
Object
- Object
- LlmCostTracker::Capture::StreamCollector
- Defined in:
- lib/llm_cost_tracker/capture/stream_collector.rb
Instance Attribute Summary collapse
-
#provider ⇒ Object
readonly
Returns the value of attribute provider.
Instance Method Summary collapse
- #event(data, type: nil) ⇒ Object
- #finish!(errored: false) ⇒ Object
-
#initialize(provider:, model:, latency_ms: nil, provider_response_id: nil, provider_project_id: nil, provider_api_key_id: nil, provider_workspace_id: nil, pricing_mode: nil, metadata: {}, context_tags: nil, request: nil) ⇒ StreamCollector
constructor
A new instance of StreamCollector.
- #model=(value) ⇒ Object
- #provider_response_id=(value) ⇒ Object
- #usage(input_tokens:, output_tokens:, **extra) ⇒ Object
Constructor Details
#initialize(provider:, model:, latency_ms: nil, provider_response_id: nil, provider_project_id: nil, provider_api_key_id: nil, provider_workspace_id: nil, pricing_mode: nil, metadata: {}, context_tags: nil, request: nil) ⇒ StreamCollector
Returns a new instance of StreamCollector.
15 16 17 18 19 20 21 22 23 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/capture/stream_collector.rb', line 15 def initialize(provider:, model:, latency_ms: nil, provider_response_id: nil, provider_project_id: nil, provider_api_key_id: nil, provider_workspace_id: nil, pricing_mode: nil, metadata: {}, context_tags: nil, request: nil) @provider = provider.to_s @model = model @latency_ms = latency_ms @provider_response_id = provider_response_id @provider_project_id = provider_project_id @provider_api_key_id = provider_api_key_id @provider_workspace_id = provider_workspace_id @pricing_mode = pricing_mode @metadata = ( || {}).deep_dup @context_tags = ( || LlmCostTracker::Tags::Context.).deep_dup @request = request @events = [] @captured_bytes = 0 @overflowed = false @explicit_usage = nil @started_at = LlmCostTracker::Timing.now_monotonic @finished = false @recording = false @mutex = Mutex.new end |
Instance Attribute Details
#provider ⇒ Object (readonly)
Returns the value of attribute provider.
13 14 15 |
# File 'lib/llm_cost_tracker/capture/stream_collector.rb', line 13 def provider @provider end |
Instance Method Details
#event(data, type: nil) ⇒ Object
61 62 63 64 65 66 |
# File 'lib/llm_cost_tracker/capture/stream_collector.rb', line 61 def event(data, type: nil) @mutex.synchronize do ensure_open! capture_event(data, type: type) unless data.nil? end end |
#finish!(errored: false) ⇒ Object
89 90 91 92 93 94 |
# File 'lib/llm_cost_tracker/capture/stream_collector.rb', line 89 def finish!(errored: false) snapshot = claim_recording_slot return if snapshot.nil? record_snapshot(snapshot, errored: errored) end |
#model=(value) ⇒ Object
47 48 49 50 51 52 |
# File 'lib/llm_cost_tracker/capture/stream_collector.rb', line 47 def model=(value) @mutex.synchronize do ensure_open! @model = value end end |
#provider_response_id=(value) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/llm_cost_tracker/capture/stream_collector.rb', line 54 def provider_response_id=(value) @mutex.synchronize do ensure_open! @provider_response_id = value end end |
#usage(input_tokens:, output_tokens:, **extra) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/llm_cost_tracker/capture/stream_collector.rb', line 68 def usage(input_tokens:, output_tokens:, **extra) if extra.key?(:batch) raise ArgumentError, "`batch:` is no longer accepted by stream.usage; " \ "pass `pricing_mode: :batch` to track_stream" end @mutex.synchronize do ensure_open! @provider_response_id = extra.delete(:provider_response_id) || @provider_response_id @provider_project_id = extra.delete(:provider_project_id) || @provider_project_id @provider_api_key_id = extra.delete(:provider_api_key_id) || @provider_api_key_id @provider_workspace_id = extra.delete(:provider_workspace_id) || @provider_workspace_id @explicit_usage = Usage::TokenUsage.build( **extra.slice(*Usage::TokenUsage.members), input_tokens: input_tokens, output_tokens: output_tokens ) end end |