Tool Calls

The API supports OpenAI-compatible function calling. Define your functions in the request; the model decides when to call them, and the server returns the call details for you to execute on the client

Step 1 — Define tools and get a tool call

import OpenAI from 'openai';

const client = new OpenAI({
  apiKey: process.env.API_KEY,
  baseURL: 'http://localhost:9000/v1',
});

const completion = await client.chat.completions.create({
  model: 'openai/gpt-4o',
  messages: [
    { role: 'user', content: 'What is the weather in Tokyo?' }
  ],
  tools: [
    {
      type: 'function',
      function: {
        name: 'get_weather',
        description: 'Get the current weather for a city',
        parameters: {
          type: 'object',
          properties: {
            location: {
              type: 'string',
              description: 'City name, e.g. Tokyo, Japan'
            },
            unit: {
              type: 'string',
              enum: ['celsius', 'fahrenheit'],
              description: 'Temperature unit'
            }
          },
          required: ['location']
        }
      }
    }
  ],
  tool_choice: 'auto',
  built_in_tools: 'none'  // disable server tools so only yours are offered
});

console.log(completion.choices[0].message.tool_calls);

Response — model decides to call your function

Step 2 — Execute the function and send the result back

Parse arguments, call your function, then continue the conversation with a tool message:

Controlling tool selection

tool_choice value

Behaviour

"auto" (default)

Model decides whether to call a tool or respond directly

"none"

Model must not call any tool

"required"

Model must call at least one tool

{ "type": "function", "function": { "name": "fn" } }

Model must call this exact function

Multi-tool call in one turn

The model can call multiple tools at once:

Send all tool results back in one follow-up request, one tool message per call:

Last updated