Implementing Loops in Agent Service Workflows

Agent Service allows for dynamic control flow within workflows using loops. Loops enable iterative processing, conditional execution, and controlled stopping criteria. This guide demonstrates how to configure loops in YAML-based workflows.

How Loops Work in Agent Service

Loops in Agent Service are defined within the plan section of the YAML. A loop iterates through a workflow based on specified conditions and merges outputs at the end of execution. The key components of a loop include:

  • Workflow Execution: The loop runs a workflow repeatedly until a stopping condition is met.
  • Iteration Limit: The loop can be restricted to a maximum number of iterations.
  • Condition Handling: Loops can terminate based on logical conditions (e.g., when a generated output matches a specified value).
  • Data Merging: Outputs from loop iterations can be merged into a final output.

Example: Looping Until a Condition is Met

The following YAML defines a loop that iterates through a workflow that generates a word based on a seed input. The loop stops if the generated word contains "Zebra".

concurrency_default: false
user_input:
  word:
    type: ShortText
    
workflows:
  generate_keyword:
    - name: generate_keyword_prompt
      type: jinja
      config:
        log_output: true
        output_template:
          jinja_template_str: "We are playing a game where you need to pick a word based on a seed. It is your turn to guess a word for the seed {{word}}. Please output your choice of word and nothing else: "
      inputs:
        word: word

    - name: generate_keyword
      type: generation
      config:
        llm_model: gpt-4o
        max_tokens: 10
        temperature: 1.0
      inputs:
        input_prompt: generate_keyword_prompt.output

  format_final_word:
    - name: format_final_word_node
      type: jinja
      config:
        output_template:
          jinja_template_str: "The final word is {{final_word}}"
      inputs:
        final_word: final_word

plan:
  - name: simple_loop
    workflow:
      workflow_name: generate_keyword
    condition:
      logical_operator: NOT
      conditions:
        - condition_input_var: word
          operator: contains
          reference_var: Zebra
    max_iter: 1
    loop_inputs:
      word:
        node_name: generate_keyword
        default_source: word
    merge_outputs:
      final_word: generate_keyword

  - workflow_name: format_final_word
    workflow_inputs:
      final_word: simple_loop.final_word.output

Breaking Down the Loop Implementation

1. Defining the Workflow

The generate_keyword workflow generates a word based on an input seed. The generated word is then evaluated in the loop condition.

2. Configuring the Loop in the plan Section

  • workflow_name: generate_keyword → The loop runs this workflow.
  • condition → The loop terminates if the word contains “Zebra”.
  • max_iter: 1 → The loop executes at most once, but it can be adjusted for more iterations.
  • loop_inputs → The generated word is fed back into the next iteration.
  • merge_outputs → The final output from the loop is stored and passed to the next workflow.

3. Finalizing the Output

After the loop completes, the format_final_word workflow formats the final result.

Use Cases for Loops

  • Iterative Refinement: Improve model outputs by reprocessing results over multiple iterations.
  • Controlled Execution: Define stopping conditions based on specific output patterns.
  • Dynamic Data Processing: Merge and analyze outputs across multiple loop cycles.

This example demonstrates how loops enable flexible, condition-driven processing within Agent Service, making workflows more dynamic and adaptable.