Should the Agent Loop Continue
Problem Statement
Decide whether a ReAct-style agent loop should take another step, given step and budget guards.
Background
The loop continues only if all hold: the goal is not yet done, the step count is below max_steps, and tokens used are below max_tokens. Any single violation stops the loop.
Your Task
def should_continue(done, steps, tokens, max_steps, max_tokens):
Return True if the loop should continue, else False.
Input Format
- done (bool), steps (int), tokens (int), max_steps (int), max_tokens (int).
Output Format
- A boolean.
Sample
print(should_continue(False, 3, 100, 10, 1000))
Output:
True
Example:
print(should_continue(False, 3, 100, 10, 1000))
True
- Check if the goal is complete: Since
doneisFalse, the conditionnot doneevaluates toTrue, allowing the loop to potentially continue. - Verify the step limit: Compare the current steps against the maximum, calculating 3<10, which is
Truebecause the agent has not exhausted its step budget. - Verify the token limit: Compare the tokens used against the maximum, calculating 100<1000, which is
Truebecause the agent has not exhausted its token budget. - Combine the conditions: The loop continues only if all guards pass, so we evaluate
True and True and True, resulting inTrue. - The final output is True
Constraints:
- Continue only if
not done and steps < max_steps and tokens < max_tokens. - Any failed condition returns False.
1. Background Knowledge
In ReAct (Reasoning + Acting) agent architectures, an LLM alternates between reasoning steps and tool-calling actions. Because each iteration consumes computational resources and API tokens, production systems wrap the loop in guard conditions that enforce hard limits. These guards prevent runaway loops, cost overruns, and infinite recursion when the model fails to converge on a solution.
The three primary guards are:
- Completion flag: A boolean indicating whether the agent has determined the goal is achieved.
- Step budget: A maximum number of reasoning/action iterations allowed.
- Token budget: A maximum number of tokens (input + output) consumed across all iterations.
The loop continues only when all guards are satisfied simultaneously. This is a logical AND of multiple independent constraints. If any single guard is violated, the loop must halt immediately. This pattern is common in any iterative agent framework, including AutoGPT, LangChain agents, and custom ReAct implementations.
2. Algorithm Approach
This is a straightforward boolean guard evaluation problem. The approach is:
- Evaluate each guard condition independently.
- Combine them with logical AND (and in Python).
- Return the combined result.
There is no iteration, recursion, or data structure manipulation. The entire logic reduces to a single boolean expression. The key insight is recognizing that "the loop continues only if all hold" maps directly to a conjunction of conditions.
3. Step-by-Step Strategy
- Identify the completion check: The loop should stop if done is True. So the continuation condition for this guard is not done.
- Identify the step budget check: The loop should stop if steps has reached or exceeded max_steps. So the continuation condition is steps < max_steps.
- Identify the token budget check: The loop should stop if tokens has reached or exceeded max_tokens. So the continuation condition is tokens < max_tokens.
- Combine with AND: The loop continues only if all three conditions are true. In Python, this is a single and chain.
- Return the boolean result: No additional logic is needed.
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
Editor locked
The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.