A working agent loop is almost embarrassingly short.
In Python it was about 32 lines: call the model, if it asks for a tool you run the tool, append the result, loop. Mine worked on the first try with a fake model that misbehaved on purpose.
Then real work broke six things before any intelligence mattered: a bad temperature param before the first request, stop reasons beyond end_turn, tool errors that crashed the process, no budget so a stuck agent kept spending, no visibility into what the model received, and conversation cost because the full messages list is re-sent every turn.
The model only requests tools. Your code runs them. while True leaves the stop decision to the model. A shell tool needs a human between the model and your files.
Guards on every exit are the product: return tool failures as is_error, cap turns/time/tokens, log each turn, approve shell before it runs. Without that, a stuck agent keeps running until the bill arrives.
That is also why tools like Claude Code look the way they do. The loop was never the hard part.
Friend link (no paywall):
https://pub.towardsai.net/i-built-an-ai-agent-loop-from-scratch-the-loop-was-the-easy-part-c6746fc1b295?sk=018ae98a2215e38afa3292326baa158e


