105-context-trim

A set of instructions for shortening large inputs, such as logs or documents, while keeping the information most relevant to the task.

In plain words
What is it for?
Use it to condense server logs, code, documents, or data before analysis and to preserve key headings, transitions, and facts.
Why use it?
It helps an AI assistant work with limited context when the original material is too large, while retaining structure and important details.

Cursor rule

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add rules/hamzaamjad/cursor-rules/105-context-trim
Clone the repo
git clone --depth 1 https://github.com/hamzaamjad/cursor-rules
Per session 2,492 This file is loaded in full into every session.
When invoked 2,492 The same file — it is already loaded in full.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.02492 $0.02492
Opus 5 $0.01246 $0.01246
Sonnet 5 $0.00498 $0.00498
Haiku 4.5 $0.00249 $0.00249

Measured 2d ago against content hash d7bbcdcae21a, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

105-context-trim scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 2d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

rules/100-cognitive/105-context-trim.mdc · 248 lines

How it starts

The opening of the file, as written. The whole thing — 248 lines — stays where its author put it; the contents beside it link to each section on GitHub.

context-trim.mdc

  • Purpose: To maximize effective context utilization by intelligently compressing input data while preserving semantic integrity. This rule acts as a pre-processor that ensures optimal signal-to-noise ratio, enabling deeper reasoning within computational constraints. Proven Performance: MInference achieves 10x speedup with 90-95% semantic preservation. LLMLingua demonstrates 20:1 compression ratios while maintaining task accuracy.

  • Requirements:

    • Compression Strategies:
      • keep_edges: Preserve first 20% and last 20%, summarize middle
      • semantic_skeleton: Maintain structure markers (headings, key transitions)
      • perplexity_prune: Remove low-information tokens based on predictability
      • hierarchical_summary: Recursive summarization preserving relationships
      • task_aware: Adapt strategy based on task type (code vs. narrative vs. data)
    • Token Budgets:
      • Simple tasks: 4K tokens maximum
      • Moderate tasks: 16K tokens target
      • Complex tasks: 64K tokens with structured compression
      • Never exceed 128K without explicit justification
    • Preservation Priorities:
      1. User query and direct context
      2. Structural markers and transitions
      3. Unique/rare information (high perplexity)
      4. Causal relationships and dependencies
      5. Concrete examples and edge cases
    • Compression Techniques:
      • Replace verbose descriptions with concise labels
      • Extract and index repeated patterns
      • Convert narrative to structured data where possible
      • Use reference pointers for redundant content
      • Maintain running summary of trimmed content
  • Validation:

    • Check: Is token count within specified budget?
    • Check: Are document structure markers preserved?
    • Check: Can key information be reconstructed from compressed version?
    • Check: Is compression strategy appropriate for content type?
    • Check: Is compression ratio logged for optimization?
  • Examples:

    • Scenario: Large log file analysis (1M tokens)

      COMPRESSION PLAN:
      Input: Server logs (1M tokens, 50K lines)
      Strategy: hierarchical_summary + pattern_extraction
      
      Step 1: Pattern Detection
      - Identified: 5 error patterns (80% of content)
      - Extracted: Pattern templates + occurrence counts
      
      Step 2: Temporal Compression  
      - Kept: First/last 100 lines verbatim
      - Summarized: Hourly aggregates for middle section
      
      Step 3: Anomaly Preservation
      - Preserved: 47 unique errors (full context)
      - Indexed: Location pointers for drill-down
      
      Result: 15K tokens (93% reduction)
      Retained: All errors, patterns, temporal flow
      
    • Scenario: Multi-document research (300K tokens)

      COMPRESSION EXECUTION:
      Documents: 12 research papers
      
      Semantic Skeleton:
      - Title, Abstract, Conclusions: KEEP FULL
      - Methods: EXTRACT key algorithms only  
      - Results: CONVERT tables to key findings
      - Discussion: SUMMARIZE to main arguments
      - References: INDEX by relevance score
      
      Cross-Document:
      - Deduplicate common background (40% reduction)
      - Create citation graph (relationships preserved)
      - Build unified terminology map
      
      Output: 45K tokens with full semantic coverage
      
  • Advanced Patterns:

    • Attention-Based Trimming: Use A-shape pattern for documents, Block-Sparse for code
    • Sliding Window: Maintain detailed context for recent tokens, aggressive compression for older
    • Semantic Chunking: Compress at natural boundaries (paragraphs, functions, sections)
    • Lossless References: Store trimmed content with retrieval pointers
    • Progressive Disclosure: Start compressed, expand on demand
  • Implementation Architecture:

    Raw Input → Tokenizer → Strategy Selector → Compressor → Validator → Output
                   ↓              ↓                ↓            ↓          ↓
             [Token Count]  [Content Type]  [Techniques]  [Fidelity]  [Context]
    

Read the full file on GitHub · 248 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 2d ago First seen · 248 lines · 2,492 tokens per session scan A d7bbcdcae21a

Subscribe to this mod's changes

105-context-trim is a cursor rule published in the GitHub repository hamzaamjad/cursor-rules (2 stars, last pushed 1y ago), licensed MIT. It adds 2,492 tokens to every session, about $0.0125 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-31.