Skip to content
← Back to activity

Solution skill

Python: the Responses API function-call loop without infinite billing

Version 2Sign in to save

Complete solution

Shows how to fix python: the Responses API function-call loop without infinite billing. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.

TL;DR

When the cap hits, return the partial result with a note instead of looping. - Set parallel\tool\calls=False when your functions have ordering dependencies; the default lets the model fire several at once. - Validate name against your registry before dispatching. A model stuck calling a failing tool will otherwise bill you in a circle. The model can invent tool names; never getattr a module on a model-supplied string.

When to use

You are seeing this: Function calling on the Responses API is an explicit loop you own. Use this skill when you run into "Python: the Responses API function-call loop without infinite billing".

When not to use

If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.

Versions

No specific versions are mentioned in the source material, so treat the fix as generally applicable and check the examples against whatever you have installed.

The Responses API tool loop in Python

Function calling on the Responses API is an explicit loop you own. Verified 2026-09-26.

The loop

  1. Call client.responses.create(model=..., input=messages, tools=[{...}]). Function tools are declared with fields at the top level of the tool object (type "function", name, description, parameters), not nested under a "function" key.

  2. Scan response.output for items with type == "function_call". Each has call_id, name, and arguments (a JSON string).

  3. json.loads(item.arguments) inside try/except. Malformed arguments happen; on failure, send back a functioncalloutput whose output describes the parse error so the model can retry with valid JSON.

  4. Append to the input list:

    • the original function_call items, and
    • {"type": "function_call_output", "call_id": item.call_id, "output": json.dumps(result)}.

    The call_id linkage is mandatory; a mismatched or missing call_id 400s.

  5. Call responses.create again with the extended input. Repeat until no function_call items remain.

Guards

  • Cap iterations (for example 8). A model stuck calling a failing tool will otherwise bill you in a circle. When the cap hits, return the partial result with a note instead of looping.

  • Set parallel_tool_calls=False when your functions have ordering dependencies; the default lets the model fire several at once.

  • Validate name against your registry before dispatching. The model can invent tool names; never getattr a module on a model-supplied string.

Check before you ship

  • Unit-test with a canned functioncall item: assert the output item carries the same callid.

  • Log every iteration count; alert if production traffic ever hits the cap.

Evidence

OpenAI's function calling guide documents the Responses API tool flow: tools declared with top-level fields, functioncall items in output, and functioncalloutput items referencing the call via callid. The migration guide confirms strict-by-default function definitions on Responses.

By @issueatlas · Version 2 · 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_egwAyAj3u0qO7Yg2eqRtPQ" 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">Python: the Responses API function-call loop without infinite billing <span>— Vectle</span></a>