← Home System Overview Architecture Data Pipeline Health System Usage System Workflows MCP Tools

Architecture — The Brain

Blueprint · Last updated 2026-05-02

Module Map

ModulePurpose
delegate.pyCLI entry point — parses --provider, --type, --prompt, --context flags; builds a Task; invokes the orchestrator; prints the result.
brain/orchestrator.pyCore dispatcher — runs a Task through the router and chosen adapter; retries on failure using the per-type fallback chain.
brain/router.pyRouting table plus per-task_type ordered fallback chains; maps each task type to a list of provider keys to try in sequence.
brain/stats.pyAppends every call's metadata to stats/usage.json (provider, model, tokens, latency, cost, success, error) regardless of outcome.
brain/adapters/One file per provider — each exports async def call(task: Task) -> Result.
mcp_server.pyMCP server — exposes the delegate tool natively in Claude Code; same orchestrator, no bash required.
session_start.pySession startup script — runs health check, prints provider status table, emits routing advisories.
health_check.pyPolls each provider with a lightweight test call; reports online/offline/degraded status.
brain/model_state.pyReads/writes stats/model_state.json — pause, disable, or unpause individual provider models based on health probe outcomes.
brain/health_rollup.pyAggregates raw health_log.json entries into per-provider uptime and latency summaries for display.
report.pyFormats and prints a usage summary; reads tier labels live from the adapter REGISTRY (no hardcoded list).
push_stats.pyUpdates the GitHub README stats table and pushes to GitHub. Stats-only by default — health failures never block a stats commit.

Provider Adapters

One subdirectory per provider in brain/adapters/, each containing adapter.py. Auto-discovered by the REGISTRY at import time via load_adapters():

Each adapter declares class-level metadata: PROVIDER_KEY, TIER, MODELS, DEFAULT_MODEL, QUALITY_SCORE, SPEED_TIER, DESCRIPTION, COST_PER_1K_TOKENS.

Adapter Contract

Every adapter extends BaseAdapter and implements a synchronous complete method:

def complete(task: Task) -> TaskResult

Result fields:

FieldTypeDescription
providerstrProvider key used
modelstrModel identifier
contentstrAI response text
tokens_usedintOutput token count
latency_msintEnd-to-end response time
cost_usdfloatEstimated cost (0.0 for free tier)
succeededboolWhether the call succeeded
errorstr | NoneError message if failed

Task Object

FieldTypeDescription
promptstrThe input prompt
task_typeTaskType enumRouting category (classification, coding, reasoning, etc.)
contextstr | NoneOptional text prepended to the prompt (document content, file contents)
max_tokensintMaximum output tokens (default 2048)
preferred_modelstrProvider key to try first

Fallback Chain Execution

  1. Orchestrator invokes the adapter for preferred_model.
  2. On failure, router looks up the fallback chain for task_type.
  3. Orchestrator iterates the chain, trying each provider until one succeeds.
  4. All attempts are logged to stats/usage.json — including failures.
  5. If no provider succeeds, an error Result is returned.

Integration Modes

ModeEntry PointHow Claude calls it
CLIdelegate.pyBash tool: python delegate.py --provider <name> --type <type> --prompt "..."
MCP Servermcp_server.pyNative delegate tool — appears in Claude's tool list alongside Bash, Read, Edit