Skip to content

Tool Calling in Detail

Summary

Tool calling is the ability of a model to return a structured command rather than just writing text: which tool to call and with what arguments.

What is Tool Calling

When a model understands that an external tool is needed to answer a question, it does not execute the tool itself. Instead, it returns a special object:

{
  "name": "get_weather",
  "arguments": {
    "city": "Moscow"
  }
}

Your code executes this function and sends the result back to the model.

How It Works

sequenceDiagram
    participant U as User
    participant L as LLM
    participant C as Code
    participant T as Tool

    U->>L: What is the weather in Moscow?
    L->>C: tool_call: get_weather({city: "Moscow"})
    C->>T: Execute weather API request
    T-->>C: +12 °C
    C->>L: Result: +12 °C
    L->>U: It is currently +12 °C in Moscow

Step 1. Describe the Tools

The model needs to know which functions are available. The description is usually passed in JSON Schema format:

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

Step 2. The Model Decides What to Call

On the question "What is the weather in Moscow?", the model understands:

  • the tool get_weather is needed;
  • the argument city is `