Class: Mistri::Budget
- Inherits:
-
Object
- Object
- Mistri::Budget
- Defined in:
- lib/mistri/budget.rb
Overview
Optional per-run ceilings: turns, tokens, dollars, wall-clock seconds. Nothing is enforced unless the host sets it; an empty budget never stops a run. Pure limits with no clock of its own, so one Budget shared across agents or runs behaves identically for each: the loop measures and asks between turns, and a run always finishes the turn it is in.
Instance Method Summary collapse
- #cost? ⇒ Boolean
-
#exceeded(turns:, usage:, elapsed: 0) ⇒ Object
The reason the run should stop, or nil to continue.
-
#initialize(turns: nil, tokens: nil, cost_usd: nil, wall_clock: nil) ⇒ Budget
constructor
A new instance of Budget.
- #none? ⇒ Boolean
- #validate_provider!(provider) ⇒ Object
- #validate_usage!(usage) ⇒ Object
Constructor Details
#initialize(turns: nil, tokens: nil, cost_usd: nil, wall_clock: nil) ⇒ Budget
Returns a new instance of Budget.
10 11 12 13 14 15 |
# File 'lib/mistri/budget.rb', line 10 def initialize(turns: nil, tokens: nil, cost_usd: nil, wall_clock: nil) @turns = turns @tokens = tokens @cost_usd = cost_usd @wall_clock = wall_clock end |
Instance Method Details
#cost? ⇒ Boolean
19 |
# File 'lib/mistri/budget.rb', line 19 def cost? = !@cost_usd.nil? |
#exceeded(turns:, usage:, elapsed: 0) ⇒ Object
The reason the run should stop, or nil to continue.
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/mistri/budget.rb', line 41 def exceeded(turns:, usage:, elapsed: 0) return :turns if @turns && turns >= @turns return :tokens if @tokens && usage.total_tokens >= @tokens if @cost_usd validate_usage!(usage) return :cost if usage.cost.total >= @cost_usd end return :wall_clock if @wall_clock && elapsed >= @wall_clock nil end |
#none? ⇒ Boolean
17 |
# File 'lib/mistri/budget.rb', line 17 def none? = [@turns, @tokens, @cost_usd, @wall_clock].all?(&:nil?) |
#validate_provider!(provider) ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/mistri/budget.rb', line 21 def validate_provider!(provider) return unless cost? return if provider.respond_to?(:prices_usage?) && provider.prices_usage? raise ConfigurationError, "cost budget requires a catalogued model, list-priced origin, and deterministic " \ "standard service tier for #{provider.model.inspect}" end |
#validate_usage!(usage) ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/mistri/budget.rb', line 30 def validate_usage!(usage) return unless cost? return if usage.cost.known? raise BudgetError.new( "cost budget cannot continue after a request returned unpriced usage; not retrying", usage: ) end |