agentscope-ai/agentscope · error · RuntimeError

The system prompt {suffix}exceed(s) the compression threshol

Error message

The system prompt {suffix}exceed(s) the compression threshold ({threshold} tokens), cannot be compressed.

What it means

During context compression (_compress_context_impl), agentscope splits history into a protected part (system prompt plus any existing summary) and a compressible part. If the token count of the system prompt (and accumulated summary) alone meets or exceeds the compression threshold, there is nothing left to compress, so it raises this RuntimeError telling the developer the threshold is too low or the prompt too large.

Source

Thrown at src/agentscope/agent/_agent.py:457

        if estimated_tokens < threshold:
            return

        logger.info(
            "[AGENT %s]: Current token count %d exceeds the threshold %d, "
            "activating compression.",
            self.name,
            int(estimated_tokens),
            int(threshold),
        )

        if len(self.state.context) == 0:
            # The system prompt and the summary (if exists) exceeds the
            # threshold, which cannot be compressed, raise the error to the
            # developer!
            suffix = ""
            if self.state.summary:
                suffix = "and the compression summary "
            raise RuntimeError(
                f"The system prompt {suffix}exceed(s) the compression "
                f"threshold ({threshold} tokens), cannot be compressed.",
            )

        # Split the context into the ones to be compressed, and the others to
        # be reserved
        tools = kwargs.get("tools", [])
        (
            msgs_to_compress,
            msgs_to_reserve,
        ) = await self._split_context_for_compression(
            cfg.reserve_ratio * self.model.context_size,
            tools,
        )

        if len(msgs_to_compress) == 0:
            # The reserve ratio is too large so that although it exceeds the
            # trigger threshold, the context to be compressed is empty

View on GitHub (pinned to e90f1c7592)

Solutions

  1. Raise the compression threshold: increase trigger_ratio (and/or the model's max context), so the system prompt fits below it
  2. Shorten the system prompt or move bulky content (tool docs, examples) into the first compressible user message instead
  3. If the summary is the culprit, reset or shorten the compression summary (start a new session or clear state.summary) and rely on a shorter summary prompt
  4. Switch to a model with a larger context window

Example fix

# before
agent = ReActAgent(
    sys_prompt=VERY_LONG_PROMPT,
    context_config=ContextConfig(trigger_ratio=0.3, reserve_ratio=0.1),
)
await agent.compress_context()  # raises: system prompt >= threshold

# after
agent = ReActAgent(
    sys_prompt=shortened_prompt,
    context_config=ContextConfig(trigger_ratio=0.6, reserve_ratio=0.2),
)
await agent.compress_context()
Defensive patterns

Strategy: validation

Validate before calling

import tiktoken  # or the tokenizer your model uses

def estimate_tokens(text: str) -> int:
    return len(tiktoken.get_encoding("cl100k_base").encode(text))

threshold = int(model_max_tokens * cfg.trigger_ratio)
sys_tokens = estimate_tokens(agent.sys_prompt) + estimate_tokens(getattr(agent.state, "summary", "") or "")
assert sys_tokens < threshold, (
    f"system prompt + summary ({sys_tokens} tokens) >= threshold ({threshold}); "
    "raise trigger_ratio or shorten the prompt"
)

Type guard

from typing import TypeGuard

def prompt_fits_threshold(sys_tokens: int, summary_tokens: int, threshold: int) -> TypeGuard[int]:
    return (sys_tokens + summary_tokens) < threshold

Try / catch

try:
    await agent.compress_context()
except RuntimeError as e:
    if "exceed(s) the compression threshold" in str(e):
        agent.context_config.trigger_ratio = min(0.9, agent.context_config.trigger_ratio + 0.2)
        await agent.compress_context()
    else:
        raise

Prevention

When it happens

Trigger: Calling agent.compress_context() (or execute_chain triggering it) with a very long system prompt relative to the threshold, or after repeated compressions whose summary grew large (the message then says 'The system prompt and the compression summary exceed(s)...'). Small trigger_ratio * max_tokens makes this likely.

Common situations: Huge hand-written system prompts (agents with tool docs, RAG instructions), a growing summary from repeated compressions in long sessions, or a low compression threshold (trigger_ratio) combined with a large prompt. Also when max_tokens of the model is small relative to prompt size.

Related errors


AI-assisted analysis of agentscope-ai/agentscope@e90f1c7592 (2026-08-28). Data as JSON: /api/errors/4e6a37d37670bf5d. Report an issue: GitHub.