Module: RubyCode::Pricing
- Defined in:
- lib/rubycode/pricing.rb
Overview
Calculates cost estimates based on provider pricing
Constant Summary collapse
- RATES =
Prices per 1M tokens (as of March 2026)
{ gemini: { "gemini-2.5-flash" => { input: 0.0375, output: 0.15, cached: 0.01 }, "gemini-2.5-pro" => { input: 1.25, output: 5.00, cached: 0.31 }, "gemini-3-flash-preview" => { input: 0.0375, output: 0.15, cached: 0.01 } }, openai: { "gpt-4o" => { input: 2.50, output: 10.00, cached: 1.25 }, "gpt-4o-mini" => { input: 0.15, output: 0.60, cached: 0.075 }, "o1" => { input: 15.00, output: 60.00 } }, deepseek: { "deepseek-chat" => { input: 0.14, output: 0.28, cached: 0.014 }, "deepseek-reasoner" => { input: 0.55, output: 2.19, cached: 0.014 } }, openrouter: { "anthropic/claude-sonnet-4.5" => { input: 3.00, output: 15.00, cached: 0.30 }, "anthropic/claude-opus-4.6" => { input: 15.00, output: 75.00, cached: 1.50 } }, ollama: { "default" => { input: 0.0, output: 0.0 } } }.freeze
Class Method Summary collapse
- .calculate_cost(adapter:, model:, tokens:) ⇒ Object
- .calculate_token_cost(token_count, rate_per_million) ⇒ Object
- .find_rates(adapter, model) ⇒ Object
- .format_cost(cost_usd) ⇒ Object
Class Method Details
.calculate_cost(adapter:, model:, tokens:) ⇒ Object
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/rubycode/pricing.rb', line 31 def self.calculate_cost(adapter:, model:, tokens:) rates = find_rates(adapter, model) return 0.0 unless rates input_cost = calculate_token_cost(tokens.input, rates[:input]) output_cost = calculate_token_cost(tokens.output, rates[:output]) cached_cost = calculate_token_cost(tokens.cached, rates[:cached] || 0) input_cost + output_cost - cached_cost # Cached reduces cost end |
.calculate_token_cost(token_count, rate_per_million) ⇒ Object
46 47 48 |
# File 'lib/rubycode/pricing.rb', line 46 def self.calculate_token_cost(token_count, rate_per_million) (token_count / 1_000_000.0) * rate_per_million end |
.find_rates(adapter, model) ⇒ Object
42 43 44 |
# File 'lib/rubycode/pricing.rb', line 42 def self.find_rates(adapter, model) RATES.dig(adapter.to_sym, model) || RATES.dig(adapter.to_sym, "default") end |
.format_cost(cost_usd) ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/rubycode/pricing.rb', line 50 def self.format_cost(cost_usd) if cost_usd < 0.01 cents = (cost_usd * 100).round(4) "$#{cents}¢" else "$#{cost_usd.round(4)}" end end |