Context Engineering

The discipline of optimizing and minimizing the context (prompts and reference material) fed into an LLM. Curates only the exact information required for a task to reduce noise, improve inference accuracy, and curb cost and memory usage.

General Meaning

As the context fed into an LLM grows longer, the model becomes more likely to overlook key facts buried in the middle (the Lost in the Middle phenomenon) or get distracted by irrelevant passages. Larger input token counts also increase inference latency, API costs, and memory consumption.

Context Engineering goes beyond tweaking prompt phrasing: it uses deterministic preprocessing to extract, filter, and compress only the exact information needed so the model receives the smallest, clearest input capable of producing an accurate answer.

Why It Matters

  • Higher accuracy: Stripping away irrelevant noise lets the model focus its attention on the exact facts and instructions needed for the decision.
  • Saving memory and compute resources: Reducing input token counts lowers KV cache memory footprint and shortens prefill latency.
  • Stronger instruction following: Concise inputs make it much less likely for the model to drift away from system constraints or JSON schema contracts.

How the Site Factory Uses It

In the Site Factory, whenever tasks are passed to local LLMs or external AI agents, deterministic preprocessing narrows the input to specific lines and fields rather than dumping entire files. Under ops/local-first.md, source.local_extract explicitly specifies the input filename and line range (e.g., lines: [1, 6]), capping the payload at a maximum of 81 lines and 9,000 characters.

Similarly, session handoffs (handoff) summarize only the essential state needed for the next session rather than re-reading entire past conversation transcripts, and instruction documents (rules.md and CLAUDE.md) are scoped to the sections relevant to the current task. On the output side, engine/llm.py sets "think": false during structured extraction to suppress verbose reasoning traces and emit only the target JSON structure.

Caveats & Operational Rules

  • Never dump entire documents just because the context window is large: Even if a model supports over 100K tokens, feeding massive uncurated files increases the risk of distraction from irrelevant text.
  • Do not trim so aggressively that prerequisite context is lost: Cutting essential surrounding lines destroys the basis for accurate extraction; the Site Factory explicitly specifies line ranges and required term lists in the schema to prevent under- or over-trimming.
  • Do not let multi-turn conversation history accumulate indefinitely: Carrying long trial-and-error chat logs forward introduces stale noise; write a clean handoff at task boundaries and start fresh sessions.

Related Terms

References

  • Book: Chip Huyen, "AI Engineering", Chapter 5 (Prompt and Context Construction)

Verified: 2026-11-08