Skip to main content
Glama

optimize_context

Selects the mathematically optimal context subset within a token budget, using relevance scoring and automatic query refinement to improve 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 carries the burden. It details the algorithm (0/1 Knapsack), scoring dimensions (recency, access frequency, semantic similarity, information density), query refinement behavior, and output ordering. No contradictions; it transparently explains all behavioral traits.

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 well-structured with clear sections (purpose, algorithm, refinement, output ordering, args). Though slightly verbose, every sentence adds value. It is front-loaded with the core purpose, making it easy for an AI to quickly understand.

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 presence of an output schema, the description need not explain return values. It comprehensively covers the tool's purpose, parameters, algorithm, query refinement, and invocation guidance. All information needed for correct selection and usage is present.

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 description coverage is 0%, but the description compensates fully. It explains that 'query' can be vague and will be expanded, and that 'token_budget' defaults to 128K. This adds meaning beyond the raw schema, clarifying their roles and acceptable values.

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 states the tool selects the 'mathematically optimal context subset for a token budget,' using specific verbs (select, optimize) and resources (context). It distinguishes itself as 'the core tool' among siblings, setting it apart from other context-related tools like 'compile_docs' or 'prefetch_related'.

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 explicitly advises to 'call it before sending context to the LLM,' providing clear usage context. It mentions query refinement for vague queries, but does not discuss when not to use this tool or directly compare to siblings, though its core positioning implies it should be used before other context tools.

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