How to add retry system in flow as we do using the code

I have a production n8n workflow where journal entries are generated using an LLM. I validate the output using an IF node to check whether the total debit and credit amounts are balanced.

However, sometimes the LLM misses an entry, which causes an imbalance.

What I want to achieve:

  • If the entries are unbalanced, I want to retry the workflow from a specific node (not the entire workflow).

  • This retry should happen only once.

  • If the second attempt still results in an imbalance, the workflow should continue as it currently does (no further retries).

My challenges:

  1. How can I track and limit retries to only one attempt?

Any suggestions or best practices for implementing this kind of retry control in n8n would be helpful.

Hi @NISHANT_GARG
You can use the general {{ $runIndex }} to track retries, and just connect your IF node false branch basically unbalanced output back to the LLM to retry, and there add a second IF statement to check the {{ $runIndex }} to limit retries to one attempt, something like if $runIndex < 1 , loop back otherwise, continue the workflow as normal.

the $runIndex approach works but honestly i’d do it a bit differently — i’ve had issues with $runIndex being unreliable in loops depending on how the workflow is structured.
what works well for me: use a Set node before your LLM call that sets something like retryCount = 0. then after your IF node (the one checking debit/credit balance), on the false branch — add another IF that checks {{ $json.retryCount }} < 1. if true, use a Set node to bump retryCount to 1 and loop back to the LLM node. if false, just continue with your normal “unbalanced” flow.
this way you’re explicitly tracking the retry yourself instead of relying on execution internals. easier to debug too because you can see the retryCount value in each execution step.
one more thing — if the LLM keeps producing unbalanced entries, it’s probably worth tweaking your prompt rather than adding more retries. i’ve found that including a concrete example of a balanced journal entry in the prompt and adding something like “verify that total debits equal total credits before responding” cuts the error rate way down. cheaper than burning a second LLM call every time.

Great point, thanks.

@NISHANT_GARG forget the loop, just chain a second LLM node off the IF false branch — no counter needed, retry is limited to one by design since there’s nothing to loop back to. feed it the actual delta so it knows what to fix:

swap the HTTP Request for whatever LLM node you’re actually using, plug in your creds and real prompt template with the expression fields adjusted to match your output field names.