Skip to content

Function Calling

Function calling (or tool use) lets an LLM request to execute specific functions in your application. Instead of guessing at data, the model can ask to look it up, perform calculations, or trigger actions.

You define the available functions and the model decides when and how to call them.

Tool Definition
{
  "tools": [
    {
      "name": "get_weather",
      "description": "Get current weather for a city",
      "parameters": {
        "type": "object",
        "properties": {
          "city": { "type": "string" },
          "units": {
            "type": "string",
            "enum": ["celsius", "fahrenheit"]
          }
        },
        "required": ["city"]
      }
    }
  ]
}

When a user asks about the weather, the model recognizes it needs real-time data and generates a function call instead of guessing.

User Message
What's the weather like in Tokyo right now?

The model doesn't make up weather data. Instead, it returns a structured function call for your app to execute.

Model Response (function call)
{
  "function_call": {
    "name": "get_weather",
    "arguments": {
      "city": "Tokyo",
      "units": "celsius"
    }
  }
}

Your application executes the function and sends the result back to the model.

Function Result (sent back to model)
{
  "temperature": 22,
  "condition": "partly cloudy",
  "humidity": 65,
  "wind": "12 km/h NE"
}

The model then uses the real data to write a natural language response for the user.

Final Response
Right now in Tokyo, it's 22°C and partly cloudy. The
humidity is at 65% with a light northeast wind at
12 km/h. A pleasant day!

The flow:

  1. You define tools the model can use
  2. The model decides when to call a tool
  3. Your app executes the function
  4. The result goes back to the model
  5. The model writes a human-readable answer

This pattern powers AI assistants that can book flights, query databases, send emails, and more.

Common function calling use cases:
- Database queries
- API calls (weather, stock prices, etc.)
- Calculations
- File operations
- Sending notifications
- Multi-step workflows (agent patterns)

Tips:
- Write clear function descriptions
- Validate arguments before executing
- Handle errors gracefully
- Limit which functions are available per context