Skip to main content
Function calling bridges the gap between language models and real-world systems. Instead of only generating text, the model can signal that it wants to invoke a function you define — looking up a database, calling an external API, or running a calculation. Your code executes the function and returns the result, and the model incorporates that result into its final response. This is the foundational pattern for building AI agents.

How It Works

You include a tools array in your request describing the functions available to the model. If the model determines that a function call is the best way to answer the user’s request, it returns a response with finish_reason: "tool_calls" and a tool_calls array instead of a plain text reply. You then execute the requested function with the provided arguments, send the result back as a tool role message, and make one more API call to get the model’s final answer.

Step-by-Step

Controlling Tool Use with tool_choice

The tool_choice parameter lets you override the model’s default behavior. Use "none" when you want to ask the model a follow-up question without triggering a tool call. Use a specific function name when the user action unambiguously maps to a single function and you want to skip the model’s decision step.

Parallel Function Calls

The model can request multiple function calls in a single turn by returning more than one entry in the tool_calls array. Execute all requested functions in parallel, then send all results back in one follow-up request — one tool message per call, each referencing its corresponding tool_call_id. This significantly reduces round-trip latency for agents that need to gather several pieces of information at once.