Short-term memory is the agent's context window — everything the LLM can "see" in one generation. Context is finite and expensive. Strategies to manage it: sliding window (keep only last N messages), summary memory (compress older messages into a summary), token-based trimming (remove messages to fit a budget), and selective retention (keep tool results, drop intermediate thoughts). Context management directly impacts agent coherence and cost.
Optimal ordering of context components for agent coherence.
The original user goal is the most important context element. Mark it as pinned so it's never trimmed. Agents that lose sight of the goal wander into tangential tasks.
Use tiktoken (for OpenAI models) or the model's tokenizer to count tokens precisely. A 1000-character message may be 250 or 500 tokens depending on content.
Trimming discards potentially critical information. Summarisation compresses it. Use tiktoken to monitor context; when it exceeds 70% of the model's limit, summarise older exchanges.
Sign in to share your feedback and join the discussion.