Class: Legion::CLI::Chat::Tools::ArbitrageStatus

Inherits:
Tools::Base
  • Object
show all
Defined in:
lib/legion/cli/chat/tools/arbitrage_status.rb

Constant Summary collapse

TIERS =
%i[basic moderate reasoning].freeze

Class Method Summary collapse

Methods inherited from Tools::Base

deferred, deferred?, description, error_response, extension, handle_exception, input_schema, log, mcp_category, mcp_tier, runner, sticky, tags, text_response, tool_name, trigger_words

Class Method Details

.arbitrage_available?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/legion/cli/chat/tools/arbitrage_status.rb', line 30

def self.arbitrage_available?
  defined?(Legion::LLM::Arbitrage)
end

.call(capability: nil) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/legion/cli/chat/tools/arbitrage_status.rb', line 20

def self.call(capability: nil)
  return 'LLM arbitrage module not available.' unless arbitrage_available?

  if capability
    format_tier(capability.to_sym)
  else
    format_overview
  end
end

.format_overviewObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/legion/cli/chat/tools/arbitrage_status.rb', line 34

def self.format_overview
  arb = Legion::LLM::Arbitrage
  lines = ["LLM Cost Arbitrage\n"]
  lines << format('  Enabled: %<v>s', v: arb.enabled? ? 'YES' : 'no')
  lines << ''
  lines << '  Cost Table (per 1M tokens):'
  lines << '  Model                                       Input   Output'
  lines << "  #{'' * 58}"

  arb.cost_table.sort_by { |_, v| v[:input] }.each do |model, costs|
    lines << format('  %<m>-40s %<i>7.2f %<o>8.2f',
                    m: model, i: costs[:input], o: costs[:output])
  end

  if arb.enabled?
    lines << ''
    lines << '  Cheapest per tier:'
    TIERS.each do |tier|
      pick = arb.cheapest_for(capability: tier)
      lines << format('    %<tier>-12s -> %<pick>s', tier: tier, pick: pick || 'none')
    end
  end

  lines.join("\n")
end

.format_tier(tier) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/legion/cli/chat/tools/arbitrage_status.rb', line 60

def self.format_tier(tier)
  arb = Legion::LLM::Arbitrage
  return format('Invalid tier: %<t>s. Use: %<valid>s', t: tier, valid: TIERS.join(', ')) unless TIERS.include?(tier)

  pick = arb.cheapest_for(capability: tier)
  cost = pick ? arb.estimated_cost(model: pick) : nil

  lines = [format("Arbitrage for tier: %<t>s\n", t: tier)]
  if pick
    lines << format('  Selected model: %<m>s', m: pick)
    lines << format('  Estimated cost: $%<c>.6f (1K in + 500 out)', c: cost) if cost
  else
    lines << '  No eligible model found (arbitrage may be disabled)'
  end
  lines.join("\n")
end