Stop Infinite ReAct Loops: Deterministic Cycle Detection in Spring AI with Java 21 Record Patterns

Runaway ReAct agent loops are the single fastest way to blow through a enterprise LLM budget in a single deployment. If you rely on soft system prompt instructions or simple max-message limits to stop infinite agent execution, your production setup is a liability.

Why Most Developers Get This Wrong

Relying on hard iteration caps: Setting max-iterations=10 in your orchestrator just defers the bill spike without solving deterministic tool loops.

Naive string matching: Checking raw LLM text output misses cycles when tool arguments contain dynamic values like timestamps or ephemeral tracing IDs.