This skill should be used when the user asks to "understand context", "explain context windows", "design agent architecture", "debug context issues", "optimize context usage", or discusses context components, attention mechanics, progressive disclosure, or context budgeting. Provides foundational understanding of context engineering for AI agent systems.
Add this skill
npx mdskills install muratcankoylan/context-fundamentalsComprehensive educational content on context engineering with solid principles and examples
1---2name: context-fundamentals3description: This skill should be used when the user asks to "understand context", "explain context windows", "design agent architecture", "debug context issues", "optimize context usage", or discusses context components, attention mechanics, progressive disclosure, or context budgeting. Provides foundational understanding of context engineering for AI agent systems.4---56# Context Engineering Fundamentals78Context is the complete state available to a language model at inference time. It includes everything the model can attend to when generating responses: system instructions, tool definitions, retrieved documents, message history, and tool outputs. Understanding context fundamentals is prerequisite to effective context engineering.910## When to Activate1112Activate this skill when:13- Designing new agent systems or modifying existing architectures14- Debugging unexpected agent behavior that may relate to context15- Optimizing context usage to reduce token costs or improve performance16- Onboarding new team members to context engineering concepts17- Reviewing context-related design decisions1819## Core Concepts2021Context comprises several distinct components, each with different characteristics and constraints. The attention mechanism creates a finite budget that constrains effective context usage. Progressive disclosure manages this constraint by loading information only as needed. The engineering discipline is curating the smallest high-signal token set that achieves desired outcomes.2223## Detailed Topics2425### The Anatomy of Context2627**System Prompts**28System prompts establish the agent's core identity, constraints, and behavioral guidelines. They are loaded once at session start and typically persist throughout the conversation. System prompts should be extremely clear and use simple, direct language at the right altitude for the agent.2930The right altitude balances two failure modes. At one extreme, engineers hardcode complex brittle logic that creates fragility and maintenance burden. At the other extreme, engineers provide vague high-level guidance that fails to give concrete signals for desired outputs or falsely assumes shared context. The optimal altitude strikes a balance: specific enough to guide behavior effectively, yet flexible enough to provide strong heuristics.3132Organize prompts into distinct sections using XML tagging or Markdown headers to delineate background information, instructions, tool guidance, and output description. The exact formatting matters less as models become more capable, but structural clarity remains valuable.3334**Tool Definitions**35Tool definitions specify the actions an agent can take. Each tool includes a name, description, parameters, and return format. Tool definitions live near the front of context after serialization, typically before or after the system prompt.3637Tool descriptions collectively steer agent behavior. Poor descriptions force agents to guess; optimized descriptions include usage context, examples, and defaults. The consolidation principle states that if a human engineer cannot definitively say which tool should be used in a given situation, an agent cannot be expected to do better.3839**Retrieved Documents**40Retrieved documents provide domain-specific knowledge, reference materials, or task-relevant information. Agents use retrieval augmented generation to pull relevant documents into context at runtime rather than pre-loading all possible information.4142The just-in-time approach maintains lightweight identifiers (file paths, stored queries, web links) and uses these references to load data into context dynamically. This mirrors human cognition: we generally do not memorize entire corpuses of information but rather use external organization and indexing systems to retrieve relevant information on demand.4344**Message History**45Message history contains the conversation between the user and agent, including previous queries, responses, and reasoning. For long-running tasks, message history can grow to dominate context usage.4647Message history serves as scratchpad memory where agents track progress, maintain task state, and preserve reasoning across turns. Effective management of message history is critical for long-horizon task completion.4849**Tool Outputs**50Tool outputs are the results of agent actions: file contents, search results, command execution output, API responses, and similar data. Tool outputs comprise the majority of tokens in typical agent trajectories, with research showing observations (tool outputs) can reach 83.9% of total context usage.5152Tool outputs consume context whether they are relevant to current decisions or not. This creates pressure for strategies like observation masking, compaction, and selective tool result retention.5354### Context Windows and Attention Mechanics5556**The Attention Budget Constraint**57Language models process tokens through attention mechanisms that create pairwise relationships between all tokens in context. For n tokens, this creates n² relationships that must be computed and stored. As context length increases, the model's ability to capture these relationships gets stretched thin.5859Models develop attention patterns from training data distributions where shorter sequences predominate. This means models have less experience with and fewer specialized parameters for context-wide dependencies. The result is an "attention budget" that depletes as context grows.6061**Position Encoding and Context Extension**62Position encoding interpolation allows models to handle longer sequences by adapting them to originally trained smaller contexts. However, this adaptation introduces degradation in token position understanding. Models remain highly capable at longer contexts but show reduced precision for information retrieval and long-range reasoning compared to performance on shorter contexts.6364**The Progressive Disclosure Principle**65Progressive disclosure manages context efficiently by loading information only as needed. At startup, agents load only skill names and descriptions—sufficient to know when a skill might be relevant. Full content loads only when a skill is activated for specific tasks.6667This approach keeps agents fast while giving them access to more context on demand. The principle applies at multiple levels: skill selection, document loading, and even tool result retrieval.6869### Context Quality Versus Context Quantity7071The assumption that larger context windows solve memory problems has been empirically debunked. Context engineering means finding the smallest possible set of high-signal tokens that maximize the likelihood of desired outcomes.7273Several factors create pressure for context efficiency. Processing cost grows disproportionately with context length—not just double the cost for double the tokens, but exponentially more in time and computing resources. Model performance degrades beyond certain context lengths even when the window technically supports more tokens. Long inputs remain expensive even with prefix caching.7475The guiding principle is informativity over exhaustiveness. Include what matters for the decision at hand, exclude what does not, and design systems that can access additional information on demand.7677### Context as Finite Resource7879Context must be treated as a finite resource with diminishing marginal returns. Like humans with limited working memory, language models have an attention budget drawn on when parsing large volumes of context.8081Every new token introduced depletes this budget by some amount. This creates the need for careful curation of available tokens. The engineering problem is optimizing utility against inherent constraints.8283Context engineering is iterative and the curation phase happens each time you decide what to pass to the model. It is not a one-time prompt writing exercise but an ongoing discipline of context management.8485## Practical Guidance8687### File-System-Based Access8889Agents with filesystem access can use progressive disclosure naturally. Store reference materials, documentation, and data externally. Load files only when needed using standard filesystem operations. This pattern avoids stuffing context with information that may not be relevant.9091The file system itself provides structure that agents can navigate. File sizes suggest complexity; naming conventions hint at purpose; timestamps serve as proxies for relevance. Metadata of file references provides a mechanism to efficiently refine behavior.9293### Hybrid Strategies9495The most effective agents employ hybrid strategies. Pre-load some context for speed (like CLAUDE.md files or project rules), but enable autonomous exploration for additional context as needed. The decision boundary depends on task characteristics and context dynamics.9697For contexts with less dynamic content, pre-loading more upfront makes sense. For rapidly changing or highly specific information, just-in-time loading avoids stale context.9899### Context Budgeting100101Design with explicit context budgets in mind. Know the effective context limit for your model and task. Monitor context usage during development. Implement compaction triggers at appropriate thresholds. Design systems assuming context will degrade rather than hoping it will not.102103Effective context budgeting requires understanding not just raw token counts but also attention distribution patterns. The middle of context receives less attention than the beginning and end. Place critical information at attention-favored positions.104105## Examples106107**Example 1: Organizing System Prompts**108```markdown109<BACKGROUND_INFORMATION>110You are a Python expert helping a development team.111Current project: Data processing pipeline in Python 3.9+112</BACKGROUND_INFORMATION>113114<INSTRUCTIONS>115- Write clean, idiomatic Python code116- Include type hints for function signatures117- Add docstrings for public functions118- Follow PEP 8 style guidelines119</INSTRUCTIONS>120121<TOOL_GUIDANCE>122Use bash for shell operations, python for code tasks.123File operations should use pathlib for cross-platform compatibility.124</TOOL_GUIDANCE>125126<OUTPUT_DESCRIPTION>127Provide code blocks with syntax highlighting.128Explain non-obvious decisions in comments.129</OUTPUT_DESCRIPTION>130```131132**Example 2: Progressive Document Loading**133```markdown134# Instead of loading all documentation at once:135136# Step 1: Load summary137docs/api_summary.md # Lightweight overview138139# Step 2: Load specific section as needed140docs/api/endpoints.md # Only when API calls needed141docs/api/authentication.md # Only when auth context needed142```143144## Guidelines1451461. Treat context as a finite resource with diminishing returns1472. Place critical information at attention-favored positions (beginning and end)1483. Use progressive disclosure to defer loading until needed1494. Organize system prompts with clear section boundaries1505. Monitor context usage during development1516. Implement compaction triggers at 70-80% utilization1527. Design for context degradation rather than hoping to avoid it1538. Prefer smaller high-signal context over larger low-signal context154155## Integration156157This skill provides foundational context that all other skills build upon. It should be studied first before exploring:158159- context-degradation - Understanding how context fails160- context-optimization - Techniques for extending context capacity161- multi-agent-patterns - How context isolation enables multi-agent systems162- tool-design - How tool definitions interact with context163164## References165166Internal reference:167- [Context Components Reference](./references/context-components.md) - Detailed technical reference168169Related skills in this collection:170- context-degradation - Understanding context failure patterns171- context-optimization - Techniques for efficient context use172173External resources:174- Research on transformer attention mechanisms175- Production engineering guides from leading AI labs176- Framework documentation on context window management177178---179180## Skill Metadata181182**Created**: 2025-12-20183**Last Updated**: 2025-12-20184**Author**: Agent Skills for Context Engineering Contributors185**Version**: 1.0.0186
Full transparency — inspect the skill content before installing.