Module: Legion::LLM::Pipeline::Steps::Metering

Extended by:
Legion::Logging::Helper
Included in:
Executor
Defined in:
lib/legion/llm/pipeline/steps/metering.rb

Class Method Summary collapse

Class Method Details

.build_event(**opts) ⇒ Object



14
15
16
# File 'lib/legion/llm/pipeline/steps/metering.rb', line 14

def build_event(**opts)
  identity_fields(opts).merge(token_fields(opts)).merge(timing_and_context(opts))
end

.flush_spoolObject



22
23
24
# File 'lib/legion/llm/pipeline/steps/metering.rb', line 22

def flush_spool
  Legion::LLM::Metering.flush_spool
end

.identity_fields(opts) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/legion/llm/pipeline/steps/metering.rb', line 26

def identity_fields(opts)
  {
    node_id:      opts[:node_id],
    worker_id:    opts[:worker_id],
    agent_id:     opts[:agent_id],
    request_type: opts[:request_type],
    tier:         opts[:tier],
    provider:     opts[:provider],
    model_id:     opts[:model_id]
  }
end

.publish_event(event) ⇒ Object



55
56
57
# File 'lib/legion/llm/pipeline/steps/metering.rb', line 55

def publish_event(event)
  Legion::LLM::Metering.emit(event)
end

.publish_or_spool(event) ⇒ Object



18
19
20
# File 'lib/legion/llm/pipeline/steps/metering.rb', line 18

def publish_or_spool(event)
  publish_event(event)
end

.timing_and_context(opts) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/legion/llm/pipeline/steps/metering.rb', line 46

def timing_and_context(opts)
  {
    latency_ms:     opts.fetch(:latency_ms, 0),
    wall_clock_ms:  opts.fetch(:wall_clock_ms, 0),
    routing_reason: opts[:routing_reason],
    recorded_at:    Time.now.utc.iso8601
  }
end

.token_fields(opts) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/legion/llm/pipeline/steps/metering.rb', line 38

def token_fields(opts)
  input    = opts.fetch(:input_tokens, 0)
  output   = opts.fetch(:output_tokens, 0)
  thinking = opts.fetch(:thinking_tokens, 0)
  { input_tokens: input, output_tokens: output, thinking_tokens: thinking,
    total_tokens: input + output + thinking }
end