Module: RubyLLM::Agents::Audio::SpeechPricing

Extended by:
SpeechPricing
Included in:
SpeechPricing
Defined in:
lib/ruby_llm/agents/audio/speech_pricing.rb

Overview

Dynamic pricing resolution for text-to-speech models.

Uses a three-tier pricing cascade (no hardcoded prices):

  1. Configurable pricing table - user overrides via config.tts_model_pricing

  2. LiteLLM (via shared DataStore) - comprehensive, community-maintained

  3. ElevenLabs API - dynamic multiplier × user-configured base rate

When no pricing is found, returns 0 to signal unknown cost.

All prices are per 1,000 characters.

Examples:

Get cost for a speech operation

SpeechPricing.calculate_cost(provider: :openai, model_id: "tts-1", characters: 5000)
# => 0.075

User-configured pricing

RubyLLM::Agents.configure do |c|
  c.tts_model_pricing = {
    "eleven_v3" => 0.24,
    "tts-1" => 0.015
  }
end

Instance Method Summary collapse

Instance Method Details

#all_pricingObject

Expose all known pricing for debugging/console inspection



78
79
80
81
82
83
84
# File 'lib/ruby_llm/agents/audio/speech_pricing.rb', line 78

def all_pricing
  {
    litellm: litellm_tts_models,
    configured: config.tts_model_pricing || {},
    elevenlabs_api: elevenlabs_api_pricing
  }
end

#calculate_cost(provider:, model_id:, characters:) ⇒ Float

Calculate total cost for a speech operation

Parameters:

  • provider (Symbol)

    :openai or :elevenlabs

  • model_id (String)

    The model identifier

  • characters (Integer)

    Number of characters synthesized

Returns:

  • (Float)

    Total cost in USD



42
43
44
45
# File 'lib/ruby_llm/agents/audio/speech_pricing.rb', line 42

def calculate_cost(provider:, model_id:, characters:)
  price_per_1k = cost_per_1k_characters(provider, model_id)
  ((characters / 1000.0) * price_per_1k).round(6)
end

#cost_per_1k_characters(provider, model_id) ⇒ Float

Get cost per 1,000 characters for a model

Parameters:

  • provider (Symbol)

    Provider identifier

  • model_id (String)

    Model identifier

Returns:

  • (Float)

    Cost per 1K characters in USD (0 if unknown)



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ruby_llm/agents/audio/speech_pricing.rb', line 52

def cost_per_1k_characters(provider, model_id)
  # Tier 1: User config overrides (highest priority)
  if (config_price = from_config(model_id))
    return config_price
  end

  # Tier 2: LiteLLM (via shared adapter/DataStore)
  if (litellm_price = from_litellm(model_id))
    return litellm_price
  end

  # Tier 3: ElevenLabs API multiplier × user-configured base rate
  if provider == :elevenlabs && (api_price = from_elevenlabs_api(model_id))
    return api_price
  end

  # No pricing found — return user-configured default or 0
  config.default_tts_cost || 0
end

#refresh!Object

Force refresh of cached pricing data



73
74
75
# File 'lib/ruby_llm/agents/audio/speech_pricing.rb', line 73

def refresh!
  Pricing::DataStore.refresh!
end