Tool Use Fundamentals

Medium 25 min read

Why Tools Matter

Why Tool Use Matters

The Problem: LLMs can only generate text -- they cannot browse the web, query databases, execute code, or interact with external systems on their own.

The Solution: Tools bridge the gap between language understanding and real-world action, giving agents the ability to fetch data, compute results, and modify external state.

Real Impact: Tool use transforms LLMs from knowledge retrieval systems into action-taking agents that can accomplish real tasks.

Real-World Analogy

Think of tools as a craftsperson's workshop:

  • Tool Schema = The label on each tool explaining what it does
  • Parameters = The settings and adjustments on each tool
  • Execution = Actually using the tool to shape the material
  • Results = The output or measurement the tool provides
  • Error Handling = Knowing when a tool breaks and grabbing another

What Tools Enable

Information Retrieval

Search engines, databases, APIs -- tools let agents access up-to-date information beyond training data.

Computation

Code execution, calculators, data analysis -- tools handle precise computations LLMs cannot do reliably.

External Actions

Sending emails, creating files, updating databases -- tools let agents change the state of the world.

Verification

Checking facts, validating outputs, running tests -- tools provide ground truth for agent reasoning.

Tool Definition

tool_schema.py
# Define a tool using JSON Schema format
tools = [{
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "Get current weather for a location.",
        "parameters": {
            "type": "object",
            "properties": {
                "location": {
                    "type": "string",
                    "description": "City name, e.g. San Francisco"
                },
                "unit": {
                    "type": "string",
                    "enum": ["celsius", "fahrenheit"]
                }
            },
            "required": ["location"]
        }
    }
}]

Tool Execution Flow

Tool Execution Flow
Agent decides tool Tool Call name + params Execute run function Result return data Agent uses result

Handling Tool Results

tool_execution.py
import json

def execute_tool(tool_call, available_tools):
    func_name = tool_call.function.name
    args = json.loads(tool_call.function.arguments)

    if func_name in available_tools:
        try:
            result = available_tools[func_name](**args)
            return {
                "role": "tool",
                "tool_call_id": tool_call.id,
                "content": json.dumps(result)
            }
        except Exception as e:
            return {
                "role": "tool",
                "tool_call_id": tool_call.id,
                "content": f"Error: {str(e)}"
            }

Building Custom Tools

Tool TypeDescriptionExample
API WrapperWraps an external REST APIWeather, stock prices
Database QueryRuns SQL or NoSQL queriesCustomer lookup
Code ExecutorRuns Python/JS in sandboxCalculations, transforms
File OperationsRead, write, modify filesReport generation
CommunicationSend messages/notificationsEmail, Slack, webhooks

Quick Reference

Best PracticeDescriptionWhy
Clear descriptionsWrite detailed tool docsLLM uses description to decide when to call
Typed parametersUse JSON Schema typesReduces invalid parameter errors
Error messagesReturn helpful errorsAgent can self-correct
IdempotencySafe to call multiple timesAgent may retry on failure
Scoped accessLimit what tools can doPrevents unintended side effects