Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
cupel
Context window management pipeline for LLM applications.
Given a set of context items — messages, documents, tool outputs, memory — and a token budget, Cupel determines the optimal subset and ordering for a model's context window. The pipeline follows a fixed six-stage flow: classify inputs, score every candidate for relevance, deduplicate, sort, slice the list to fit the budget, and place the selected items in an attention-optimal order. Each configurable stage (scorer, slicer, placer) is independently swappable through trait implementations, and every inclusion/exclusion decision carries a traceable reason.
Cupel is framework-agnostic. It accepts pre-counted token lengths and returns
plain Vec<ContextItem> — no LLM client, tokenizer, or async runtime required.
Glossary
| Term | Description |
|---|---|
| ContextItem | An immutable record representing a single piece of context (message, document, tool output, etc.). Constructed via ContextItemBuilder. |
| ContextBudget | Token budget constraints: hard ceiling (max_tokens), soft goal (target_tokens), output reserve, per-kind reserved slots, and safety margin. Validated at construction. |
| Scorer | A trait that computes a relevance score for a context item. Eight built-in implementations cover common strategies. |
| Slicer | A trait that selects items from the scored list to fit within the budget. Built-in: GreedySlice, KnapsackSlice, QuotaSlice. |
| Placer | A trait that determines the final presentation order. Built-in: ChronologicalPlacer, UShapedPlacer. |
| Pipeline | The fixed six-stage executor: Classify, Score, Deduplicate, Sort, Slice, Place. Built via Pipeline::builder(). |
| ContextKind | Extensible string enum classifying item type (Message, Document, ToolOutput, Memory, SystemPrompt). Case-insensitive comparison. |
| ContextSource | Extensible string enum identifying item origin (Chat, Tool, Rag). Case-insensitive comparison. |
Quickstart
A minimal pipeline that scores three items by recency, greedily fills a 500-token budget, and places results in chronological order:
use HashMap;
use Utc;
use *;
#
Multi-scorer pipeline
A more realistic configuration combining KindScorer and RecencyScorer
through a weighted composite. QuotaSlice reserves at least 20% of the
budget for tool outputs while capping messages at 50%. UShapedPlacer
positions the highest-scored items at the start and end of the window where
LLM attention is strongest.
use HashMap;
use Utc;
use *;
#
Scorers
| Scorer | Use case | Mechanism |
|---|---|---|
RecencyScorer |
Prefer recent items | Rank-based; requires timestamp |
PriorityScorer |
Explicit importance ranking | Rank-based; requires priority field |
KindScorer |
Weight by content type | Absolute; configurable weight map |
TagScorer |
Weighted tag matching | Absolute; configurable tag weights |
FrequencyScorer |
Boost commonly-tagged items | Relative; requires tags |
ReflexiveScorer |
Pass through future_relevance_hint | Absolute; requires hint field |
CompositeScorer |
Combine multiple strategies | Weighted average; meta-scorer |
ScaledScorer |
Normalize any scorer to [0,1] | Min-max normalization; wrapper |
Serde support
Enable the serde feature for Serialize/Deserialize on all model types:
[]
= { = "1.2", = ["serde"] }
ContextBudget and ContextItem validate constraints on deserialization —
invalid JSON is rejected at the boundary, not at pipeline runtime.
License
MIT