Function Calling
Beginner 11 min read Function Calling

Function Calling Overview

From talk to action

Function calling lets an LLM request an action instead of writing prose about it. You declare tools; the model emits structured tool-call arguments; your code executes and feeds results back. That round trip turns chat into an interface.

function callingtoolstool callargumentsround trip
Prerequisites: Structured Outputs
Before we start

What you will learn

  • Explain the function-call round trip
  • Describe how tools are declared to the model
  • Handle basic tool-call execution safely
Introduction

What is this?

Function calling is a structured flow: you pass tool definitions (name, description, JSON-schema arguments) to the model; when appropriate the model returns a tool_call (not free text) naming a function and precise arguments; your app executes the function with strict validation; the result goes back into the conversation. Chat becomes capable of doing things.

Motivation

Why do you need it?

  • Structured calls beat 'please run X and tell me' — no prose to parse.
  • The model decides latency/scope: it only calls when the plan requires data.
  • Arguments arrive schema-shaped, so execution is type-safe by design.
  • It's the reliable primitive underneath agents and tool-using products.
Think about it

Mental model

Imagine

A concierge desk

Think of

Function calling

You don't hand the concierge your life; you ask, and they fill a small card (tool call: book taxi, 5pm, to airport) — structured fields, not essays. Your job (the app) is only to validate the card and relay the answer back if needed.

Mechanics

How it works

  • Declare tools: name, description, JSON-schema arguments.
  • Send user message + tool list to the model.
  • Model replies with a tool_call block (function + args) or plain text.
  • Your code validates args against the schema, executes the function.
  • Return the tool result to the model as a tool role message.

The function-call round trip.

Ask
Call
Execute
Return
Answer
Real world

In production

  • Chat apps add search/DB/calculator tools the model can invoke.
  • Automation flows trigger backend actions from chat intents.
  • Eval harnesses assert correct tool choice and argument validity.
Guidelines

Best practices

  • Keep tool descriptions crisp; they steer selection.
  • Validate args strictly — never execute unvalidated.
  • Design schemas the model can obey (flat, small, useful).
  • Cap the number of round trips per request.
Watch out

Common mistakes

  • Executing arguments the schema didn't check.
  • Too many overlapping tools confusing selection.
  • Forgetting to feed the tool result back into the loop.
Root cause analysis

Common issues

Error
Model never calls tools
Root cause

Descriptions unclear or task doesn't need it

Fix

Be explicit about WHEN to call in descriptions/prompt.

Error
Tool calls fail validation mid-loop
Root cause

Schema mismatch with real model output

Fix

Track actual args; widen schemas; add a retry with error feedback.

When things break

Debugging tips

  • Right action, wrong tool → sharpen descriptions / reduce overlap.
  • Missing tool_call in reply → the model judged no tool needed; check the prompt.
  • Args fail schema → enum/schema too strict vs realistic output.
Interviews

Interview checkpoint

Test yourself

Quick quiz

0/2
1

A tool_call contains…

2

Tool results must be returned to the model to continue the loop.

Go deeper

Official documentation