Context engine

From borghei/Claude-Skills · View interactive version

Context engine is an AI agent skill from the GitHub repository borghei/Claude-Skills. Context management engine for AI coding agents. Use when building agent memory systems, optimizing context windows, allocating token budgets, designing RAG pipelines for code, or managing persistent multi-session agent state.

How to use this skill

Skill file

Context Engine - AI Agent Context Management

Decide what an agent should have in its context window, what it should keep in persistent memory, and what it should retrieve on demand, then measure and trim the window to fit. The goal is an agent that has the facts it needs for the current step and nothing that crowds them out.

When to Use

Clarify First

Before designing or analyzing, settle the inputs below. When one is missing or vague and would change the result, ask about it; when it wouldn't, use a sensible default and say which one you used.

Ask about the two or three inputs that matter most for this request, not the whole list. If the user wants a result straight away, produce it and state your assumptions at the top.

Workflow

  1. Measure. Run context_analyzer.py on the files or prompt you plan to load. Token counts are estimates (about 4 characters per token); use the provider's token-counting endpoint when you need exact numbers.
  2. Budget. Run context_budget_planner.py with the window size and each component's size. It flags components outside their band and, on overflow, lists what to evict first.
  3. Decide where each piece lives, using these rules:
  1. Compact before the cliff. Trigger compaction at a high-water mark (around 70-80% of the window), not at 100%: evict superseded tool outputs first, then summarize-and-replace old history, never the anchors. Write durable conclusions to memory before evicting them. Where the platform offers server-side context editing or compaction, prefer it to hand-rolled summaries.
  2. Prune what you load. context_pruner.py strips comments, docstrings, import blocks and duplicates from code; for Markdown and text it only removes duplicate blocks and extra blank lines, so headings and structure survive.
  3. Verify the result: the task still succeeds with the trimmed context (rerun the step or an eval), and the budget planner reports no overflow.

Quick Start

python3 scripts/context_analyzer.py references/ --budget 128000
python3 scripts/context_budget_planner.py --window-size 200000 --system 4000 --history 60000 --tools 90000 --rag 40000 --reserve-output 8000
python3 scripts/context_pruner.py scripts/memory_indexer.py --aggressive
python3 scripts/memory_indexer.py references/ --query "compaction high-water mark" --top 3

Tools Overview

| Tool | What it does | Key flags | Exit codes |

|------|--------------|-----------|------------|

| context_analyzer.py | Estimated tokens, relevance and suggestions for files or a prompt | <path> or --prompt, --budget, --json | 0 ok, 2 nothing to analyze |

| context_budget_planner.py | Allocate a window across system, memory, history, tools, RAG and reserved output; flag bands and overflow; eviction order | --window-size (required), component sizes, --json | 0 fits, 1 overflow, 2 bad arguments |

| context_pruner.py | Prune code (comments, docstrings, imports, braces, duplicates) or prose (duplicates, blank runs only) | <path> or --stdin [--prose], --level light|moderate|aggressive, --output <dir>, --json | 0 ok, 2 no files / no input |

| memory_indexer.py | TF-IDF index and search over a directory of notes or docs | <dir>, --query, --top, --index-only, --stats | 0 ok, 2 not a directory / nothing indexable |

Anti-Patterns

Mistake: Loading everything that might be relevant "to be safe".

Why it happens: Large windows make it possible, and missing context feels riskier than extra context.

Instead: Irrelevant tokens dilute attention and cost money on every turn. Load anchors, retrieve the rest, and check the budget.

Mistake: Compacting at the limit.

Why it happens: Nothing forces action until the window is full.

Instead: Set a high-water mark and compact while there is headroom to write a good summary.

Mistake: Evicting history without saving what was learned.

Why it happens: Compaction is treated as deletion.

Instead: Write decisions and open questions to memory first; then evict. Compaction is only safe when memory holds the conclusions.

Files

Reference Documentation

Load the reference that matches the task — keep this file lean and pull detail on demand:

Scope & Limitations

Covers context budgeting, memory layering, compaction, code retrieval strategy and multi-agent context hand-off. Does not cover vector store or embedding infrastructure, agent role design or orchestration, monitoring, or prompt writing.

Related skills: engineering/rag-architect (vector stores, embeddings), engineering/agent-designer and engineering/agent-workflow-designer (agent roles, orchestration), engineering/llm-cost-optimizer (caching and token cost), engineering/codebase-onboarding (project summaries to bootstrap context).

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.