Home/Work/Case 03

Context
Engine

A context compiler for investment agents. It answers one question: what should this agent see, inside this budget, and why?

RoleWrote the memo, built the engine
WhenJun–Aug 2026 · Decasonic
StackNode 24 · TypeScript 5.9 · Express 5
ConsoleReact · Vite · TanStack Query
9stages per compose
2shard budget, then a deterministic fallback
0500 errors, by design
22assertions in the verification suite
01 / The problem

Three ways context
goes wrong.

An agent asks: I'm agent X, my task is Y, it's about company Z, and I have about 6,000 tokens. What should I actually be looking at?

Most systems answer with "the top results from a vector search". That fails in three ways, and each one needed its own fix.

Too much: cost, latency and diluted signal.
Wrong: stale, cross-tenant, personal, or web text carrying an injection.
Unexplainable: nobody can say why the agent saw what it saw.
Rank by value per token, gate before ranking, and make every answer replayable.
02 / How a compose works

Safety first.
Then value.

Four ideas carry the nine stages.

Idea 01 · Gate before value

Reject first, with a reason.

Before anything is ranked, sources are filtered: inactive ones, human exclusions, untrusted sources for agents that can write, tenancy mismatches, personal data, workflow restrictions. Every rejection is written down with its reason.

Idea 02 · Value per token

Stop when the next slice isn't worth it.

Pinned sources go in first. The rest are sorted by value per token, penalized for redundancy, and packed until the marginal utility falls below a floor. Allocation stops there, even with budget left.

Idea 03 · Band the prompt

Untrusted text never gives orders.

The prompt is built as directive, task, evidence and a tail restatement. The lowest trust tier can never reach the directive band, enforced in code and by a database CHECK constraint. Low-trust text is wrapped as data to analyse.

Idea 04 · Learn with a human gate

It can find improvements. It can't ship them.

Shadow A/B runs, ablations and inclusion trials measure what each source is worth. The proposer drafts a change only with enough evidence, and a person approves it. Revert is one click.

Composed inside the 2,000 ms budget · open the trace
  1. Classify: the objective sorted into one of eight task types, or "general" when unsure.
  2. Load: the profile for this agent and task, the source catalog, measured utility and today's spend.
  3. Gate: rejections written down with reasons.
  4. Value: each source scored on task fit, entity match, trust and freshness.
  5. Allocate: packed by value per token until the floor.
  6. Assemble: fetched through the gateway, injection-scanned, tokens counted for real, near-duplicates removed.
  7. Band: directive, task, evidence, tail.
  8. Probe: an advisory check that the context is sufficient.
  9. Emit: a content-addressed object that replays byte for byte.
03 / The contract

Ask once.
Replay forever.

What an agent sends, and what it gets back.

RequestPOST /api/dce/context/compose
{ "agent": "screener", "objective": "Screen this inbound deck", "entity": "company:…", "budget_tokens": 6000}
Responsea context object
{ "hash": "sha256:…", "bands": ["directive", "task", "evidence", "tail"], "slices": [ … ], "excluded": [ { "reason": … } ], "fallback_used": false}

The judge is judged too.

An LLM scores outputs criterion by criterion, but the overall score is recomputed in code from fixed weights. The model's own total is never trusted. Rubrics are immutable: a change is a new version.

Humans label a golden set, and the system measures rank correlation, mean error and bias between its judge and the humans, so it knows how much to trust itself.

Six operator screens.

Compose inspector, with "replay from snapshots".
"Why this context", a printable decision record.
Frontier: quality against input tokens.
Utility, proposals and the golden set.
04 / Decisions worth explaining
01Why stop with budget left?

Because the last few slices usually cost more attention than they add. Past the marginal-utility floor, more context dilutes the signal and raises cost and latency.

02How does it never return a 500?

A 2-second timeout races the whole pipeline. A timeout, an exception or a rate-limit breach all return a deterministic static prompt with fallback_used set to true. Summaries degrade to deterministic extracts under 2.5 seconds, and embedding dedupe falls back to shingle overlap.

03Where does it stop and another system start?

A clean boundary with the firm's context graph: the graph owns the directive band, meaning truth and routing. The engine owns the evidence band: fetch, budget, order and format. The graph never retrieves, and the engine never judges truth.

04How do you know it works?

A 22-assertion verification suite runs against a live server and checks determinism, replay identity, trust-tier protection, tenancy isolation, fail-open behaviour and token accounting. The spec is executable.

Node 24TypeScript 5.9Express 5ZodPinogpt-tokenizerSupabaseClaude · OpenAI · Gemini · xAI

Need something like this? I build these end to end, as a full-time engineer, on contract, or with SKAI Group.

Book a 30-min call
Next case · 04Investor Day
Contact

Let's build

Agents giving answers nobody can explain? Let's look at what they're seeing.