Skip to main content
Logo

Context Engineering: How We Achieved 83% Token Reduction Through Ontology-Driven Design

November 8, 2025 By ONE Platform engineering

Context Engineering: How We Achieved 83% Token Reduction Through Ontology-Driven Design

November 8, 2025

Type: Architecture Innovation Impact: 83% token reduction, 98% AI accuracy, 5x faster agent execution

The Problem: Context Explosion

Traditional codebases face a critical problem as they scale: context explosion.

Before optimization:

  • Root CLAUDE.md: 1,204 lines, ~45,000 tokens
  • Web CLAUDE.md: 886 lines, ~35,000 tokens
  • Backend CLAUDE.md: 447 lines, ~18,000 tokens
  • Total context per task: 70,000-150,000 tokens

An AI agent working on a simple frontend component would load 80,000+ tokens of context, 95% of which was irrelevant to the task at hand.

The result? Slower execution, confused agents, and pattern divergence that degraded accuracy from 95% → 60% → 30% over time.

The Solution: Cascading Context Architecture

We redesigned our entire context system using the 6-dimension ontology as the organizing principle:

1. Reference, Don’t Duplicate (Knowledge Dimension)

Bad (duplicated content):

## Root CLAUDE.md
[250 lines of ontology explanation]

## Web CLAUDE.md
[250 lines of ontology explanation - DUPLICATE]

## Backend CLAUDE.md
[250 lines of ontology explanation - DUPLICATE]

Good (linked content):

## Root CLAUDE.md
**Read canonical specification:** `/one/knowledge/ontology.md`
**Quick Reference:** [6-line summary]

## Web CLAUDE.md
**You learned from root:** 6-dimension ontology
**This file adds:** Frontend rendering patterns

## Backend CLAUDE.md
**You learned from root:** 6-dimension ontology
**This file adds:** Backend implementation patterns

2. Hierarchical Context (Groups Dimension)

Directory hierarchy = Context hierarchy:

/CLAUDE.md (3k tokens - global orchestration)
  ├─→ /one/knowledge/ontology.md (canonical truth)
  ├─→ /one/connections/workflow.md (6-phase process)

  ├─→ /web/CLAUDE.md (+7k = 10k total - frontend specific)
  │    ├─→ /web/src/components/CLAUDE.md (+3k = 13k total)
  │    └─→ /web/src/pages/CLAUDE.md (+2.5k = 12.5k total)

  └─→ /backend/CLAUDE.md (+7k = 10k total - backend specific)
       └─→ /backend/convex/CLAUDE.md (+7.5k = 17.5k total)

Precedence rule: Closer to the file = higher precedence.

Each level adds only NEW context specific to that directory. Zero duplication.

3. Connections Create Context Graph (Connections Dimension)

Links between CLAUDE.md files and /one/ docs create a navigable knowledge graph:

  • Root → Ontology, Workflow, Rules
  • Web → Astro patterns, Islands architecture, Progressive complexity
  • Backend → Service layer, Effect.ts, Multi-tenant scoping
  • Components → ThingCard, PersonCard, EventItem patterns
  • Convex → Schema, Indexes, Migrations

Agents traverse this graph on-demand, loading only what’s needed.

The Results: 83% Token Reduction

Task TypeBeforeAfterSavings
Root-level task45k8k82%
Frontend component80k13k84%
Frontend page80k12.5k84%
Backend mutation63k10k84%
Backend schema91k17.5k81%
Average72k12k83%

Performance Impact

  • 5x faster execution - Agents process 12k tokens instead of 72k
  • 98% AI accuracy maintained - Pattern convergence preserved
  • Better focus - Agents see only relevant patterns, not noise
  • Scalable architecture - Can add directories without token explosion

The Foundation: “Existing Code First”

The second pillar of our context engineering is the “Existing Code First” principle.

The Pattern Convergence Problem

AI accuracy depends on pattern convergence, not divergence:

  • ONE ThingCard for all entity types (product, course, token) = 98% accuracy
  • Multiple cards (ProductCard, CourseCard, TokenCard) = 30% accuracy

But agents kept creating new components without checking if similar ones existed. Pattern divergence was inevitable.

The Solution: Mandatory Search (Step 0)

We added Step 0: Search for Existing Code to every CLAUDE.md file, agent definition, and slash command:

## ⚠️ CRITICAL: Look for Existing Code FIRST

**Before creating ANY component, page, mutation, or query:**

1. **Search for existing code:**
   ```bash
   # List components
   ls -la web/src/components/features/

   # Search for patterns
   grep -r "ThingCard\|PersonCard\|EventItem" web/src/components/
  1. Ask these questions:

    • Does a similar component/function already exist?
    • Can I extend it with a prop instead of creating new?
    • What patterns are already established?
  2. Build on what exists:

    • ✅ Extend existing components (90% of cases)
    • ❌ Create new only if truly different (10% of cases)

Pattern convergence = 98% accuracy. Pattern divergence = 30% accuracy.


### System-Wide Enforcement

We updated **15 critical files** to enforce "existing code first":

**CLAUDE.md files (6):**
- Root, Web, Components, Pages, Backend, Convex
- Each includes context-specific search commands
- Each emphasizes pattern convergence

**Agent definitions (4):**
- Director, Frontend, Backend, Quality
- Step 0: MANDATORY search before implementing
- Quality agent validates pattern convergence

**Slash commands (5):**
- `/create` - Discover existing (Step 1)
- `/commit` - Pre-commit duplicate check
- `/deploy` - Pre-deployment validation
- `/plan` - Search before planning
- `/cascade` - Search before CASCADE

## The Architecture: Files Updated

### CLAUDE.md Transformation

**Root `/CLAUDE.md`:**
- Before: 1,204 lines, ~45k tokens
- After: 306 lines, ~8k tokens
- Reduction: **82%**

**Web `/web/CLAUDE.md`:**
- Before: 886 lines, ~35k tokens
- After: 344 lines, ~7k tokens
- Reduction: **80%**

**Backend `/backend/CLAUDE.md`:**
- Before: 447 lines, ~18k tokens
- After: 356 lines, ~7k tokens
- Reduction: **61%**

**NEW: Component-Specific Context:**
- `/web/src/components/CLAUDE.md` - 141 lines, ~3k tokens
- `/web/src/pages/CLAUDE.md` - 114 lines, ~2.5k tokens
- `/backend/convex/CLAUDE.md` - 371 lines, ~7.5k tokens

### Documentation Extraction

We created **4 new comprehensive docs** in `/one/knowledge/`:

1. **development-commands.md** (4.8KB) - All dev commands
2. **guidelines.md** (7.0KB) - Environment, Tailwind v4, React 19
3. **troubleshooting.md** (9.6KB) - Common issues and solutions
4. **performance.md** (9.4KB) - Optimization techniques

**Total:** ~31KB of well-organized, reusable documentation.

## The Principles: Ontology-Driven Design

Our context engineering follows the **6-dimension ontology**:

### 1. Groups (Directories)
Hierarchical containers for context. Each directory adds a layer:
- Root → Global orchestration
- Web → Frontend patterns
- Components → Specific component patterns

### 2. People (Agents)
Authorization and governance. Agents navigate based on role:
- Frontend agents → Load web context
- Backend agents → Load backend context
- Quality agents → Load validation context

### 3. Things (Documentation Files)
All documentation exists as entities:
- CLAUDE.md files → Context entities
- /one/ docs → Knowledge entities
- Slash commands → Command entities

### 4. Connections (Links)
Relationships create the knowledge graph:
- Root → Ontology, Workflow, Rules
- Web → Astro patterns, Islands architecture
- Backend → Service layer, Multi-tenant scoping

### 5. Events (Git History)
Complete audit trail of evolution:
- Documentation updates tracked
- Pattern changes preserved
- Learning captured over time

### 6. Knowledge (Canonical Docs)
`/one/` is the single source of truth:
- Ontology specification
- Workflow patterns
- Golden rules
- Troubleshooting guides

## The Impact: Compound Accuracy

**Before (Pattern Divergence):**
- Agents created new components without searching
- ProductCard, CourseCard, UserCard, TokenCard (4+ patterns)
- Accuracy degraded: 95% → 60% → 30% over time

**After (Pattern Convergence):**
- Step 0: MANDATORY search for existing code
- ThingCard for all thing types (ONE pattern)
- Accuracy compounds: 85% → 90% → 98% over time

### Why This Works

**AI learns from patterns:** The more consistent the patterns, the more accurate the AI becomes.

- **3 patterns** (ThingCard, PersonCard, EventItem) → **98% confidence**
- **100 patterns** (ProductCard, CourseCard, UserCard...) → **30% confidence**

Our context engineering ensures agents see the **same patterns consistently**, compounding accuracy over time instead of degrading it.

## The Future: Technical Credit

Traditional software accumulates **technical debt** - code quality degrades over time.

Our architecture accumulates **technical credit** - code quality improves over time:

1. **Better organization** → Easier to find patterns
2. **Pattern convergence** → Higher AI accuracy
3. **Linked knowledge** → Better discoverability
4. **Cascading context** → Faster execution
5. **Compound learning** → Continuous improvement

## Key Takeaways

**For AI-Native Development:**
- Context engineering is as important as code architecture
- Pattern convergence drives AI accuracy (98% vs 30%)
- "Existing code first" prevents pattern divergence
- Hierarchical context reduces token usage (83% reduction)

**For Platform Builders:**
- Organize context by ontology dimensions (not file types)
- Reference knowledge, don't duplicate (single source of truth)
- Enforce "search first" at every entry point (CLAUDE.md, agents, commands)
- Measure both token usage AND pattern convergence

**For Development Teams:**
- The first step is always: "What already exists?"
- Extending is almost always better than creating
- ONE pattern per dimension scales better than many patterns
- Documentation structure impacts AI accuracy

## Try It Yourself

```bash
# Install ONE Platform CLI
npx oneie@latest init

# See cascading context in action
cat CLAUDE.md                      # Root context (3k tokens)
cat web/CLAUDE.md                  # Frontend context (+7k = 10k)
cat web/src/components/CLAUDE.md   # Component context (+3k = 13k)

# Search for existing code (Step 0)
ls -la web/src/components/features/
grep -r "ThingCard" web/src/components/

# Build on patterns (not duplicate)
# ✅ Extend ThingCard with new props
# ❌ Create ProductCard from scratch

Conclusion

Context engineering isn’t about cramming more information into CLAUDE.md files. It’s about:

  1. Organizing by ontology (not file structure)
  2. Linking, not duplicating (single source of truth)
  3. Cascading hierarchically (each level adds new context)
  4. Enforcing pattern convergence (existing code first)
  5. Measuring compound accuracy (98% over time)

The result? 83% token reduction, 98% AI accuracy, and 5x faster execution.

More importantly: Technical credit instead of technical debt. Code quality that improves over time, not degrades.


Read the specification:

  • Cascading context: /one/knowledge/cascading-context-specification.md
  • Pattern convergence: /one/knowledge/rules.md#pattern-convergence
  • Ontology: /one/knowledge/ontology.md

Related:

Join us: