Skip to content

While-loop iteration reference ​

This page lists every field and option. The overview shows what it does and a first example.

The loop block gives conditional iteration with no fixed limit. This makes kdeps workflows Turing complete.

Set on a resource.

items iterates over a fixed list. loop repeats a resource body when an expression is true. Without while:, it repeats a fixed number of times. It has full set()/get() state. To pause for a fixed duration between iterations, add every:. This makes the loop a scheduled task.

Basic usage ​

yaml
# resources/count.yaml

actionId: countToFive
name: Count to Five
flow:
  loop:
    while: "loop.index() < 5"
    maxIterations: 1000   # safety cap (default: 1000)
after:
  - "{{ set('result', loop.count()) }}"
apiResponse:
  success: true
  response:
    count: "{{ get('result') }}"

loop.index() < 5 runs the body for index 0 - 4, producing 5 iterations. Each iteration's apiResponse becomes one element of the streaming response.

Loop context ​

Inside the loop body, special callables are available:

CallableDescription
loop.index()Current iteration index (0-based)
loop.count()Current iteration count (1-based)
loop.results()Results accumulated from all prior iterations

Method syntax ​

yaml
# resources/example.yaml
after:
  - "{{ set('idx', loop.index()) }}"
  - "{{ set('cnt', loop.count()) }}"
  - "{{ set('prev', loop.results()) }}"

Comparison: loop vs item ​

LoopItems equivalentDescription
loop.index()item.index()Current index (0-based)
loop.count()item.count()Current count (1-based)
loop.results()item.values()All prior results
set('key', val, 'loop')set('key', val, 'item')Loop-scoped storage
get('key', 'loop')get('key', 'item')Read loop-scoped value

Loop-scoped storage ​

To keep variables in the loop context, use 'loop' as a storage type hint. This is the same as the 'item' type for items iteration:

yaml
# resources/example.yaml
loop:
  while: "default(get('step', 'loop'), 0) < 3"
  maxIterations: 10
after:
  - "{{ set('step', loop.count(), 'loop') }}"

loop.results() - self-referential termination ​

loop.results() returns a slice of all results from previous iterations. This enables patterns where the termination condition depends on what the loop has already produced:

yaml
# resources/example.yaml
loop:
  while: "len(loop.results()) < 3"
  maxIterations: 10
after:
  - "{{ set('n', loop.count()) }}"

The loop runs until it has 3 results, with no limit on the number of iterations. This is an important pattern for mu-recursion and unbounded search.

Streaming response ​

When apiResponse is present, every iteration produces one response map. Multiple per-iteration responses constitute a streaming response - a slice returned to the caller. This mirrors how items with apiResponse works.

yaml
flow:
  # resources/example.yaml
  loop:
    while: "loop.index() < 3"
    maxIterations: 10
after:
  - "{{ set('tick', loop.count()) }}"
apiResponse:
  success: true
  response:
    tick: "{{ get('tick') }}"

Three iterations -> three apiResponse maps -> streaming slice of length 3.

No iterations -> empty slice.

maxIterations safety cap ​

maxIterations is a configurable upper bound on the number of iterations. It prevents accidental infinite loops in production. The loop stays Turing complete, because you can set the limit to any positive integer.

  • Default: 1000
  • Set to any positive integer for tighter or looser control
  • The loop stays Turing complete because you can configure the limit. It is not fixed
yaml
# resources/example.yaml
loop:
  while: "true"
  maxIterations: 50000   # allow up to 50k iterations

every: - repeated scheduled tasks ​

To pause the loop for a fixed duration between iterations, add every:. This makes the loop a repeated scheduled task (ticker pattern). Supported units: ms (milliseconds), s (seconds), m (minutes), h (hours).

yaml
flow:
  # resources/example.yaml
  loop:
    while: "loop.index() < 10"
    every: "5s"           # wait 5 seconds between each iteration
    maxIterations: 100
after:
  - "{{ set('tick', loop.count()) }}"
apiResponse:
  success: true
  response:
    tick: "{{ get('tick') }}"
    at:   "{{ loop.count() }}"

kdeps skips the sleep after the last iteration. As a result, the caller receives results with no unnecessary delay at the end.

Combining while: "true" with every: for infinite polling ​

yaml
flow:
  # resources/example.yaml
  loop:
    while: "true"          # run until maxIterations
    every: "30s"           # poll every 30 seconds
    maxIterations: 1440    # up to 12 hours (1440 x 30 s)
component:
  name: exec
  with:
    command: "poll-service.sh"
after:
  - "{{ get('execResource').exitCode == 0 ? set('done', true) : set('noop', 0) }}"

Duration format ​

ExampleMeaning
"500ms"500 milliseconds
"1s"1 second
"2m"2 minutes
"1h"1 hour

Validation rejects an invalid every: value (for example, "not-a-duration").

at: - specific dates and times ​

Use at: to fire the loop body at a list of specific dates and/or times, in order. The engine waits until each scheduled time. Then it runs the body for that iteration.

every: and at: are mutually exclusive - set only one per loop block.

yaml
flow:
  # resources/example.yaml
  loop:
    while: "loop.index() < 2"
    maxIterations: 10
    at:
      - "2026-03-15T10:00:00Z"   # RFC3339 absolute timestamp
      - "2026-03-15T14:30:00Z"
after:
  - "{{ set('tick', loop.count()) }}"
apiResponse:
  success: true
  response:
    tick: "{{ get('tick') }}"

Supported date/time formats ​

FormatExampleBehaviour
RFC3339 (UTC)"2026-03-15T10:00:00Z"Fire at that exact instant
RFC3339 (offset)"2026-03-15T10:00:00+02:00"Fire at that exact instant
Local datetime"2026-03-15T10:00:00"Treated as local time
Time of day"10:00" or "10:00:00"Next occurrence of that time today; tomorrow if already past
Date only"2026-03-15"Midnight (00:00:00) of that date, local time

Daily recurring example ​

yaml
flow:
  # resources/example.yaml
  loop:
    while: "true"
    maxIterations: 30  # run for 30 days
    at:
      - "08:00"   # fire at 08:00 every morning (next occurrence)
      - "20:00"   # fire at 20:00 every evening
component:
  name: exec
  with:
    command: "daily-report.sh"

If a time entry is already in the past the engine fires immediately (no sleep).
An invalid entry (e.g. "not-a-date") causes an error before any iterations run.

Condition syntax ​

The while: field is optional. If you do not set it, the loop runs until maxIterations (default 1000), or until it uses all at: entries.

If you set it, kdeps evaluates the expression with expr-lang. All boolean expressions are valid:

yaml
# Counter
while: "loop.index() < 10"

# Data-driven termination
while: "get('done') == nil"

# Prior-results driven
while: "len(loop.results()) < 5"

# Mutable state
while: "int(default(get('phase'), 0)) < 3"

# Mathematical search (mu-recursion)
while: "int(loop.count()) * int(loop.count() + 1) / 2 <= 20"

The while field is a plain expr-lang boolean expression string.

Turing completeness ​

The three primitives of Turing completeness are:

PrimitiveHow kdeps provides it
Unbounded iterationloop.while with configurable maxIterations
Mutable stateset() / get() across iterations
Conditional branchingArbitrary boolean while expression + validations.skip

Together with loop.results() feeding back into the while condition, the system can simulate any computable function - including mu-recursion (search until an unpredictable condition is met).

Examples ​

Accumulator (sum 1+2+3+4 = 10) ​

yaml
flow:
  # resources/example.yaml
  loop:
    while: "loop.index() < 4"
    maxIterations: 100
after:
  - "{{ set('sum', int(default(get('sum'), 0)) + loop.count()) }}"
apiResponse:
  success: true
  response:
    partial_sum: "{{ get('sum') }}"

State-machine phase transition ​

yaml
flow:
  # resources/example.yaml
  loop:
    while: "int(default(get('phase'), 0)) < 3"
    maxIterations: 10
after:
  - "{{ set('phase', int(default(get('phase'), 0)) + 1) }}"
apiResponse:
  success: true
  response:
    phase: "{{ get('phase') }}"

Conditional early exit (flag-based) ​

yaml
flow:
  # resources/example.yaml
  loop:
    while: "get('done') == nil"
    maxIterations: 100
component:
  name: exec
  with:
    command: "check-condition.sh"
after:
  - "{{ get('execResource').exitCode == 0 ? set('done', true) : set('noop', 0) }}"
apiResponse:
  success: true
  response:
    iterations: "{{ loop.count() }}"

Collect n results ​

yaml
flow:
  # resources/example.yaml
  loop:
    while: "len(loop.results()) < 5"
    maxIterations: 50
chat:
  context:
    prompt: "Generate item {{ loop.count() }}"
apiResponse:
  success: true
  response:
    item: "{{ get('chatResource') }}"

Downstream resource reads loop output ​

A resource that runs a loop and a downstream resource that reads the final state:

yaml
# resources/compute.yaml
actionId: compute
flow:
  loop:
    while: "loop.index() < 3"
    maxIterations: 10
after:
  - "{{ set('computed', loop.count()) }}"

---
# resources/respond.yaml
actionId: respond
flow:
  requires: [compute]
apiResponse:
  success: true
  response:
    value: "{{ get('computed') }}"   # reads final value set by the loop

When to use loop vs items ​

Use loop when...Use items when...
Number of iterations is not known in advanceYou have a fixed list to process
Termination depends on runtime stateYou want to iterate over a pre-computed array
You need mutable accumulation across iterationsEach item is independent
Implementing search / retry / polling patternsBatch processing of a dataset
Running a repeated scheduled task (every:)One-shot batch over a dataset
Firing at specific dates or times (at:)Batch processing of a dataset

See also ​

kdeps has the Apache 2.0 License.