Fix issue #2434: Allow tools to specify if they permit repeated usage

Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
Devin AI
2025-03-21 12:35:52 +00:00
parent 03f1d57463
commit 245399bca0
5 changed files with 190 additions and 0 deletions

View File

@@ -546,6 +546,47 @@ def test_agent_moved_on_after_max_iterations():
assert output == "42"
@pytest.mark.vcr(filter_headers=["authorization"])
def test_agent_repeated_tool_usage_respects_allow_repeated_usage(capsys):
@tool
def repeatable_tool(anything: str) -> float:
"""A tool that allows being used repeatedly with the same input."""
return 42
# Patch the tool to set allow_repeated_usage to True
repeatable_tool.allow_repeated_usage = True
agent = Agent(
role="test role",
goal="test goal",
backstory="test backstory",
max_iter=4,
llm="gpt-4",
allow_delegation=False,
verbose=True,
)
task = Task(
description="Use the repeatable tool with the same input multiple times.",
expected_output="The result of using the repeatable tool",
)
# force cleaning cache
agent.tools_handler.cache = CacheHandler()
agent.execute_task(
task=task,
tools=[repeatable_tool],
)
captured = capsys.readouterr()
# Should NOT show the repeated usage error
assert (
"I tried reusing the same input, I must stop using this action input. I'll try something else instead."
not in captured.out
)
@pytest.mark.vcr(filter_headers=["authorization"])
def test_agent_respect_the_max_rpm_set(capsys):
@tool