How do you control loops in an agent graph?
You control loops in an agent graph by defining when the workflow may repeat, what changes on each iteration, and exactly when it must exit. The loop should be governed by explicit state and routing rules rather than an open-ended instruction to “keep trying.”
Example: Fixing generated API tests
An agent generates tests, validates coverage, and revises only missing cases:
Generate tests → Validate → Decide
- Validation passed: Exit to the report node.
- Fixable gap and attempts remain: Revise the tests, increment the attempt counter, and return to validation.
- No progress, unsafe retry, or limit reached: Exit with a clear failure or request human input.
Suppose validation discovers that a required 401 Unauthorized test is missing. One revision adds the case. If validation passes afterward, the loop stops; the agent does not revise the test suite again.
Six practical controls
- Track iteration state. Store the attempt count, latest validation result, remaining gaps, and previous error. Do not rely on conversation history alone for operational counters.
- Use explicit routing conditions. Choose between continue, success, and stop/escalate from validated state, not unrestricted model-generated commands.
- Set a hard iteration limit. For example, permit at most three revisions for a particular task. The suitable value depends on its cost and risk.
- Enforce resource budgets. Apply deadlines, token or tool-call limits, and cancellation rules in addition to the attempt counter.
- Detect stalled progress. Stop if repeated iterations return the same error, produce unchanged results, or fail to reduce measurable gaps.
- Protect side effects. Before retrying a payment, notification, or database mutation, check whether the previous attempt succeeded. Use idempotency keys or reconciliation when supported.
What does a graph router check?
A conceptual routing rule is:
- If all acceptance criteria pass, go to END.
- Otherwise, if attempts are below the limit, a meaningful fix is available, and budgets remain, go to REVISE.
- Otherwise go to STOP_OR_ESCALATE.
For frameworks such as LangGraph, the iteration counter and status can be stored in graph state, with a conditional edge choosing the next node. Runtime recursion or step limits provide an additional guardrail, but are not substitutes for business-level success and failure conditions.
Common mistakes
- Incrementing the counter only after a successful retry, so failures never consume the budget.
- Repeating an LLM call without new feedback or changed inputs.
- Checking only the maximum iteration count while ignoring wall-clock and API-cost limits.
- Treating a timed-out external action as safe to repeat automatically.
- Exiting after a fixed number of tries without reporting whether the goal was achieved.
In short
A controlled agent loop has a measurable goal, bounded retries, progress checks, safe state transitions, and a reliable exit path. These controls make iterative workflows useful without allowing infinite execution or accidental duplicate actions.
Learning Sequence
AI Agents Curriculum
Explore Related Questions
Deepen your understanding across related AI Agents topics.