Skip to content
echogeco
Tools and the agent loop · Loops and context

The loop that ran for 400 turns

Find what is wrong with an agent loop that polled a deployment for nearly 400 turns, cost 1,600 times a normal run, and ended only when the context filled.

critiqueeasyExercise · 3 min · 18 of 77
On this page

A deployment agent ran for nearly 400 turns on a task that normally takes six. Nobody noticed until the invoice did.

The situation

A team's release agent has eight tools, including trigger_deploy(service) and get_deploy_status(deploy_id), and a system prompt and tool set of 3,000 tokens. A normal run is six turns: trigger, poll twice, read the log, post a summary, stop. Last Thursday the deploy backend hung and get_deploy_status returned the same 2 KB status object, {"state": "pending", …}, for an hour. The agent called it again, and again, with the same argument, each call and its result adding about 500 tokens to the history. The run ended on turn 395, when the send filled the window and the API rejected it. The loop's code is:

while True:
    reply = model(messages)
    if reply.stop_reason == "end_turn":
        return reply.text
    messages += run_tools(reply.calls)

The trace for the run records the final answer (none) and the total tokens. Assume a 200k-token window, about four characters per token, P_in = $3 per million tokens and P_out = $15 per million; a normal run reads about 25,000 input tokens.

The question

Say what is wrong with this loop and its trace, and what the team ships instead, with the cost of Thursday's run worked out.

A strong answer · Rubric · Where answers go wrong

Write your answer first, then open