Skip to main content

4. BSG Compression & LLM Injection

4.1 Rendering Modes

Batho Structured Graph (BSG) supports multiple rendering modes to accommodate different use cases:

ModeBudgetUse CaseOutput
fullUnlimitedDeveloper referencebsg_full.json
hierarchicalUnlimitedDirectory overviewsbsg_hierarchical.json
compressed4K–40K tokensLLM prompt injectionbsg_compressed.json

Mode Selection Guidelines

  • Full: When you need complete context for debugging or deep analysis
  • Hierarchical: For directory-level understanding or architectural reviews
  • Compressed: For LLM interactions where token budget is constrained

4.2 Token Budget Algorithm

The compressed rendering algorithm prioritizes entities based on importance scoring:

Figure 7: Token Budget Algorithm - Flowchart showing how the compressed rendering mode prioritizes entities within token constraints. Start Render, check Mode. If compressed, set Token Budget to N and use Priority Queue by Importance. Include High-Priority Entities until Budget Exhausted. If budget exhausted, Truncate with Hash and Output JSON. If full mode, No Budget Cap and Output JSON. If hierarchical, Tree Structure and Output JSON.

Figure 7: Token Budget Algorithm - Flowchart showing how the compressed rendering mode prioritizes entities within token constraints.

Priority Scoring Factors

Entities are scored based on:

FactorWeightDescription
Public API30%Functions/methods not starting with _
Import Fan-in25%How many files reference this entity
Semantic Tags25%Tags from rule plugins (api, auth, orm, db)
Complexity10%Cyclomatic complexity estimate
Recency10%Recently modified entities

Example: Compressed Output

{
"version": "bsg.v1",
"metadata": {
"render_mode": "compressed",
"token_budget": 8000,
"entities_included": 1542,
"truncated": 89
},
"entities": [
{
"id": "UserService.create",
"type": "METHOD",
"tags": ["api", "auth"],
"summary": "Creates a new user with hashed password"
}
]
}

4.3 Plugin-Based Semantic Overlay

BSG Plugins are YAML-defined rule sets that annotate the graph with semantic tags:

Plugin CategoryPluginsPurpose
Foundationbsg_graph_foundationCore graph structure
Securitybsg_hardcoded_secret_catcher, bsg_auth_boundary_shieldDetect secrets, auth boundaries
Performancebsg_nplus1_query_catcher, bsg_resource_leak_preventerFind perf anti-patterns
Reliabilitybsg_dependency_blast_radius, bsg_silent_failure_catcherImpact analysis
Infrastructurebsg_iac_drift_sentinel, bsg_schema_migration_enforcerIaC / schema governance
APIbsg_api_contract_guardianAPI contract enforcement

Plugin Configuration Example

# batho.yaml
rules:
enabled: true
auto_load_all_plugins: true
builtin_plugins:
- bsg_core
- bsg_silent_failure_catcher
- bsg_dependency_blast_radius
- bsg_hardcoded_secret_catcher

4.4 LLM Integration

BSG output is optimized for LLM consumption:

AspectOptimization
FormatJSON with minimal whitespace
StructureFlat entity list with summaries
ReferencesUUID-based cross-links
TagsSemantic category markers
EncodingUTF-8 with ASCII-safe fallback