Building Autonomous AI Agents · Module 2 · Memory, Scratchpads & Context Windows
Managing the Agent Scratchpad
6 min read · lesson 3 of 6
As an agent loops through 5, 10, or 20 tool executions, its message history grows rapidly. This working log is called the scratchpad. Without careful management, the scratchpad will quickly exhaust the context window and trigger exponential latency and cost.
Effective Pruning Strategies
- Sliding Window with Summary: Retain the original system prompt and the last N turns, while condensing older intermediate tool outputs into a brief summary paragraph.
- Observation Truncation: Never dump raw 5MB JSON payloads into the scratchpad. Filter the tool response to only include relevant fields before feeding it to the agent.
- Deduplication: If the agent calls the same lookup tool multiple times with identical arguments, suppress redundant logs.
function sanitizeToolOutput(raw: Record<string, unknown>) {
// Extract only needed properties to preserve token budget
const { id, status, totalAmount, itemsCount } = raw;
return { id, status, totalAmount, itemsCount };
}Always track total token usage per step. If an agent hits 80% of its max token budget, trigger an emergency summarization step before the next action.
Practice
Take a sample raw API payload with 40 fields and write a filter function that extracts only the 4 most critical values for an LLM.