Class: Ask::Agent::Extensions::RateLimiter
- Inherits:
-
Object
- Object
- Ask::Agent::Extensions::RateLimiter
- Defined in:
- lib/ask/agent/extensions/rate_limiter.rb
Instance Method Summary collapse
- #before_tool_call(tool_call, _context) ⇒ Object
-
#initialize(max_calls_per_minute: 20, max_tool_calls_per_turn: 5) ⇒ RateLimiter
constructor
A new instance of RateLimiter.
- #reset_turn! ⇒ Object
Constructor Details
#initialize(max_calls_per_minute: 20, max_tool_calls_per_turn: 5) ⇒ RateLimiter
Returns a new instance of RateLimiter.
7 8 9 10 11 12 13 |
# File 'lib/ask/agent/extensions/rate_limiter.rb', line 7 def initialize(max_calls_per_minute: 20, max_tool_calls_per_turn: 5) @max_calls_per_minute = max_calls_per_minute @max_tool_calls_per_turn = max_tool_calls_per_turn @turn_calls = 0 @minute_window = [] @mutex = Mutex.new end |
Instance Method Details
#before_tool_call(tool_call, _context) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/ask/agent/extensions/rate_limiter.rb', line 15 def before_tool_call(tool_call, _context) now = Time.now @mutex.synchronize do @turn_calls += 1 if @turn_calls > @max_tool_calls_per_turn return { action: :block, reason: "Exceeded #{@max_tool_calls_per_turn} tool calls per turn" } end @minute_window << now @minute_window.reject! { |t| now - t > 60 } if @minute_window.size > @max_calls_per_minute return { action: :block, reason: "Exceeded #{@max_calls_per_minute} tool calls per minute" } end end { action: :proceed } end |
#reset_turn! ⇒ Object
36 37 38 |
# File 'lib/ask/agent/extensions/rate_limiter.rb', line 36 def reset_turn! @mutex.synchronize { @turn_calls = 0 } end |