Function Calling & Tool Use
LLMs cannot do anything. They generate text. That is the entire capability. They cannot check the weather, query a database, send an email, run code, or read a file. Every "AI agent" you have ever seen is an LLM generating JSON that says which function to call -- and then your code actually calling it. The model is the brain. Tools are the hands. Function calling is the nervous system connecting them. Related: Phase 11 · 14 (Model Context Protocol) — when a tool is shared across hosts, graduate from inline function-calling to an MCP server. This lesson covers the inline case; MCP covers the protocol case. Implement a function calling loop: define tool schemas, parse the model's tool-call JSON, execute functions, and return results. Design tool schemas with clear descriptions and typed parameters that the model can reliably invoke. Build a multi-turn agent loop that chains multiple function calls to answer complex queries. Handle function calling edge cases: parallel tool calls, error propagation, and preventing infinite tool loops. You build a chatbot. A user asks: "What's the weather in Tokyo right now?" The model responds: "I don't have access to real-time weather data, but based on the season, Tokyo is likely around 15 degrees Celsius..." That is a hallucination dressed in a disclaimer. The model does not know the…
Function Calling & Tool Use: LLMs cannot do anything. They generate text. That is the entire capability. They cannot check the weather, query a database, send…
This free lesson is part of the AI Engineering from Scratch curriculum. Read the full explanation, run the lesson code, and verify the result in the interactive reader or from the repository source.
Browse the complete course catalog or open this lesson on GitHub.