Implementation:Microsoft Autogen TerminationCondition Combinators
| Knowledge Sources | |
|---|---|
| Domains | Multi-Agent Systems, Conversation Control, Boolean Algebra, AI Agents |
| Last Updated | 2026-02-11 00:00 GMT |
Overview
Concrete tools for defining and composing conversation stopping rules provided by Microsoft AutoGen.
Description
AutoGen provides a family of termination condition classes that can be instantiated individually or composed using Python's | (OR) and & (AND) operators. The three most commonly used atomic conditions are:
TextMentionTermination: Scans each message's text content for a specific substring. When found, the conversation stops. Optionally filters by source agent name. This is the standard mechanism for agents to signal task completion (e.g., by saying "TERMINATE").
MaxMessageTermination: Counts messages across turns and stops when the total reaches a threshold. By default, onlyBaseChatMessageinstances are counted (not agent events), but this can be changed with theinclude_agent_eventflag.
TokenUsageTermination: Tracks cumulative token usage from model responses and stops when any of the configured limits (total, prompt, or completion tokens) is exceeded.
The | operator creates an OrTerminationCondition that stops when any constituent condition triggers. The & operator creates an AndTerminationCondition that stops only when all constituent conditions have triggered. These combinators can be chained to express arbitrarily complex stopping logic.
Usage
Import termination conditions from autogen_agentchat.conditions and pass them to team constructors via the termination_condition parameter. Compose multiple conditions with | and & operators before passing.
Code Reference
Source Location
- Repository: Microsoft AutoGen
- File (atomic conditions):
python/packages/autogen-agentchat/src/autogen_agentchat/conditions/_terminations.py - File (combinators):
python/packages/autogen-agentchat/src/autogen_agentchat/base/_termination.py(lines 79-85)
Signature
class TextMentionTermination(TerminationCondition):
def __init__(self, text: str, sources: Sequence[str] | None = None) -> None:
...
class MaxMessageTermination(TerminationCondition):
def __init__(self, max_messages: int, include_agent_event: bool = False) -> None:
...
class TokenUsageTermination(TerminationCondition):
def __init__(
self,
max_total_token: int | None = None,
max_prompt_token: int | None = None,
max_completion_token: int | None = None,
) -> None:
...
# Combinator operators (defined on TerminationCondition base class):
# condition_a | condition_b -> OrTerminationCondition
# condition_a & condition_b -> AndTerminationCondition
Import
from autogen_agentchat.conditions import TextMentionTermination, MaxMessageTermination, TokenUsageTermination
I/O Contract
Inputs
TextMentionTermination:
| Name | Type | Required | Description |
|---|---|---|---|
| text | str | Yes | The substring to search for in message content. When found, the conversation stops. |
| sources | Sequence[str] or None | No | If provided, only messages from these agent names are checked. If None, all messages are checked. |
MaxMessageTermination:
| Name | Type | Required | Description |
|---|---|---|---|
| max_messages | int | Yes | Maximum number of messages allowed before stopping. |
| include_agent_event | bool | No | Whether to count BaseAgentEvent messages in addition to BaseChatMessage. Defaults to False. |
TokenUsageTermination:
| Name | Type | Required | Description |
|---|---|---|---|
| max_total_token | int or None | No | Maximum cumulative total tokens (prompt + completion). At least one of the three limits must be provided. |
| max_prompt_token | int or None | No | Maximum cumulative prompt tokens. |
| max_completion_token | int or None | No | Maximum cumulative completion tokens. |
Outputs
| Name | Type | Description |
|---|---|---|
| instance | TerminationCondition | A termination condition instance (or composed condition) to pass to a team's termination_condition parameter. |
| __call__ result | StopMessage or None | When evaluated, returns a StopMessage with a descriptive reason if the condition is met, or None if the conversation should continue. |
Usage Examples
Basic Example
from autogen_agentchat.conditions import TextMentionTermination, MaxMessageTermination
# Stop when the agent says "TERMINATE"
text_termination = TextMentionTermination("TERMINATE")
# Stop after 10 messages
max_msg_termination = MaxMessageTermination(max_messages=10)
# Combine with OR: stop when EITHER condition is met
termination = text_termination | max_msg_termination
Token Budget with Text Signal
from autogen_agentchat.conditions import (
TextMentionTermination,
MaxMessageTermination,
TokenUsageTermination,
)
# Stop on "DONE" from the summarizer agent, OR after 50k total tokens, OR after 30 messages
termination = (
TextMentionTermination("DONE", sources=["summarizer"])
| TokenUsageTermination(max_total_token=50000)
| MaxMessageTermination(max_messages=30)
)
AND Composition
from autogen_agentchat.conditions import TextMentionTermination
# Stop only when BOTH the reviewer AND the editor have said "APPROVED"
termination = (
TextMentionTermination("APPROVED", sources=["reviewer"])
& TextMentionTermination("APPROVED", sources=["editor"])
)