tool-use
Tool Use
Tool Use (or Function Calling) is the core mechanism of agency. It allows an LLM to recognize when it needs external information or needs to perform an action. Instead of hallucinating an answer, the model outputs a structured command (like a JSON object) to call a specific function (e.g., get_weather(city="London")). The system executes the function and feeds the result back to the model.
When to Use
- Real-time Data: When the answer requires current information (stock prices, weather, sports scores).
- Computational Tasks: When precise math or data processing is needed (using a calculator or Python REPL).
- System Interaction: When the agent needs to modify the environment (sending emails, updating databases, creating files).
- Private Data Access: Querying internal knowledge bases or APIs.
Use Cases
- Search: Integrating Google Search or Bing to answer current events questions.
- Code Execution: Using a Python sandbox to generate charts or analyze CSV files.
- API Integration: Connecting to Slack, Jira, or GitHub to automate workflows.
Implementation Pattern
def tool_use_loop(user_query):
messages = [{"role": "user", "content": user_query}]
# Available tools definition
tools = [{
"name": "get_stock_price",
"parameters": {"symbol": "string"}
}]
# Step 1: Agent decides to call a tool
response = llm.chat(messages, tools=tools)
if response.tool_calls:
# Step 2: System executes the tool
tool_call = response.tool_calls[0]
result = execute_tool(tool_call.name, tool_call.arguments)
# Step 3: Result is fed back to the Agent
messages.append(response.message) # Keep the assistant's "intent"
messages.append({
"role": "tool",
"tool_call_id": tool_call.id,
"content": str(result)
})
# Step 4: Agent generates final answer using tool result
final_answer = llm.chat(messages)
return final_answer
return response.content
Troubleshooting
| Problem | Cause | Fix |
|---|---|---|
| Agent calls wrong tool | Ambiguous tool description | Start each tool description with an active verb; add "DO NOT USE FOR..." |
| Tool call arguments malformed | Model hallucinated parameters | Add JSON Schema validation; return clear error messages back to agent |
| Agent loops on tool failures | No retry limit | Set max_retries=2; after limit, return error and let agent decide next step |
| Tool not available in some environments | Missing dependency | Check tool.available() before including in tool list; graceful degradation |
More from lauraflorentin/skills-marketplace
multi-agent-collaboration
A structural pattern where multiple specialized agents communicate and coordinate to solve a problem that is too complex for a single agent. Use when user asks to "build a multi-agent system", "agents working together", "agent collaboration", or mentions team of agents, distributed agents, or swarm.
21reflection
A recursive pattern where an agent evaluates and critiques its own output to iteratively improve quality and catch errors. Use when user asks to "add self-reflection", "agent introspection", "self-critique", or mentions self-evaluation, meta-cognition, or quality self-assessment.
18human-in-the-loop
A hybrid pattern where the system pauses execution to request human approval, input, or disambiguation before proceeding with critical actions. Use when user asks to "add human approval", "require human review", "human-in-the-loop", or mentions approval workflows, human oversight, or escalation.
16planning
A high-level cognitive pattern where an agent formulates a structured sequence of actions (a plan) before executing any of them, ensuring goal-directed behavior. Use when user asks to "add planning to my agent", "task planning", "agent planning", or mentions plan generation, plan execution, or step-by-step planning.
14parallelization
A concurrency pattern where multiple agent tasks are executed at the same time to speed up processing or gather diverse perspectives. Use when user asks to "run agents in parallel", "parallelize tasks", "concurrent execution", or mentions parallel processing, fan-out, or batch execution.
13routing
A control flow pattern where a central component classifies an input request and directs it to the most appropriate specialized agent or tool. Use when user asks to "route between agents", "agent routing", "task dispatch", or mentions classifier routing, intent detection, or agent selection.
12