Sub topic 1
How a model sees and calls your tools
Prerequisites
This chapter assumes you have called an LLM API before, can read JSON, know the basic JSON Schema types (string, integer, object, array), and can read short Python examples.
What the model actually receives
A model cannot run your code or see your codebase. It sees text. When you pass tools to an API, the provider turns each definition into text in the model's context. That text includes the tool's name, its description, and its parameter schema.
From this text alone, the model decides two things:
- Whether to call a tool at all, and which one.
- What arguments to send.
If a definition is vague, the model guesses. Most tool failures trace back to the definition, not to the model.
The tool-calling loop
Your application and the model take turns. The model never executes anything itself.
In steps:
- You send the user's message and the list of tool definitions.
- The model replies with a tool call: a tool name and a JSON object of arguments. It may also reply with plain text if no tool fits.
- Your code runs the real function.
- You send the output back as a tool result.
- The model reads the result and either answers or calls another tool.
Steps 2 to 5 can repeat several times in one conversation.
The three parts of a definition
Every definition has the same three parts. The example below is shown in the Anthropic-style layout. OpenAI-style APIs name the schema field parameters instead of input_schema.
{
"name": "get_order",
"description": "Look up one order by its ID. Use this when the user gives an order ID such as 'ord_1001'. If they do not have an ID, use search_orders instead.",
"input_schema": {
"type": "object",
"properties": {
"order_id": {
"type": "string",
"description": "Order ID, for example 'ord_1001'."
}
},
"required": ["order_id"]
}
}| Part | What the model uses it for |
|---|---|
| `name` | Identifies the tool and appears in the tool call |
| `description` | Decides whether this tool fits the request |
| `input_schema` | Shapes the arguments: names, types, allowed values |
The following chapters cover each part in detail. For now, remember that the description is the main signal for choosing a tool. The schema is the main signal for filling in arguments.
A minimal loop in code
This sketch shows where your responsibility sits. The call_model function stands in for your provider's API call.
def get_order(order_id: str) -> dict:
return {"id": order_id, "status": "shipped"}
TOOLS = {"get_order": get_order}
def handle_tool_call(name: str, arguments: dict) -> dict:
func = TOOLS.get(name)
if func is None:
return {"is_error": True, "message": f"Unknown tool {name!r}."}
return func(**arguments)Note that handle_tool_call treats the model's output as input to check, not as trusted data. Later chapters cover validation.
Common errors at this stage
- Assuming the model sees your code. It sees only the definition. A docstring in Python is invisible unless you copy it into
description. - Treating the call as a guarantee. The model may pick the wrong tool or send a bad argument.
- Forgetting the result is also input. Whatever you return goes back into the model's context and shapes its next step.
Exercise
In the tool-calling loop, which part of your system actually runs the function, and what does the model contribute?
Show answer
Your application runs the function. The model contributes only the tool name and the JSON arguments, chosen from the definitions you supplied.