Agent loop
The agent loop is the cycle at the heart of every agent: send the conversation to the model, and if it asks for tools, run them, append the results and send again.
It ends when the model answers in plain text or a limit is hit. The message list is the whole state of the run.
Because the state is just a list, you can log it, trim it, save it and replay it. Frameworks add features around the loop, but the loop itself fits in about thirty lines.
Common mistake. Running the loop without a step limit. A confused model will keep calling tools and keep billing you.
In practice. Cap steps, tokens and wall-clock time in code, detect repeated identical calls, and test the loop with a scripted model that replays recorded replies.