Function calling in code
Parse tool calls from the model, run your functions safely, return results to the chat.
Reviewed July 2026 · OpenAI / Anthropic tool calling
1Try it yourself
Code Lab
Tools in code
Wire function calling: tool name → function → result → reply.
2Read & reflect
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
3Spark check
Continue learning · glossary & guides
- Tool args should be… validated / passed straight to shell
- Destructive actions need… human confirm / auto-run