From Text Generation to Action
Read a little, play a little. No scary maths, and no rush.
Answers are nice; actions pay the bills
"What's my order status?" can't be answered from training data — it needs a live lookup. Tool calling (function calling) lets the model act: it emits a structured request like "call get_order with id 12847," your code runs it, and the result flows back into the conversation. Text generation becomes a control plane for real systems.
The schema is the UI (for the model)
The model never sees your code — only a JSON description. So the description is the interface: name it like a verb, describe exactly when to use it, and type every parameter:
{
"name": "get_order",
"description": "Fetch live order status. Use when the user gives an order id.",
"parameters": {
"type": "object",
"properties": { "id": { "type": "string" } },
"required": ["id"]
}
}
Vague descriptions ("handles orders") produce vague behavior (calling it for returns, refunds, and weather). Precise descriptions produce precise calls.
The loop, step by step
- User asks something needing live data or action.
- Model replies with a tool call (not text).
- Your code executes it — real API, real database, real side effects.
- The observation goes back into context; the model composes the final answer.
Try /animations/tool-calling to watch it move.
One request, end to end
A user writes: "Where's my order 12847?"
- The model sees the
get_orderdescription and replies with a tool call:get_order(id="12847"). - Your code checks the arguments before running anything: is this a plausible order ID, and does it belong to this user? Then it queries the order system.
- The result,
{"status": "shipped", "eta": "Oct 12"}, goes back to the model as a tool message. - The model answers: "Your order has shipped and should arrive on October 12."
If the lookup fails, return the error as the tool result, for example {"error": "order not found"}, rather than crashing. The model can then explain the problem to the user or ask them to double-check the number.
Several calls at once
Models can request more than one tool call in a single turn, such as checking an order and a refund status together. Your code runs them, concurrently if they are independent, and returns every result before the model writes its answer.
Safety rails before side effects
Read-only tools (lookups, search) can run freely. Anything that changes the world — refunds, deletes, sends — needs rails: human approval above a threshold, idempotency keys so retries don't double-charge, least-privilege credentials per tool, and a full audit log of who called what with which arguments.
How many tools is too many?
Every tool's description is sent with every request, so tools cost input tokens even when unused. More importantly, a model choosing among fifty similar tools picks the wrong one more often than a model choosing among five. Keep tool names and descriptions clearly distinct, merge near-duplicates, and when you genuinely need many tools, route first: a quick classification decides which small group of tools this request can see.
Remember this
- Describe tools like you'd brief a clever intern: when, with what, and what never.
- Reads run free; writes need approval, idempotency, and logs.
Check your understanding
5 questions · first-try answers earn the most XP
My notes
Saved in this browser. Highlight a line above and save it, or write it in your own words.
Nothing saved yet. Your highlights will live here.
References
Finished reading?
Ticking it here also ticks the chapter in the sidebar, the section count and your streak — it is all one number.
Practice this in an incident
Related chapters
Spotted a mistake or want a topic covered? Report an issue