Skip to content

Latest commit

 

History

History
166 lines (122 loc) · 7.2 KB

File metadata and controls

166 lines (122 loc) · 7.2 KB

Token Optimization Analysis

Summary

⚠️ ACTUAL MEASURED RESULTS (not estimates):

Original MCP total: 9,618 tokens (15 tools) Ultra-optimized MCP total: 9,234 tokens (15 tools) Actual savings: 384 tokens (4.0% reduction)

Why lower than expected?

  • Schema JSON optimization: 528 tokens (22%)
  • MCP protocol overhead: ~7,200 tokens (unchanged)
  • Result: Only 4% total reduction because protocol overhead dominates

Key insight: The MCP protocol adds 303% overhead (~7,200 tokens) for metadata/serialization, which our optimizations cannot reduce.

Available Optimization Levels

Level 1: Standard (schemas.optimized.ts) ✅ ACTIVE

  • Token reduction: 22% (528 tokens)
  • MCP total: ~7,500 tokens (down from ~9,618)
  • Changes: Compressed descriptions, removed verbose property descriptions
  • Compatibility: 100% - all tool/property names unchanged
  • Recommended for: All users

Level 2: Ultra (schemas.ultra.ts) ⚡ AVAILABLE

  • Token reduction: ~35-40% (~800+ tokens)
  • MCP total: ~6,000 tokens (estimated)
  • Changes: Shortened tool names + property names
    • trm.submitCandidatetrm.submit
    • sessionIdsid
    • repoPathrepo
    • checkpointIdcid
  • Compatibility: Requires client code updates
  • Recommended for: Advanced users needing maximum token savings

Switching to Ultra Mode

# Backup current
cp src/tools/schemas.ts src/tools/schemas.optimized.backup.ts

# Apply ultra optimization
cp src/tools/schemas.ultra.ts src/tools/schemas.ts

# Rebuild
npm run build

Note: Ultra mode requires updating any client code that calls these tools to use the shortened names.

Key Changes (Standard Optimization)

1. Description Compression (468 tokens saved)

Tool Original Optimized Saved
startSession "Initialize a TRM session on a local repository with evaluation commands and halting policy." (23 tokens) "Init TRM session with eval commands & halt policy." (13 tokens) 10
submitCandidate "Apply candidate changes and run evaluation. STRONGLY PREFERRED: Use 'diff' mode (per-file diffs) or 'patch' mode (unified diff) for efficiency. Use trm.getFileContent first to read current file state, then generate diffs. Only use 'files' mode for new files or complete rewrites (discouraged for large files)." (79 tokens) "Apply changes & eval. Prefer diff/patch modes; use getFileContent first." (18 tokens) 61
getFileContent "Read current content of files from the repository. Use this before generating diffs to ensure accurate changes. Returns file contents indexed by path." (30 tokens) "Read file contents from repo." (8 tokens) 22
validateCandidate "Validate candidate changes without applying them (dry-run). Returns validation results with errors, warnings, and preview of changes." (25 tokens) "Validate changes (dry-run)." (6 tokens) 19
getSuggestions "Get AI-powered suggestions for code improvements based on evaluation results and code analysis. Returns top suggestions prioritized by criticality." (25 tokens) "Get AI improvement suggestions." (6 tokens) 19
getFileLines "Read a specific line range from a file. Returns lines with line numbers for easy reference. Useful for reading large files incrementally without loading entire content." (33 tokens) "Read file line range." (6 tokens) 27
suggestFix "Generate actionable fix candidates based on error analysis from the last evaluation. Returns ready-to-apply candidates that can be directly submitted via trm.submitCandidate. Analyzes TypeScript errors, test failures, and lint issues to provide concrete fix suggestions." (50 tokens) "Generate fix candidates from eval errors." (9 tokens) 41

2. Property Description Removal (60 tokens saved)

Removed verbose property descriptions where the property name is self-explanatory:

  • repoPath: "Absolute path to the project repository" → removed
  • zNotes: "Optional initial reasoning notes/hints" → removed
  • rationale: "LLM notes: why these changes, expected effects, hypotheses" → removed
  • path: "Relative path to file" → removed (4 occurrences)
  • diff: "Unified diff format (git diff style)" → removed
  • patch: "Complete unified diff (git diff output)" → removed
  • files: "Complete file contents (use only for new files)" → removed
  • description: "Optional description for the checkpoint" → removed

3. Example Removal

Removed inline examples from descriptions:

  • paths description: Removed example ['src/server.ts', 'package.json']

MCP Protocol Overhead

The /context report shows 9,618 tokens for the 15 tools (~641 tokens/tool), but the actual JSON schema is only 2,384 tokens (~159 tokens/tool).

MCP overhead: ~7,234 tokens (~303% overhead)

This overhead likely includes:

  • Protocol metadata
  • JSON serialization formatting
  • Type information expansion
  • Tool registration structures

Important: The 22.1% schema reduction should translate proportionally to the MCP-reported token count.

Implementation Status

Standard optimization applied (schemas.optimized.ts → schemas.ts) ✅ Build successfulREADME.md updatedOriginal schema backed up (schemas.original.ts)

Validation

All 15 tools maintain identical functionality:

  • ✅ Parameter schemas unchanged
  • ✅ Required fields unchanged
  • ✅ Type constraints unchanged
  • ✅ Default values unchanged
  • ✅ oneOf unions unchanged

Trade-offs

Pros:

  • 22% token reduction improves context availability
  • Avg 36 tokens saved per tool
  • No functionality loss
  • Cleaner, more maintainable definitions
  • Zero breaking changes

Cons:

  • Less detailed descriptions (may require users to reference docs)
  • No inline examples in schemas
  • Property names must be more self-documenting

Recommendations

1. Keep Ultra Optimization (Active) ✅

The 4% savings (384 tokens) is worth keeping. Every token counts.

2. Focus on Usage Patterns for Bigger Wins

For more significant token savings:

  • Use getFileLines instead of getFileContent: 30-50% savings on large files
  • Only read files you'll modify: Avoid loading entire codebase
  • Keep changes small: Smaller candidates = less context needed

3. MCP Protocol is the Real Bottleneck

The 7,200 token overhead from MCP protocol (303%) is the real issue. This requires improvements in the MCP SDK itself, not our schemas.

4. Reality Check

  • ❌ Schema optimizations provide 4% savings (not 22%)
  • ✅ Usage pattern optimizations provide 30-50% savings
  • ✅ Combined with tool features, can achieve up to 40% total efficiency gain

Actual Measured Results

Original:  9,618 tokens (641 tokens/tool avg)
Ultra:     9,234 tokens (616 tokens/tool avg)
Savings:     384 tokens (4.0% reduction)

Top 3 tools by savings:

  1. submitCandidate → submit: -158 tokens (16.6%)
  2. getFileLines → lines: -74 tokens (10.9%)
  3. startSession → start: -70 tokens (7.7%)

This frees up 384 tokens for other context (equivalent to ~100 lines of code).

For detailed tool-by-tool comparison, see token-comparison.md.

Analysis Script

Use node analyze-tokens.js to compare token usage between schema versions.