Skip to course content
Free LangChain course

LangChain for LLM Applications and RAG

Unit 07.02: Stopping conditions for a simple agent

An agent is a loop. Every loop needs a way out, and one condition is not enough.

Four conditions, each catching a different shape

Step ceiling, token ceiling, time ceiling, and a repeat detector on identical calls.

The code runs a loop until something stops it.

MAX_STEPS = 5
MAX_TOKENS = 10_000
MAX_SECONDS = 30

steps, tokens, seconds = 0, 0, 0.0
history = []

while True:
    steps += 1
    tokens += 2_400
    seconds += 4.5
    history.append(("read_account", "ACC-1187"))

    stop = None
    if steps >= MAX_STEPS:
        stop = f"step ceiling ({MAX_STEPS})"
    elif tokens >= MAX_TOKENS:
        stop = f"token ceiling ({MAX_TOKENS:,})"
    elif seconds >= MAX_SECONDS:
        stop = f"time ceiling ({MAX_SECONDS}s)"
    elif history.count(history[-1]) >= 3:
        stop = f"repeated call {history[-1]} three times"

    print(f"step {steps}: tokens={tokens:>6,} seconds={seconds:>5.1f}"
          f"{'  STOP -- ' + stop if stop else ''}")
    if stop:
        break

# Four independent conditions, because each catches a shape the others miss. The
# repeat detector fires here before any ceiling does, which on a tight loop is
# two paid calls saved.

The repeat detector fires before any ceiling does, which on a tight loop is two paid calls saved. It catches the shape where an agent calls the same tool with the same arguments and expects a different answer.

The ceilings catch the other shapes: slow drift that never repeats exactly, an agent making expensive calls rather than many of them, and one that is simply slow. Each is a different failure and each needs its own bound.

The mistake this prevents

The mistake is a step ceiling alone because it is simplest. An agent making five very large calls stays under a step ceiling of ten and can cost more than fifty small ones. Bound tokens and time as well as steps.

Takeaway

Bound steps, tokens, time and repeated calls. Each catches a failure shape the others miss, and the repeat detector usually fires first.