Function Calling

Let models invoke structured actions in your application.

Overview

Function calling (also called "tool use") lets models invoke structured actions in your application. Describe your functions and the model responds with a JSON call when it decides to use one — enabling interaction with external systems, databases, and APIs.

Defining tools

Pass a tools array in your request with JSON Schema definitions:

TypeScript
1const completion = await client.chat.completions.create({
2 model: 'gpt-4o',
3 messages: [{ role: 'user', content: 'What is the weather in Tokyo?' }],
4 tools: [
5 {
6 type: 'function',
7 function: {
8 name: 'get_weather',
9 description: 'Get current weather for a location',
10 parameters: {
11 type: 'object',
12 properties: {
13 location: { type: 'string', description: 'City name' },
14 unit: { type: 'string', enum: ['celsius', 'fahrenheit'] },
15 },
16 required: ['location'],
17 },
18 },
19 },
20 ],
21});

Handling tool calls

If the model decides to call a function, the response includes a tool_calls array instead of regular content. Execute the function and send the result back:

TypeScript
1const toolCall = completion.choices[0].message.tool_calls[0];
2const args = JSON.parse(toolCall.function.arguments);
3
4// Execute your function
5const weather = await getWeather(args.location, args.unit);
6
7// Send the result back
8const followUp = await client.chat.completions.create({
9 model: 'gpt-4o',
10 messages: [
11 { role: 'user', content: 'What is the weather in Tokyo?' },
12 completion.choices[0].message,
13 {
14 role: 'tool',
15 tool_call_id: toolCall.id,
16 content: JSON.stringify(weather),
17 },
18 ],
19});

Supported models

Function calling is supported by models with the "Function Calling" capability, including GPT-4o, Claude Sonnet/Opus, and Gemini models. Check the Models page and filter by "Tools" capability.