Skip to content

TokenLimiter

autogen.beta.middleware.builtin.token_limiter.TokenLimiter #

TokenLimiter(max_tokens, chars_per_token=4)

Bases: MiddlewareFactory

Truncate message history to fit within a token budget.

Uses a simple character-based estimate (chars_per_token chars per token) unless a custom tokenizer is provided.

Source code in autogen/beta/middleware/builtin/token_limiter.py
def __init__(self, max_tokens: int, chars_per_token: int = 4) -> None:
    if max_tokens < 1:
        raise ValueError("max_tokens must be greater than 0")
    if chars_per_token < 1:
        raise ValueError("chars_per_token must be greater than 0")
    self._max_chars = max_tokens // chars_per_token