{"record":{"id":"4e6a37d37670bf5d","repo":"agentscope-ai/agentscope","slug":"the-system-prompt-suffix-exceed-s-the-compressio","errorCode":null,"errorMessage":"The system prompt {suffix}exceed(s) the compression threshold ({threshold} tokens), cannot be compressed.","messagePattern":"The system prompt (.+?)exceed\\(s\\) the compression threshold \\((.+?) tokens\\), cannot be compressed\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/agentscope/agent/_agent.py","lineNumber":457,"sourceCode":"        if estimated_tokens < threshold:\n            return\n\n        logger.info(\n            \"[AGENT %s]: Current token count %d exceeds the threshold %d, \"\n            \"activating compression.\",\n            self.name,\n            int(estimated_tokens),\n            int(threshold),\n        )\n\n        if len(self.state.context) == 0:\n            # The system prompt and the summary (if exists) exceeds the\n            # threshold, which cannot be compressed, raise the error to the\n            # developer!\n            suffix = \"\"\n            if self.state.summary:\n                suffix = \"and the compression summary \"\n            raise RuntimeError(\n                f\"The system prompt {suffix}exceed(s) the compression \"\n                f\"threshold ({threshold} tokens), cannot be compressed.\",\n            )\n\n        # Split the context into the ones to be compressed, and the others to\n        # be reserved\n        tools = kwargs.get(\"tools\", [])\n        (\n            msgs_to_compress,\n            msgs_to_reserve,\n        ) = await self._split_context_for_compression(\n            cfg.reserve_ratio * self.model.context_size,\n            tools,\n        )\n\n        if len(msgs_to_compress) == 0:\n            # The reserve ratio is too large so that although it exceeds the\n            # trigger threshold, the context to be compressed is empty","sourceCodeStart":439,"sourceCodeEnd":475,"githubUrl":"https://github.com/agentscope-ai/agentscope/blob/e90f1c7592896cc95f6e5ee506194f533378247d/src/agentscope/agent/_agent.py#L439-L475","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Raise the compression threshold: increase trigger_ratio (and/or the model's max context), so the system prompt fits below it","Shorten the system prompt or move bulky content (tool docs, examples) into the first compressible user message instead","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","Switch to a model with a larger context window"],"exampleFix":"# before\nagent = ReActAgent(\n    sys_prompt=VERY_LONG_PROMPT,\n    context_config=ContextConfig(trigger_ratio=0.3, reserve_ratio=0.1),\n)\nawait agent.compress_context()  # raises: system prompt >= threshold\n\n# after\nagent = ReActAgent(\n    sys_prompt=shortened_prompt,\n    context_config=ContextConfig(trigger_ratio=0.6, reserve_ratio=0.2),\n)\nawait agent.compress_context()","handlingStrategy":"validation","validationCode":"import tiktoken  # or the tokenizer your model uses\n\ndef estimate_tokens(text: str) -> int:\n    return len(tiktoken.get_encoding(\"cl100k_base\").encode(text))\n\nthreshold = int(model_max_tokens * cfg.trigger_ratio)\nsys_tokens = estimate_tokens(agent.sys_prompt) + estimate_tokens(getattr(agent.state, \"summary\", \"\") or \"\")\nassert sys_tokens < threshold, (\n    f\"system prompt + summary ({sys_tokens} tokens) >= threshold ({threshold}); \"\n    \"raise trigger_ratio or shorten the prompt\"\n)","typeGuard":"from typing import TypeGuard\n\ndef prompt_fits_threshold(sys_tokens: int, summary_tokens: int, threshold: int) -> TypeGuard[int]:\n    return (sys_tokens + summary_tokens) < threshold","tryCatchPattern":"try:\n    await agent.compress_context()\nexcept RuntimeError as e:\n    if \"exceed(s) the compression threshold\" in str(e):\n        agent.context_config.trigger_ratio = min(0.9, agent.context_config.trigger_ratio + 0.2)\n        await agent.compress_context()\n    else:\n        raise","preventionTips":["Keep the system prompt lean; move bulky docs into compressible user messages","Choose trigger_ratio so that threshold = trigger_ratio * max_tokens comfortably exceeds the system prompt size","Reset or bound the compression summary in very long sessions","Estimate prompt tokens with the model's tokenizer before starting long-running agent loops"],"tags":["context-compression","system-prompt","token-limit","agent"],"backgroundTag":"prompt-exceeds-context-window","analyzedSha":"e90f1c7592896cc95f6e5ee506194f533378247d","analyzedAt":"2026-08-28T18:24:12.087Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}