Hybrid Architectures

Multi-model routing strategies

Hybrid Model Architecture Guide

Cost-optimized strategies for routing AIWG workloads across multiple models and providers. Covers cascade patterns, task classification, cost modeling, and AIWG configuration templates for multi-provider setups.


When to Use This Guide

Use this guide if you are:

  • Running AIWG at scale and want to reduce API spend
  • Mixing Claude, GPT/Codex, and local models in one deployment
  • Building custom orchestration that routes tasks to different model tiers
  • Using the RLM addon for sub-agent delegation across providers
  • Designing a team setup where different agents use different backends

Quick Start

# Deploy SDLC framework with cost-optimized routing
# Reasoning agents → Claude Opus
# Coding agents → Claude Sonnet
# Efficiency agents → Claude Haiku
aiwg use sdlc

# Override specific tiers for cost reduction
aiwg use sdlc \
  --reasoning-model claude-opus-4-6 \
  --coding-model claude-sonnet-4-6 \
  --efficiency-model claude-haiku-3-5

# Preview without deploying
aiwg use sdlc --dry-run

Core Principle: Match Model Capability to Task Complexity

The most effective cost reduction strategy is not using cheaper models — it is using the right model for each task.

Task ComplexityAppropriate TierExample Tasks
Analytical, strategicopus (reasoning)Architecture review, threat modeling, requirements analysis
Technical, multi-stepsonnet (coding)Code generation, debugging, test writing
Mechanical, repetitivehaiku (efficiency)Formatting, summarization, file operations, diff generation

Mismatches are expensive in both directions: using opus for formatting wastes money; using haiku for architecture decisions wastes developer time reviewing low-quality output.


Model Cascade Pattern

A cascade uses a fast, cheap model first and escalates to a more capable model only when necessary. This reduces cost on workloads where most inputs are simple, with occasional complex cases.

Cascade Architecture

User request
    │
    ▼
[Fast model: haiku/gpt-5-codex-mini]
    │
    ├── Simple case detected → Return result directly
    │
    └── Complex case detected → Escalate
            │
            ▼
        [Mid-tier: sonnet/codex-mini-latest]
            │
            ├── Resolved → Return result
            │
            └── Requires deep reasoning → Escalate
                        │
                        ▼
                [Premium: opus/gpt-5.3-codex]
                        │
                        └── Return final result

Implementing Cascade in AIWG

Use the RLM addon's sub-agent spawning to implement cascades:

# Deploy RLM addon
aiwg use rlm

# RLM spawns focused sub-agents per file/task
# Configure sub-agent model in RLM agent definition

In a custom orchestration script:

import anthropic

client = anthropic.Anthropic()

def cascade_review(code: str) -> dict:
    # First pass: fast classification
    fast_response = client.messages.create(
        model="claude-haiku-3-5",
        max_tokens=200,
        messages=[{
            "role": "user",
            "content": f"""Classify this code review task complexity.
Return JSON: {{"complexity": "simple|medium|complex", "reason": "..."}}

Code:
{code[:2000]}"""
        }]
    )

    complexity = extract_json(fast_response.content[0].text)["complexity"]

    if complexity == "simple":
        # Use haiku for the full review
        model = "claude-haiku-3-5"
    elif complexity == "medium":
        # Escalate to sonnet
        model = "claude-sonnet-4-6"
    else:
        # Escalate to opus for complex cases
        model = "claude-opus-4-6"

    review_response = client.messages.create(
        model=model,
        max_tokens=2000,
        messages=[{
            "role": "user",
            "content": f"Review this code for issues:\
\
{code}"
        }]
    )

    return {
        "model_used": model,
        "complexity": complexity,
        "review": review_response.content[0].text
    }

Cascade Confidence Thresholds

Define explicit confidence signals for escalation. Avoid using vague "I'm not sure" patterns:

SignalAction
Output contains TODO or [NEEDS REVIEW] markersEscalate
Response length below expected minimumEscalate
Model flags security-relevant code pathsEscalate
Task involves unfamiliar framework or patternEscalate
Fast model timeout exceededEscalate

Cross-Model Consistency Strategies

When multiple models contribute to the same SDLC artifact, outputs must be consistent. Different models have different defaults for formatting, terminology, and structure.

Template-Enforced Consistency

Provide an explicit output template to every model regardless of tier. Do not rely on the model to infer structure:

REVIEW_TEMPLATE = """
## Review: {filename}

### Findings
| Severity | Line | Description | Remediation |
|----------|------|-------------|-------------|
{findings_table}

### Summary
{summary}

### Verdict
{verdict}
"""

def review_with_template(code: str, model: str) -> str:
    return client.messages.create(
        model=model,
        messages=[{
            "role": "user",
            "content": f"""Review this code and fill in the template below exactly.
Do not add sections. Do not change column names.

Template:
{REVIEW_TEMPLATE}

Code to review:
{code}"""
        }]
    ).content[0].text

Normalization Pass

After collecting outputs from multiple models, run a normalization pass using a cheap model to enforce formatting consistency:

def normalize_outputs(outputs: list[str]) -> str:
    combined = "\
\
---\
\
".join(outputs)

    return client.messages.create(
        model="claude-haiku-3-5",  # Cheap normalization pass
        messages=[{
            "role": "user",
            "content": f"""Merge these review outputs into a single report.
Use consistent formatting. Remove duplicates. Preserve all unique findings.
Return only the merged report, no preamble.

Outputs to merge:
{combined}"""
        }]
    ).content[0].text

Shared Vocabulary

Define a glossary in your agent system prompts to prevent different models from using different terms for the same concept:

Terminology used in this project:
- "endpoint" not "route" or "handler"
- "authentication" not "auth" or "authn"
- "requirement" not "story" or "ticket"
- CRITICAL / HIGH / MEDIUM / LOW for severity (not P0/P1 or other schemes)

Task Classification Framework

Classify tasks before assigning a model. This prevents over-spending on simple tasks and under-spending on critical ones.

Task Classification Matrix

DimensionLow (haiku)Medium (sonnet)High (opus)
Reasoning depthPattern matching, formattingMulti-step logic, code synthesisTrade-off analysis, strategic decisions
Output criticalitySummary, draftImplementation, testArchitecture, security
Error toleranceHigh (review later)Medium (test coverage)Low (must be correct)
Context neededSingle file or snippetModule or feature scopeSystem-wide
AmbiguityClearly specifiedSome interpretation neededOpen-ended problem

Classification Examples

TaskTierReason
Generate inline code commentshaikuMechanical, low error tolerance, single file
Write unit tests for a functionsonnetMulti-step, moderate complexity
Design authentication systemopusStrategic, high stakes, cross-cutting
Format JSON outputhaikuDeterministic transformation
Debug intermittent race conditionopusComplex reasoning, system-wide context
Summarize a PR diffhaikuPattern matching, low criticality
Review PR for security issuesopusHigh stakes, cannot miss findings
Convert REST endpoint to GraphQLsonnetTechnical transformation, moderate complexity

Cost Comparison Framework

Pricing Reference (February 2026)

ProviderModelInput (per 1M tokens)Output (per 1M tokens)
Anthropicclaude-opus-4-6~$15~$75
Anthropicclaude-sonnet-4-6~$3~$15
Anthropicclaude-haiku-3-5~$0.25~$1.25
OpenAIgpt-5.3-codexPremium tierPremium tier
OpenAIcodex-mini-latest$1.50$6.00
OpenAIgpt-5-codex-miniBudget tierBudget tier
Local (Ollama)llama3.3:70b~$0.015*~$0.015*
Local (Ollama)codellama:34b~$0.008*~$0.008*

*Electricity cost estimate at $0.15/kWh on RTX 4090.

Example Cost Calculation: SDLC Sprint

Scenario: 10-engineer team, 2-week sprint, using AIWG for full SDLC.

ActivityTierRequestsAvg TokensCost (Claude)
Architecture review (1x)opus520K in / 5K out$0.75 + $0.375
Feature implementation (20x)sonnet2008K in / 4K out$4.80 + $12.00
Test generation (20x)sonnet1005K in / 3K out$1.50 + $4.50
Code comments (all files)haiku5002K in / 1K out$0.25 + $0.625
PR summaries (30x)haiku304K in / 1K out$0.03 + $0.0375
Security review (1x)opus1015K in / 8K out$2.25 + $6.00

Total estimated cost: ~$33/sprint for a 10-engineer team.

Compare to equivalent manual effort: 2 hours/engineer/week in code review and documentation = $2,000+ in developer time.

Cost Optimization Scenarios

Scenario A: All-Opus (Baseline, No Optimization)

500 requests × 10K avg tokens × $15/1M input = $75
500 requests × 3K avg tokens × $75/1M output = $112.50
Total: ~$188/sprint

Scenario B: Tiered Routing (AIWG Default)

Opus: 15 requests = $1.13
Sonnet: 300 requests = $6.30 + $13.50 = $19.80
Haiku: 185 requests = $0.28 + $0.66 = $0.94
Total: ~$22/sprint

Scenario C: Haiku-Heavy with Escalation

Initial haiku pass: 480 requests = $1.20 + $0.60 = $1.80
Escalated to sonnet: 60 requests = $1.44 + $2.70 = $4.14
Escalated to opus: 10 requests = $0.75 + $3.75 = $4.50
Total: ~$10.44/sprint

Scenario C requires custom cascade logic. Scenario B (AIWG's default tiered routing) is the best starting point.


AIWG-Specific: RLM Addon Multi-Provider Sub-Agents

The RLM addon supports spawning sub-agents across different model backends. This is the primary mechanism for hybrid architectures in AIWG.

# Deploy RLM addon
aiwg use rlm

# RLM sub-agent spawning uses the configured model per agent definition
# Each spawned sub-agent inherits the model from its agent file
/rlm-batch "src/**/*.ts" "extract exported function signatures"

Configuring Sub-Agent Models

In the RLM agent definition, set the model for sub-tasks:

---
name: rlm-agent
model: sonnet    # Orchestrator uses sonnet
description: Recursive decomposition specialist
---

You orchestrate large-scale operations by decomposing work into
sub-agent tasks. Each sub-agent is spawned with the model appropriate
for the task type:
- Extraction and search tasks: haiku
- Analysis and synthesis: sonnet
- Security and architecture review: opus

Multi-Provider Sub-Agent Pattern

When AIWG is deployed to multiple providers, sub-agents can target different backends:

Orchestrator (Claude Code, opus)
    │
    ├── Batch extraction tasks → Ollama llama3.2:3b (local, haiku-tier)
    │
    ├── Code review sub-agents → codex-mini-latest (Codex provider)
    │
    └── Security review → claude-opus-4-6 (Claude provider)

This requires deploying AIWG to multiple providers and configuring environment-based routing.


Configuration Templates

Single-Provider Tiered Setup

For teams using Claude Code exclusively:

{
  "claude": {
    "reasoning": { "model": "claude-opus-4-6" },
    "coding": { "model": "claude-sonnet-4-6" },
    "efficiency": { "model": "claude-haiku-3-5" }
  },
  "shorthand": {
    "opus": "claude-opus-4-6",
    "sonnet": "claude-sonnet-4-6",
    "haiku": "claude-haiku-3-5"
  }
}

Hybrid Claude + Local Setup

Routing efficiency tasks to local models, keeping reasoning on Claude:

{
  "claude": {
    "reasoning": { "model": "claude-opus-4-6" },
    "coding": { "model": "claude-sonnet-4-6" },
    "efficiency": { "model": "claude-haiku-3-5" }
  },
  "openai": {
    "reasoning": { "model": "llama3.3:70b" },
    "coding": { "model": "codellama:34b" },
    "efficiency": { "model": "llama3.2:3b" }
  }
}

Set `OPENAI_BASE_URL=http://localhost:11434/v1` for the local endpoint.

Air-Gapped Setup

Full local model stack:

{
  "openai": {
    "reasoning": { "model": "llama3.3:70b" },
    "coding": { "model": "qwen2.5-coder:14b" },
    "efficiency": { "model": "llama3.2:3b" }
  },
  "shorthand": {
    "opus": "llama3.3:70b",
    "sonnet": "qwen2.5-coder:14b",
    "haiku": "llama3.2:3b"
  }
}

Cost-Minimized Cloud Setup

Minimizing spend while keeping cloud convenience:

{
  "openai": {
    "reasoning": { "model": "gpt-5.3-codex" },
    "coding": { "model": "codex-mini-latest" },
    "efficiency": { "model": "gpt-5-codex-mini" }
  },
  "shorthand": {
    "opus": "gpt-5.3-codex",
    "sonnet": "codex-mini-latest",
    "haiku": "gpt-5-codex-mini"
  }
}

Deploying to Multiple Providers

AIWG supports deploying to multiple providers in sequence. Each provider gets its own set of deployed artifacts with provider-appropriate model substitution:

# Deploy to Claude Code (primary)
aiwg use sdlc

# Deploy to Codex (parallel use or team members on Codex)
aiwg use sdlc --provider codex

# Deploy to Cursor
aiwg use sdlc --provider cursor

# Verify all deployments
aiwg status

Each deployment translates `model: opus/sonnet/haiku` shorthand into the correct provider-specific identifier. A single source agent definition serves all providers.


Monitoring and Iteration

Track Model Distribution

After implementing tiered routing, verify the distribution is sensible:

# Check which model each agent uses after deployment
grep -r "^model:" .claude/agents/ | sort | uniq -c | sort -rn
# Expected: most agents use sonnet or haiku, few use opus

Cost Signals to Watch

SignalPossible CauseAction
Opus usage > 30% of requestsOver-assignment to opus tierReview agent tier assignments
High output token countAgents generating verbose outputAdd length constraints to system prompts
Many escalations in cascadeThreshold too aggressiveTune classification confidence thresholds
Quality complaints despite low costHaiku used for wrong task typesRe-classify affected agent to higher tier

See Also

  • `docs/models/claude-optimization.md` — Claude-specific prompt engineering
  • `docs/models/gpt-optimization.md` — GPT/Codex integration patterns
  • `docs/models/local-models.md` — Local model deployment and sizing
  • `docs/configuration/model-configuration.md` — Full models.json reference
  • `agentic/code/addons/rlm/README.md` — Sub-agent orchestration with RLM
  • `agentic/code/frameworks/sdlc-complete/config/models.json` — AIWG default model assignments