Skip to main content
Glama

optimize_context

Select the most relevant code fragments within your token budget, auto-refining vague queries into precise prompts to maximize context relevance and reduce AI hallucination.

Instructions

Select a high-value 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 provided, the description fully carries the behavioral disclosure burden. It reveals the underlying algorithm (0/1 Knapsack), scoring dimensions (recency, frequency, semantic similarity, density), the query refinement side effect, and output ordering rationale. This is far beyond what annotations or schema could convey.

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

Conciseness5/5

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

The description is well-structured with clear sections for query refinement, output ordering, and argument semantics. Every sentence adds information—algorithm details, scoring dimensions, and practical guidance. Though longer than one sentence, it is information-dense and free of padding.

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?

Despite having an output schema, the description enriches the context by explaining the output ordering, the query_refinement.refined_query field, and the algorithmic rationale. For a tool with moderate complexity, it covers purpose, behavior, input semantics, and usage context comprehensively.

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%, so the description must compensate. It fully explains both parameters: token_budget as maximum tokens with a default of 128K, and query as the current query/task that can be vague and is expanded by query refinement. This adds meaningful semantics beyond the bare schema fields.

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 opens with a specific verb and resource: 'Select a high-value context subset for a token budget.' It clearly states what the tool does and distinguishes it from sibling tools by positioning it as 'the core tool' for context selection, with a unique knapsack-based optimization approach.

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 gives explicit usage guidance: 'This is the core tool — call it before sending context to the LLM.' It also explains query refinement behavior and when vague queries are acceptable. However, it does not explicitly mention when to avoid this tool or name alternatives, so it falls short of a 5.

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