限制代理可进行的工具调用次数有助于防止循环并更好地控制成本和性能。

使用 Agno 可以非常简单地做到这一点。你只需在初始化 Agent 或 Team 时传递 tool_call_limit 参数即可。

示例

from agno.agent import Agent
from agno.models.openai.chat import OpenAIChat
from agno.tools.yfinance import YFinanceTools

agent = Agent(
    model=OpenAIChat(id="gpt-4o-mini"),
    tools=[YFinanceTools(company_news=True, cache_results=True)],
    tool_call_limit=1, # Agent 不会执行超过一次的工具调用。
)

# 将执行第一次工具调用。第二次调用将优雅地失败。
agent.print_response(
    "Find me the current price of TSLA, then after that find me the latest news about Tesla.",
    stream=True,
)

注意事项

  • 如果 Agent 尝试 一次性 运行超过限制次数的工具调用,限制仍然有效。只会执行允许次数的工具调用。
  • 该限制是在 整个运行过程中 有效的,而不是针对 Agent 触发的单个请求。