context-engineering

Design effective prompts and optimize context for AI models and agents. Covers prompt engineering foundations (clarity, examples, chain of thought, XML structure), the four context engineering strategies (Write, Select, Compress, Isolate), agent memory and session patterns, and before/after examples showing measurable improvement. Use when writing system prompts, designing agent workflows, optimizing context windows, building prompt templates, structuring few-shot examples, reducing context rot, or improving AI output quality.

8 stars

Best use case

context-engineering is best used when you need a repeatable AI agent workflow instead of a one-off prompt.

Design effective prompts and optimize context for AI models and agents. Covers prompt engineering foundations (clarity, examples, chain of thought, XML structure), the four context engineering strategies (Write, Select, Compress, Isolate), agent memory and session patterns, and before/after examples showing measurable improvement. Use when writing system prompts, designing agent workflows, optimizing context windows, building prompt templates, structuring few-shot examples, reducing context rot, or improving AI output quality.

Teams using context-engineering should expect a more consistent output, faster repeated execution, less prompt rewriting.

When to use this skill

  • You want a reusable workflow that can be run more than once with consistent structure.

When not to use this skill

  • You only need a quick one-off answer and do not need a reusable workflow.
  • You cannot install or maintain the underlying files, dependencies, or repository context.

Installation

Claude Code / Cursor / Codex

$curl -o ~/.claude/skills/context-engineering/SKILL.md --create-dirs "https://raw.githubusercontent.com/goondocks-co/open-agent-kit/main/.agents/skills/context-engineering/SKILL.md"

Manual Installation

  1. Download SKILL.md from GitHub
  2. Place it in .claude/skills/context-engineering/SKILL.md inside your project
  3. Restart your AI agent — it will auto-discover the skill

How context-engineering Compares

Feature / Agentcontext-engineeringStandard Approach
Platform SupportNot specifiedLimited / Varies
Context Awareness High Baseline
Installation ComplexityUnknownN/A

Frequently Asked Questions

What does this skill do?

Design effective prompts and optimize context for AI models and agents. Covers prompt engineering foundations (clarity, examples, chain of thought, XML structure), the four context engineering strategies (Write, Select, Compress, Isolate), agent memory and session patterns, and before/after examples showing measurable improvement. Use when writing system prompts, designing agent workflows, optimizing context windows, building prompt templates, structuring few-shot examples, reducing context rot, or improving AI output quality.

Where can I find the source code?

You can find the source code on GitHub using the link provided at the top of the page.

SKILL.md Source

# Context Engineering

Design effective prompts and optimize the full context window for AI models and agents to produce consistently high-quality output.

## Quick Start

### Recipe 1: Improve a vague prompt

**Before:** `Review this code and give feedback.`

**After:**
```xml
<task>Review the provided code for correctness, performance, and maintainability.</task>
<output-format>
For each issue: file/line, severity (critical|warning|suggestion), problem (one sentence), fix (concrete code change).
If no issues, state "No issues found" and list one strength.
</output-format>
<code>{{code_to_review}}</code>
```

**Why it works:** Specifies evaluation criteria, defines output structure, separates instructions from data with XML tags.

### Recipe 2: Design a system prompt

Use the **altitude concept** -- not so high it's useless ("be helpful"), not so low it's brittle ("always use 4 spaces").

1. **Define the role** in one sentence: what the agent IS and IS NOT
2. **Set altitude** -- right level: "Follow the project's existing naming conventions. When none exists, prefer descriptive names over short ones."
3. **Add constraints** using RFC 2119 language (MUST, SHOULD, MAY)
4. **Include 1-2 canonical examples** of ideal output
5. **Define non-goals** to prevent scope creep

See `references/system-prompt-design.md` for full anatomy and templates.

### Recipe 3: Structure agent context with the 4 strategies

When your agent produces inconsistent or degraded output, apply the four strategies:

| Strategy | Action | Example |
|----------|--------|---------|
| **Write** | Craft persistent instructions | System prompt with altitude-appropriate rules |
| **Select** | Choose what enters context | Use `oak_search` to retrieve only relevant code |
| **Compress** | Reduce tokens, preserve signal | Summarize prior conversation turns, delegate to sub-agents |
| **Isolate** | Move information out of context | Store reference docs externally, load just-in-time |

```
1. Start with Write: define a clear system prompt
2. Apply Select: give the agent only the tools and context it needs
3. Add Compress: implement compaction for long sessions
4. Use Isolate: move large reference material behind retrieval
```

See `references/context-engineering-framework.md` for the full framework.

## The Evolution: Prompt to Context Engineering

| Level | Focus | Scope | Key Insight |
|-------|-------|-------|-------------|
| **Basic Prompting** | The question you type | Single user message | Clarity matters |
| **Prompt Engineering** | Crafting effective prompts | Prompt + examples + structure | Technique amplifies quality |
| **Context Engineering** | Everything the model sees | System prompt + tools + memory + retrieved docs + conversation history | The context window IS the product |

Context engineering manages **everything** the model sees -- system prompt, tools, retrieved documents, conversation history, and memory. Every token either helps or hurts. The entire context window is a design surface.

## Prompt Engineering Foundations

These eight techniques form the foundation. Master them before moving to context engineering.

| # | Technique | When to Use | Key Rule |
|---|-----------|-------------|----------|
| 1 | **Be clear and direct** | Always -- this is the baseline | State exactly what you want; remove ambiguity |
| 2 | **Use examples (multishot)** | Output format matters, or the task is nuanced | Show 2-3 examples of ideal input/output pairs |
| 3 | **Chain of thought** | Reasoning, math, multi-step logic | Ask the model to think step-by-step before answering |
| 4 | **Use XML tags** | Separating instructions from data, structured output | Wrap distinct sections in descriptive tags like `<context>`, `<instructions>` |
| 5 | **Give Claude a role** | Specialized tasks needing domain expertise | Set the role in the system prompt, not the user message |
| 6 | **Chain complex prompts** | Multi-step workflows, pipelines | Break into sequential steps; output of step N feeds step N+1 |
| 7 | **Long context tips** | Large documents, many files | Put key instructions at top and bottom; use XML to delineate sections |
| 8 | **Extended thinking** | Complex reasoning, planning, self-correction | Enable thinking to let the model plan before responding |

**Combining techniques:** Most effective prompts use 3-5 techniques together. A code review prompt might combine clarity (#1), XML structure (#4), role (#5), and examples (#2).

See `references/prompt-foundations.md` for detailed explanations, examples, and anti-patterns for each technique.

## Context Engineering Framework (4 Strategies)

### Write: Craft the persistent context

The Write strategy covers everything you author in advance: system prompts, tool descriptions, and instruction files. This is the context you control completely.

**The altitude concept** is the most important principle. Your instructions need to be at the right level of abstraction:

| Altitude | Example | Problem |
|----------|---------|---------|
| Too high | "Write clean code" | No actionable guidance -- the agent fills in its own interpretation |
| Too low | "Always use `Array.prototype.reduce` for aggregation" | Brittle -- breaks when context changes, prevents good judgment |
| Right | "Prefer declarative patterns over imperative loops. Choose the array method that most clearly expresses intent." | Guides without constraining |

**Canonical examples** are powerful -- a single well-chosen example communicates more than paragraphs of rules:

```xml
<system>
You generate API documentation from source code.
<example>
<input>
def calculate_tax(amount: float, rate: float = 0.08) -> float:
    """Calculate sales tax for a given amount."""
    return round(amount * rate, 2)
</input>
<output>
### `calculate_tax(amount, rate=0.08)`
Calculate sales tax for a given amount.
- `amount` (float): The pre-tax amount
- `rate` (float, optional): Tax rate as decimal. Default: 0.08
- **Returns:** float -- Tax amount, rounded to 2 decimal places.
</output>
</example>
</system>
```

**Project-level Write strategy:** Use the `/project-governance` skill to create a constitution (`oak/constitution.md`) and agent instruction files. Your constitution IS your project-level system prompt. The altitude concept applies directly: "write good code" is useless; "copy `src/features/auth/service.py` for new services" is actionable.

See `references/context-engineering-framework.md` for the full Write strategy guide.

### Select: Choose what enters the context window

Select the right information to include; keep everything else out.

**For tools:** Provide the **minimal viable toolset** (unused tools cause confusion), write **unambiguous descriptions** (the model uses these to decide when to call), and prefer **specific tools** over general-purpose ones.

**For retrieved context:** Quality over quantity (3 relevant snippets beat 20 loose ones), recency matters, and always include source attribution (file paths, line numbers).

**Anti-pattern -- context stuffing:**
```
# Bad: dumping everything "just in case"
context = all_docs + all_code + all_history + all_memories
# Good: selecting what's relevant
context = search_relevant_code(task) + get_recent_decisions(task)
```

### Compress: Reduce tokens while preserving information

Compress rather than truncate. Preserve signal while reducing cost.

**Strategies:** Conversation summarization (replace old turns with structured summary), note-taking scratchpad (running notes vs. re-reading history), sub-agent delegation (offload research, return only findings), and tool result cleanup (extract then clear verbose output).

**When to compress:** Context exceeds 50% capacity, agent forgets earlier instructions, responses repeat or lose focus, or tool results contain mostly irrelevant data.

### Isolate: Move information out of the main context

Store information externally and load just-in-time, rather than keeping it in context at all times.

**Patterns:** Just-in-time retrieval (load docs only when needed), progressive disclosure (summary first, details on demand), hybrid context loading (rules in system prompt, reference material behind search), and memory-as-a-tool (store externally, retrieve on demand).

**Progressive disclosure example:** Agent sees summary ("Auth uses JWT") -> needs details (retrieves auth docs) -> needs code (retrieves auth service file). Each step loads only what's needed.

See `references/context-engineering-framework.md` for the complete framework with extended examples.

## Agent Context Patterns

### Sessions vs memory

| Concept | Scope | Lifetime | Purpose |
|---------|-------|----------|---------|
| **Session** | Single conversation | Ephemeral | Working memory for the current task |
| **Memory** | Cross-session | Persistent | Accumulated knowledge and learnings |

The common mistake: keeping everything in the session instead of writing important things to persistent memory.

### Context rot

Output quality degrades as conversations grow longer due to: **dilution** (instructions buried under tool results), **position bias** (models attend more to beginning/end, losing middle), **contradiction accumulation**, and **working memory overflow**.

**Signs:** Agent forgets earlier constraints, responses become generic/repetitive, re-asks answered questions, quality drops vs. early turns.

**Mitigation:** Apply Compress (summarize and compact) and Isolate (move reference material behind retrieval). For long-running agents, implement periodic compaction checkpoints.

### Memory types

| Type | What It Stores | Example | OAK CI Tool |
|------|---------------|---------|-------------|
| **Episodic** | What happened | "Refactored auth module in session #42" | `oak_search --type memory` |
| **Procedural** | How to do things | "To add a feature, copy the strategic_planning exemplar" | `oak_remember` with type `discovery` |
| **Semantic** | Facts and concepts | "The API uses JWT tokens with 1-hour expiry" | `oak_remember` with type `discovery` |

### Memory-as-a-tool pattern

Store externally, retrieve on demand -- instead of accumulating in context:

```
1. Discover → oak_remember("auth tokens expire after 1 hour", type="discovery")
2. Later   → oak_search("auth token expiry") retrieves just-in-time
3. Stale   → oak_resolve_memory(id, status="resolved") cleans up
```

This implements both Compress (don't hold everything) and Isolate (retrieve when needed).

### Compaction strategies

| Strategy | How It Works | Best For |
|----------|-------------|----------|
| **Last-N** | Keep only the last N conversation turns | Simple, predictable token budget |
| **Recursive summarization** | Summarize old turns, keep recent ones verbatim | Long conversations with important early context |
| **Hybrid** | Summarize + keep pinned messages (system prompt, key decisions) | Complex agents with critical persistent instructions |

See `references/agent-context-patterns.md` and `references/memory-and-sessions.md` for detailed patterns and implementation guidance.

## CI Integration: Context Engineering in Practice

OAK's Team tools directly implement the four context engineering strategies:

| Strategy | CI Tool | What It Does |
|----------|---------|--------------|
| **Write** | `oak_remember` / `oak-dev ci remember` | Persist learnings, decisions, and gotchas to long-term memory |
| **Select** | `oak_search` / `oak-dev ci search` | Retrieve only the code and memories relevant to the current task |
| **Isolate** | `oak_context` / `oak-dev ci context` | Assemble just-in-time context for a specific task, pulling from code and memory |
| **Consolidate** | `oak_resolve_memory` / `oak-dev ci resolve` | Resolve stale observations to prevent context rot in the memory store |

### Example workflow

```bash
# WRITE: Store a discovery for future sessions
oak-dev ci remember "Payment service retries 3x with exponential backoff" --type discovery
# SELECT: Find relevant code for a task
oak-dev ci search "payment retry logic" --type code -n 5
# ISOLATE: Assemble just-in-time context
oak-dev ci context "fix payment timeout bug" -f src/services/payment.py
# CONSOLIDATE: Clean up after fixing
oak-dev ci resolve <memory-id>
```

## Before/After Gallery

### Vague prompt to structured prompt

**Before:** `Write tests for the user service.`

**After:**
```xml
<task>Write unit tests for UserService: create_user, get_user_by_email, delete_user.</task>
<constraints>
- pytest with fixtures for setup/teardown
- Test success and error paths for each method
- Mock external API calls (email verification)
- Descriptive test names: test_create_user_with_duplicate_email_raises_conflict
</constraints>
```

### Bloated system prompt to altitude-optimized

**Before** (micromanaging): "Always use f-strings. Always use pathlib. Always use type hints. Use black with line length 88. Use dataclasses for DTOs. Never use print()..."

**After** (right altitude):
```xml
<role>You are a Python developer working in this project.</role>
<principles>
- Follow existing patterns. Find the closest implementation and mirror its style.
- Prefer modern Python (3.10+). When in doubt, check what the codebase uses.
- Write code that reads clearly without comments. Comment only non-obvious "why" decisions.
</principles>
<quality>Run `make check` before considering any change complete.</quality>
```

### Context-stuffed agent to compress + isolate

**Before:** 40,000 tokens (2K rules + 3K README + 8K tool descriptions + 12K API docs + 15K conversation = half irrelevant)

**After:** 8,000 tokens (800 altitude-optimized prompt + 1,200 for 5 relevant tools + 2,000 retrieved context + 4,000 compressed conversation = all relevant)

See `references/before-after-examples.md` for the full gallery with additional transformations.

## When to Use What

| Situation | Strategy | Key Technique | Reference |
|-----------|----------|---------------|-----------|
| Writing a new system prompt | Write | Altitude concept, canonical examples | `references/system-prompt-design.md` |
| Prompt gives inconsistent results | Foundations | Add examples, use XML structure | `references/prompt-foundations.md` |
| Agent losing track of goals | Compress | Conversation summarization, compaction | `references/context-engineering-framework.md` |
| Agent context window filling up | Isolate | Just-in-time retrieval, progressive disclosure | `references/agent-context-patterns.md` |
| Agent forgetting earlier decisions | Write + Compress | Pin critical context, summarize history | `references/memory-and-sessions.md` |
| Need to measure prompt quality | Evaluate | A/B testing, rubric scoring | `references/evaluation-and-testing.md` |
| Setting project-wide AI rules | Write | Constitution as system prompt | `/project-governance` skill |
| Agent repeating mistakes across sessions | Write | Memory-as-a-tool, `oak_remember` | `references/memory-and-sessions.md` |
| Complex multi-step reasoning | Foundations | Chain of thought, extended thinking | `references/prompt-foundations.md` |
| Output format is wrong | Foundations | Examples (multishot), XML tags | `references/prompt-foundations.md` |

## Deep Dives

For detailed guidance on specific topics, consult the reference documents:

- **`references/prompt-foundations.md`** -- Core prompt engineering techniques with examples and anti-patterns
- **`references/context-engineering-framework.md`** -- The four strategies (Write, Select, Compress, Isolate) in depth
- **`references/agent-context-patterns.md`** -- Sessions, memory types, context rot, and compaction strategies
- **`references/system-prompt-design.md`** -- System prompt anatomy, the altitude concept, and reusable templates
- **`references/memory-and-sessions.md`** -- Memory types, the memory-as-a-tool pattern, and OAK CI integration
- **`references/before-after-examples.md`** -- Extended gallery of prompt and context transformations
- **`references/evaluation-and-testing.md`** -- Measuring prompt quality, A/B testing, and iterative improvement

Related Skills

playwright-cli

8
from goondocks-co/open-agent-kit

Automates browser interactions for web testing, form filling, screenshots, and data extraction. Use when the user needs to navigate websites, interact with web pages, fill forms, take screenshots, test web applications, or extract information from web pages.

swarm

8
from goondocks-co/open-agent-kit

Search across multiple projects in your organization's swarm. Use when you need cross-project patterns, org-level conventions, shared decisions, or want to know how other projects solved a problem. Complements the oak (team) skill which covers single-project knowledge. Also use when the user mentions swarm_search, swarm_fetch, swarm_nodes, swarm_status, or asks about what other teams/projects have done, cross-project dependencies, org-wide patterns, or collective knowledge across the organization.

swarm-report

8
from goondocks-co/open-agent-kit

Generate a cross-project comparative report from your organization's swarm. Use when you need an overview of all connected projects, want to compare activity levels, find shared patterns, or identify collaboration opportunities across teams. Also use when the user asks for a "swarm report", "cross-project analysis", "org overview", or wants to understand how projects relate to each other. Requires an active swarm connection.

project-governance

8
from goondocks-co/open-agent-kit

Create and maintain project constitutions and agent instruction files. Use when establishing coding standards, adding project rules, creating agent guidance files, syncing rules across agents, or improving AI agent consistency. Also supports RFC/ADR documents for formalizing technical decisions that feed into the constitution.

pattern-finder

8
from goondocks-co/open-agent-kit

Find recurring code patterns, architectural decisions, and shared conventions across projects in your organization's swarm. Use when you want to discover what patterns other teams use, find candidates for shared libraries, or standardize approaches across the org. Also use when the user asks to "find patterns across projects", "what conventions do other teams follow", "shared code opportunities", or anything about org-wide code reuse. Requires an active swarm connection.

oak

8
from goondocks-co/open-agent-kit

Find out what happened, what was decided, and what depends on what in your codebase. Use this skill whenever you need to: recall past decisions or discussions ("what did we decide about X?"), check what might break before refactoring ("what depends on this module?"), find conceptually similar code that grep would miss ("all the retry/backoff logic"), look up past bugs, gotchas, or learnings, query session history or agent run costs, store observations about the codebase, or understand how components connect end-to-end. Powered by semantic search, memory lookup, and direct SQL against the Oak CI database (.oak/ci/activities.db). Also use when the user mentions oak_search, oak_context, oak_remember, oak_resolve_memory, or asks to run queries against activities.db or oak.

dependency-audit

8
from goondocks-co/open-agent-kit

Audit dependencies across projects in your organization's swarm. Use when you need to find version conflicts, outdated packages, or security risks across teams. Also use when the user asks for a "dependency audit", "version check across projects", "package alignment", or wants to standardize dependencies org-wide. Requires an active swarm connection.

cad-engineering-specialist

9
from vamseeachanta/digitalmodel

**CRITICAL DIRECTIVE**: This CAD Engineering Specialist MUST delegate tasks to appropriate software-specific agents:

marine-engineering-excel-analyzer

9
from vamseeachanta/digitalmodel

Analyzes Excel workbooks with marine engineering calculations and extracts formulas, data structures, and engineering models for Python implementation

reverse-engineering-rust-malware

9
from killvxk/cybersecurity-skills-zh

使用 IDA Pro 和 Ghidra 对 Rust 编译的恶意软件进行逆向工程,掌握处理非空终止字符串、提取 crate 依赖项和 Rust 特有控制流分析的专项技术。

reverse-engineering-ransomware-encryption-routine

9
from killvxk/cybersecurity-skills-zh

对勒索软件加密例程进行逆向工程,以识别密码学算法、密钥生成缺陷,以及通过静态和动态分析挖掘潜在的解密机会。

reverse-engineering-malware-with-ghidra

9
from killvxk/cybersecurity-skills-zh

使用 NSA 的 Ghidra 反汇编器和反编译器对恶意软件二进制文件进行逆向工程,在汇编和伪 C 代码层面理解其内部逻辑、密码学例程、C2 协议和规避技术。适用于恶意软件逆向工程、反汇编分析、反编译、二进制分析或理解恶意软件内部机制等请求。