Draft
Conversation
Introduces middleware for automatic context window pruning and token estimation. Co-authored-by: jcourson8 <jcourson8@gmail.com>
|
Cursor Agent can help with this pull request. Just |
Co-authored-by: jcourson8 <jcourson8@gmail.com>
Removes ManagedContextAgent and consolidates logic into contextWindow middleware. Introduces priority functions for flexible message selection. Co-authored-by: jcourson8 <jcourson8@gmail.com>
Co-authored-by: jcourson8 <jcourson8@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Background
Managing conversation history to fit within a Language Model's context window is a common challenge, often leading to complex, manual token counting and pruning logic. This PR addresses this by introducing a robust and developer-friendly context management system. The goal is to provide a clean, composable, and automatic way to ensure prompts remain within token limits, enhancing the AI SDK's "easy AI" philosophy.
Summary
This PR implements a
contextWindowmiddleware for the AI SDK, designed to automatically prune message histories to fit within a specified token limit. Key features include:LanguageModelV3model, making context management transparent to the application logic.sliding-window(default),keep-boundaries, andpriority-basedstrategies to control which messages are retained.maxPromptTokensfor popular models (e.g., GPT-4o, Claude 3.5 Sonnet, Gemini 1.5 Pro) to simplify setup and ensure safe defaults.onPrunecallback for observability and debugging when messages are removed.This system is implemented in a new
/packages/ai/src/context/directory, including core logic, token estimation, and comprehensive tests.Manual Verification
The following examples demonstrate the functionality:
examples/ai-core/src/middleware/context-window-basic.ts: Shows basic usage of thecontextWindowmiddleware with a long conversation history, demonstrating automatic pruning.examples/ai-core/src/middleware/context-window-preset.ts: Illustrates how to usecontextWindowForModelwith model-specific presets for simplified configuration.examples/ai-core/src/middleware/context-window-priority.ts: Highlights thepriority-basedstrategy with customassignPrioritylogic, ensuring important messages are retained.To verify:
pnpm tsx examples/ai-core/src/middleware/context-window-basic.ts, etc.).onPrunecallback messages, to confirm that messages are being pruned as expected and the final prompt fits within the token limits.Checklist
pnpm changesetin the project root)Future Work
tiktoken) directly into presets or as optional dependencies for more precise token counting.