Replace evicted older turns with a summary while keeping the active conversation intact.
The full prompt is available without an account.
Implement CompressMemory(max_tokens, count_tokens, summarizer).
- add(role, content) appends a message.
- render() returns a list of messages that fits within max_tokens,
always keeping the first system message if present.
- When non-system messages exceed the budget, replace the oldest evicted
messages with a single compressed message:
{"role": "system", "content": "[Compressed summary: <summarizer(texts)>]"}.
- The compressed summary counts toward the token budget and should be
positioned right after the pinned system message (if any) and before
any remaining uncompressed messages.
Read-only Python 3.12 preview. Sign in to edit and execute it.
class CompressMemory:
def __init__(self, max_tokens: int, count_tokens, summarizer):
self.max_tokens = max_tokens
self.count_tokens = count_tokens
self.summarizer = summarizer
self.messages = []
def add(self, role: str, content: str) -> None:
self.messages.append({"role": role, "content": content})
def render(self) -> list[dict]:
# TODO
return []