context-engine
Context management engine for AI coding agents. Handles context window optimization, persistent memory across sessions, context retrieval strategies, token budget allocation, and knowledge graph construction from codebases. Use when building agent memory systems, optimizing context windows, designing RAG pipelines for code, or managing multi-session agent state.
What this skill does
# Context Engine - AI Agent Context Management **Tier:** POWERFUL **Category:** Engineering **Tags:** context management, AI agents, memory systems, RAG, token optimization, knowledge graphs ## Overview Context Engine provides production-grade patterns for managing what AI agents know, remember, and retrieve. It covers the full lifecycle: ingestion of project knowledge, optimal packing of context windows, persistent memory across sessions, and retrieval-augmented generation for large codebases. The difference between a useful agent and a hallucinating one is context management. ## Core Capabilities ### 1. Context Window Architecture Every AI agent operates within a finite context window. Mismanaging it is the #1 cause of degraded agent performance. #### Token Budget Allocation Framework | Segment | Budget % | Purpose | Priority | |---------|----------|---------|----------| | System Instructions | 5-10% | Agent identity, rules, constraints | Fixed (always loaded) | | Task Context | 20-30% | Current task description, requirements | High (per-request) | | Relevant Code | 25-40% | Source files, dependencies, types | Dynamic (retrieved) | | Conversation History | 10-20% | Prior turns, decisions made | Sliding window | | Tool Results | 5-15% | Command output, search results | Ephemeral | | Reserved Buffer | 5-10% | Output generation headroom | Protected | #### Context Packing Strategies **Greedy Relevance Packing** ``` 1. Score all candidate context by relevance to current task 2. Sort by score descending 3. Pack until budget exhausted 4. Always reserve output buffer ``` - Pros: Simple, fast, works well for focused tasks - Cons: Misses cross-cutting context, no diversity **Tiered Loading** ``` Tier 0 (always loaded): System prompt, project rules, active file Tier 1 (task-specific): Related files, type definitions, tests Tier 2 (on-demand): Documentation, examples, history Tier 3 (retrieved): Search results, RAG chunks ``` - Pros: Predictable, debuggable, respects fixed costs - Cons: Requires upfront tier classification **Adaptive Compression** ``` 1. Load full context for first pass 2. Identify low-signal sections (boilerplate, repetitive code) 3. Summarize or truncate low-signal sections 4. Re-pack with compressed context 5. Preserve high-signal sections verbatim ``` - Pros: Maximizes information density - Cons: Risk of losing important details in compression ### 2. Memory Architecture #### Three-Layer Memory Model ``` ┌─────────────────────────────────────────────────┐ │ Layer 1: Working Memory (Context Window) │ │ Scope: Current conversation/task │ │ Lifetime: Single session │ │ Storage: In-context tokens │ │ Update: Every turn │ ├─────────────────────────────────────────────────┤ │ Layer 2: Session Memory (Persistent Store) │ │ Scope: Project-level learnings │ │ Lifetime: Across sessions │ │ Storage: MEMORY.md, .claude/rules/, CLAUDE.md │ │ Update: End of session or on discovery │ ├─────────────────────────────────────────────────┤ │ Layer 3: Knowledge Base (Indexed Corpus) │ │ Scope: Full codebase + documentation │ │ Lifetime: Persistent, versioned │ │ Storage: Vector store, graph DB, file index │ │ Update: On commit / scheduled reindex │ └─────────────────────────────────────────────────┘ ``` #### Memory Promotion Protocol Knowledge flows upward through layers based on recurrence and value: | Signal | Action | Example | |--------|--------|---------| | Pattern seen 1x | Working memory only | "This file uses tabs" | | Pattern seen 2-3x | Candidate for session memory | "Project uses pnpm everywhere" | | Pattern confirmed across sessions | Promote to CLAUDE.md/rules | "Always use pnpm, never npm" | | Pattern is domain knowledge | Add to knowledge base | "Auth flow uses JWT + refresh tokens" | #### Staleness Detection Context has a shelf life. Stale context causes hallucinations. ``` Freshness Score = f(last_verified, change_frequency, confidence) Fresh (< 7 days, file unchanged): Use directly Aging (7-30 days, file changed): Re-verify before using Stale (> 30 days): Flag, re-retrieve, or discard Unknown (never verified): Treat as low-confidence ``` ### 3. Retrieval Strategies for Code #### File-Level Retrieval Best for: navigating to the right file when the agent knows what it needs. ``` Query: "authentication middleware" Strategy: 1. Filename pattern match: *auth*, *middleware* 2. Import graph: files that import auth modules 3. Symbol search: exported functions matching auth* 4. Content search: files containing auth-related patterns 5. Rank by: recency of edit + import centrality + name match ``` #### Chunk-Level Retrieval (RAG for Code) Best for: finding specific implementations within large files. **Chunking Strategy for Source Code:** - Chunk by function/class boundaries (never mid-function) - Include the function signature + docstring + body as one chunk - Attach metadata: file path, language, exports, imports - Overlap: include 2 lines above/below for context - Max chunk size: 200 lines (larger functions get sub-chunked by logical block) **Embedding Considerations:** - Code-specific embeddings (CodeBERT, StarCoder embeddings) outperform general text embeddings by 15-30% on code retrieval tasks - Hybrid search (keyword + semantic) outperforms either alone - Index function signatures separately for fast symbol lookup #### Dependency-Aware Retrieval When retrieving a function, also retrieve: 1. Its type definitions (interfaces, types it uses) 2. Its direct dependencies (imported functions it calls) 3. Its tests (to understand expected behavior) 4. Its callers (to understand usage context) This "context neighborhood" approach prevents the agent from seeing a function in isolation. ### 4. Knowledge Graph Construction #### Codebase Graph Schema ``` Nodes: - File (path, language, size, last_modified) - Function (name, signature, docstring, complexity) - Class (name, methods, properties, inheritance) - Module (name, exports, dependencies) - Test (name, covers, assertions) - Config (type, values, affects) Edges: - IMPORTS (File → File) - CALLS (Function → Function) - IMPLEMENTS (Class → Interface) - TESTS (Test → Function) - CONFIGURES (Config → Module) - DEPENDS_ON (Module → Module) ``` #### Graph Queries for Context | Agent Question | Graph Query | Context Retrieved | |---------------|-------------|-------------------| | "How does auth work?" | Subgraph around auth module, 2 hops | Auth files + dependencies + tests | | "What breaks if I change X?" | Reverse dependency traversal from X | All callers + their tests | | "What's the API surface?" | All exported functions from API modules | Route handlers + types + middleware | | "How is this tested?" | TEST edges from target function | Test files + fixtures + mocks | ### 5. Context Window Optimization Patterns #### Pattern: Sliding Window with Anchors For long conversations, maintain fixed "anchor" messages while sliding recent history. ``` [System Prompt] ← Fixed anchor (never evicted) [Task Definition] ← Fixed anchor [Key Decision #1] ← Pinned (user marked as important) [Key Decision #2] ← Pinned ... [Turn N-4] ← Sliding window starts here [Turn N-3] [Turn N-2] [Turn N-1] [Current Turn] [Output Buffer] ← Reserved ``` #### Pattern: Progressive Summarization When conversation exceeds budget: 1. Summarize oldest turns into a "conversation summary" block 2. Keep the summary as a single anchor message 3. Update summary every N turns 4. Always keep: first system message, task definition, last 5 turns #### Pattern: Selective Tool Result Caching Tool outputs (file reads, search results, command output) consume the most to
Related in AI Agents
skill-development
IncludedComprehensive meta-skill for creating, managing, validating, auditing, and distributing Claude Code skills and slash commands (unified in v2.1.3+). Provides skill templates, creation workflows, validation patterns, audit checklists, naming conventions, YAML frontmatter guidance, progressive disclosure examples, and best practices lookup. Use when creating new skills, validating existing skills, auditing skill quality, understanding skill architecture, needing skill templates, learning about YAML frontmatter requirements, progressive disclosure patterns, tool restrictions (allowed-tools), skill composition, skill naming conventions, troubleshooting skill activation issues, creating custom slash commands, configuring command frontmatter, using command arguments ($ARGUMENTS, $1, $2), bash execution in commands, file references in commands, command namespacing, plugin commands, MCP slash commands, Skill tool configuration, or deciding between skills vs slash commands. Delegates to docs-management skill for official documentation.
reprompter
IncludedTransform messy prompts into well-structured, effective prompts — single or multi-agent. Use when: "reprompt", "reprompt this", "clean up this prompt", "structure my prompt", rough text needing XML tags and best practices, "reprompter teams", "repromptception", "run with quality", "smart run", "smart agents", multi-agent tasks, audits, parallel work, anything going to agent teams. Don't use when: simple Q&A, pure chat, immediate execution-only tasks. See "Don't Use When" section for details. Outputs: Structured XML/Markdown prompt, quality score (before/after), optional team brief + per-agent sub-prompts, agent team output files. Success criteria: Single mode quality score ≥ 7/10; Repromptception per-agent prompt quality score 8+/10; all required sections present, actionable and specific.
adaptive-compaction
IncludedAdaptive add-on policy and recovery layer that decides WHEN to compact, prune, snapshot, or fork -- replacing fixed-percent auto-compaction across Claude Code, Codex, and MCP-capable hosts. Trigger on auto-compact timing or damage: "when should I compact", "is it safe to compact now or start a fresh session", "auto-compact fires too early/mid-task", "switching to an unrelated task but the window still has space", "context rot", "answers get worse the longer the session runs", "the agent forgot the plan or my decisions after it summarized", "add a layer on top that manages context without changing the agent", raising autoCompactWindow to give the policy room, or installing/tuning a cross-tool compaction policy or PreCompact hook -- even when "compaction" is never said but the problem is context-window pressure or post-summarization memory loss. Do NOT use to summarize a conversation, build RAG, write a summarization prompt (decides WHEN not HOW), or answer max-context-length trivia.
agent-skill-creator
IncludedCreate cross-platform agent skills from workflow descriptions. Activates when users ask to create an agent, automate a repetitive workflow, create a custom skill, or need advanced agent creation. Triggers on phrases like create agent for, automate workflow, create skill for, every day I have to, daily I need to, turn process into agent, need to automate, create a cross-platform skill, validate this skill, export this skill, migrate this skill. Supports single skills, multi-agent suites, transcript processing, template-based creation, interactive configuration, cross-platform export, and spec validation.
llm-wiki
IncludedUse when building or maintaining a persistent personal knowledge base (second brain) in Obsidian where an LLM incrementally ingests sources, updates entity/concept pages, maintains cross-references, and keeps a synthesis current. Triggers include "second brain", "Obsidian wiki", "personal knowledge management", "ingest this paper/article/book", "build a research wiki", "compound knowledge", "Memex", or whenever the user wants knowledge to accumulate across sessions instead of being re-derived by RAG on every query.
skill-master
IncludedAgent Skills authoring, evaluation, and optimization. Create, edit, validate, benchmark, and improve skills following the agentskills.io specification. Use when designing SKILL.md files, structuring skill folders (references, scripts, assets), ingesting external documentation into skills, running trigger evals, benchmarking skill quality, optimizing descriptions, or performing blind A/B comparisons. Keywords: agentskills.io, SKILL.md, skill authoring, eval, benchmark, trigger optimization.