Tool Call Limit

This cookbook shows how to use tool call limit to control the number of tool calls an agent can make.

1"""
2Tool Call Limit
3=============================
4
5This cookbook shows how to use tool call limit to control the number of tool calls an agent can make.
6"""
7
8from kern.agent import Agent
9from kern.models.openai import OpenAIResponses
10from kern.tools.yfinance import YFinanceTools
11
12# ---------------------------------------------------------------------------
13# Create Agent
14# ---------------------------------------------------------------------------
15agent = Agent(
16 model=OpenAIResponses(id="gpt-5-mini"),
17 tools=[YFinanceTools()],
18 tool_call_limit=1,
19)
20
21# ---------------------------------------------------------------------------
22# Run Agent
23# ---------------------------------------------------------------------------
24if __name__ == "__main__":
25 # It should only call the first tool and fail to call the second tool.
26 agent.print_response(
27 "Find me the current price of TSLA, then after that find me the latest news about Tesla.",
28 stream=True,
29 )

Run the Example

1# Clone and setup repo
2git clone https://github.com/kern-ai/kern.git
3cd kern/cookbook/02_agents/04_tools
4
5# Create and activate virtual environment
6./scripts/demo_setup.sh
7source .venvs/demo/bin/activate
8
9python tool_call_limit.py