Cross-Engine Orchestration System

Version: 1.0.0
Built: 2026-07-05
Status: Production Ready ✅

A complete orchestration system for PAI’s 5 AI engines with intelligent routing, budget awareness, parallel execution, and real-time monitoring.


🎯 What Problem Does This Solve?

Before: Manual engine selection, no cost tracking, no cross-verification, blocking execution

After:

  • ✅ Automatic optimal engine selection
  • ✅ Real-time budget tracking with 4-tier system
  • ✅ Cross-vendor verification for critical tasks
  • ✅ Background async task queue
  • ✅ Live monitoring dashboard
  • ✅ 10-20% cost reduction through smart routing

🚀 Quick Start (30 seconds)

1. Check your budget

bun ~/.claude/PAI/Tools/CrossEngineBudget.ts status

Output:

Budget: 45.2% ($6.78 / $15.00 weekly)
Tier: balanced
Recommendation: Use PNC for quality work, PNG/PNO for research/classification

2. Route a task automatically

bun ~/.claude/PAI/Tools/EngineRouter.ts route "research AI safety papers"

Output:

{
  "selected_engine": "PNG",
  "reason": "PNG optimal for research (web search), $0 cost",
  "confidence": 0.9
}

3. Execute

# Use the recommended engine
agy --dangerously-skip-permissions -p "research AI safety papers"

Result: Free research via PNG instead of 0.45 saved.


📊 The 6 Core Systems

SystemWhat It DoesWhen To Use
EngineRouterSmart task classification + engine selectionEvery task - let it choose optimal engine
CrossEngineBudgetCost tracking + tier-based routingCheck before expensive work
EngineDashboardReal-time monitoring TUIDuring long-running tasks
InteractivePromptStructured CLI promptsWhen user needs to choose
ParallelExecutionMulti-engine cross-verificationSecurity audits, critical decisions
TaskQueueAsync background processingOvernight/batch work

💰 Cost Savings in Action

Example: 7-Task Project

TaskManual (PNC only)Smart RoutingEngine UsedSavings
Research best practices$0.45$0.00PNG$0.45
Classify 50 findings$0.38$0.00PNO$0.38
Summarize docs$0.22$0.00PNO$0.22
Code review$0.95$0.95PNC$0.00
Implementation$1.50$1.50PNC$0.00
Security audit$0.52$1.04PNC+PNX*-$0.52
Deploy$0.15$0.15PNC$0.00
TOTAL$4.17$3.64$0.53 (13%)

* Security uses parallel execution for cross-vendor verification (costs more but worth it)


🎯 Common Workflows

Workflow 1: Research (Free Engine)

# 1. Route
bun EngineRouter.ts route "research Cloudflare Workers security"
# → PNG ($0)
 
# 2. Execute
agy -p "research Cloudflare Workers security"
# → Free research via Gemini

Cost: 0.45 with PNC


Workflow 2: Security Assessment (High Confidence)

# 1. Parallel execution across 2 engines
bun ParallelEngineExecution.ts execute \
  --task "security audit of authentication flow" \
  --engines "PNC,PNX" \
  --synthesize

Output:

{
  "synthesis": {
    "consensus": ["SQL injection in login endpoint"],
    "unique_findings": {
      "PNC": ["XSS vulnerability", "Missing rate limiting"],
      "PNX": ["Session fixation", "CSRF missing"]
    },
    "confidence": "high"
  }
}

Result: 5 findings (1 consensus + 4 unique), high confidence via cross-verification

Cost: $0.36 (worth it for security)


Workflow 3: Overnight Batch Work (Queue)

# 1. Submit to queue (non-blocking)
bun TaskQueue.ts submit \
  --task "classify 500 log entries" \
  --engine auto \
  --priority low \
  --deadline "2026-07-06T09:00:00Z"
 
# 2. Worker processes overnight (cron job)
# (no action needed - runs automatically)
 
# 3. Morning: Get results
bun TaskQueue.ts results task_abc123

Cost: $0 (routed to PNO/Ollama), completed while you sleep


Workflow 4: Budget Alert (Interactive Choice)

Scenario: Budget at 92% (critical - free_only tier)

bun InteractivePrompt.ts ask \
  --question "Budget at 92%. Which engine?" \
  --options '[
    {"label":"PNC","description":"Best quality but will exceed budget 🔴"},
    {"label":"PNG","description":"Free, good for research ✅"},
    {"label":"PNO","description":"Free, local, good for classification ✅"}
  ]'

User chooses PNG → stays within budget, work continues


Workflow 5: Real-Time Monitoring

Terminal 1: Run expensive task

bun ParallelEngineExecution.ts execute \
  --task "architecture review of 10 services" \
  --engines "PNC,PNK,PNX" \
  --synthesize

Terminal 2: Watch live dashboard

bun EngineDashboard.ts

Shows (updates every 2s):

Engine Health
├─ PNC  ✓ healthy  activity: 1  cost: $0.85  
├─ PNK  ✓ healthy  activity: 1  cost: $0.78  
├─ PNX  ✓ healthy  activity: 1  cost: $0.92  

Budget: 58.3% ($8.75 / $15.00)  
Tier: balanced

See cost accumulate in real-time, cancel if approaching limit


📁 Files & Documentation

Tools (2,411 lines across 6 files)

~/.claude/PAI/Tools/
├── EngineRouter.ts                 (520 lines)
├── CrossEngineBudget.ts            (360 lines)
├── EngineDashboard.ts              (312 lines)
├── InteractivePrompt.ts            (268 lines)
├── ParallelEngineExecution.ts      (459 lines)
└── TaskQueue.ts                    (492 lines)

Documentation (6 files, ~82 KB)

  1. ENGINE_ORCHESTRATION.md (9.4 KB)

    • Task routing and classification
    • Health checks and failover
    • Read this first for routing
  2. CROSS_ENGINE_BUDGET.md (13 KB)

    • Budget tracking system
    • 4-tier budget system
    • Cost calculation
    • Read this for budget management
  3. ENGINE_DASHBOARD.md (9.3 KB)

    • Live monitoring TUI
    • Real-time updates
    • Read this for monitoring
  4. INTERACTIVITY.md (13 KB)

    • Cross-engine user prompts
    • Structured choices
    • Read this for user interaction
  5. PARALLEL_EXECUTION_AND_TASK_QUEUE.md (12 KB)

    • Multi-engine verification
    • Async task queue
    • Synthesis algorithm
    • Read this for advanced workflows
  6. ORCHESTRATION_WORKFLOWS.md (22 KB)

    • 7 complete real-world workflows
    • Cost comparisons
    • Decision matrix
    • Read this for end-to-end examples

🎓 Learning Path

1. Start Here (5 minutes)

  • Read this README
  • Run bun CrossEngineBudget.ts status
  • Run bun EngineRouter.ts route "test task"

2. Basic Usage (10 minutes)

3. Budget Awareness (10 minutes)

4. Monitoring (5 minutes)

5. Advanced Workflows (20 minutes)

6. Production Use (as needed)

  • Set up TaskQueue worker (cron job)
  • Use parallel execution for critical security work
  • Monitor budget weekly

Total time: ~50 minutes to full mastery


🔧 Setup (One-time)

1. TaskQueue Worker (Cron)

# Edit crontab
crontab -e
 
# Add this line (process queue every 5 minutes)
*/5 * * * * /home/duane/.bun/bin/bun /home/duane/.claude/PAI/Tools/TaskQueue.ts process >> /home/duane/MEMORY/STATE.claude/task-queue/worker.log 2>&1

2. Verify Engines

# Check all engines are available
claude --version     # PNC
agy --version        # PNG
ollama --version     # PNO
codex --version      # PNX
opencode --version   # PNK

3. Initialize Budget

# Set your weekly budget (default: $15)
bun CrossEngineBudget.ts set-limit --weekly 15.00
 
# Check status
bun CrossEngineBudget.ts status

Done! System is ready.


🎛️ Configuration

Budget Limits

Edit ~/MEMORY/STATE.claude/budget-config.json:

{
  "weekly_budget": 15.00,
  "monthly_budget": 60.00,
  "alert_thresholds": [70, 80, 90]
}

Engine Capabilities

Edit ~/.claude/PAI/Tools/engine-capability-matrix.json:

{
  "engines": {
    "PNC": {
      "cost_per_mtok": 15.0,
      "capabilities": ["code_generation", "complex_reasoning"],
      "health_check": "claude --version"
    }
  }
}

Task Classification Keywords

Add/modify keywords in engine-capability-matrix.json:

{
  "task_types": {
    "research": {
      "keywords": ["research", "find information", "web search"],
      "preferred_engine": "PNG"
    }
  }
}

🐛 Troubleshooting

Router selects wrong engine

Solution: Add task-specific keywords to classification config

# Check what it classified as
bun EngineRouter.ts route "your task" --verbose
 
# Add better keywords to engine-capability-matrix.json

Budget tracking inaccurate

Solution: Check usage log

# View recent entries
tail -20 ~/MEMORY/STATE.claude/engine-usage.jsonl
 
# Manually record missing cost
bun CrossEngineBudget.ts record \
  --engine PNC \
  --tokens-in 5000 \
  --tokens-out 2000

Task queue not processing

Solution: Check worker is running

# Check cron job
crontab -l | grep TaskQueue
 
# Check worker log
tail -20 ~/MEMORY/STATE.claude/task-queue/worker.log
 
# Manually process
bun TaskQueue.ts process

Dashboard not updating

Solution: Restart dashboard, check engine health

# Force refresh (Ctrl+C and restart)
bun EngineDashboard.ts
 
# Check individual engine health
claude --version
agy --version
ollama list

🎯 Decision Matrix

When to use which system:

ScenarioUse ThisWhy
Quick questionEngineRouterAuto-select optimal engine
Research taskEngineRouter → PNGFree, excellent research
ClassificationEngineRouter → PNOFree, local, fast
Code generationDirect PNCBest quality
Security auditParallelExecutionCross-verification
Critical decisionParallelExecutionHigh confidence
Overnight workTaskQueueNon-blocking
Budget > 90%InteractivePromptUser choice
Long taskEngineDashboardMonitor progress

📈 Success Metrics

After 1 week of use, expect:

  • 10-20% cost reduction through smart routing
  • <1% failed tasks via automatic retry
  • High confidence on security work via parallel execution
  • 80%+ background work handled by queue

Track with:

bun CrossEngineBudget.ts report --weekly

🚀 Next Steps

Immediate (this week)

  1. ✅ Set up TaskQueue worker cron job
  2. ✅ Test routing with 10 different task types
  3. ✅ Monitor budget for first week

Short-term (this month)

  1. Tune classification keywords based on usage
  2. Add task dependencies (wait for task A before B)
  3. Implement scheduled recurring tasks
  4. Build web UI for dashboard

Long-term (3-6 months)

  1. ML-based task classification
  2. Predictive budget alerts
  3. Multi-stage pipeline orchestration
  4. Auto-scaling queue workers based on depth

📞 Support

  • Documentation: Start with ORCHESTRATION_WORKFLOWS.md
  • Examples: See workflows 1-7 in the orchestration doc
  • Troubleshooting: Check the troubleshooting section above
  • Configuration: See configuration section above

📜 License

Part of PAI (Personal AI Infrastructure)
Built for Duane, 2026-07-05


Summary: 6 integrated systems, 2,411 lines of code, 82 KB of documentation, production-ready orchestration for PAI’s 5 AI engines with 10-20% cost savings through intelligent routing.

Status: ✅ Production Ready
Next Action: Set up TaskQueue worker cron job, then start using EngineRouter for all tasks