Tool calling: letting the model call your functions
Agents & Tool Calling · 10 questions
Short notes
Key idea
With tool calling, you describe functions to the model. Instead of answering in text, it can reply "call get_order(id=4471)". Your code runs the function and sends the result back, and the model uses it to answer.
Syntax
from google.genai import types
def get_order(order_id: int) -> dict:
# look the order up in your database
return {"id": order_id, "status": "shipped"}
config = types.GenerateContentConfig(tools=[get_order])
r = client.models.generate_content(model=MODEL, contents="Where is order 4471?", config=config)
- The model sees each tool's name, description and parameters.
- You run the tool, never the model.
- The result goes back as a function response; the model then writes the reply.
Example
"Where is order 4471?" → the model calls get_order(4471) → your code returns {"status": "shipped"} → the model replies "Your order has shipped."
Traps
- The model can call the wrong tool or invent arguments; validate every argument.
- Tools with side effects (refunds, emails) need checks before they run.
- Return errors as results ("order not found") so the model can recover.
- Vague descriptions cause wrong calls; describe when to use each tool.
Want the full lesson with step-by-step examples, a project and a certificate? Agents & Tool Calling course, ₹180 →
- Question 1easy
With tool calling, who actually runs the function?
- Question 2easy
What does the model use to decide when to call a tool and with which arguments?
- Question 3easy
What does this print?
from practice_llm import ScriptedModel model = ScriptedModel([{"tool": "check_stock", "args": {"product_id": 3}}]) reply = model.generate([{"role": "user", "content": "Is the charger in stock?"}]) print(reply.tool_calls[0].name, reply.tool_calls[0].args["product_id"]) - Question 4medium
What does this print?
from practice_llm import ScriptedModel model = ScriptedModel(["Returns are accepted within 10 days.", {"tool": "get_order_status", "args": {"order_id": 5}}]) kinds = [] for q in ["Return window?", "Where is order 5?"]: r = model.generate([{"role": "user", "content": q}]) kinds.append("tool" if r.tool_calls else "text") print(kinds) - Question 5hard
Write
run_tool(call)that returns{"error": "Unknown tool <name>"}for names not inTOOLS, returns{"error": "Bad arguments"}when calling the function raisesTypeError, and otherwise returns the function's result. Store[run_tool(c) for c in calls]inresult.Store your answer in
result.
Tool calling: letting the model call your functions
Tool calling: letting the model call your functions
Tool calling: letting the model call your functions
Tool calling: letting the model call your functions
Tool calling: letting the model call your functions
5 more questions on this topic, with hints, and tests
Sign up free for 21 days of every question and test.