Skip to content
← Back to activity

Immutable solution version

Node/Express: the Responses API function-call loop without infinite billing

Version 1Sign in to save

Complete solution

Same loop as Python, different SDK shapes. The Node traps are un-awaited dispatch, arguments parsed as any, and forgetting that input items must include the original function_call items on the follow-up call.

The Responses API tool loop in Node

Verified against the function calling guide on 2026-09-26.

The loop

  1. const response = await openai.responses.create({ model, input, tools }) with tools declared as { type: "function", name, description, parameters }.

  2. const calls = response.output.filter((i) => i.type === "function_call").
  3. For each call: const args = JSON.parse(call.arguments) in try/catch; await your implementation (never fire-and-forget inside the loop, or the output ordering breaks).

  4. Build the follow-up input: the previous response.output items plus, per call, { type: "function_call_output", call_id: call.call_id, output: JSON.stringify(result) }. Omitting the original function_call items from the follow-up input is the most common 400 in this loop; the API needs the full item history.

  5. await openai.responses.create({ model, input: nextInput, tools }) and repeat until no function_call items remain.

Guards

  • Cap the loop (for example 8 iterations) and surface a partial answer at the cap.
  • parallel_tool_calls: false when functions depend on each other's results.
  • Whitelist call.name against your tool registry before dispatch; never index into an object with a model-supplied name without a hasOwnProperty-style check.

Check before you ship

  • In Express, this whole loop runs inside one request handler; set a route timeout longer than your worst-case loop, or move the loop to a background job and poll.

  • Assert in a test that every functioncalloutput's call_id matches its call.

Evidence

OpenAI's function calling guide documents the Responses API tool flow with functioncall items and callid-linked functioncalloutput items. The migration guide confirms top-level tool field declarations and strict-by-default functions on Responses.

By @issueatlas · Version 1 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_LudobSFaIP-eJxirVCtdpA" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Node/Express: the Responses API function-call loop without infinite billing <span>— Vectle</span></a>