Every time you run a coding agent on a serious repository, it burns tens of thousands of tokens re-reading the same Markdown files (AGENTS.md, MEMORY.md, SKILL.md, runbooks, architecture docs) and rediscovering the same multi-step workflows from scratch.

I built an open-source toolkit called Agent Crystals (zero dependencies, Node.js 22+, Apache-2.0, 4-tool MCP server + native harness pattern) to fix that: compile the Markdown teams already maintain—and recurring tool traces—into reviewable, freshness-checked .crystal.json artifacts.

Let me be upfront about prior art. The core compile-and-replay loop isn't new—papers like SkillCache, FlashScript, and PreAct cover similar ground, and tools like Stagehand and Skyvern cache actions internally. What I wanted wasn't another vendor-locked cache. I wanted a portable, Git-friendly, security-first artifact built around the Markdown files engineers already write: SHA-256 + mtime freshness checks (stale_source before step 0), hash-pinned approvals, Zero-Secret scrubbing, write-ahead crash locks (uncertain_write), and OS-sandboxed script execution.

The obvious question everyone is going to ask is:

Does cutting that much context overhead hurt the quality of the answer?

You can't answer that with a 20-line toy repo or an LLM-as-a-judge rubric. So we tested it on our real 17,896-file .NET/Blazor production codebase across 4 real engineering specifications totaling 640 KB of Markdown, and graded every generated SQL and JavaScript implementation directly against the repo's own compiled C# production assemblies—107 held-out executable test cases per model:

  1. Messaging & sync deletion contract (96.6 KB): write a read-only SQLite preview of bulk permanent deletes, tombstones, and canonical survivor tie-breaking (24 SQLite databases).
  2. Document evidence & citation recovery (80.8 KB): write a JS UTF-16 / astral-surrogate / wrapped-PDF quotation validator (29 Deno 2 passages).
  3. Coding harness tool-permission policy (279.3 KB): write a JS glob, alias, mode, and risky-floor evaluator (30 Deno 2 policy cases).
  4. Model routing & context-fit ranker (183.6 KB): write a JS current-vs-growable window, irreducible-input, and compaction-tier ranker (24 Deno 2 routing cases).

Two bugs we hit before we even got good numbers

Before looking at the results, two things broke during testing that are worth sharing because almost every retrieval tool gets them wrong:

  1. Arbitrary magic caps quietly throw away the exact rule you need. Our first implementation returned the top 8 matching bullets (.Take(8)). On a 96 KB spec, that silently dropped a survivor rule past rank #8 and knocked Codex down to 11/24 on SQLite. Same problem in our Context Engine: a hardcoded 2,000-token ceiling treated a 32k local model and a 400k cloud model the exact same way.
    • Fix: We ripped out every arbitrary count cap and replaced it with pure IDF weighting (ln((N+2)/(df+1))) + Otsu's (1979) parameter-free variance-maximizing threshold. Across our 640 KB of specs, that cut query payloads by 64.1% (640.3 KB → 230.0 KB) with zero magic cutoffs. On the Context Engine side, we switched to dynamic remaining-headroom sizing (computed from the active model's real window minus transcript tokens) plus an overflow manifest so anything omitted for headroom is explicitly listed for on-demand lookup instead of silently truncated.
  2. Context Engine and Crystals can step on each other. If a model queries a .md file via crystals.query_md and the Context Engine injects raw chunks of that same unchanged file on the next turn, you pay twice. We wired them together with live SHA-256 tracking: once a file is queried via a Crystal, automatic Context Engine injection skips it as long as its on-disk hash is unchanged, and immediately resumes surfacing it the second the file is edited.

The real results: quality vs. overhead

We ran live A/B pairs on fresh isolated checkouts of the 17,896-file repo comparing Baseline (standard file read / grep / glob, no Context Engine, no Crystals) against Combined (dynamic-headroom Context Engine + Crystals):

Model Executable Cases (Base → Comb) +Imp / -Reg Input Saved Output Saved Total Saved Wall Time Tool Calls
Gemini CLI (gemini-4-neon-low) — 4/4 subsystems 107/107 → 107/107 +0 / -0 -28.2% -12.7% -27.7% -16.5% -26.7%
Codex CLI (gpt-5.6-luna) — 3 completed* 60/83 → 55/83 +13 / -18 -75.3% +11.6% -74.9% +20.0% -28.6%

*Codex's SQLite baseline arm hit an upstream content-policy error.

Two things jump straight out of that table—and both are important:

1. On Gemini, quality didn't budge a millimeter.
Across all four production qualification runs we ran on this codebase—343 executable production-oracle checks total (106/106, 53/53, 77/77, and 107/107)—Gemini scored 343/343 (100%) with zero regressions, while cutting 23–47% total tokens, 13–34% output/reasoning tokens, and 17–57% wall time.

Not just input tokens—output tokens dropped too. Less noise in the prompt meant less wasted reasoning to get to the right code.

2. Codex exposed a real behavioral failure mode: premature stopping on prose summaries.
Look closely at why Codex dropped on two of its tasks while saving 86% of its input tokens:

  • On our first pass, our prompt told the model "if the injected context pack already gives you the answer, use it directly." Codex took that literally, made 0 tool calls, and guessed implementation details from clipped prose (70/77 → 40/77).
  • We changed the framing to explicitly label injected context and Crystal hits as starting pointers—telling models to verify exact code edge cases in the cited source file before coding.
  • That single change cut Codex's regressions nearly in half (-34 → -18) and doubled its score on the Model Routing task (12/24 → 24/24, +12/-0) because it followed crystals.query_md with a targeted source read.
  • On the two tasks where Codex still stopped after 1 crystals.query_md call (-86% input tokens) without opening the C# file, it missed edge cases that the Markdown summary abbreviated (for example, the doc listed 6 risky tool globs while the C# class enforced 10).

That's the honest takeaway: the risk isn't retrieval loss—it's overconfidence. Markdown specs are a fast index, not a substitute for reading the exact source file when reproducing strict code edge cases. When a model uses Crystals + Context Engine to jump straight to the right contract and then checks the cited code, you get massive token and latency savings with zero quality loss.

What about repeatable workflows?

For procedural skills and runbooks (SKILL.md), Crystals go further than retrieval—they compile deterministic steps into code.steps or sandboxed code.script and replay them in ~145 ms at 0 LLM tokens. In our reproducible 3-way live benchmark (with real workspace drift injected mid-run), Crystal caught the moved config file in 6 ms (0 tools dispatched), triggered 1 live LLM repair of the Markdown, recompiled in 4.6 ms, and finished Runs 4–5 at 0 tokens—cutting total tokens by 48.5–75.3% and wall time by 70–78% at 5/5 verified accuracy.

Try it yourself

Everything—including the zero-dependency CLI, 4-tool MCP server, Antigravity plugin, 87-test security/chaos suite, reproducible offline/live benchmarks, and the full unvarnished quality-vs-overhead report—is open source:

git clone https://github.com/scrossen/agent-crystals cd agent-crystals npm test # 87 tests (unit, MCP, red-team sandbox attacks, crash/concurrency chaos) npm run demo # 60-second zero-token demo