← Reference · Nestor G Pestelos Jr

Reference Document

Context Engineering

A citable reference on context engineering: the systematic discipline of token budgeting, memory tiering, attention degradation mitigation, prompt caching, and structured payload assembly for LLMs.

Companion Formats & Essays

Jump to Section

1. Definition: A Data-Pipeline Discipline

Context Engineering is the systematic design, curation, optimization, and real-time assembly of the token sequence passed to a Large Language Model on each execution turn.

Unlike prompt engineering (which focuses on phrasing, tone, and subjective instructions), context engineering treats context as a strictly constrained, high-latency, expensive computing cache. It is governed as a deterministic ETL (Extract, Transform, Load) data pipeline:

2. Attention Degradation & "Lost in the Middle"

While modern models support context windows from 128k to 2M tokens, effective recall is non-uniform across the context window.

Empirical evaluations (Liu et al., 2023) show a distinct U-shaped retrieval accuracy curve:

Context Position Relative Attention Weight Recommended Payload Content
Primacy (First 15%) High (>95% recall) Core system persona, security invariants, behavioral constraints, API schemas, and tool definitions.
Middle (15% – 85%) Degraded (<60% recall in dense context) Supporting reference documents, raw RAG search results, background context. (Must be pre-filtered and relevance-ranked).
Recency (Final 15%) Highest (>98% recall) Immediate turn task, user input, latest tool execution output, and explicit output formatting rules.

3. The Four-Part Structured Prompt Framework

Production system prompts avoid freeform prose. They partition instructions into four deterministic sections:

Component Header Function
1. Role & Identity <identity> / # Role Establishes expertise domain, operational persona, register (e.g. ASD-STE100 technical brevity), and decision authority.
2. Task & Context <context> / # Task Defines the exact objective, relevant environmental state, file paths, and input data.
3. Constraints & Gates <rules> / # Constraints Negative constraints ("Never overwrite originals without confirmation", "Never execute destructive shell commands"), safety thresholds, and reality filters.
4. Output Schema <output_format> / # Format Explicit JSON Schema, XML tag envelope, or tabular structure required for downstream parsing.

4. The Three-Tier Memory Hierarchy

A massive context window cannot replace a persistent, structured memory hierarchy (Mitra, 2026):

Tier Storage Medium Lifecycle & Scope Retrieval Mechanism
Working Memory (Short-Term) In-memory conversation buffer Active session turns (sliding window of latest 5–15 turns). Direct injection into payload.
Episodic Memory (Medium-Term) Git commit logs, session transcripts, daily journals Day-to-day session history and task logs. Time-based search and progressive summarization sweeps.
Semantic Memory (Long-Term) Curated atomic notes, vector database, Knowledge Trees Durable domain rules, user preferences, codebase architecture maps. Vector similarity, keyword grep, and knowledge-graph traversal.

5. Prompt Caching Architecture

Modern frontier inference providers (Anthropic, OpenAI, DeepSeek) support KV (Key-Value) Prompt Caching. When sequential requests share an identical prefix of tokens, the provider reuses the computed attention states from memory rather than re-computing them.

6. Compression, Pruning & Compaction

7. Context Drift & Chat Contamination

Chat Contamination:

In long multi-turn sessions, intermediate errors, exploratory false starts, and apologetic conversational filler accumulate in the turn buffer. The LLM pays attention to its own past degraded outputs, causing hallucination cascading. Solution: periodic compaction, scratchpad wiping, and fresh-session re-anchoring.

Instruction Drift:

As the context length grows past 50,000+ tokens, the model's adherence to initial system constraints weakens due to distance decay. Mitigated by re-anchoring critical constraints in the recency position immediately before the active user prompt.