$ lexprog.com

// notes from an old coder -- php, databases, and the occasional rant

[November 04, 2026] AI

Cap Agent Loops Explicitly

Set a hard max on tool rounds per user request. A confused agent will otherwise call search forever.

────────────────────────────────────────────────────────

Cap Agent Loops Explicitly

Tip: Infinite tool loops are a billing incident

Set a hard max on tool rounds per user request. A confused agent will otherwise call search forever.

for step in range(MAX_STEPS):
    msg = model.step(messages)
    if not msg.tool_calls:
        break

Gotcha: Retries plus loops multiply cost

A transport retry that restarts the whole agent run from step 0 doubles spend. Checkpoint after each successful tool.

run_agent(task)  # called again on HTTP 502

Source: https://platform.openai.com/docs/guides/function-calling

────────────────────────────────────────────────────────
<-- back to posts