Reference · How-to · ~12 min

How to add function calling

Let the model request actions; your code executes them safely.

Let the model request actions; your code executes them safely.

Steps

1. **Define tools** — name, description, JSON schema for parameters

2. **Send tools** with the chat request

3. If the model returns a **tool call**, parse arguments

4. **Run your function** (validate args first!)

5. Send **tool result** back as a message; model writes final reply

Tool schema example

{
  "name": "get_weather",
  "description": "Get current weather for a city",
  "parameters": {
    "type": "object",
    "properties": {
      "city": { "type": "string" }
    },
    "required": ["city"]
  }
}

Safety rules

  • Allowlist callable functions
  • Never `eval()` user-provided code
  • Confirm destructive actions with the user
  • **Try the lessons:** `what-are-tools` (Lane C) · `tools-in-code` (Lane D)