AI Context Engineer
When to Use
- Designing what enters the model context each turn
- Optimizing cost/latency via context strategy and token budgeting
- Building context pipelines for agents (prefix, retrieval, history, user input)
- Implementing summarization, compaction, or rolling history
- Debugging context-related failures (lost instructions, overflow, distraction, ignored constraints)
- Choosing delimiters, XML blocks, or structured context formats
When NOT to Use
- Persistent memory store design or long-term recall architecture →
ai-memory-developer
- Full RAG ingest/chunk/embed/index pipelines →
ai-engineer
- AI org operations, release governance, or SLOs →
ai-lead-ops
- Structured token/cost improvement roadmaps with phased KPIs →
ai-token-improvement-plan-engineer
- Commercial/enterprise AI solution architecture →
applied-ai-architect-commercial-enterprise
Related skills
| Need |
Skill |
| Memory stores and long-term recall |
ai-memory-developer |
| RAG ingest/chunk/embed |
ai-engineer |
| System and tool prompts |
prompt-engineer |
| Red-team injection via context |
ai-redteam |
| Cost and production SLAs |
ai-lead-ops |
| Token reduction program and roadmap |
ai-token-improvement-plan-engineer |
| Commercial/enterprise AI architecture |
applied-ai-architect-commercial-enterprise |
| Token efficiency research and ablations |
research-engineer-scientist-tokens |
Core Workflows
1. Context budget and layout
Allocate tokens (example 128k window):
| Block |
Budget % |
Priority |
| System policy + tools |
15–25% |
Fixed, never truncated |
| Retrieved docs / memory |
30–45% |
High, reranked |
| Conversation history |
25–40% |
Compress oldest first |
| User current message |
5–10% |
Never drop |
Use explicit XML/markdown sections: <policy>, <tools>, <context>, <history>, <user>.
See references/context_layout.md for templates and delimiter rules.
2. History management
| Strategy |
When |
| Full recent window |
Short chats, high-stakes instructions in last N turns |
| Rolling summary |
Long sessions; summarize every K turns |
| Anchor messages |
Pin system + key user constraints; summarize middle |
| Structured state |
Replace chat with JSON task state for agents |
Preserve: user goals, constraints, unresolved tool errors, pending confirmations.
See references/history_compression.md for summarization prompts and pitfalls.
3. Retrieval into context
- Query from user message + state summary
- Retrieve candidates (RAG chunks, memories, tool outputs)
- Deduplicate overlapping passages
- Order by relevance; add source labels
- Truncate with sentence boundaries; show "[truncated]" when cut
See references/retrieval_packing.md for packing algorithms and citation format.
4. Caching and prefetch
- Cache stable prefix (system + tools) where provider supports prompt caching
- Prefetch retrieval while user types (optional)
- Invalidate cache on prompt version change
See references/caching_prefetch.md for provider notes and invalidation.
5. Debug context failures
| Symptom |
Likely cause |
Fix |
| Ignored instruction |
Buried in middle / summarized away |
Move to system or last user turn |
| Hallucinated doc |
Weak retrieval |
Raise threshold; require citation |
| Overflow error |
No budget enforcement |
Pre-flight token count; compress |
| Tool confusion |
Ambiguous schemas in context |
Separate tool block; shorten descriptions |
Log token counts per block in dev/staging.
See references/debugging_context.md for instrumentation checklist.
When to load references
- Layout and budgets →
references/context_layout.md
- Summarization →
references/history_compression.md
- RAG/memory packing →
references/retrieval_packing.md
- Caching →
references/caching_prefetch.md
- Debugging →
references/debugging_context.md
1---2name: ai-context-engineer3description: Guides context engineering for LLM systems—assembling prompts, budgeting tokens, prioritizing sources, compressing history, caching, structured context blocks, and debugging context-related failures (lost instructions, overflow, distraction). Use when designing what enters the model context each turn, optimizing cost/latency via context strategy, building context pipelines for agents, implementing summarization or compaction, or fixing "model ignored X" issues—not for persistent memory store design (ai-memory-developer), full RAG index pipelines (ai-engineer), or AI org operations (ai-lead-ops). For a structured token/cost improvement roadmap with phased initiatives and KPIs, use ai-token-improvement-plan-engineer. For commercial/enterprise AI solution architecture (RAG, copilots, platform selection), use applied-ai-architect-commercial-enterprise. Token research and compression ablations: research-engineer-scientist-tokens.4---56# AI Context Engineer78## When to Use910- Designing what enters the model context each turn11- Optimizing cost/latency via context strategy and token budgeting12- Building context pipelines for agents (prefix, retrieval, history, user input)13- Implementing summarization, compaction, or rolling history14- Debugging context-related failures (lost instructions, overflow, distraction, ignored constraints)15- Choosing delimiters, XML blocks, or structured context formats1617## When NOT to Use1819- Persistent memory store design or long-term recall architecture → `ai-memory-developer`20- Full RAG ingest/chunk/embed/index pipelines → `ai-engineer`21- AI org operations, release governance, or SLOs → `ai-lead-ops`22- Structured token/cost improvement roadmaps with phased KPIs → `ai-token-improvement-plan-engineer`23- Commercial/enterprise AI solution architecture → `applied-ai-architect-commercial-enterprise`2425## Related skills2627| Need | Skill |28|---|---|29| Memory stores and long-term recall | `ai-memory-developer` |30| RAG ingest/chunk/embed | `ai-engineer` |31| System and tool prompts | `prompt-engineer` |32| Red-team injection via context | `ai-redteam` |33| Cost and production SLAs | `ai-lead-ops` |34| Token reduction program and roadmap | `ai-token-improvement-plan-engineer` |35| Commercial/enterprise AI architecture | `applied-ai-architect-commercial-enterprise` |36| Token efficiency research and ablations | `research-engineer-scientist-tokens` |3738## Core Workflows3940### 1. Context budget and layout4142**Allocate tokens (example 128k window):**4344| Block | Budget % | Priority |45|---|---|---|46| System policy + tools | 15–25% | Fixed, never truncated |47| Retrieved docs / memory | 30–45% | High, reranked |48| Conversation history | 25–40% | Compress oldest first |49| User current message | 5–10% | Never drop |5051Use explicit XML/markdown sections: `<policy>`, `<tools>`, `<context>`, `<history>`, `<user>`.5253**See `references/context_layout.md` for templates and delimiter rules.**5455### 2. History management5657| Strategy | When |58|---|---|59| Full recent window | Short chats, high-stakes instructions in last N turns |60| Rolling summary | Long sessions; summarize every K turns |61| Anchor messages | Pin system + key user constraints; summarize middle |62| Structured state | Replace chat with JSON task state for agents |6364Preserve: user goals, constraints, unresolved tool errors, pending confirmations.6566**See `references/history_compression.md` for summarization prompts and pitfalls.**6768### 3. Retrieval into context69701. Query from user message + state summary712. Retrieve candidates (RAG chunks, memories, tool outputs)723. Deduplicate overlapping passages734. Order by relevance; add source labels745. Truncate with sentence boundaries; show "[truncated]" when cut7576**See `references/retrieval_packing.md` for packing algorithms and citation format.**7778### 4. Caching and prefetch7980- Cache stable prefix (system + tools) where provider supports prompt caching81- Prefetch retrieval while user types (optional)82- Invalidate cache on prompt version change8384**See `references/caching_prefetch.md` for provider notes and invalidation.**8586### 5. Debug context failures8788| Symptom | Likely cause | Fix |89|---|---|---|90| Ignored instruction | Buried in middle / summarized away | Move to system or last user turn |91| Hallucinated doc | Weak retrieval | Raise threshold; require citation |92| Overflow error | No budget enforcement | Pre-flight token count; compress |93| Tool confusion | Ambiguous schemas in context | Separate tool block; shorten descriptions |9495Log token counts per block in dev/staging.9697**See `references/debugging_context.md` for instrumentation checklist.**9899## When to load references100101- **Layout and budgets** → `references/context_layout.md`102- **Summarization** → `references/history_compression.md`103- **RAG/memory packing** → `references/retrieval_packing.md`104- **Caching** → `references/caching_prefetch.md`105- **Debugging** → `references/debugging_context.md`