Pillar · Context engineering

Context engineering: choose what the model sees.

Prompt engineering phrases one request. Context engineering decides everything the model sees all session, where most failures and most of the token bill live.

The short answer

Context engineering is the practice of curating the exact tokens a coding agent sees on each turn: system instructions, retrieved code, memory, tool definitions, and history.

Read the full answer

The community-standard taxonomy is four levers, Write (persist knowledge outside the window), Select (retrieve the right slice at the right moment), Compress (shrink what enters via caps, compaction and folding), and Isolate (give subtasks fresh windows via subagents). Anvaya implements all four as infrastructure: the Mind graph Writes experience as typed, decaying nodes; six-stage Prism retrieval plus submodular Quorum packing Selects under a 2000/4096-token budget; the context guard and macro compaction Compress by default, with micro-fold measured at −11.0% median uncached (n=5, verification 19/19) and gated for promotion; subagents and output caps Isolate noise. The per-node 12–25x ratio and 2–4K target are design estimates, not a measured session average.

Select, under a budget

Select is a knapsack, not a search box.

Selection decides which candidates earn a seat in 4,096 tokens, by value per token, near-duplicates pushed out.

Token budget · liveillustrative
Served11 of 14
Tokens3,310 / 4,096
  1. 0.94decision keep WAL mode for the store180served
  2. 0.91bug fix retry loop double-charged on a 502420served
  3. 0.88pattern handlers return Result<_, ApiError>260served
  4. 0.86concept an invoice is immutable once issued140served
  5. 0.83synthesis billing failures cluster at the gateway edge610served
  6. 0.79approach tried: idempotency keys in Redis, dropped380served
  7. 0.74bug fix timezone drift in the nightly settlement job720served
  8. 0.71decision Postgres for ledgers, SQLite for local state230served
  9. 0.66pattern every migration ships with a down step160served
  10. 0.62concept settlement window is T+2 business days90served
  11. 0.55approach tried: batching webhooks per merchant540over budget
  12. 0.49bug fix flaky test: clock not frozen in fixtures310over budget
  13. 0.44pattern feature flags are read once per request120served
  14. 0.38synthesis the gateway owns retries; callers never do880over budget

11 of 14 served, 3,310 of 4,096 tokens.

Sample candidates under the real ceiling: 4,096 tokens at 0.85 fill, 12 nodes max. The shipped pack also weighs novelty.anvaya-mind-rs/crates/mind-services/src/quorum.rs · mind-types/src/config.rs

The four levers

Write, select, compress, isolate.

The vocabulary the whole industry converged on, implemented as running code, not advice.

Write: persist outside the windowInstruction files hold standing knowledge. The Mind turns session outcomes into typed nodes with half-lives, so it improves instead of rotting.
Select: retrieve at the moment of needSix-stage Prism retrieval, then submodular Quorum packing under a 2000/4096-token budget. The few nodes that matter, not the graph.
Compress: keep only what the step needsA context guard caps each tool output at 30% of the window; compaction fires at 80%, hard-drop at 95%. Micro-fold (−11.0% uncached) stays gated.
Isolate: keep unrelated work apartSubagents get fresh windows and return 1–2K summaries; tool outputs are capped before they enter context.

The numbers

What bad context costs.

Observed session data and per-node design estimates from Anvaya's memory design work.

8.59M tokens, one sessionObserved Claude Code session at 97.2% cache: ~90% of tokens re-reading files, ~10% reasoning. Without memory, every session re-learns the codebase at full price.
~50K → 2–4K per turnPackedContext design estimate: six-stage retrieval plus Quorum packing serves a few thousand curated tokens instead of tens of thousands of raw file reads.
12–25x per node typeFiles ~20x, decisions ~16x, bugfixes ~24x, threads ~25x, per-node design estimates. Measured end-to-end: micro-fold −11.0% median uncached (n=5, verification 19/19).

Questions

Asked about context engineering.

What is context engineering?

The set of strategies for curating the optimal token set a model sees on each inference call, system instructions, retrieved code, memory, tool outputs, history. Anthropic's one-liner: the smallest possible set of high-signal tokens that maximizes the chance of the desired outcome. Prompt engineering phrases one request; context engineering designs the whole information environment across a session.

Why doesn't a bigger context window fix bad context?

Models attend unevenly across long windows (lost-in-the-middle), every extra token costs money on every subsequent turn, and irrelevant content actively degrades answers: 30–70% of typical agent context is noise. A focused 5K context routinely beats a stuffed 50K one. Windows are headroom, not an invitation.

How does Anvaya do context engineering automatically?

Three compounding layers: the Mind graph stores experience as typed, decaying, drift-checked nodes; Prism + Quorum retrieve and pack only the top few under a token budget each turn; the context guard, macro compaction and (gated) micro-fold control what enters. Session N+1 draws on calibrated memory instead of a blank slate, so context quality can rise with use, which Phase 6 of the benchmark program measures.

What is the 8.59M-token problem?

An observed Claude Code session that burned 8.59M tokens (97.2% cache) with ~90% spent re-reading files and ~10% reasoning, a 175:1 input-to-output ratio. It is the canonical example of what happens without a memory layer: every session pays full price to re-learn the codebase.

Do I still need AGENTS.md or CLAUDE.md with Anvaya?

Yes, keep short, non-inferable rules in versioned instruction files (they load every turn and survive compaction). Anvaya handles the rest: the accumulated why behind decisions, bug history, and patterns that don't belong in a hand-maintained file. Studies show well-scoped instruction files cut runtime ~29% and output tokens ~17%.

Run agents that fit on your laptop.

23.4 MB median own-process memory, measured across 1030 runs (994 verified) and fourteen harnesses. One command installs the harness and the Mind together. Nothing leaves your machine on the Ollama path.

Developer beta. macOS on Apple Silicon and Linux on x86_64 or arm64. Memory figure: --no-mind, macOS/ARM, n=3 per cell. Pre-1.0, pricing not set.