agentleFS
lauren-frameworklauren-framework/lauren-ai/skills/context-window-management/SKILL.md

context-window-management

Skill0 starsChanged 6 months agoMITView on GitHub

Shows how to manage an agent's context window using ShortTermMemory sliding-window trimming and manual message truncation helpers. Use when limiting token usage, implementing conversation history pruning, or estimating prompt sizes before sending to the LLM.

8 other projects use a version of this.

Adopted by

Most recently taken up by

The file

Files
SKILL.md

Use codemap find "SymbolName" to locate any symbol before reading — it gives exact file + line range and is faster than grep across the whole repo.

Agent Context Window Management & Trimming

Overview

ShortTermMemory automatically trims the oldest non-system messages when the conversation grows beyond max_tokens. Configure it per-agent via @agent(memory=ShortTermMemory(max_tokens=N)).


Configuring a sliding window

# agents.py — from __future__ import annotations is safe here
from __future__ import annotations
from lauren_ai import agent, ShortTermMemory
from lauren_ai._memory._stores import InMemoryConversationStore

@agent(
    model="claude-opus-4-6",
    system="You are a helpful assistant.",
    memory=ShortTermMemory(max_tokens=4000),
    conversation_store=InMemoryConversationStore(),
)
class ContextManagedAgent: ...

ShortTermMemory.messages() returns a trimmed snapshot without mutating the internal buffer. The oldest non-system messages are dropped first.


Manual trim helper

def trim_messages(messages: list[dict], max_turns: int = 10) -> list[dict]:
    """Keep system messages + the last *max_turns* user/assistant pairs."""
    system = [m for m in messages if m.get("role") == "system"]
    turns  = [m for m in messages if m.get("role") != "system"]
    if len(turns) > max_turns * 2:   # *2 for user + assistant
        turns = turns[-(max_turns * 2):]
    return system + turns

Token estimation

def estimate_tokens(messages: list[dict]) -> int:
    """Rough token estimate — 4 chars ≈ 1 token."""
    return sum(len(m.get("content", "")) // 4 for m in messages)

ShortTermMemory.token_estimate uses the same heuristic internally.


When to use each approach

ApproachWhen to use
ShortTermMemory(max_tokens=N)Automatic per-run sliding window
trim_messages(msgs, max_turns=N)Manual preprocessing before injecting history
memory.trim_to_fit(max_tokens)Mutate the buffer in-place (use sparingly)

Full ShortTermMemory API

mem = ShortTermMemory(max_tokens=8000)
mem.add_user("Hello")          # add a user message
mem.add_assistant(completion)  # add a Completion or dict
mem.add_tool_result(result)    # add a ToolResult
msgs = mem.messages()          # trimmed snapshot
snap = mem.snapshot()          # deep copy, no trimming
mem.restore(snap)              # load a snapshot
mem.clear()                    # wipe the buffer
tok  = mem.token_estimate      # heuristic token count

Reference files

FileContents
src/lauren_ai/_memory/__init__.pyShortTermMemory implementation
src/lauren_ai/_memory/_stores.pyInMemoryConversationStore
src/lauren_ai/_agents/_runner.pyMemory creation and restore in run()

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.