Skip to content
This repository was archived by the owner on Sep 28, 2025. It is now read-only.

Implement tool call limits to prevent expensive runaway behavior#9

Draft
Copilot wants to merge 2 commits into
mainfrom
copilot/fix-8
Draft

Implement tool call limits to prevent expensive runaway behavior#9
Copilot wants to merge 2 commits into
mainfrom
copilot/fix-8

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Aug 5, 2025

This PR implements configurable tool call limits to prevent LLMs from making excessive tool calls that could result in expensive runaway behavior.

Problem

Without any external limits for tool calls, LLMs can keep "hammering" tools indefinitely, leading to:

  • Expensive API costs
  • Resource exhaustion
  • Performance degradation
  • Infinite loops in tool calling

Solution

Implemented a comprehensive tool call limiting system at the BaseTool level with agent-level tracking:

Key Features

  • Configurable limits: Added maxToolCalls field to Agent configuration (default: 0 = unlimited)
  • Per-tool tracking: Each tool is tracked independently per session
  • Session-based counters: Tool call counts are maintained per session
  • Graceful handling: When limits are exceeded, tool calls are rejected with clear error messages
  • Automatic reset: Counters are automatically reset during session summarization
  • Backward compatibility: Default unlimited behavior preserves existing functionality

Implementation Details

// Agent configuration
type Agent struct {
    // ... existing fields
    MaxToolCalls int `json:"maxToolCalls,omitempty"` // 0 means unlimited
}

// Tool call tracking
type ToolCallTracker struct {
    sessionToolCalls map[string]map[string]int // sessionID -> toolName -> count
}

Configuration Example

{
  "agents": {
    "orchestrator": {
      "model": "gpt-4o",
      "maxTokens": 5000,
      "maxToolCalls": 10,
      "name": "Security Orchestrator"
    }
  }
}

Benefits

  • Cost Control: Prevents expensive runaway tool usage
  • Performance: Avoids infinite loops in tool calling
  • Resource Management: Ensures fair resource allocation
  • Debugging: Makes it easier to identify problematic tool usage patterns

Testing

Added comprehensive test coverage including:

  • Tool call tracking functionality
  • Multi-tool and multi-session scenarios
  • Edge cases and error conditions
  • Integration with existing agent workflow

The implementation follows common patterns seen in agentic systems for tool call management and provides a foundation for more sophisticated rate limiting features in the future.

Fixes #8.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: yyovil <149292478+yyovil@users.noreply.github.com>
Copilot AI changed the title [WIP] limit max tool calls for a single tool. Implement tool call limits to prevent expensive runaway behavior Aug 5, 2025
Copilot AI requested a review from yyovil August 5, 2025 15:30
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

limit max tool calls for a single tool.

2 participants