Knowledge Basephilsj

KB retrieval and token budget — tiered fetch, labels, edge search

decisiondraft owner: philip · updated 2026-11-14

relates: edge-via-seam · agent-skill-packaging · build-process

KB retrieval and token budget

As the KB grows past dozens of entries, agents must fetch only what a task needs, and token cost per task must stay bounded. Proposed contract (draft — awaiting confirmation): tiered retrieval, labels for cross-cutting filters, and edge hybrid search behind the seam.

Problem it solves

The tiered contract (progressive disclosure)

tierartifactcostwhen
T0machine/index.dsl — one line per entry (`idtypetitlepathstatus`)~30 tokens/entryalways first, every task
T1machine/<type>.dsl + one-line "use when" per entrysmallonce the type is known
T2full markdown of selected entries onlyper-entrybefore instantiating or citing
T3relates/uses neighbours, machine/code/<repo>.dslon demandonly when the task touches them

Rules: cite kb:<id> instead of re-reading; never load the whole repo; index first is already AGENTS.md law — this formalizes the tiers.

Filtering mechanisms

  1. Type projections (exists) — directory = type; task→type mapping via SPEC §9 seams (design reads patterns/frameworks/decisions; copy reads copy/; code context reads machine/code/).
  2. Labels/tags (proposed SPEC v1.12) — optional tags: [a, b] inline list in frontmatter; compile emits machine/by-tag/<tag>.dsl. Tags are cross-cutting (domain:aml, stage:design, surface:ui), never a substitute for type.
  3. Relates graph (exists) — expand to neighbours only when the task needs them.
  4. Edge hybrid search — Cloudflare AI Search (GA, MCP endpoint per instance) as the production retrieval adapter: keyword+semantic, top-k with relevance thresholds, returns chunks not whole entries. Behind the seam: local runner = keyword scan over machine/index.dsl; production = AI Search. Git stays canonical; search is a projection, never truth.

Token reduction (published methods, applied)

Decision (proposed)

  1. Adopt the T0→T3 tiered contract as the agent consumption contract (SPEC §9 update).
  2. Add optional tags to frontmatter + machine/by-tag/ projections in SPEC v1.12, with a governance rule: tags are few, controlled, and reviewed like types.
  3. Wire AI Search as the edge retrieval adapter behind the seam when the KB site gains search; local runner stays keyword-over-index.
  4. Measure tokens per task in learner-retro runs — retrieval efficiency becomes a retro metric.

Anti-patterns

Sources