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

# SelfMoASeq Guide

> Sample one model several times, then merge the answers a few at a time into one final response

`SelfMoASeq` (Sequential Self-Mixture of Agents) asks one model the same question several times and merges the answers into one. A proposer agent writes the samples. An aggregator agent then works through them a few at a time: each step reads the best answer so far plus the next few samples and writes a new best answer. Because each step sees only a small window, the merge stays within the model's context however many samples you take.

Reference: [SelfMoASeq](https://docs.swarms.world/api/self-moa-seq.md).

## How a run works

1. **Sample.** The proposer answers the task `num_samples` times. Its history is cleared before each sample, so every sample is an independent draw. Samples are generated one after another.
2. **Start.** The first sample becomes the best answer so far.
3. **Merge.** Each step takes the next `window_size - reserved_slots` samples, adds the best answer so far, and asks the aggregator to combine them. The aggregator runs at temperature 0, and its reply becomes the new best answer.
4. **Stop.** The run ends when every sample is merged or after `max_loops` steps.

`run()` never generates more samples than `max_loops` steps can merge: at most `1 + max_loops * (window_size - reserved_slots)`.

## 1. Merge a few samples

```python theme={null}
from swarms import SelfMoASeq

moa = SelfMoASeq(
    model_name="gpt-5.4-mini",
    num_samples=5,
    window_size=3,
    reserved_slots=1,
    verbose=False,
)

result = moa.run(
    "Write a three-sentence product description for a reusable "
    "water bottle that keeps drinks cold for 24 hours."
)

print(result["final_output"])
```

With `window_size=3` and `reserved_slots=1`, each step merges two new samples into the best answer. Five samples take two steps: sample 1 starts as the best, then samples 2 and 3, then samples 4 and 5.

`verbose=False` turns off the progress logs. The proposer and aggregator still print each of their replies in a panel.

## 2. Read the result

`run()` returns a dict. Continuing from step 1:

```python theme={null}
print("samples:", len(result["all_samples"]))
print("aggregation steps:", result["aggregation_steps"])
print("metrics:", result["metrics"])
print("finished at:", result["timestamp"])

for i, sample in enumerate(result["all_samples"], 1):
    print(f"--- sample {i}\n{sample}")

print("tokens:", moa.usage["total_tokens"])
```

A run printed:

```
samples: 5
aggregation steps: 2
metrics: {'total_samples_generated': 5, 'total_aggregations': 2, 'total_tokens_used': 0, 'execution_time_seconds': 8.759874}
finished at: 2026-12-02T13:42:47.037022
--- sample 1
Stay refreshed all day with this reusable water bottle, ...
...
tokens: 1595
```

| Key | What it holds |
| - | - |
| `final_output` | The last merged answer |
| `all_samples` | Every sample the proposer wrote, in order |
| `aggregation_steps` | How many merge steps ran |
| `metrics` | Counts and time for this run. `total_tokens_used` is always `0`; read token counts from `moa.usage` |
| `task` | The task you passed |
| `timestamp` | When the run finished, as an ISO 8601 string |

`metrics` is reset at the start of every `run()`, and `get_metrics()` returns a copy of the latest run's metrics. `moa.usage` adds up the provider token usage of the proposer and the aggregator across every run.

## 3. Size the window and the loop limit

`max_loops` caps the merge steps, and so the number of samples. Here `num_samples=10` asks for ten, but one step of three new samples can only merge four, so only four are generated:

```python theme={null}
from swarms import SelfMoASeq

capped = SelfMoASeq(
    model_name="gpt-5.4-mini",
    num_samples=10,
    window_size=4,
    reserved_slots=1,
    max_loops=1,
    verbose=False,
)

result = capped.run("Suggest a name for a coffee shop that also sells books.")

print(len(result["all_samples"]), "samples,", result["aggregation_steps"], "step")
print(result["final_output"])
```

This prints `4 samples, 1 step`, then the merged list of names.

How the settings fit together:

| Setting | Default | Effect |
| - | - | - |
| `num_samples` | `30` | Samples to generate, at least 2 |
| `window_size` | `6` | Window size, at least 2 |
| `reserved_slots` | `3` | Subtracted from the window. Each step merges `window_size - reserved_slots` new samples. Must be less than `window_size` |
| `max_loops` | `10` | Most merge steps, at least 1 |

The defaults make 30 proposer calls and 10 aggregator calls on `gpt-5.4`. Start smaller while you try a task.

## 4. Use a different model for each role

`proposer_model_name` and `aggregator_model_name` override `model_name` for one role each. A cheaper model can draft while a stronger one merges. `temperature` applies to the proposer only, and `max_tokens` caps each reply from both agents.

```python theme={null}
from swarms import SelfMoASeq

moa = SelfMoASeq(
    proposer_model_name="gpt-5.4-mini",
    aggregator_model_name="gpt-5.4",
    temperature=1.0,
    num_samples=4,
    window_size=4,
    reserved_slots=1,
    max_tokens=800,
    verbose=False,
)

result = moa.run(
    "Give three concrete tips for writing clear error messages in a CLI tool."
)

print(moa.proposer.model_name, "->", moa.aggregator.model_name)
print(result["final_output"])
```

This prints `gpt-5.4-mini -> gpt-5.4`, then the merged tips. The two agents are on the instance as `moa.proposer` and `moa.aggregator`. `top_p`, when set, goes to both.

## 5. Invalid settings fail early

The constructor checks its settings before it builds any agent, so a bad value fails without a model call:

```python theme={null}
from swarms import SelfMoASeq

for settings in (
    {"window_size": 1},
    {"window_size": 3, "reserved_slots": 3},
    {"temperature": 2.5},
    {"num_samples": 1},
    {"max_loops": 0},
):
    try:
        SelfMoASeq(model_name="gpt-5.4-mini", verbose=False, **settings)
    except ValueError as error:
        print(settings, "->", error)
```

Output:

```
{'window_size': 1} -> window_size must be at least 2
{'window_size': 3, 'reserved_slots': 3} -> reserved_slots must be less than window_size
{'temperature': 2.5} -> temperature must be between 0 and 2
{'num_samples': 1} -> num_samples must be at least 2
{'max_loops': 0} -> max_loops must be at least 1
```

`run()` raises `ValueError` for an empty task. A failed model call is raised as it is; there are no retries inside `SelfMoASeq`.

## Tips

* **Use it for open-ended writing and answers**, where merging several drafts beats any one of them. For a single correct answer, [MajorityVoting](https://docs.swarms.world/api/majority-voting.md) is cheaper.
* **Raise `temperature`** when the samples come back too alike. In step 1 the five samples differed by only a few words.
* **Expect sequential calls.** Samples and merges run one after another, so time grows with `num_samples`.

## Next steps

* [SelfMoASeq reference](https://docs.swarms.world/api/self-moa-seq.md)
* [MixtureOfAgents](https://docs.swarms.world/api/mixture-of-agents.md), which merges answers from different agents instead of one model


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.