Schedule icon
List icon
Get icon
Query icon
Script icon
Docker icon
If icon
Toggle icon
Loop icon
Kill icon
Set icon
Fail icon
Log icon
Return icon

AI Agent Looping Flow Pause Or Disable

Switch off a looping agent flow's triggers and kill its running executions through native Kestra tasks, counting only runs since the last applied remediation.

Categories
AI

What it does

This flow stops a looping agent flow instead of only watching it. Once an hour it reads the real recent executions of one configured agent flow with the native executions.Query task, tests them against genuine loop signatures (several failed runs in a row, the same task failing across several runs, or a failure rate above the configured cap), and when a loop is found it switches off that flow's triggers with triggers.Toggle and kills its running executions with executions.Kill, then reports what it did. Switching the triggers off stops the flow firing, but a manual run stays possible, so re-enabling is a human decision after the loop cause is fixed. Runs since the last applied remediation only are counted (the remediation time is stored in KV on every applied remediation), so an hourly run cannot re-flag failures that were already acted on. A detected loop is always reported with its evidence. The schedule trigger passes the target flow identity explicitly, so a scheduled run cannot fail for missing inputs. A run that cannot read the history fails closed instead of reporting a clean bill of health.

Prerequisites

  • A Kestra instance with API access and a KESTRA_API_TOKEN secret in the namespace this flow runs in, holding a token permitted to read executions and flows, switch triggers, and kill executions.
  • A real agent flow to watch. Repoint the schedule trigger inputs and the flow inputs (tenant, Kestra URL, namespace, agent flow id) at your own instance and flow before relying on the defaults, which point at a local instance and an example flow id as a runnable example.
  • Note the blast radius before enabling the schedule: on a LOOP verdict with apply_remediation true this flow switches off the target flow's triggers, so the flow stops firing until a human re-enables it. That is the intended effect, and it is why the loop signatures require repeated failures rather than a single failure. A manual run of the target flow stays possible while its triggers are off, so switching triggers off is not the same as disabling the flow outright. With apply_remediation false (the default) a LOOP verdict only reports, so the flow is safe to run for observation before opting in.

How it works

  1. hourly_loop_remediation (io.kestra.plugin.core.trigger.Schedule) fires hourly and passes tenant, Kestra URL, namespace, and agent flow id explicitly as trigger inputs, so a scheduled run carries the target identity without depending on manual input.
  2. check_target_flow (io.kestra.plugin.kestra.flows.List) lists the flows in the namespace as a pre-check, so a typo in the target flow id fails closed instead of reading as a clean history.
  3. read_last_remediation (io.kestra.plugin.core.kv.Get) reads the timestamp of the last applied remediation from KV, under a key carrying the target flow id so two watched flows never share a floor. Runs at or before that timestamp are excluded from the loop signatures, so the hourly re-flagging defect is fixed at the source.
  4. query_target_history (io.kestra.plugin.kestra.executions.Query) reads recent executions of exactly the target flow, filtered by namespace and flow id and limited to the lookback window, with FETCH returning the light rows in the task output.
  5. evaluate_loop (io.kestra.plugin.scripts.python.Script) tests the three loop signatures. Light rows carry no taskRunList and no startDate, so rows are re-sorted by state history date in evaluation rather than trusting search order, and the repeated-task signature fetches the full execution for FAILED ids only. An unreadable history, a missing target flow, rows with no datable state history, or a failed FAILED-id detail fetch yields INCONCLUSIVE, and it also collects the ids of executions still in RUNNING, CREATED, or QUEUED state.
  6. remediate (io.kestra.plugin.core.flow.If) acts only on a LOOP verdict with apply_remediation true. It switches off every trigger of the target flow with triggers.Toggle (no trigger id means every trigger of the flow), kills each still running execution with executions.Kill in a Loop, and only then records the remediation time with kv.Set. If any step fails, the run fails outright, so it never claims a remediation that did not happen. On any other verdict, or on a LOOP verdict with apply_remediation false, it makes no call at all.
  7. triage (io.kestra.plugin.core.flow.If) fails the execution itself (io.kestra.plugin.core.execution.Fail) on an INCONCLUSIVE verdict, and otherwise logs the verdict, counts, and remediation action.
  8. deliver (io.kestra.plugin.core.debug.Return) reports the verdict and the remediation outcome together.

How to adapt it

  • Tune the loop signatures per flow: a flow that fails legitimately in bursts needs a higher consecutive cap or a higher rate cap, while a flow that must never fail twice in a row wants the consecutive cap at 2.
  • Add a notification task on the LOOP branch (Slack, PagerDuty, or email) carrying the loop evidence and the switched-off flow id, so the owning team learns the triggers were switched off from the alert, not from a missing run.
  • If your Kestra setup uses a tenant other than main, set tenant in both the trigger inputs and the flow inputs: the API paths carry the tenant segment, and a wrong tenant reads the wrong history.
  • Keep a manual re-enable runbook next to this flow: re-enabling is a human decision after the loop cause is fixed, and this flow deliberately provides no auto re-enable path.
  • To exercise this flow against a real instance, run Kestra locally with docker run --rm -p 8080:8080 kestra/kestra:latest server local, create an agent flow that fails repeatedly, and point the trigger inputs at it with apply_remediation true.
See How

New to Kestra?

Use blueprints to kickstart your first workflows.