For the complete documentation index, see llms.txt. Markdown versions of documentation pages are available by appending .md to the page URL.
Primary navigation

Functions

Define functions, handle pending calls, and return results.

Function tools let an agent call your application code. You define the function and its arguments. The agent requests a call, your code returns a result, and the harness continues the turn.

Your handler can run in an application server, a worker, or an environment you control. Attaching an environment to a session does not automatically run function tools there.

If you use function calling in the Responses API, you can reuse your function implementation with the session flow described here.

Define a function

Add a function definition to agent.tools when you configure the agent. Give it a name, a description, and a JSON Schema for its arguments:

{
  "type": "function",
  "name": "get_customer",
  "description": "Look up a customer by ID.",
  "parameters": {
    "type": "object",
    "properties": { "customer_id": { "type": "string" } },
    "required": ["customer_id"],
    "additionalProperties": false
  }
}

Handle required actions

When the agent needs a function result, the session emits agent.session.requires_action. Read the pending calls from event.session.required_actions. You can also retrieve the session and read session.required_actions without streaming.

The Node SDK can handle calls for you. When you register toolHandlers on a creation or follow-up stream, the helper dispatches each matching function_call from agent.session.turn.item.added as you consume the stream. Let the helper submit those results. Running the same function again on agent.session.requires_action can duplicate side effects.

For calls you handle manually, set an application deadline. A pending function action has no result deadline field. After a delay or disconnect, retrieve the session and check that the call is still pending before submitting a result. If you can’t complete it, return an error or cancel the turn.

A function entry in required_actions looks like this:

{
  "type": "function_call",
  "turn_id": "turn_123",
  "call_id": "call_123",
  "name": "get_customer",
  "arguments": { "customer_id": "123" }
}

Run the named function with the supplied arguments. Use required_actions to decide which calls need results; a function_call item in session history alone does not establish that a result is pending.

Return the result

Send agent.session.input.tool_result to the session events endpoint. Copy turn_id and call_id from the pending action:

  • For success, set success: true and supply output as a string or a supported content array. Serialize JSON objects to strings.
  • For an error, set success: false and supply an error message that the agent can use.

For each pending get_customer call, run your lookup and return its result. Here, action is the entry from required_actions:

Return a function result
const result = {
  turn_id: action.turn_id,
  call_id: action.call_id,
};
let outcome;

outcome = {
  success: true,
  output: JSON.stringify(getCustomer(action.arguments)),
};

await client.beta.agents.sessions.events.create(sessionId, {
  events: [
    { type: "agent.session.input.tool_result", ...result, ...outcome },
  ],
});

The harness continues the turn after it receives the required results. Follow session events and items to check the turn’s outcome and retrieve its output.

Result size

If the API rejects a tool result as too large, reduce its size to less than 4 MiB (4,194,304 bytes), leaving a little room for Agents API metadata. Retry with the same turn_id and call_id while the call is still pending.

Recover after a disconnect

Retrieve the session to find pending actions. If you already ran a function, submit its saved result with the same turn_id and call_id.

For functions with side effects, store results durably by session, turn, and call ID. If execution might have succeeded but no result was saved, check the outcome before running the function again.

Load functions on demand

Functions load eagerly by default. To defer a function, set defer_loading: true on its definition and include { "type": "tool_search" } in agent.tools. See Tool search for a complete example.