> ## Documentation Index
> Fetch the complete documentation index at: https://ultrarag.openbmb.cn/llms.txt
> Use this file to discover all available pages before exploring further.

# Loop Structure

In tasks such as multi-turn reasoning, multi-hop Q\&A, or multi-turn retrieval, a single execution process often fails to obtain an ideal final answer. In this case, a Loop Structure can be used to repeatedly execute specific modules, thereby achieving iterative information refinement and continuous result optimization.

## Usage Example

```yaml examples/rag_loop.yaml icon="https://mintcdn.com/ultrarag/T7GffHzZitf6TThi/images/yaml.svg?fit=max&auto=format&n=T7GffHzZitf6TThi&q=85&s=69b41e79144bc908039c2ee3abbb1c3b" highlight="16-28" theme={null}
# MCP Server
servers:
  benchmark: servers/benchmark
  retriever: servers/retriever
  prompt: servers/prompt
  generation: servers/generation
  evaluation: servers/evaluation
  custom: servers/custom

# MCP Client Pipeline
pipeline:
- benchmark.get_data
- retriever.retriever_init
- generation.generation_init
- retriever.retriever_search
- loop:
    times: 3
    steps:
    - prompt.gen_subq
    - generation.generate:
        output:
          ans_ls: subq_ls
    - retriever.retriever_search:
        input:
          query_list: subq_ls
        output:
          ret_psg: temp_psg
    - custom.merge_passages
- prompt.qa_rag_boxed
- generation.generate
- custom.output_extract_from_boxed
- evaluation.evaluate
```

In the loop structure, the Pipeline uses the following keywords to define tool modules that need to be executed repeatedly:

* `loop`: Declares a loop block, indicating that the steps inside it will be executed repeatedly;
* `times`: Specifies the maximum number of iterations for the loop;
* `steps`: Defines the sequence of tool calls to be executed in each round of the loop.

<Note>If you wish to dynamically control loop termination conditions, you can use it in conjunction with the Branch Structure (branch) and Router Server. See [Branch Structure](/pages/en/rag_client/branch) for details.</Note>
