Δ Delta
Read Online · Part III — Advanced Techniques · Chapter 12 of 19

Context Engineering — What the Model Knows

From the free book Delta: Closing the Specification Gap (Dhuri, 2026). The PDF is the canonical edition — figures, tables, and code formatting are simplified online.

Five context layers, prompt caching economics, and the architecture of a up to ~90% on cached prefixes

“Context engineering is the difference between a model that knows your codebase and a model that invents it. The model never knows anything you did not put in its context window.” — The most expensive context mistake is not using too many tokens. It is using the wrong tokens and wondering why the output is generic.

In This Chapter

  1. Five context layers and the caching strategy for each
  2. Prompt caching: C# and Java implementations with up to ~90% on cached prefixes
  3. pgvector RAG for Java teams on PostgreSQL
  4. Context compression: representing your codebase without pasting it
  5. The minimum sufficient context principle: quality, not just cost

~90% cheaper cached reads on stable prompt prefixes using prompt caching (whole-workload savings typically 50–80%). A team making 2,000 AI-assisted generations per month at $0.15/call reduces API costs from $300 to $30 by caching the system prompt. Source: Anthropic API documentation (see docs.anthropic.com/prompt-caching) See Research & Statistics Notes (see Appendix J)

12.1 Five Context Layers Layer

Content

Cache?

Management Rule

System

Role, platform standards, domain rules

Yes — always

One file. Version-controlled. Review on model upgrade. Cache every call.

Knowledge

ADRs, regulatory docs, API contracts

Yes — per session

Retrieved via MCP or RAG at session start. Refreshed on data change.

Code

Interfaces, pattern examples, domain types

Sometimes

Manual RAG or MCP filesystem. Fresh per implementation task.

Task

The specific instruction and constraints

No

Assembled from .md template. Different every call.

Tool Results

MCP outputs: file reads, DB queries

Sometimes

Cache if stable. Refresh if data can change.

ANTI-PATTERN — Pasting the Entire Codebase as Context

AVOID: Including 2,000 lines of unrelated code ”to give the model full context.”

USE: Use minimum sufficient context: the interface to implement + one representative pattern example + relevant domain types. Nothing else. Why: The model attends to everything in its context window. Irrelevant code pulls statistical prediction in irrelevant directions. Less context, when it is the right context, produces better output.

12.2 Prompt Caching — C# Implementation C# — Anthropic SDK with Prefix Caching // File: src/Infrastructure/AI/CachedPromptClient.cs using Anthropic; namespace YourCompany.Infrastructure.AI; public sealed class CachedPromptClient(AnthropicClient client) { // Stable prefix: cached on every call — ~90% cost reduction for ,→ this block private const string SystemPrompt = """ <identity>Senior C# engineer on our payment platform</identity ,→ > <standards>MediatR 12 / Result<T> / Money<USD> / EF Core 9</ ,→ standards> <domain_rules>Money<USD> for all amounts. PHI never in logs.</ ,→ domain_rules> """; public async Task<string> GenerateAsync( string taskPrompt, string? codeContext = null, CancellationToken ct = default) { var system = new List<ContentBlock> { // CacheControl marks this block for caching new TextContent(SystemPrompt) { CacheControl = new ,→ CacheControlEphemeral() } }; var user = new List<ContentBlock>(); if (codeContext is not null) // Code context cached when reused across a session user.Add(new TextContent(codeContext) { CacheControl = new CacheControlEphemeral() }); user.Add(new TextContent(taskPrompt)); // task: NEVER cached var response = await client.Messages.CreateAsync( new MessageCreateParams { Model = Models.ClaudeSonnet4_6

// pinned snapshot;
,→ Opus 4.8 is the current flagship as of mid-2026, MaxTokens = 8096, System = system, Messages = [new Message { Role = RoleEnum.User, ,→ Content = user }] }, ct); return response.Content.OfType<TextContent>().First().Text; } }

QUICK WIN — 30 minutes

Calculate your team’s monthly API spend. Identify the stable prefix in your most common call (system prompt + domain context). Implement CacheControlEphemeral on that prefix. For a team making 1,000 calls/month at $0.15/call: from $150 to $15 per month. Implement this week.

12.3 pgvector RAG — Java Teams on PostgreSQL Java — Spring AI + pgvector Context Retrieval // File: src/main/java/com/yourcompany/ai/CodeContextRetriever.java package com.yourcompany.ai; import org.springframework.ai.vectorstore.SearchRequest; import org.springframework.ai.vectorstore.VectorStore; import org.springframework.stereotype.Service; import lombok.RequiredArgsConstructor; import java.util.stream.Collectors; @Service @RequiredArgsConstructor public class CodeContextRetriever { private final VectorStore vectorStore; /** Returns top-5 relevant code snippets as XML-tagged context. */ ,→ public String retrieve(String task, String domain) { var results = vectorStore.similaritySearch( SearchRequest.builder() .query(task) .topK(5) .filterExpression("domain == '" + domain + "'") .build()); return results.stream() .map(d -> """ <code_snippet file='%s' type='%s'>%s</code_snippet> """.formatted( d.getMetadata().get("file"), d.getMetadata().get("type"), d.getContent())) .collect(Collectors.joining("\n", "<code_context>\n", "\n</code_context>")); } }

THE SPEC PREFIX IS ALSO THE CHEAP PREFIX The stable part of a well-engineered context — role, domain background, constraint block, type definitions — is exactly what providers let you cache, at up to ~90% off the per-token rate on the cached portion. So the discipline that makes context clear (put the durable, reusable material in a stable prefix) is the same discipline that makes it cheap to send on every call. Structure for clarity; bill for the cache. See Chapter 16.4 for the cost argument in full.

Key takeaways

TRY IT NOW

  1. Calculate your team’s monthly API cost. What percentage is the stable prefix? What is the annual saving from caching it?
  2. Map your most common code generation workflow to the five context layers. Which layers are implicit (inconsistent)? Which are explicit (managed)?
  3. For teams with PostgreSQL: design the pgvector schema for code context RAG. What would you index: interfaces, pattern examples, or domain types?
UP NEXT — Chapter 13: MCP — Give Your AI Eyes and Hands

Right now, your AI generates code by predicting what code looks like. Chapter 13 shows you how to give it access to your actual codebase, your actual database schema, and your actual team stan-

94 dards — so it generates code based on what your system actually is, not what codebases usually are.

95

Cite this chapter
Dhuri, Sandeep. (2026). Delta: Closing the Specification Gap. Acuity Press. Chapter 12.
DOI