An AG2 group chat without a stop rule can talk to itself until the budget runs out. A termination condition is a callable that looks at the messages since it was last called and returns a stop signal when the conversation should end: after a set number of messages, when a keyword such as TERMINATE appears, when a token budget is used, or after a set time. Pick one condition before the first run, and reset it before reusing it, because a condition that has fired must be reset before it can stop another run.
What you need
- Python with the autogen package installed
- An AG2 group chat team with at least two agents
- A stop rule chosen in advance, such as a message count or a keyword
Step 1: Choose the condition and attach it to the team
Pick one built-in condition, such as a maximum message count or a text mention, and attach it when you create the team. For a group chat team the condition is checked after each agent responds.
Step 2: Tell the agents how the chat ends
Write the stop keyword or the expected ending into the agents’ instructions, for example that the assistant should reply with TERMINATE when the task is done, so the condition has something to catch.
Step 3: Run the chat and confirm it stops
Run the team and confirm it stops at the condition and does not keep going. If it runs past the limit, the condition is attached to the wrong team or the keyword never appears.
Worked example: synthetic group chat run
The table below is a synthetic example. It shows the expected result of termination; no chat was run.
| Check | Expected | Result |
|---|---|---|
| Chat stops at message limit | Yes | Pass |
| Stop keyword caught | Yes | Pass |
| No extra turns after stop | 0 extra | Pass |
| Final answer delivered | Yes | Pass |
Expected result (UNTESTED). the chat stops at the chosen condition, delivers the final answer, and takes no extra turns. This check was not run live; the table shows the expected result for the synthetic example.
Limits
- A termination condition is stateful and must be reset before it can stop another run.
- A keyword condition only works if the agents actually say the keyword, so write it into their instructions.
- A message-count limit counts agent and task messages together, so set it with both in mind.
