Tools and Function Calling
How a model asks your code to act: the tool-call loop built by hand, schemas, validation, errors, parallel calls, dangerous tools, MCP and a safe tool runner.
A language model can only produce text. It cannot read today's weather, multiply two big numbers exactly or send an email. Tool calling (also called function calling) is the trick that fixes this: you describe functions to the model, the model answers with a request such as 'call get_weather for Pune', your program runs the function and sends the result back, and the model writes the final answer. The model never runs anything. Your code does, which is where all the safety work lives.
This phase builds the loop by hand with a scripted fake model that returns messages in the real OpenAI-compatible shape, so you see every field: the tools list in the request, the tool_calls in the reply (where arguments is a JSON string you must parse), and the tool message that carries the result back. Then it adds what production needs: good descriptions and schemas, validation of untrusted arguments, errors fed back so the model can recover, several calls in one turn, confirmation for dangerous actions, and a short, verified look at the Model Context Protocol (MCP).
Two labs let a real model call a tool and let you answer it, so you walk the whole loop with a real model once. The phase ends with the Playbook: a safe tool runner in Python, Java and JavaScript. The coming AI Agents course builds on exactly this loop.
Practise this phase
Hands-on exercises: write the code, run it, and press Check to have it tested automatically.