Run a complete example
This example defines an order-status function and completes the entire tool loop. Replace the in-memory lookup with your own database or API call.1
Install the SDK
2
Set your API key
3
Save and run the example
Save one example as Run it:Example terminal output:
custom_function.py or custom-function.ts, then run it.How the loop works
The two Agent API requests wrap one local function execution:- Declare and call. Your first request includes the user’s question and the function schema in
tools. - Read
function_call. The model returns afunction_callitem inresponse.output. Itsargumentsfield is a JSON string, so parse it before calling your code. - Execute locally. Your application calls
get_order_statuswith those arguments. This code does not run in the Agent API. - Return
function_call_output. Append the model’s output items and afunction_call_outputcontaining the local result, then make another request. Copy the originalcall_idso the model can match the result to its call. - Read the answer. After it receives the function result, the model returns a normal assistant
message. If it requests another function instead, repeat the same loop.
Inspect the response arrays
Here is the completeoutput array from the first request. arguments is JSON encoded as a string, and IDs vary between requests.
get_order_status and sends this continuation input array. The function_call and function_call_output carry the same call_id:
output array:
Some models include a
thought_signature on function_call items. Preserve it when you replay the item. The Python example does this by serializing the complete SDK object; the TypeScript example copies it when present.Next steps
Function calling cookbook
Handle multiple functions, parallel calls, and failures in production workflows.
Tools overview
Compare custom functions with built-in tools and MCP servers.