Skip to main content
Glama

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

TableJSON Schema
NameRequiredDescriptionDefault
queryNo
token_budgetNo

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes
Behavior5/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations, the description fully discloses behavior: algorithm (0/1 Knapsack), scoring dimensions (recency, frequency, semantic similarity, information density), query refinement for vague queries, and output ordering. No contradictions.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is somewhat lengthy but well-structured with front-loaded purpose. It includes necessary technical details without being overly verbose, earning its sentences.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness5/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's complexity (algorithm, query refinement, ordering), the description covers all critical aspects. Since an output schema exists, return values don't need further explanation.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters5/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema coverage is 0%, so description adds essential meaning: 'query' is for semantic relevance and can be vague, 'token_budget' defaults to 128K. It explains the role of each parameter clearly.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly defines the tool's purpose: 'Select the mathematically optimal context subset for a token budget.' It uses a specific verb ('select') and resource ('context subset'), and it distinguishes itself from sibling tools by being the core context optimization tool.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides clear usage context: 'This is the core tool — call it before sending context to the LLM.' It implies when to use but does not explicitly state when not to use or mention alternatives among the many siblings.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/juyterman1000/entroly'

If you have feedback or need assistance with the MCP directory API, please join our Discord server