Your agent_with_tool gives the agent one choice. What happens when you give it two tools with different purposes?
The agent reads the prompt and picks whichever tool fits. If the prompt asks for word count it calls the word counter; if it asks for character count it calls the character counter.
Exactly. Two @agent.tool_plain functions registered on the same agent instance. The agent decides based on the prompt:
agent = Agent(model)
@agent.tool_plain
def word_count(text: str) -> int:
return len(text.split())
@agent.tool_plain
def char_count(text: str) -> int:
return len(text)
return agent.run_sync(prompt).outputCan the agent call both tools in one response? Or does it pick exactly one?
It picks based on the prompt. For 'count the words in this text' it calls word_count. For 'how many characters?' it calls char_count. For an ambiguous prompt it uses its judgment. Here's the full function:
def agent_two_tools(prompt: str) -> str:
agent = Agent(model)
@agent.tool_plain
def word_count(text: str) -> int:
return len(text.split())
@agent.tool_plain
def char_count(text: str) -> int:
return len(text)
result = agent.run_sync(prompt)
output = result.output
print(f"Two-tool response: {output}")
return outputI gave the agent two employees and it hired the right one for each task. I'm running an HR department in 10 lines.
The agent's decision is based on the tool names and signatures you provided. Clear names like word_count and char_count make the decision obvious. Ambiguous names lead to wrong tool selection.
I can extend this — add a revenue calculator, a churn rate formula, a CAC calculator. The agent picks the right formula from the menu.
Tool selection is only as good as your naming and documentation. For production agents, add a docstring to each tool function — the model reads it to understand when to call the tool. """Count the number of words in text.""" removes all ambiguity.
@agent.tool_plain Functionsagent = Agent(model)
@agent.tool_plain
def word_count(text: str) -> int:
return len(text.split())
@agent.tool_plain
def char_count(text: str) -> int:
return len(text)
return agent.run_sync(prompt).outputThe agent selects the tool based on the prompt and the function names/signatures.
Descriptive function names + typed parameters = unambiguous tool selection. For production, add docstrings.
The agent may call one, both, or neither depending on the prompt. The response is always a string in .output.
Your agent_with_tool gives the agent one choice. What happens when you give it two tools with different purposes?
The agent reads the prompt and picks whichever tool fits. If the prompt asks for word count it calls the word counter; if it asks for character count it calls the character counter.
Exactly. Two @agent.tool_plain functions registered on the same agent instance. The agent decides based on the prompt:
agent = Agent(model)
@agent.tool_plain
def word_count(text: str) -> int:
return len(text.split())
@agent.tool_plain
def char_count(text: str) -> int:
return len(text)
return agent.run_sync(prompt).outputCan the agent call both tools in one response? Or does it pick exactly one?
It picks based on the prompt. For 'count the words in this text' it calls word_count. For 'how many characters?' it calls char_count. For an ambiguous prompt it uses its judgment. Here's the full function:
def agent_two_tools(prompt: str) -> str:
agent = Agent(model)
@agent.tool_plain
def word_count(text: str) -> int:
return len(text.split())
@agent.tool_plain
def char_count(text: str) -> int:
return len(text)
result = agent.run_sync(prompt)
output = result.output
print(f"Two-tool response: {output}")
return outputI gave the agent two employees and it hired the right one for each task. I'm running an HR department in 10 lines.
The agent's decision is based on the tool names and signatures you provided. Clear names like word_count and char_count make the decision obvious. Ambiguous names lead to wrong tool selection.
I can extend this — add a revenue calculator, a churn rate formula, a CAC calculator. The agent picks the right formula from the menu.
Tool selection is only as good as your naming and documentation. For production agents, add a docstring to each tool function — the model reads it to understand when to call the tool. """Count the number of words in text.""" removes all ambiguity.
@agent.tool_plain Functionsagent = Agent(model)
@agent.tool_plain
def word_count(text: str) -> int:
return len(text.split())
@agent.tool_plain
def char_count(text: str) -> int:
return len(text)
return agent.run_sync(prompt).outputThe agent selects the tool based on the prompt and the function names/signatures.
Descriptive function names + typed parameters = unambiguous tool selection. For production, add docstrings.
The agent may call one, both, or neither depending on the prompt. The response is always a string in .output.
Create a free account to get started. Paid plans unlock all tracks.