Build garageTry it → read → next · ~11 min

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.

On paths: Builder · Student

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

  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