Lesson 20 ยท Intermediate

Function Calling and Tool Use

Understand how models can call tools, APIs, calculators, or business functions instead of only returning plain text.

Read the explanation carefully, then review the examples and coding section. The goal is to understand both the concept and how it appears inside a real application workflow.

Explanation

Tool use lets a model select structured actions such as database lookup, search, calculation, or ticket creation.

The model decides when a tool is needed, then passes arguments in a structured format.

Tool outputs can be fed back into the model to produce the final user-facing answer.

Why this topic matters in practice

In generative AI products, the model is only one part of the system. The surrounding workflow determines whether the output is useful, safe, and maintainable. This lesson matters because it helps you connect the idea to tasks such as tutoring, search, copilots, business assistants, and production automation.

Examples

Weather assistant

The model calls a weather function instead of guessing today's conditions.

Support workflow

The model looks up order status before answering a customer.

Finance helper

The model calls a calculator for totals rather than doing complex arithmetic from memory.

A toy function-calling pattern

The code below is intentionally concise so the underlying pattern stays clear. It focuses on the application logic you can reuse, even if you later switch model providers or deployment environments.

def get_order_status(order_id):
    return {"order_id": order_id, "status": "Shipped", "eta_days": 2}

tool_request = {"tool": "get_order_status", "arguments": {"order_id": "A123"}}

if tool_request["tool"] == "get_order_status":
    result = get_order_status(**tool_request["arguments"])
    print("Tool result:", result)

How the coding section works

  • Real model platforms can return tool calls in structured JSON-like formats.
  • The application remains responsible for validating arguments and executing the tool safely.
  • Tool use makes answers more reliable because external systems provide the facts.

Implementation advice

When turning this lesson into a real feature, think beyond the code snippet itself. Decide what inputs should be allowed, how you will validate outputs, how you will recover from errors, and how you will measure whether the feature is actually helping users. Those surrounding choices often determine whether an AI feature feels polished or unreliable.

Summary / key takeaways

  • Tool use extends models beyond static text generation.
  • Safe applications validate tool calls before execution.
  • Function calling is a foundation for assistants, copilots, and agentic workflows.

Exercises

  1. List three tools that would be useful for an AI tutor application.
  2. Why is tool validation important?
  3. Write a toy function for retrieving a course title by lesson number.