Aptness
Aptness.ai

Branches and Loops: Control Flow in Agents

Real workflows aren't straight lines. If/Else branches, Loop/Repeat blocks, and secure Environment Variables let your agent make decisions, process lists, and keep secrets out of plain sight.

2 min read
Aptness · Tutorials
Branches and Loops: Control Flow in Agents

A straight line of steps gets you surprisingly far — but real workflows need to make decisions and repeat work. Custom Agents give you three logic building blocks for exactly that: If/Else, Loop/Repeat, and Environment Variables.

If/Else: branch on a condition

The If/Else node splits your workflow into two paths. You give it a left-hand value, a comparison, and (for most comparisons) a right-hand value. At run time the agent evaluates the condition and follows the matching branch.

The available comparisons cover the cases you'll actually reach for:

  • Equal to, and not equal to
  • Greater than, greater than or equal to
  • Less than, less than or equal to
  • Is true, and is false

Because either side can pull from a previous step's output, you can branch on anything your agent has already seen: "if the AI classified this email as urgent, notify the team — otherwise just log it."

Loop / Repeat: do it N times

The Loop / Repeat block runs the steps inside it a set number of times. It's drawn as a pair — a Loop Start that holds the settings and a Loop End that closes the block — with your body steps wired between them.

A few things make it genuinely useful:

  • The iteration count can be a fixed number, or it can be driven by a previous step — for example, the number of items returned by an earlier action.
  • The current iteration index is available to the steps inside the loop, so each pass can work on a different item.
  • Results are collected across every iteration, so after the loop you have the full set of outputs, not just the last one.

That's how you turn "scrape one page" into "scrape every URL in this list," or "send one message" into "message each person who matched."

Loop Start carries the configuration; Loop End is just the marker that closes the block. There's nothing to fill in on the End node — it's structural.

Environment Variables: secrets, kept secret

API keys, tokens, account IDs — values you want to reuse but never paste into a dozen fields. The Environment Variables node stores them as key–value pairs in one place, so any step in the agent can reference them without the value being scattered (or visible) across the canvas.

Define it once, reference it everywhere, and change it in a single spot when it rotates.

Putting it together

The three compose naturally. Imagine an agent that:

  1. Pulls a list of new sign-ups from a sheet
  2. Loops over each one
  3. Uses an If/Else to send VIPs down a different path than everyone else
  4. Authenticates each call with a token from Environment Variables

That's a real, branching, repeating workflow — built entirely from visual blocks. Pair it with an AI step for the judgment calls and you've got something that genuinely thinks before it acts.

Share
The Aptness Brief · Weekly

One short email. Every Friday.

New posts, product updates, and the most useful agent recipes from the community — handpicked by our team. No fluff, unsubscribe anytime.

No spam. Read by 12,000+ builders and operators.