Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

86: Add default limit for tools completions #87

Open
wants to merge 14 commits into
base: main
Choose a base branch
from

Conversation

rhys117
Copy link

@rhys117 rhys117 commented Apr 1, 2025

Resolves #86

Purpose

Introduces a configurable limit on tool completions to prevent infinite loops and excessive API usage. This feature adds protection against scenarios where AI responses might trigger continuous tool calls.

Implementation Details

  • Added max_tool_completions configuration option (default: 25 calls)
  • Added per-chat override capability through RubyLLM.chat(max_tool_completions: N)
  • Implemented ToolCallCompletionsReachedError when limit is exceeded
  • Added tracking of tool completions via number_of_tool_completions counter
  • Can be overridden for unlimited tool completions with nil

Usage Example

# Global configuration
RubyLLM.configure do |config|
  config.max_tool_completions = 10  # Set default limit
end

# Per-chat override
chat = RubyLLM.chat(max_tool_completions: 15)

Testing

  • Added RSpec test cases for OpenAI + Claude
  • Verified both global configuration and per-chat override functionality
  • Confirmed error raised when limit is reached

Documentation

  • Added section in tools.md guide explaining the feature
  • Included examples for both global and per-chat configuration

TODO

  • Add VCR cassettes for testing with additional providers (I do not have API keys for these providers):
    • Anthropic
    • Gemini
  • Review error messaging for clarity

@@ -105,6 +107,10 @@ def handle_tool_calls(response, &)
end

def execute_tool(tool_call)
raise ToolCallsLimitReachedError, "Tool calls limit reached: #{@max_tool_calls}" if max_tool_calls_reached?
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be worth discussing if this should be handled via the chat instead of raising an unhandled error.

@rhys117 rhys117 marked this pull request as draft April 1, 2025 13:54
- Otherwise the chat object could not have 'ask' executed on again due to malformed  messages
@crmne crmne added the enhancement New feature or request label Apr 2, 2025
@rhys117 rhys117 marked this pull request as ready for review April 7, 2025 12:15
@rhys117 rhys117 changed the title 86: Add default limit for tools calls 86: Add default limit for tools completions Apr 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Looping Tool Calls
2 participants