Class: Legion::TTY::Components::TokenTracker

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/tty/components/token_tracker.rb

Constant Summary collapse

MODEL_PRICING =

Rates per 1k tokens (input/output) — current as of 2026-03

{
  'claude-opus-4-6' => { input: 0.015, output: 0.075 },
  'claude-sonnet-4-6' => { input: 0.003, output: 0.015 },
  'claude-haiku-4-5' => { input: 0.0008, output: 0.004 },
  'gpt-4o' => { input: 0.0025, output: 0.010 },
  'gpt-4o-mini' => { input: 0.00015, output: 0.0006 },
  'gpt-4.1' => { input: 0.002, output: 0.008 },
  'gemini-2.0-flash' => { input: 0.0001, output: 0.0004 },
  'gemini-2.5-pro' => { input: 0.00125, output: 0.01 },
  'local' => { input: 0.0, output: 0.0 }
}.freeze
PROVIDER_PRICING =
{
  'anthropic' => { input: 0.003, output: 0.015 },
  'claude' => { input: 0.003, output: 0.015 },
  'openai' => { input: 0.0025, output: 0.010 },
  'gemini' => { input: 0.0001, output: 0.0004 },
  'bedrock' => { input: 0.003, output: 0.015 },
  'azure' => { input: 0.0025, output: 0.010 },
  'ollama' => { input: 0.0, output: 0.0 },
  'local' => { input: 0.0, output: 0.0 }
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(provider: 'claude', model: nil) ⇒ TokenTracker

Returns a new instance of TokenTracker.



33
34
35
36
37
38
39
# File 'lib/legion/tty/components/token_tracker.rb', line 33

def initialize(provider: 'claude', model: nil)
  @provider = provider
  @model = model
  @total_input_tokens = 0
  @total_output_tokens = 0
  @total_cost = 0.0
end

Instance Attribute Details

#total_costObject (readonly)

Returns the value of attribute total_cost.



31
32
33
# File 'lib/legion/tty/components/token_tracker.rb', line 31

def total_cost
  @total_cost
end

#total_input_tokensObject (readonly)

Returns the value of attribute total_input_tokens.



31
32
33
# File 'lib/legion/tty/components/token_tracker.rb', line 31

def total_input_tokens
  @total_input_tokens
end

#total_output_tokensObject (readonly)

Returns the value of attribute total_output_tokens.



31
32
33
# File 'lib/legion/tty/components/token_tracker.rb', line 31

def total_output_tokens
  @total_output_tokens
end

Instance Method Details

#summaryObject



54
55
56
57
58
59
# File 'lib/legion/tty/components/token_tracker.rb', line 54

def summary
  input = format_number(@total_input_tokens)
  output = format_number(@total_output_tokens)
  cost = format('%.4f', @total_cost)
  "Tokens: #{input} in / #{output} out | Cost: $#{cost}"
end

#track(input_tokens:, output_tokens:, model: nil) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/legion/tty/components/token_tracker.rb', line 45

def track(input_tokens:, output_tokens:, model: nil)
  @model = model if model
  @total_input_tokens += input_tokens.to_i
  @total_output_tokens += output_tokens.to_i
  rates = rates_for(@model, @provider)
  @total_cost += (input_tokens.to_i * rates[:input] / 1000.0) +
                 (output_tokens.to_i * rates[:output] / 1000.0)
end

#update_model(model) ⇒ Object



41
42
43
# File 'lib/legion/tty/components/token_tracker.rb', line 41

def update_model(model)
  @model = model
end