Zero to AI Engineer

Module 18 of 54

Module 18: Function Calling & Tools

5 min read811 words
What you'll learn
Explain what a "tool" is for an LLMUnderstand function callingSee how LangChain wires tools to modelsApply safe tool-use rules

"On its own, an LLM can only talk. Give it tools, and it can search, calculate, and act on the real world."

Level: Intermediate · Time: ~12 min · Prerequisites: Module 16

Learning Objectives

By the end of this module, you will be able to:

  • Explain what a "tool" is for an LLM
  • Understand function calling
  • See how LangChain wires tools to models
  • Apply safe tool-use rules

1. Tools Give LLMs Hands

A tool is any function an LLM can invoke — a web search, a calculator, a database query, sending an email. Without tools, an LLM is frozen in its training data and can only produce text. With tools, it can fetch fresh information and take actions.

Explain like I'm new: An LLM with tools is like a smart assistant with a phone and a computer. Instead of guessing today's weather, it can look it up. Instead of doing shaky mental math, it can use a calculator. Tools turn "I think…" into "I checked."

2. Function Calling: How It Works

Modern LLMs support function calling: you describe your tools (name, what they do, inputs), and the model can respond with a request to call one.

The loop:

  1. You give the model a question and a list of available tools.
  2. The model replies: "call get_weather(city='Paris')."
  3. Your app actually runs the function and returns the result.
  4. The model uses that result to answer.

A concrete trace: the user asks "What's the weather in Paris?" The model, seeing a get_weather tool, doesn't guess — it emits a structured request naming the tool and its arguments ({"city": "Paris"}). Your code runs the real function, gets back "18°C, cloudy," and hands that to the model, which then writes a natural reply. The model supplied the decision; your code supplied the facts.

Key idea: The LLM never runs code itself — it requests a tool call, and your app executes it. The model decides which tool and when; you stay in control of what actually happens. That separation is critical for safety.

Explain like I'm new: Function calling is like a doctor writing a prescription. The doctor (the model) decides what's needed and writes it down precisely, but the pharmacist (your app) is the one who actually fills it. The model can request; only your code can act.

3. Tools in LangChain

LangChain makes defining and attaching tools clean:

python
[object Object], langchain_core.tools ,[object Object], tool

,[object Object],
,[object Object], ,[object Object],(,[object Object],) -> ,[object Object],:
    ,[object Object],
    ,[object Object], a + b

model_with_tools = model.bind_tools([add])

Now the model can choose to call add when a question needs it. This is the foundation for agents (next module). Notice how much the @tool decorator captures automatically: the function name becomes the tool name, the docstring ("Add two numbers.") becomes the description the model reads, and the type hints (a: int, b: int) tell the model what arguments to supply. That's why clear names, docstrings, and types aren't just good style here — they're literally the instructions the model uses to decide whether and how to call your tool.

Real-world use case: A finance assistant is given tools for get_stock_price, currency_convert, and search_news. Ask "what's Apple worth in euros?" and it calls the price tool, then the converter — chaining real data into a correct answer instead of guessing.

4. Safe Tool Use

Tools are powerful, so they're risky:

  • Least privilege — give each tool the minimum access it needs (read-only when possible)
  • Validate inputs — never trust arguments blindly
  • Human approval — for risky actions (spending, deleting, emailing many people)
  • Sandbox code-running tools

Common mistake: Giving an LLM a powerful, broad tool "to be flexible" (like raw database write access). The more a tool can do, the more damage a mistake — or a prompt injection — can cause. Start narrow.

Hands-On: Try This

Try this: List 3 tools you'd give a "travel planner" LLM (e.g., search flights, check calendar, book hotel). For each, note the least access it needs and whether it should require your approval before acting. That's tool design in a nutshell.

✅ Checkpoint

  1. Why do LLMs need tools?
  2. Who actually runs a tool when the model "calls" it?
  3. Name one safe tool-use rule.

Answers: 1) To get fresh data and take real actions beyond text. 2) Your app runs it; the model only requests it. 3) e.g., least privilege, validate inputs, human approval for risky actions.

Key Takeaway: Tools let an LLM act on the world — search, calculate, query, send. Via function calling, the model requests a tool and your app executes it, keeping you in control. LangChain makes defining and binding tools simple (bind_tools). Always apply safe rules: least privilege, input validation, and human approval for risky actions.

Further Learning

Adapted from the LangChain for Beginners curriculum (MIT License).