Skip to main content
Tool calling allows models to call external functions or APIs, enabling dynamic, structured workflows inside chat interactions.

βš™οΈ How It Works

  1. Define Tools β€” Provide a list of available tools with names, descriptions, and parameters.
  2. Start Chat β€” Send user messages as usual.
  3. Receive Tool Call β€” The model may decide to call a tool based on context.
  4. Execute Tool β€” Your backend runs the function and returns the result.
  5. Continue Chat β€” Pass the result back into the conversation for further interaction.

πŸ—‚οΈ Tool Definition Example

{
  "tools": [
    {
      "type": "function",
      "function": {
        "name": "getWeather",
        "description": "Get the current weather for a city.",
        "parameters": {
          "type": "object",
          "properties": {
            "location": {
              "type": "string",
              "description": "City and country, e.g., 'Paris, France'"
            }
          },
          "required": ["location"]
        }
      }
    }
  ]
}
πŸ“€ Request Example
{
  "model": "openai/gpt-4o",
  "messages": [
    { "role": "user", "content": "What's the weather in Tokyo?" }
  ],
  "tools": [ ... ],
  "tool_choice": "auto"
}
Field Type Required Description tools object[] βœ… Array of tool definitions tool_choice string βœ… β€œauto” lets the model decide when to invoke tools πŸ“¨ Tool Call Response Example
{
  "choices": [
    {
      "message": {
        "role": "assistant",
        "content": null,
        "tool_calls": [
          {
            "id": "call-abc",
            "type": "function",
            "function": {
              "name": "getWeather",
              "arguments": "{\"location\":\"Tokyo, Japan\"}"
            }
          }
        ]
      }
    }
  ]
}
πŸ“Œ Note: You need to parse arguments and execute the function. πŸ” Returning Tool Result
{
  "role": "tool",
  "tool_call_id": "call-abc",
  "content": "{\"temperature\":\"28Β°C\",\"condition\":\"Sunny\"}"
}
The model will continue responding based on the tool’s output. βœ… Best Practices β€’ Define clear descriptions for tools to guide the model. β€’ Use JSON Schema for input validation. β€’ Handle errors gracefully and provide fallback messages if needed. β€’ Log all tool calls for monitoring and debugging.