Workflow agent loops

  • Updated

The Loop logic element in the workflow agent builder in Optimizely Opal automates repetitive tasks in your workflows. Specifically, it functions as a "For Each" loop. It iterates over a collection of items, such as URLs, keywords, or data sets, and runs a defined set of actions for each item. Use a loop to process every item in a collection without building a separate branch for each one.

Screenshot of a workflow agent in the builder where the Loop logic item is highlighted.

A loop accepts the following steps inside its body:

  • Specialized agent steps – Run an agent on each item.
  • Conditional steps – Branch logic within each iteration.
  • Nested loops – Add loops within loops for multi-level iteration.
  • Code steps – Run custom code on each item. A loop cannot start with a code step, so place one after the loop's first step.

When to use loops

Use a loop when you perform the same operation on multiple pieces of data. For example:

  • Iterate through data sets – Systematically go through a collection of data points, performing a specific action for each one.
  • Process lists of items – Apply the same action (for example, create content, update status, or generate reports) to each item in a collection, such as a list of assets, campaigns, keywords, variations, or segments.
  • Generate multiple variations – Create multiple versions of content or other outputs based on different parameters from a list of inputs.

How to add a loop to a workflow

After you create a workflow agent from scratch and configure workflow, you can optionally add loops to your workflow.

  1. Drag and drop a Loop from the Logic section into the workspace.
  2. (Optional) Rename your loop and add a Description.
  3. (Optional) Set the execution mode with the Run iterations in parallel toggle. It defaults to On. See Set the loop execution mode.
  4. Connect an agent that provides the collection of items to iterate on. Click the Agent and click a connector circle. Drag the connector to the Loop.
  5. Drag and drop an agent, condition, or another loop into the loop.

    creenshot of the workflow agent builder where a specialized agent step sits inside the body of a loop.
  6. Click Update to save your workflow agent.

Set the loop execution mode

A loop runs its iterations in one of two modes. Match the mode to the way your iterations relate to each other. The right mode finishes the loop as fast as the work lets it and still returns reliable output.

  • Parallel – Every iteration starts at the same time and runs concurrently. Use parallel mode when iterations are independent and none of them needs output from another. For example, a loop that generates a draft blog post for each keyword in a list.
  • Sequential – Iterations run one at a time. Each iteration waits for the previous one to finish before it starts. Use sequential mode when iterations depend on each other or must run in a set order. For example, a loop that scrapes each product page in turn. Every iteration builds on the summary of the pages before it.

The Run iterations in parallel toggle controls the mode.

Screenshot of the For Loop configuration with Run iterations in parallel highlighted.

Change the execution mode at any time. The change applies to the next workflow run. The structure of the loop and the steps inside it stay as they are. Complete the following steps:

  1. Open your workflow agent in the workflow builder.
  2. Click the loop step you want to change.
  3. Toggle Run iterations in parallel on to run every iteration concurrently, or off to run them one at a time.
  4. Click Update.

Note: Both modes run every iteration before the loop reports a result. A failed iteration does not stop the iterations that follow it. The loop completes the remaining iterations and then reports the failure.

Best practices and considerations

Keep the following in mind when working with loops:

  • Nesting – Use up to three nested loops to create complex iterative processes.
  • Performance – Be mindful of the number of iterations. Large loops increase the execution time of the workflow agent.
  • Variable scope – Confirm which variables the loop reads from outside its body and which ones each iteration writes. Data flows correctly only when that boundary is clear.
  • Throughput – Toggle Run iterations in parallel on for large collections of independent items. A parallel loop finishes faster because every iteration runs at the same time.
  • Shared state – Toggle Run iterations in parallel off when iterations write to the same resource or read a variable that another iteration updates. Concurrent writes conflict with each other.
  • Order of results – Parallel iterations complete in any order. Toggle Run iterations in parallel off when a downstream step depends on results arriving in the order of the source collection.

Example: dynamic landing page generator

Example scenario – Your marketing team runs a campaign targeting customer segments such as small business owners, enterprise clients, and non-profit organizations. Each segment requires a slightly different landing page with tailored messaging, hero images, and calls-to-action (CTAs) to maximize conversion. Manually creating and maintaining dozens of unique landing pages is time-consuming and prone to errors.

Create a workflow agent that uses a Loop to generate these landing pages automatically.

The Loop takes a list of your customer segments as its input from the previous agent. Each segment profile includes attributes such as the following:

  • segment_nameSmall Business
  • hero_image_urlurl_to_small_biz_image.jpg
  • headline_textBoost Your Small Business Growth
  • cta_button_textStart Your Free Trial
  • target_audience_descriptionEntrepreneurs and startups

Loop body (for each segment)

The loop runs the following steps for each segment:

  1. Fetch template – The agent starts with a base landing page template.
  2. Customize content – The Customize landing page agent inserts headline_text, cta_button_text, and target_audience_description into the matching sections of the template.
  3. Update visuals – The Update visuals agent replaces the hero_image_url, which is the default hero image on the page.
  4. Publish page – The Publish page agent publishes the customized landing page with a unique URL. For example, yourdomain.com/campaign/small-business.
  5. Output – The Loop completes and returns one customized landing page per user segment.
creenshot of the dynamic landing page generator workflow agent, where a loop contains the customize, update visuals, and publish steps.

his workflow agent deploys relevant landing pages quickly. It improves campaign performance and conversion rates, and it frees your team from repetitive manual work.

Each segment in this example produces its own landing page, and no segment needs output from another. Toggle Run iterations in parallel on so every page is generated at the same time.