optimize_context
Selects the mathematically optimal context subset within a token budget by scoring fragments on recency, frequency, semantic similarity, and entropy. Refines vague queries into precise prompts to improve selection accuracy.
Instructions
Select the mathematically optimal context subset for a token budget.
Uses 0/1 Knapsack dynamic programming to maximize relevance within the budget. Scores fragments on four dimensions: recency (Ebbinghaus decay), access frequency (spaced repetition), semantic similarity (SimHash), and information density (Shannon entropy).
QUERY REFINEMENT: Vague queries like "fix the bug" or "add feature" are automatically expanded into precise master prompts using the files already in memory. This improves context selection accuracy and reduces hallucination from selecting wrong files. The response includes query_refinement.refined_query so you can see what drove selection.
Output is ordered for optimal LLM attention: pinned/critical first, high-dependency foundation files early, then by relevance.
This is the core tool — call it before sending context to the LLM.
Args: token_budget: Maximum tokens allowed (default: 128K) query: Current query/task for semantic relevance scoring (can be vague)
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | No | ||
| token_budget | No |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |