Agent loop
An agent loop repeatedly observes state, chooses a permitted action, receives a result and decides whether to stop. The application enforces budgets and permissions.
Before you start: Review Workflow vs agent first. Python-like examples use variables, lists and functions.
Follow the mechanism
Observe
Read explicit task state and actual results.
Start with an everyday explanation
A helper checks the task list, does one allowed errand and reports back before choosing another. They cannot keep spending forever.
See a concrete example
A three-call budget allows lookup, policy retrieval and answer preparation. A fourth proposed lookup must stop or request a new budget.
A claim to question
Think-act-observe does not imply unrestricted execution or guaranteed completion.
Try before revealing the answer
Use the agent simulator to advance until the budget is exhausted, then reset and finish with sufficient evidence.
Show the worked answer
The controller must stop at the enforced limit and expose why. It should not fabricate success or silently raise its own budget.
Now explain the idea to someone else using your own example. If you cannot explain a stage, revisit it before moving on.
Engineering details and trade-offs
Represent state explicitly and distinguish proposed actions from completed ones. Stop on success, blocked authorization, repeated failure, deadline or budget.
Validate every tool call and record results. Bound retries and handle cancellation so a loop cannot silently continue after the user stops it.
Reason through the example
A three-call budget allows lookup, policy retrieval and answer preparation. A fourth proposed lookup must stop or request a new budget.
Implement and test
Use the agent simulator to advance until the budget is exhausted, then reset and finish with sufficient evidence.
Inspect the reasoning and expected result
The controller must stop at the enforced limit and expose why. It should not fabricate success or silently raise its own budget.
What to measure
State the expected result before changing a control. Record your inputs, compare the observed result and explain one limit of this model. In a real implementation, test the failure case as well as the normal path.
Answer before reading the reference
How do you prevent an agent from looping?
A defensible answer
Set step, time and token budgets; detect repeated states; use typed errors and bounded retries; define terminal outcomes. Evaluate incomplete runs and make cancellation and recovery explicit.
Prepare the follow-up
How would you safely resume after a process crash?
Show follow-up reasoning
Persist explicit state, completed tool results and idempotency identifiers. On resume, reconcile uncertain side effects before replaying them, revalidate authorization and expiry, and continue only within the remaining approved budget. A text summary alone is insufficient for transactional recovery.
Use the engineering explanation and worked task above to support your response. Identify which assumptions you would need to verify in production.
Choose a practice depth
- New to GenAI
- Define agent loop with the everyday example and name its key limitation.
- Applied experience
- Explain the example, implementation choice and a measured failure case. Use the exercise as a small design review.
- Senior or systems depth
- Answer the follow-up, compare alternatives and justify an evaluation or rollout plan with operational constraints.
Self-review your answer
0/4 checks selected. This is self-assessment, not an interview score.
Your explanation and revision notes
Loading local notes…