$npx -y skills add itsmostafa/llm-engineering-skills --skill context-engineeringStrategies for managing LLM context windows effectively in AI agents. Use when building agents that handle long conversations, multi-step tasks, tool orchestration, or need to maintain coherence across extended interactions.
| 1 | # Context Engineering |
| 2 | |
| 3 | Context engineering is the discipline of curating and maintaining the optimal set of tokens during LLM inference. Unlike prompt engineering (crafting individual prompts), context engineering focuses on what information enters the context window and when. |
| 4 | |
| 5 | ## Table of Contents |
| 6 | |
| 7 | - [Core Principles](#core-principles) |
| 8 | - [Context Management Strategies](#context-management-strategies) |
| 9 | - [System Prompt Design](#system-prompt-design) |
| 10 | - [Tool Design for Context Efficiency](#tool-design-for-context-efficiency) |
| 11 | - [Long-Horizon Task Patterns](#long-horizon-task-patterns) |
| 12 | - [Implementation Patterns](#implementation-patterns) |
| 13 | - [Best Practices](#best-practices) |
| 14 | - [References](#references) |
| 15 | |
| 16 | ## Core Principles |
| 17 | |
| 18 | ### Context as a Finite Resource |
| 19 | |
| 20 | LLMs have limited "attention budgets." As context length increases, models experience **context rot**—decreased ability to accurately recall information. The goal is finding the smallest possible set of high-signal tokens that maximize desired outcomes. |
| 21 | |
| 22 | ``` |
| 23 | Effective Context = Relevant Information / Total Tokens |
| 24 | ``` |
| 25 | |
| 26 | **Key insight**: More context isn't better. The right context is better. |
| 27 | |
| 28 | ### The Context Pollution Problem |
| 29 | |
| 30 | Every token added to context has costs: |
| 31 | - Increased latency and compute |
| 32 | - Diluted attention to important information |
| 33 | - Higher risk of hallucination from conflicting data |
| 34 | - Reduced model performance on retrieval tasks |
| 35 | |
| 36 | ## Context Management Strategies |
| 37 | |
| 38 | ### 1. Context Trimming |
| 39 | |
| 40 | Drop older conversation turns, keeping only the last N turns. |
| 41 | |
| 42 | | Aspect | Details | |
| 43 | |--------|---------| |
| 44 | | **Mechanism** | Sliding window over conversation history | |
| 45 | | **Pros** | Deterministic, zero latency, preserves recent context verbatim | |
| 46 | | **Cons** | Abrupt loss of long-range context, "amnesia" effect | |
| 47 | | **Best for** | Independent tasks, short interactions, predictable workflows | |
| 48 | |
| 49 | ```python |
| 50 | def trim_context(messages: list, keep_last_n: int = 10) -> list: |
| 51 | """Keep system message + last N turns.""" |
| 52 | system_msgs = [m for m in messages if m["role"] == "system"] |
| 53 | other_msgs = [m for m in messages if m["role"] != "system"] |
| 54 | return system_msgs + other_msgs[-keep_last_n:] |
| 55 | ``` |
| 56 | |
| 57 | ### 2. Context Summarization |
| 58 | |
| 59 | Compress prior messages into structured summaries. |
| 60 | |
| 61 | | Aspect | Details | |
| 62 | |--------|---------| |
| 63 | | **Mechanism** | LLM generates summary of older context | |
| 64 | | **Pros** | Retains long-range memory, smoother UX, scalable | |
| 65 | | **Cons** | Summarization bias risk, added latency, potential compounding errors | |
| 66 | | **Best for** | Complex multi-step tasks, long-horizon interactions | |
| 67 | |
| 68 | ```python |
| 69 | SUMMARIZATION_PROMPT = """Summarize the conversation so far, preserving: |
| 70 | 1. Key decisions made |
| 71 | 2. Important context established |
| 72 | 3. Current task state and goals |
| 73 | 4. Any constraints or preferences expressed |
| 74 | |
| 75 | Be concise but complete. Output as structured markdown.""" |
| 76 | |
| 77 | async def summarize_context(messages: list, model) -> str: |
| 78 | """Generate a summary of conversation history.""" |
| 79 | conversation_text = format_messages_for_summary(messages) |
| 80 | response = await model.generate( |
| 81 | system=SUMMARIZATION_PROMPT, |
| 82 | user=conversation_text |
| 83 | ) |
| 84 | return response.content |
| 85 | ``` |
| 86 | |
| 87 | ### 3. Hybrid Approach |
| 88 | |
| 89 | Combine trimming and summarization for optimal balance. |
| 90 | |
| 91 | ```python |
| 92 | class HybridContextManager: |
| 93 | def __init__( |
| 94 | self, |
| 95 | keep_recent: int = 5, # Recent turns to keep verbatim |
| 96 | summary_threshold: int = 20, # When to trigger summarization |
| 97 | ): |
| 98 | self.keep_recent = keep_recent |
| 99 | self.summary_threshold = summary_threshold |
| 100 | self.running_summary = "" |
| 101 | |
| 102 | def process(self, messages: list) -> list: |
| 103 | if len(messages) < self.summary_threshold: |
| 104 | return messages |
| 105 | |
| 106 | # Summarize older messages |
| 107 | old_messages = messages[:-self.keep_recent] |
| 108 | self.running_summary = summarize(old_messages, self.running_summary) |
| 109 | |
| 110 | # Return summary + recent messages |
| 111 | return [ |
| 112 | {"role": "system", "content": f"Previous context:\n{self.running_summary}"}, |
| 113 | *messages[-self.keep_recent:] |
| 114 | ] |
| 115 | ``` |
| 116 | |
| 117 | ### 4. Session Memory |
| 118 | |
| 119 | Persist reusable facts, preferences, and task state outside the context window. Load only the relevant slice for the current turn. |
| 120 | |
| 121 | | Aspect | Details | |
| 122 | |--------|---------| |
| 123 | | **Mechanism** | External store keyed by user, session, task, or resource | |
| 124 | | **Pros** | Recovers long-range context without carrying all history | |
| 125 | | **Cons** | Requires retrieval, freshness, and deletion policies | |
| 126 | | **Best for** | Agents, project work, personalization, long-running workflows | |
| 127 | |
| 128 | Separate durable memory from ephemeral scratchpads. Durable memory should contain stable facts and explicit decisions, not every intermediate thought. |