Tool schemas & registry

Tool calling lets your agent reach beyond text generation. By defining tool schemas (function name, description, parameters), the LLM can decide when and how to call external functions like search APIs, calculators, databases, or any service.

Tool calling sequence

The LLM chooses which tool to call, your code executes it, and the result goes back.

Because real-time data (weather, stock prices, database records) changes constantly. Tools let the agent fetch live information at query time instead of relying on stale training data.

patterns/05_tool_calling.py
python
# Define tool schemas for the LLM
tools = [
    {
        "type": "function",
        "function": {
            "name": "get_population",
            "description": "Get population of a country",
            "parameters": {
                "type": "object",
                "properties": {
                    "country": {
                        "type": "string",
                        "description": "Country name"
                    }
                },
                "required": ["country"]
            }
        }
    }
]

Tool schemas describe your functions using JSON Schema. The LLM reads these to decide which tool to call and what arguments to pass.

Once you have schemas, you need a registry that maps tool names to actual Python functions. The LLM returns a string like "get_population" and your code looks it up here.

patterns/05_tool_calling.py
python
# Tool registry: maps names to actual functions
tool_registry = {
    "get_population": get_population,
    "get_country_info": get_country_info,
}

# LLM decides which tool to call
response = litellm.completion(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": query}],
    tools=tools
)

The registry maps tool names (strings) to actual Python functions. When the LLM says "call get_population", we look up the function here.

Finally, extract the tool call from the response and execute it. The LLM tells you which function to call and what arguments to pass.

patterns/05_tool_calling.py
python
# Extract and execute the tool call
tool_call = response.choices[0].message.tool_calls[0]
func = tool_registry[tool_call.function.name]
result = func(**json.loads(tool_call.function.arguments))

Extract the tool name and arguments from the LLM response, look up the function in the registry, and execute it.

Quiz: Quiz

Loading practice…