Lesson 4.1 · 8 min read · about 55 min to master at a very slow pace

Tool Calling

How an LLM calls functions in your code: you describe tools with a name, description and JSON schema; the model asks for a call; your code runs it and returns the result. The full request loop, good tool design, and where MCP fits.

Not startedJump to quiz

The model never runs anything

With tool calling (also called function calling) you send the model a list of tools it may use. When a tool would help, the model replies not with text but with a request: "call get_order_status with {"order_id": "4471"}". Your code runs the function, sends the result back, and the model continues. The model only ever produces text; your application decides what actually executes.

The loop

  1. Send: messages + tool definitions.
  2. Model replies with a tool call (name + arguments).
  3. Your code validates the arguments and runs the tool.
  4. Send the result back as a tool-result message.
  5. Model replies with the final answer, or another tool call (go to 3).

Defining a tool

{
  "name": "get_order_status",
  "description": "Look up the shipping status of ONE customer order.
                  Use when the customer gives an order number.
                  Returns status, carrier and expected delivery date.",
  "input_schema": {
    "type": "object",
    "properties": {
      "order_id": {"type": "string",
                   "description": "Order number without '#', e.g. 4471"}
    },
    "required": ["order_id"]
  }
}

The description is a prompt: the model decides when and how to call the tool from it alone. Say what it does, when to use it, what it returns, and the format of each argument.

Designing good tools

  • Few, clear tools beat many overlapping ones. If you cannot say when to use tool A rather than tool B, neither can the model.
  • Return what the model needs, not raw dumps. A 200-field JSON record wastes tokens and attention; return the five fields that matter.
  • Return useful errors: "Order 4471X not found. Order numbers are 4–6 digits." lets the model recover.
  • Validate arguments in code before running anything, exactly as for structured output.
  • Prefer read-only tools; treat any tool that changes data or spends money as high-risk (lesson 4.4).

MCP: tools as a standard

The Model Context Protocol (MCP) is an open standard for exposing tools, data and prompts to LLM applications. You write an MCP server once (for example around a client's ticketing system) and any MCP-compatible application or agent can use its tools. For client work it means integrations are reusable across projects and assistants, rather than rebuilt for each one.

Stuck? Common doubts

Questions real learners asked while studying this lesson. They stay closed so they don't spoil your own thinking: open only the ones you need.

First passCan the model call several tools at once?

Many APIs allow parallel tool calls: the model asks for several independent calls in one reply (for example the weather in three cities). Your code runs them, ideally concurrently, and returns all results together. Dependent calls (look up the customer, then their orders) still need separate turns.

Going deeperHow many tools can I give a model?

There is no hard rule, but tool selection gets worse as tools multiply and overlap, and every definition costs input tokens on every call. A few to a few dozen well-separated tools is comfortable. For larger catalogues, group tools, load only those relevant to the task, or add a search step over tool descriptions.

Cheatsheet

The tool-calling loop

Send tools → model requests a call → your code validates and runs it → send result → model answers or calls again.

Good tool design

Few non-overlapping tools; descriptions say what, when and returns; compact outputs; helpful errors; validate arguments.

MCP

Model Context Protocol: an open standard so one server's tools and data work with any compatible LLM app.

See every cheatsheet by category →

Check your understanding

Score 75% or more to master this lesson and earn XP. Answer first, then check.

Q1conceptIn tool calling, who actually executes the function?
Q2diagnoseThe model keeps calling search_customers when it should call get_order_status. What should you fix first?
Q3applyYour get_customer tool returns a 200-field JSON record. The agent gets slow and misses key facts. What should you change?
Q4applyA tool call fails because the order "4471X" doesn't exist. What should the tool return to the model?
Q5conceptWhat does the Model Context Protocol (MCP) give you in client work?