Skip to main content
Custom functions let an agent use code you control, such as a database query, an internal API, or business logic. You describe the function in the request, but the Agent API never executes it for you. Your application handles the call and returns the result.

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 custom_function.py or custom-function.ts, then run it.
Run it:
Example terminal output:

How the loop works

The two Agent API requests wrap one local function execution:
  1. Declare and call. Your first request includes the user’s question and the function schema in tools.
  2. Read function_call. The model returns a function_call item in response.output. Its arguments field is a JSON string, so parse it before calling your code.
  3. Execute locally. Your application calls get_order_status with those arguments. This code does not run in the Agent API.
  4. Return function_call_output. Append the model’s output items and a function_call_output containing the local result, then make another request. Copy the original call_id so the model can match the result to its call.
  5. 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 complete output array from the first request. arguments is JSON encoded as a string, and IDs vary between requests.
The application executes get_order_status and sends this continuation input array. The function_call and function_call_output carry the same call_id:
The second request then returns this complete 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.