context-chef — AI SDK middleware for automatic history compression, tool result truncation & token budget management #14266
Replies: 1 comment
-
|
This discussion was automatically closed because the community moved to community.vercel.com/ai-sdk |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Built an AI SDK middleware that handles context window management transparently. Wrap your model once, everything else stays the same — works with
generateText,streamText, and agent tool loops.The Problem
Building multi-turn agents with AI SDK, I kept hitting the same issues:
run_bashcan return 50KB+ of output, eating 10% of your context windowreportTokenUsage()or counting tokens manually on every turn is tedious and error-proneThe Solution
A single
withContextChef()wrapper that handles all of this behind the scenes:What it does
History compression — When conversation exceeds the token budget, older messages are automatically summarized by a cheap model (e.g.
gpt-4o-mini). Recent messages are preserved. Includes a circuit breaker: if the compression model fails 3 times, it degrades gracefully instead of crashing your agent.Tool result truncation — Large tool outputs are automatically truncated with head + tail preservation (keeps the command at the top and errors at the bottom). Optionally persists the full output to a storage adapter so the LLM can retrieve it later via a
context://vfs/URI.Automatic token tracking — Extracts token usage from
generateText/streamTextresponses and feeds it back to the compression engine. No manual tracking needed.Compact (zero-cost pruning) — Mechanical message pruning that removes reasoning blocks and old tool calls without any LLM cost. Delegates to AI SDK's
pruneMessagesunder the hood:How it works
The middleware is stateful — it tracks token usage across calls to know when compression is needed. Create one wrapped model per conversation/session.
Need more control?
The middleware covers the most common use case. For advanced features like:
Use
@context-chef/coredirectly.Links
@context-chef/ai-sdk-middleware@context-chef/coreWould love feedback — especially on what other middleware hooks would be useful for AI SDK agent workflows!
Beta Was this translation helpful? Give feedback.
All reactions