Completing the Tool Use Cycle with GPT-5
Introduction & Overview
In the previous lessons, you learned how to create tool schemas and understand GPT-5's responses when it wants to use tools. You can now recognize when GPT-5 requests tool execution by checking for type: "function_call" items in the output array and extracting the necessary details from function call items. However, knowing what GPT-5 wants to do is only half the story — you still need to actually execute those tools and complete the conversation cycle.
In this lesson, you'll learn how to bridge that gap by executing the functions GPT-5 requests, capturing their results, and sending those results back to GPT-5 in the proper format. By the end of this lesson, you'll have a complete tool execution pipeline that can handle GPT-5's tool requests from start to finish, maintaining proper conversation flow throughout the entire process.
The Complete Tool Execution Flow
Before we dive into the implementation, let's understand the complete workflow we'll be building in this lesson. Here's the step-by-step process that transforms GPT-5 from a simple chatbot into a capable agent:
- Set up the foundation - Create function mappings, tool schemas, and initial conversation messages
- Send the initial request - Make the first API call to GPT-5 using
responses.createwith the user's question and available tools - Detect tool use requests - Filter through the
outputarray to find items withtype: "function_call" - Extract tool information - Pull out the function name, arguments, and call ID from each function call item
- Execute the requested functions - Parse the JSON arguments and use our function mapping to call the actual TypeScript functions with GPT-5's parameters
- Collect and format tool results - Structure all function outputs in the
function_call_outputformat with matchingcall_idvalues - Send results back to GPT-5 - Make a second API call with the complete conversation, including both the original
function_callitems AND their outputs - Display the final response - Show GPT-5's natural language answer that incorporates the tool outputs
This complete cycle enables GPT-5 to seamlessly use tools as part of its reasoning process, transforming raw function outputs into conversational responses that directly answer user questions.
Setting Up the Foundation
As covered in previous lessons, we need to map tool names to functions and prepare our tool schemas. Here's the complete setup that enables tool execution:
The tools object is the key component here — it enables dynamic function lookup when GPT-5 requests tool execution by name.
