## Definition The **agentic loop** is the runtime control flow of an [[AI Agent]]: a repeating cycle of perceive → reason → act → observe, terminating when a goal is reached, a step budget is exhausted, or a safety condition fires. ## Canonical Shape ``` while not done: observation = environment.observe() plan = llm.reason(history, observation, tools) action = plan.next_action # tool call or "answer" result = environment.act(action) history.append(observation, plan, action, result) done = is_goal_met(history) or step_count > budget ``` ## Termination Conditions Every loop needs **explicit, observable** termination: - **Goal achieved.** The model produces a final answer instead of a tool call. - **Step budget exhausted.** Cap on tool-call rounds (e.g., `--max-turns` in Claude Code). - **Time budget exhausted.** Wall-clock cap. - **Safety condition fired.** A hook or policy detected a forbidden state. - **No progress detected.** Same actions repeating; loop detector trips. Loops without all of these eventually misbehave in production. ## State Across Iterations The history accumulates fast. Three pressure points: 1. **Tool-output bloat.** Long tool outputs (file dumps, log tails) blow the context window. Mitigate by *summarising* tool outputs before re-injecting, or by isolating noisy tool use in a sub-agent. 2. **Conversation length.** Trigger [[Context Compaction]] sooner than it triggers itself. 3. **Drift.** Restate the goal periodically; or use a checkpoint file the loop reads each iteration. ## ReAct as the Inner Step Each iteration of the loop is typically a [[ReAct Pattern]] cycle — Thought → Action → Observation. ReAct is *what* happens in one step; the agentic loop is the *outer* repetition. ## Anti-patterns - **No step budget.** "I'll just let it run until done." Will run until your bill blows up. - **No progress signal.** Loop detector that only checks for *identical* actions misses semantically-identical-but-textually-different repeats. - **Goal as a string the model can paraphrase.** The model rewrites the goal more loosely each turn; lock it in a file or in the system prompt. ## Related - [[AI Agent]] - [[ReAct Pattern]] - [[Tool Use]] - [[Reflection]]