Tutorials · Chapter D (4/4) · ~11 min
Function calling in code
Try it → see it → read → next
Parse tool calls from the model, run your functions safely, return results to the chat.
Try yourself
Code Lab
Tools in code
Wire function calling: tool name → function → result → reply.
Recap
What you just did
You traced model → tool name + args → your function → final natural-language reply.
Teach
How it works
- Register tools with JSON schemas
- Model returns
{name, arguments} - Validate args → call allowlisted function
- Send
{role: "tool", content: result}back - Model writes user-facing answer
Same pattern across OpenAI, Anthropic, and others (names vary).
Use it
When you'd use this
- Calculators, CRM lookups, booking, internal APIs
- Anything live data or actions
Watch out
Watch out
Never expose destructive tools without confirmation. Validate every argument.
Try next
Try this next
How-to: add function calling · Concept: structured outputs