Agent( )

Pattern
PrepareStep: Trim Message History
Demonstrate ToolLoopAgent prepareStep for trimming long message history. Pre-seeded 24-message conversation shows context compression on the first run with per-step trim telemetry.
IntermediateJun 2026View the full pattern on desktop


Problems solved
- Trim long message histories before each agent step
- Keep recent tool results while dropping stale turns
- Observe prepareStep compression with per-step telemetry
- Prevent unbounded context growth in ToolLoopAgent loops
Use cases
- Long-running agents with many tool calls
- Context-window budget management
- Debugging what the model sees each step
- Production agents that must cap history size
AI SDK APIs
ToolLoopAgentprepareStepstepCountIstoolgateway
Tags
aiagentsprepare-stepcontext-windowmessage-trimmingtool-loop-agentai-sdkagent-api
Requirements
- Node.js
- 24+
- Memory
- 512MB
- Other
- OpenAI API key
Environment variables
OPENAI_API_KEYGet key
External services
openai
Ready to use?
View the full interactive pattern with live preview and copy the source code.
View the pattern on desktop






