Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

context-priority

Sort and trim items by priority to fit a token budget. Built for AI context windows, prompt construction, and retrieval pipelines.

npm install context-priority
import { 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 tokens

Why

When 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.

API

prioritize(items, options)

Return field Description
result Items that fit, sorted by score descending
totalTokens Token count of the result
omitted Number of items dropped

prioritizeContent(items, options)

Same as prioritize but returns only the content strings.

Custom tokenizer

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.

About

Sort and trim items by priority to fit a token budget — for AI context windows, prompt construction, and retrieval pipelines

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages