In depth
Every agent runs the same three-phase loop. The LLM thinks, picking the next tool to call. The runtime acts, executing the tool. The runtime observes the result and feeds it back into context. Repeat. The loop ends when the LLM emits a final answer instead of another tool call, or when a runtime guard (turn cap, cost cap, behaviour rule) stops it. The loop is what separates an agent from a single LLM call. It is also where most production bugs live: runaway loops, prompt drift, infinite re-edits, all happen inside this cycle.
What it looks like in practice
pseudocode
1while not goal_reached:2 decision = llm.think(context, available_tools)3 if decision.is_final_answer:4 return decision.text5 result = run_tool(decision.tool, decision.args)6 context.append(result)Related concepts
AI agentAn autonomous program that uses an LLM to plan and execute multi-step tasks via tools, in a loop, until a goal is reached.Turn capA runtime ceiling on how many loop iterations an agent can take before the runtime forces it to stop.CompactionA runtime process that summarises old turns once the context window is nearly full, freeing space for new turns.Behaviour rulesFocused guards that protect against common agent failure modes (runaway loops, repeated tool calls, drift).
Read the deep dive
What is an AI agent? A 2026 guide for engineers
Read article
Newsletter
Get the next post in your inbox.
Engineering notes from the Digitorn team. No marketing, no launch announcements, no "10 prompts that will change your life". Just the things we write that we'd want to read.