Loading preview…
Loading preview…
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.
Loading preview…
Requirements, wiring steps, and what this pattern adds to your project.
Pick how you want to pull this pattern in — then wire env vars and routes the same way.
Install from the preview toolbar
Copy the install command above and run it in your project — Pro patterns include a short-lived token.
Add environment variables to .env.local
OPENAI_API_KEY
Point client fetches at your API routes
Replace preview `/view/.../api/...` paths with `/api/...` in the installed files.
Customize the agent and tool files
Adapt prompts, tools, and stop conditions for your product — Copy for AI in the toolbar helps seed that work.
Run your dev server and open the pattern route
Install dependencies if needed, then start the app and verify responses.
OPENAI_API_KEYGet keyAI SDK APIs
ToolLoopAgentprepareStepstepCountIstoolgatewayProviders
app/page.tsxapp/layout.tsxapp/api/trim/route.tscomponents/trim-demo.tsxcomponents/trim-timeline.tsxcomponents/seed-preview.tsxlib/trim-config.tslib/seed-conversation.tslib/trim-agent.tstools/append-note.tsREADME.mdnpm packages
5
Registry components
lib/trim-agent.tsToolLoopAgent with prepareStep that returns messages.slice(-KEEP_RECENT) when over MAX_MESSAGES. Logs trim events per step in a collector array.
lib/seed-conversation.tsStatic 24-message ModelMessage[] fixture so the demo shows trimming immediately without user-built history.
trim/route.tsRuns agent.generate({ messages: [...seeded, finalUser] }) and returns trimEvents, hero metrics, and optional compare run without trim.
tools/append-note.tsSingle demo tool for the agent to call once before summarizing.
Loading preview…
Early-bird pricing — limited time

Open on desktop for the interactive preview.
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.