Module: RubyLLM::Agents::LLMTenant
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/ruby_llm/agents/core/llm_tenant.rb
Overview
DSL for declaring Rails models as LLM tenants
Provides automatic budget management and usage tracking when included in ActiveRecord models. Models using this concern can be passed as the ‘tenant:` parameter to agents.
Instance Method Summary collapse
-
#llm_api_keys ⇒ Hash
Returns API keys resolved from the DSL configuration.
-
#llm_budget_status ⇒ Hash
Returns the budget status from BudgetTracker.
-
#llm_check_budget! ⇒ void
Raises an error if over budget.
-
#llm_configure {|tenant| ... } ⇒ Tenant
(also: #llm_configure_budget)
Configure tenant with a block.
-
#llm_cost(period: nil) ⇒ BigDecimal
Returns cost for a given period.
-
#llm_cost_this_month ⇒ BigDecimal
Returns cost for this month.
-
#llm_cost_today ⇒ BigDecimal
Returns cost for today.
-
#llm_execution_count(period: nil) ⇒ Integer
Returns execution count for a given period.
-
#llm_executions_this_month ⇒ Integer
Returns executions for this month.
-
#llm_executions_today ⇒ Integer
Returns executions for today.
-
#llm_remaining_budget(type: :daily_cost) ⇒ Numeric?
Returns remaining budget for a given limit type.
-
#llm_tenant ⇒ Tenant
(also: #llm_budget)
Returns or builds the associated Tenant record.
-
#llm_tenant_id ⇒ String
Returns the tenant_id string for this model.
-
#llm_tokens(period: nil) ⇒ Integer
Returns token count for a given period.
-
#llm_tokens_this_month ⇒ Integer
Returns tokens for this month.
-
#llm_tokens_today ⇒ Integer
Returns tokens for today.
-
#llm_usage_summary(period: :this_month) ⇒ Hash
Returns a usage summary for a given period.
-
#llm_within_budget?(type: :daily_cost) ⇒ Boolean
Checks if within budget for a given limit type.
Instance Method Details
#llm_api_keys ⇒ Hash
Returns API keys resolved from the DSL configuration
Maps provider names (e.g., :openai, :anthropic) to their resolved values by calling the configured method/column on this model instance.
155 156 157 158 159 160 161 162 163 |
# File 'lib/ruby_llm/agents/core/llm_tenant.rb', line 155 def llm_api_keys api_keys_config = self.class.[:api_keys] return {} if api_keys_config.blank? api_keys_config.transform_values do |method_name| value = send(method_name) value.presence end.compact end |
#llm_budget_status ⇒ Hash
Returns the budget status from BudgetTracker
284 285 286 |
# File 'lib/ruby_llm/agents/core/llm_tenant.rb', line 284 def llm_budget_status llm_tenant.budget_status end |
#llm_check_budget! ⇒ void
This method returns an undefined value.
Raises an error if over budget
308 309 310 |
# File 'lib/ruby_llm/agents/core/llm_tenant.rb', line 308 def llm_check_budget! llm_tenant.check_budget!(self.class.name) end |
#llm_configure {|tenant| ... } ⇒ Tenant Also known as: llm_configure_budget
Configure tenant with a block
180 181 182 183 184 185 |
# File 'lib/ruby_llm/agents/core/llm_tenant.rb', line 180 def llm_configure(&block) tenant = llm_tenant yield(tenant) if block_given? tenant.save! tenant end |
#llm_cost(period: nil) ⇒ BigDecimal
Returns cost for a given period
198 199 200 201 202 |
# File 'lib/ruby_llm/agents/core/llm_tenant.rb', line 198 def llm_cost(period: nil) scope = llm_executions scope = apply_llm_period_scope(scope, period) if period scope.sum(:total_cost) || 0 end |
#llm_cost_this_month ⇒ BigDecimal
Returns cost for this month
214 215 216 |
# File 'lib/ruby_llm/agents/core/llm_tenant.rb', line 214 def llm_cost_this_month llm_cost(period: :this_month) end |
#llm_cost_today ⇒ BigDecimal
Returns cost for today
207 208 209 |
# File 'lib/ruby_llm/agents/core/llm_tenant.rb', line 207 def llm_cost_today llm_cost(period: :today) end |
#llm_execution_count(period: nil) ⇒ Integer
Returns execution count for a given period
246 247 248 249 250 |
# File 'lib/ruby_llm/agents/core/llm_tenant.rb', line 246 def llm_execution_count(period: nil) scope = llm_executions scope = apply_llm_period_scope(scope, period) if period scope.count end |
#llm_executions_this_month ⇒ Integer
Returns executions for this month
262 263 264 |
# File 'lib/ruby_llm/agents/core/llm_tenant.rb', line 262 def llm_executions_this_month llm_execution_count(period: :this_month) end |
#llm_executions_today ⇒ Integer
Returns executions for today
255 256 257 |
# File 'lib/ruby_llm/agents/core/llm_tenant.rb', line 255 def llm_executions_today llm_execution_count(period: :today) end |
#llm_remaining_budget(type: :daily_cost) ⇒ Numeric?
Returns remaining budget for a given limit type
300 301 302 |
# File 'lib/ruby_llm/agents/core/llm_tenant.rb', line 300 def llm_remaining_budget(type: :daily_cost) llm_tenant.remaining_budget(type: type) end |
#llm_tenant ⇒ Tenant Also known as: llm_budget
Returns or builds the associated Tenant record
168 169 170 |
# File 'lib/ruby_llm/agents/core/llm_tenant.rb', line 168 def llm_tenant llm_tenant_record || build_llm_tenant_record(tenant_id: llm_tenant_id) end |
#llm_tenant_id ⇒ String
Returns the tenant_id string for this model
141 142 143 144 |
# File 'lib/ruby_llm/agents/core/llm_tenant.rb', line 141 def llm_tenant_id id_method = self.class.[:id] || :id send(id_method).to_s end |
#llm_tokens(period: nil) ⇒ Integer
Returns token count for a given period
222 223 224 225 226 |
# File 'lib/ruby_llm/agents/core/llm_tenant.rb', line 222 def llm_tokens(period: nil) scope = llm_executions scope = apply_llm_period_scope(scope, period) if period scope.sum(:total_tokens) || 0 end |
#llm_tokens_this_month ⇒ Integer
Returns tokens for this month
238 239 240 |
# File 'lib/ruby_llm/agents/core/llm_tenant.rb', line 238 def llm_tokens_this_month llm_tokens(period: :this_month) end |
#llm_tokens_today ⇒ Integer
Returns tokens for today
231 232 233 |
# File 'lib/ruby_llm/agents/core/llm_tenant.rb', line 231 def llm_tokens_today llm_tokens(period: :today) end |
#llm_usage_summary(period: :this_month) ⇒ Hash
Returns a usage summary for a given period
270 271 272 273 274 275 276 277 |
# File 'lib/ruby_llm/agents/core/llm_tenant.rb', line 270 def llm_usage_summary(period: :this_month) { cost: llm_cost(period: period), tokens: llm_tokens(period: period), executions: llm_execution_count(period: period), period: period } end |
#llm_within_budget?(type: :daily_cost) ⇒ Boolean
Checks if within budget for a given limit type
292 293 294 |
# File 'lib/ruby_llm/agents/core/llm_tenant.rb', line 292 def llm_within_budget?(type: :daily_cost) llm_tenant.within_budget?(type: type) end |