Sort and trim items by priority to fit a token budget. Built for AI context windows, prompt construction, and retrieval pipelines.
npm install context-priorityimport { prioritize } from "context-priority";
const items = [
{ content: "User prefers dark mode", score: 0.9 },
{ content: "Session expired at 3pm", score: 0.2 },
{ content: "API key for stripe", score: 0.7 },
{ content: "Favorite color is blue", score: 0.3 },
];
const { result, totalTokens, omitted } = prioritize(items, { maxTokens: 20 });
// → highest-score items that fit in 20 tokensWhen building LLM applications, you often have more context than fits in the window. This library gives you a deterministic way to keep the high-value content and drop the noise — no model calls, no dependencies.
| Return field | Description |
|---|---|
result |
Items that fit, sorted by score descending |
totalTokens |
Token count of the result |
omitted |
Number of items dropped |
Same as prioritize but returns only the content strings.
prioritize(items, {
maxTokens: 4000,
tokenizer: (text) => text.split(/\s+/).length, // word count
});Default tokenizer uses a rough heuristic (~4 chars per token). Pass a tokenizer function for model-specific accuracy.