New Tool Radar

AI Agents & Automation

How to Set a Maximum Round Count So an AG2 Group Chat Stops Instead of Looping

Set max_round on an AG2 GroupChat so the conversation ends after a fixed number of speaker turns instead of running on.

Schematic diagram with three steps: Count normal rounds, Set max_round, then Test a looping chat.

A group chat repeats speaker selection, speaking and broadcasting until the conversation stops, and a confused group can repeat itself without end. The max_round setting caps the iterations of that cycle, so the chat ends after a fixed number of rounds.

Before you start

You need a working AG2 group chat. Count how many rounds an ordinary conversation takes first: the documentation example for arithmetic agents uses a maximum round of 6, and the research example uses 50, so the right number depends on the job.

Set max_round when you create the GroupChat

Pass max_round with the agent list when you create the GroupChat object, for example 6 for a short task or 12 for a research task. The number counts iterations of selecting a speaker, the agent speaking, and broadcasting the message, so one round is one agent turn, not one message.

Test with a conversation that would loop

Start a chat with a request the agents cannot finish, for example a target number the arithmetic agents cannot reach. Check the chat stops at the round limit instead of running on. Then check an ordinary request still finishes before the limit.

Worked example: Round counts for a fictional arithmetic group

The AG2 documentation uses arithmetic agents to turn a number into a target. The table shows round counts for four runs with a limit of 6.

Run Rounds used Stopped by the limit?
Reach 12 from 2 4 No
Reach 20 from 5 5 No
Reach 100 from 3 6, then stopped Yes
Reach 7 from 1 3 No

Expected result (UNTESTED): the first, second and fourth runs finish within 6 rounds, and the third stops at 6 rounds. We checked the AG2 GroupChat documentation, which shows max_round set to 6 in its example, but we did not run the chat on this machine.

Limits

A low limit can cut off a legitimate long conversation, and a high limit only delays a loop instead of stopping it. Set the limit from your own measured rounds, and add a termination word the agents can use to end the chat early when the work is done.

Sources

See all