Chapter D · 4 of 4~11 min

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

Paths & resources for this lesson

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

  1. Register tools with JSON schemas
  2. Model returns {name, arguments}
  3. Validate args → call allowlisted function
  4. Send {role: "tool", content: result} back
  5. 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
  1. Tool args should be… validated / passed straight to shell
  2. Destructive actions need… human confirm / auto-run