Back to .md Directory

AI Agent Platform as a Service — High-Level Design

**Document ID:** ARCH-HLD-380

May 2, 2026
0 downloads
3 views
ai agent rag eval workflow
View source

AI Agent Platform as a Service — High-Level Design

Document ID: ARCH-HLD-380
Version: 1.0.0
Status: Draft
Track: STU-MSFT (Track 2)
Last Updated: 2026-03-22


Table of Contents

  1. Executive Summary
  2. Design Principles
  3. System Architecture Overview
  4. Control Plane
  5. Runtime (Data) Plane
  6. Cross-Cutting Concerns
  7. Data Model Summary
  8. Deployment Topology
  9. Risks & Mitigations
  10. Open Questions
  11. Appendix: Existing Implementation References

1. Executive Summary

This document defines the vendor-neutral High-Level Design for an AI Agent Platform as a Service (PaaS) — a multi-tenant runtime that enables organizations to build, deploy, evaluate, and govern autonomous AI agents at scale.

The platform separates concerns into a Control Plane (management, configuration, governance) and a Runtime Plane (execution, memory, model invocation). This separation enables independent scaling, deployment, and evolution of management surfaces versus hot-path execution infrastructure.

Key architectural goals:

GoalMeasure
Multi-tenancy at scale10,000+ orgs, strict data isolation
Agent execution latency< 200ms p99 for tool dispatch, < 2s for first-token
Horizontal scalabilityStateless runtime nodes, queue-based load leveling
Provider agnosticismAbstract model layer supporting ≥ 4 LLM providers
Governance-firstEvery agent action auditable, policy-gated, cost-tracked

Existing codebase foundation: The Archmorph backend already implements foundational subsystems — Agent/AgentVersion models with org-scoped CRUD (migration 003), MemoryManager with token-aware windowing and pgvector-backed episodic/entity memory (migration 005), PolicyEngine with typed rule evaluation (migration 006), ModelRouter with a ModelClient protocol pattern, ToolRegistry with secure invocation, Execution model with token accounting, PromptGuard for injection defense, and AuditLogger with severity/risk classification. This HLD extends and formalizes those patterns into a production-grade platform architecture.


2. Design Principles

#PrincipleRationale
1Vendor-neutral abstractionsAll model, storage, and compute interfaces defined as protocols — no provider lock-in
2Control plane / data plane separationManagement operations (CRUD, policy, evaluation) decoupled from hot-path execution
3Defense in depthTenant isolation at network, process, data, and model layers — zero implicit trust
4Observable by defaultEvery agent step emits structured traces, metrics, and token accounting events
5Policy as codeGovernance rules are versioned, testable artifacts — not GUI-only settings
6Fail-safe executionCircuit breakers, timeouts, cost caps — agents cannot consume unbounded resources
7Idempotent operationsExecution steps and state transitions are recoverable after crash/restart
8Progressive complexitySimple agents deploy zero-config; advanced orchestration opt-in via workflow DAGs

3. System Architecture Overview

graph TB
    subgraph "External Clients"
        SDK["Agent SDK / CLI"]
        UI["Management Console"]
        API_EXT["External API Consumers"]
    end

    subgraph "API Gateway Layer"
        GW["API Gateway<br/>Auth · Rate Limit · Routing"]
    end

    subgraph "Control Plane"
        AR["Agent Registry"]
        TR["Tool Registry"]
        DSR["Data Source Registry"]
        WO["Workflow Orchestrator"]
        PE["Policy Engine"]
        EE["Evaluation Engine"]
        MR["Model Registry"]
        MKT["Marketplace Catalog"]
        COST["Cost & Token Control"]
    end

    subgraph "Runtime Plane"
        AEE["Agent Execution Engine"]
        TER["Tool Execution Runtime"]
        MEM["Memory Management"]
        THR["Thread Manager"]
        SM["State Manager"]
        SAO["Sub-Agent Orchestrator"]
        RAG["RAG Pipeline"]
        MAR["Model Abstraction & Router"]
    end

    subgraph "Data Stores"
        PG["Relational DB<br/>(Metadata, Config, Audit)"]
        VS["Vector Store<br/>(Embeddings, Semantic)"]
        OBJ["Object Store<br/>(Documents, Artifacts)"]
        CACHE["Distributed Cache<br/>(State, Sessions)"]
        MQ["Message Queue<br/>(Jobs, Events)"]
    end

    subgraph "Model Providers"
        MP1["LLM Provider A"]
        MP2["LLM Provider B"]
        MP3["LLM Provider C"]
        MP4["Self-Hosted Models"]
    end

    SDK & UI & API_EXT --> GW
    GW --> AR & TR & DSR & WO & PE & EE & MR & MKT & COST
    GW --> AEE

    AR --> PG
    TR --> PG
    DSR --> PG
    PE --> PG
    EE --> PG
    MR --> PG
    MKT --> PG
    COST --> PG

    AEE --> TER & MEM & THR & SM & SAO & MAR
    AEE --> MQ
    RAG --> VS & OBJ
    MEM --> VS & CACHE
    SM --> CACHE & PG
    THR --> PG & CACHE
    MAR --> MP1 & MP2 & MP3 & MP4

    WO --> AEE
    PE -.->|enforce| AEE
    EE -.->|evaluate| AEE
    MR -.->|config| MAR
    TR -.->|schemas| TER
    DSR -.->|connectors| RAG

Architectural boundaries:

  • API Gateway — Authentication, rate limiting, tenant routing. Single entry point for both control and runtime APIs.
  • Control Plane — Stateless services backed by a relational store. Manages all configuration, policy, and metadata. Scales independently of execution load.
  • Runtime Plane — The hot path. Agent sandboxes, model calls, tool execution. Horizontally scalable via queue-based work distribution.
  • Data Stores — Purpose-specific persistence: relational for metadata/ACID, vector for semantic search, object for documents, cache for ephemeral state, queue for async work distribution.

4. Control Plane

The Control Plane owns all management, configuration, and governance operations. It is the source of truth for what agents exist, what they can do, and what constraints apply.

4.1 Agent Registry & Lifecycle Management

Purpose: Central catalog of all agents with full lifecycle control — create, read, update, archive, version, rollback.

Existing implementation: models/agent.py defines Agent (with organization_id scoping, status lifecycle, JSON config columns) and AgentVersion (immutable config snapshots per version). routers/agents.py implements CRUD with automatic version snapshotting on create.

Lifecycle State Machine

stateDiagram-v2
    [*] --> Draft: create()
    Draft --> Active: activate()
    Draft --> Archived: archive()
    Active --> Paused: pause()
    Active --> Draft: edit()
    Active --> Archived: archive()
    Paused --> Active: resume()
    Paused --> Archived: archive()
    Archived --> Draft: restore()
    Archived --> [*]: purge()

    note right of Active
        Running in production.
        Receives traffic.
        Immutable config — 
        edits create new version.
    end note

    note right of Draft
        Safe for editing.
        Not routable.
        Config is mutable.
    end note

Data Model

FieldTypeDescription
idUUIDPrimary key
organization_idFK → OrganizationsTenant scoping
nameStringHuman-readable identifier
versionSemVer StringCurrent active version
statusEnumdraft, active, paused, archived
model_config_dataJSONModel provider, temperature, system prompt
toolsJSON ArrayAttached tool references
data_sourcesJSON ArrayAttached data source references
memory_configJSONShort-term window + long-term store configuration
policy_refFK → PoliciesDefault policy binding
created_byStringCreator identity
created_at / updated_atTimestampAudit timestamps

Versioning & Rollback

Every mutation to an active agent creates an immutable AgentVersion record:

AgentVersion {
    id: UUID
    agent_id: FK → Agent
    version: "1.2.3"
    config_snapshot: JSON  // full frozen config at this point
    created_at: Timestamp
    created_by: String
}

Rollback is implemented as: (1) load target AgentVersion.config_snapshot, (2) apply it as a new version with incremented patch number, (3) transition agent status. This preserves full audit trail — no history is ever deleted.

API Surface

OperationMethodPathDescription
CreatePOST/agentsCreate draft agent
ListGET/agents?org_id=List agents for org
GetGET/agents/{id}Get agent detail
UpdatePATCH/agents/{id}Update draft or create new version
ActivatePOST/agents/{id}/activatePromote to active
PausePOST/agents/{id}/pauseSuspend execution
ArchiveDELETE/agents/{id}Soft-delete
List VersionsGET/agents/{id}/versionsVersion history
RollbackPOST/agents/{id}/rollbackRevert to prior version
ClonePOST/agents/{id}/cloneDeep-copy agent config

4.2 Tool Registry & Attachment Management

Purpose: Central catalog of callable tools (functions, APIs, code interpreters) with schema validation, versioning, and secure attachment to agents.

Existing implementation: services/tool_registry.py implements ToolRegistry with register() (name + callable + OpenAI-compatible schema), get_schemas(), and execute() with JSON argument parsing, async/sync dispatch, and error encapsulation.

Tool Definition Schema

{
  "id": "tool-uuid",
  "name": "query_database",
  "version": "2.1.0",
  "description": "Execute read-only SQL queries against allowed tables",
  "type": "function",                           
  "runtime": "sandboxed_process | http_webhook | wasm",
  "schema": {
    "parameters": {
      "type": "object",
      "properties": {
        "query": { "type": "string", "description": "SQL SELECT statement" },
        "database": { "type": "string", "enum": ["analytics", "users_ro"] }
      },
      "required": ["query"]
    }
  },
  "constraints": {
    "max_execution_time_ms": 30000,
    "max_output_bytes": 1048576,
    "requires_approval": false,
    "allowed_scopes": ["read"]
  },
  "organization_id": "org-uuid",
  "is_public": false
}

Tool Attachment Model

Agents reference tools via a binding table, not embedded JSON, enabling:

  • Shared tools across agents within an org
  • Independent tool versioning
  • Policy-gated access (evaluated by PolicyEngine.evaluate_tool_access())
Binding FieldDescription
agent_idConsuming agent
tool_idReferenced tool
version_constraintSemVer range (e.g., ^2.0.0)
config_overridesAgent-specific tool parameters

Tool Types

TypeExecution ModelUse Case
functionIn-process callableInternal platform operations
http_webhookOutbound HTTP callExternal API integration
sandboxed_processIsolated subprocessCode execution, file processing
wasmWebAssembly sandboxUntrusted third-party tools
mcpModel Context ProtocolStandardized tool interop

4.3 Data Source Connector Registry

Purpose: Manage connections to external data systems that agents can query — databases, APIs, file stores, SaaS platforms — with credential management, schema introspection, and access control.

Connector Architecture

DataSourceConnector {
    id: UUID
    organization_id: FK
    name: String
    type: Enum[database, api, file_store, saas, custom]
    connection_config: EncryptedJSON   // credentials stored encrypted at rest
    schema_metadata: JSON              // discovered tables, fields, endpoints
    health_status: Enum[healthy, degraded, unreachable]
    last_health_check: Timestamp
    access_policy_id: FK → AgentPolicy
}

Connector Types

TypeProtocolExample Targets
databaseSQL/NoSQL driverPostgreSQL, MongoDB, Elasticsearch
apiHTTP/gRPCREST endpoints, GraphQL
file_storeObject protocolS3-compatible, NFS, SFTP
saasOAuth + RESTCRM, ticketing, analytics platforms
customPlugin SDKOrganization-specific connectors

Security Model

  • Credentials are never stored in plaintext — encrypted at rest with tenant-scoped encryption keys.
  • Connector access is gated by PolicyEngine data-access policies per agent.
  • All queries are logged to the audit trail with query text, row count, and duration.
  • Read-only mode is the default; write access requires explicit policy override.

4.4 Workflow Orchestrator

Purpose: Enable composition of multi-step, multi-agent workflows as Directed Acyclic Graphs (DAGs) with support for sequential, parallel, conditional, and autonomous execution patterns.

Workflow Definition

Workflow {
    id: UUID
    organization_id: FK
    name: String
    version: String
    trigger: Enum[manual, schedule, event, api]
    dag: DAGDefinition
    variables: JSON          // workflow-scoped variables
    timeout_ms: Integer
    retry_policy: RetryConfig
}

DAGDefinition {
    nodes: [
        {
            id: "step-1",
            type: Enum[agent_invoke, tool_call, condition, parallel_gate, 
                       human_approval, transform, sub_workflow],
            agent_id: UUID | null,
            config: JSON,
            depends_on: ["step-0"],
            timeout_ms: Integer,
            retry: { max_attempts: 3, backoff: "exponential" }
        }
    ],
    edges: [
        { from: "step-1", to: "step-2", condition: "output.confidence > 0.8" }
    ]
}

Execution Patterns

PatternDescriptionDAG Structure
SequentialSteps execute in orderLinear chain: A → B → C
Parallel fan-outIndependent steps run concurrentlyFan: A → [B, C, D] → E
Conditional branchingRoute based on step outputDiamond: A → {B if true, C if false} → D
Human-in-the-loopPause for approval before proceedingA → [APPROVAL_GATE] → B
Autonomous loopAgent decides when to stopA → B → C → (back to A if !done)
Sub-workflowNested DAG invocationA → [SubDAG] → B

DAG Scheduler

The scheduler is a pull-based system:

  1. Enqueue — Workflow trigger places DAG instance into the work queue.
  2. Resolve — Scheduler evaluates which nodes have all dependencies satisfied.
  3. Dispatch — Ready nodes are dispatched to the Agent Execution Engine.
  4. Collect — Results are written to state store; scheduler re-evaluates.
  5. Terminal — DAG completes when all leaf nodes reach terminal state.

Failure handling: Each node has independent retry policy. If retries are exhausted, the node transitions to failed, which propagates to dependent nodes unless a fallback edge is defined.


4.5 Policy Engine

Purpose: Enforce organizational governance on agent behavior — access control, content filtering, rate limiting, tool restrictions, data boundaries, and audit mandates.

Existing implementation: services/policy_engine.py implements PolicyEngine with evaluate_input() (regex blocklist matching), evaluate_output() (secret redaction, code blocking), and evaluate_tool_access() (allowlist/blocklist enforcement). models/policy.py defines AgentPolicy (typed rules with enforcement levels) and AgentPolicyBinding (agent-to-policy mapping). The existing prompt_guard.py provides hardened injection detection patterns.

Policy Types

TypeEnforcement PointDescription
inputPre-model invocationPrompt injection defense, content blocklists, PII detection
outputPost-model responseSecret redaction, content filtering, format validation
toolPre-tool executionAllowlist/blocklist, approval requirements
dataData source queriesRow-level filtering, column masking, query complexity limits
operationalExecution engineToken budgets, execution time limits, concurrency caps
costToken accountingPer-agent/per-org cost ceilings, alerting thresholds

Policy Rule Schema

{
  "id": "policy-uuid",
  "name": "Production Safety Guard",
  "policy_type": "input",
  "enforcement_level": "block",         
  "rules": {
    "blocklist_regex": [
      "ignore\\s+previous\\s+instructions",
      "reveal.*system\\s+prompt"
    ],
    "max_input_tokens": 8192,
    "require_system_prompt": true,
    "pii_detection": {
      "enabled": true,
      "action": "redact",
      "entity_types": ["SSN", "CREDIT_CARD", "EMAIL"]
    }
  },
  "is_active": true,
  "organization_id": "org-uuid"
}

Enforcement Levels

LevelBehavior
blockReject the request with HTTP 4xx; execution halted
flagAllow execution, emit warning event to observability pipeline
auditAllow execution, log to audit trail only
ignorePolicy inactive (for gradual rollout/testing)

Enforcement Pipeline

User Input
  → PolicyEngine.evaluate_input()     [input policies]
  → PromptGuard.scan_injection()       [hardened injection patterns]
  → Model Invocation
  → PolicyEngine.evaluate_output()     [output policies]
  → Response to User

4.6 Evaluation Engine

Purpose: Measure agent quality through automated scoring, ground-truth comparison, regression detection, and A/B experimentation.

Evaluation Modes

ModeDescriptionTrigger
OnlineScore every production execution in real-timeAutomatic on each response
BatchRun evaluation suite against a test datasetManual or CI/CD pipeline
A/BSplit traffic between agent variants, compare metricsExperiment configuration
RegressionCompare new version against baseline on fixed datasetPre-activation gate

Quality Metrics

MetricMeasurementSource
RelevanceModel-as-judge scoring (1-5 scale)LLM evaluator
GroundednessFactual grounding against source documentsRAG comparison
CoherenceLogical consistency across multi-turnLLM evaluator
CompletenessAnswer coverage against ground truthDataset comparison
SafetyContent policy violation ratePolicy engine stats
LatencyEnd-to-end response time distributionRuntime metrics
Cost efficiencyQuality score per dollar spentToken accounting
Tool accuracyCorrect tool selection and argument generationGround truth comparison

Evaluation Data Model

EvaluationRun {
    id: UUID
    agent_id: FK
    agent_version: String
    dataset_id: FK
    mode: Enum[online, batch, ab, regression]
    status: Enum[running, completed, failed]
    metrics: JSON              // aggregated scores
    sample_count: Integer
    created_at: Timestamp
    completed_at: Timestamp
}

EvaluationSample {
    id: UUID
    run_id: FK → EvaluationRun
    input: JSON
    expected_output: JSON | null
    actual_output: JSON
    scores: JSON               // per-metric scores for this sample
    latency_ms: Integer
    token_count: Integer
}

EvaluationDataset {
    id: UUID
    organization_id: FK
    name: String
    samples: [{ input, expected_output, metadata }]
    version: String
}

A/B Experimentation

Traffic splitting at the Agent Execution Engine level:

  1. Define experiment: agent A (control) vs agent B (variant), split ratio 90/10.
  2. Router hashes thread_id to deterministic bucket — ensures user consistency.
  3. Both variants emit metrics tagged with experiment ID.
  4. Evaluation engine computes statistical significance (two-proportion z-test or Mann-Whitney U).
  5. Dashboard surfaces winner with confidence interval.

4.7 Model Registry & Routing Configuration

Purpose: Catalog available model endpoints across providers, manage connection credentials, define routing rules (primary/fallback/cost-optimized), and track capabilities.

Existing implementation: models/model_registry.py defines ModelEndpoint with provider, version, connection config, capabilities, and pricing JSON. services/model_router.py defines a ModelClient protocol with chat(), chat_stream(), supports_tools(), supports_vision(), get_context_window() — with concrete implementations for Azure OpenAI and Anthropic.

Model Endpoint Schema

ModelEndpoint {
    id: UUID
    organization_id: FK
    name: String                       // "GPT-4o Production"
    provider: String                   // "openai", "anthropic", "mistral", "self-hosted"
    model_version: String              // "gpt-4o-2024-08"
    is_active: Boolean
    connection_config: EncryptedJSON   // endpoint URL, API keys, region
    capabilities: JSON {
        tools: Boolean
        vision: Boolean
        streaming: Boolean
        max_context_window: Integer
        output_token_limit: Integer
    }
    pricing: JSON {
        input_cost_per_1k: Float       // USD per 1K input tokens
        output_cost_per_1k: Float      // USD per 1K output tokens
    }
    health_status: Enum[healthy, degraded, down]
    request_latency_p50_ms: Integer    // trailing latency metrics
    request_latency_p99_ms: Integer
}

Routing Strategies

StrategyBehavior
fixedAlways use the specified endpoint
fallback_chainTry primary → secondary → tertiary on failure
cost_optimizedRoute to cheapest endpoint that satisfies capability requirements
latency_optimizedRoute to lowest-latency healthy endpoint
round_robinDistribute evenly across healthy endpoints
capability_matchSelect based on required capabilities (tools, vision, context window)

Routing Configuration

{
  "agent_id": "agent-uuid",
  "strategy": "fallback_chain",
  "routes": [
    {
      "endpoint_id": "ep-primary",
      "priority": 1,
      "conditions": { "max_tokens": 128000 }
    },
    {
      "endpoint_id": "ep-secondary", 
      "priority": 2,
      "conditions": { "fallback_on": ["rate_limit", "timeout", "5xx"] }
    }
  ]
}

4.8 Marketplace Catalog

Purpose: Curated catalog of shareable agents, tools, and workflow templates — enabling discovery, installation, and controlled sharing across organizations.

Catalog Entity

MarketplaceItem {
    id: UUID
    type: Enum[agent_template, tool, workflow_template, data_connector]
    name: String
    description: String
    publisher_org_id: FK
    visibility: Enum[private, org_internal, public]
    version: String
    install_count: Integer
    rating: Float
    tags: [String]
    manifest: JSON              // full config needed to instantiate
    requirements: JSON          // dependencies, min platform version
    review_status: Enum[pending, approved, rejected]
}

Publishing Flow

  1. Publisher submits item with manifest + documentation.
  2. Platform runs automated validation (schema check, security scan, policy compliance).
  3. If public, human review required before catalog listing.
  4. Approved items appear in marketplace search results.
  5. Installing an item deep-copies the manifest into the consuming org's registry.

Security Controls

  • All public tools undergo static analysis for known vulnerability patterns.
  • Published agents cannot reference private data sources or credentials.
  • Install creates an independent copy — no cross-org runtime references.
  • Version pinning prevents uncontrolled updates.

4.9 Cost & Token Observability Control

Purpose: Real-time visibility into token consumption and cost, with budgets, alerts, and enforcement to prevent runaway spending.

Existing implementation: models/execution.py tracks prompt_tokens, completion_tokens, total_tokens, and total_cost per execution.

Cost Tracking Architecture

TokenEvent {
    execution_id: FK
    agent_id: FK
    organization_id: FK
    model_endpoint_id: FK
    direction: Enum[input, output]
    token_count: Integer
    cost_usd: Float                    // computed from ModelEndpoint.pricing
    timestamp: Timestamp
}

Events are emitted on every model invocation and aggregated into rollup tables:

GranularityDimensionsRetention
Per-executionagent, model, status90 days
Hourly rollupagent, model, org1 year
Daily rolluporg, cost center3 years

Budget Controls

{
  "organization_id": "org-uuid",
  "budgets": [
    {
      "scope": "organization",
      "monthly_limit_usd": 5000.00,
      "alert_thresholds": [0.50, 0.80, 0.95],
      "enforcement": "block_at_limit"
    },
    {
      "scope": "agent",
      "agent_id": "agent-uuid",
      "daily_limit_usd": 100.00,
      "per_execution_limit_usd": 5.00,
      "enforcement": "block_at_limit"
    }
  ]
}

Alert Pipeline

  • Threshold breach → emit alert event to observability pipeline.
  • Alert routes to webhook, email, or in-platform notification based on org config.
  • block_at_limit enforcement: execution engine checks budget before model invocation and rejects if exceeded.

5. Runtime (Data) Plane

The Runtime Plane handles all hot-path execution — agent reasoning loops, model invocations, tool calls, memory operations, and state management.

5.1 Agent Execution Engine

Purpose: Isolated sandbox for executing agent reasoning loops — the core runtime that orchestrates the think → act → observe cycle.

Existing implementation: models/execution.py defines Execution with status lifecycle (queued, running, completed, failed, cancelled, timeout), step tracking as JSON array, and token/cost accounting. job_queue.py implements JobManager with async job lifecycle, SSE streaming, and progress tracking.

Execution Lifecycle

sequenceDiagram
    participant C as Client
    participant GW as API Gateway
    participant Q as Job Queue
    participant EE as Execution Engine
    participant PE as Policy Engine
    participant MAR as Model Router
    participant TR as Tool Runtime
    participant MEM as Memory Manager
    participant SM as State Manager

    C->>GW: POST /agents/{id}/execute
    GW->>PE: evaluate_input(messages)
    PE-->>GW: pass / reject
    GW->>Q: enqueue(execution)
    Q-->>C: 202 Accepted + execution_id

    Q->>EE: dequeue(execution)
    EE->>SM: create_checkpoint(initial_state)
    EE->>MEM: prepare_context(messages, memory)

    loop Agent Reasoning Loop
        EE->>MAR: chat(messages, tools)
        MAR-->>EE: response (text | tool_call)
        
        alt Tool Call
            EE->>PE: evaluate_tool_access(tool_name)
            PE-->>EE: allow / deny
            EE->>TR: execute(tool, arguments)
            TR-->>EE: tool_result
            EE->>SM: checkpoint(step_state)
        else Final Response
            EE->>PE: evaluate_output(content)
            PE-->>EE: sanitized content
            EE->>MEM: save_episodic_memory(summary)
        end
    end

    EE->>SM: finalize(execution)
    EE->>Q: complete(execution_id, result)
    Q-->>C: SSE: execution.completed

Sandbox Isolation

Each agent execution runs in an isolated context:

Isolation LayerMechanism
ProcessDedicated worker process per execution (no shared memory)
ResourceCPU/memory limits enforced via cgroups or container limits
NetworkOutbound restricted to allowlisted endpoints per tool
TimeHard timeout per execution (configurable, default 300s)
CostToken budget check before every model invocation
DataExecution sees only its own thread's messages and agent's memory

Execution Record

Execution {
    id: UUID
    agent_id: FK
    organization_id: FK
    thread_id: String
    status: Enum[queued, running, completed, failed, cancelled, timeout]
    input_data: JSON
    output_data: JSON
    steps: JSON [                      // ordered list of reasoning steps
        {
            type: "model_call" | "tool_call" | "memory_retrieval",
            input: JSON,
            output: JSON,
            tokens: { prompt, completion },
            duration_ms: Integer,
            model_endpoint_id: String
        }
    ]
    prompt_tokens: Integer
    completion_tokens: Integer
    total_tokens: Integer
    total_cost: Float
    duration_ms: Integer
    error_message: String | null
    created_at: Timestamp
    completed_at: Timestamp
}

5.2 Tool Execution Runtime

Purpose: Secure, isolated execution of tool functions invoked by agents during reasoning loops.

Existing implementation: services/tool_registry.py provides ToolRegistry.execute() with JSON argument parsing, async/sync dispatch via asyncio.to_thread(), and structured error responses.

Execution Sandboxing

┌─────────────────────────────────────────────────────┐
│ Agent Execution Engine                              │
│                                                     │
│   ┌───────────────────────────────────────────┐     │
│   │ Tool Execution Sandbox                    │     │
│   │                                           │     │
│   │  ┌─────────┐  ┌─────────┐  ┌──────────┐  │     │
│   │  │ Argument│  │ Policy  │  │ Resource │  │     │
│   │  │ Schema  │→ │ Gate    │→ │ Limiter  │  │     │
│   │  │ Validate│  │         │  │          │  │     │
│   │  └─────────┘  └─────────┘  └──────────┘  │     │
│   │       │                         │         │     │
│   │       ▼                         ▼         │     │
│   │  ┌─────────────────────────────────────┐  │     │
│   │  │ Isolated Execution Environment     │  │     │
│   │  │ (subprocess / container / WASM)    │  │     │
│   │  └─────────────────────────────────────┘  │     │
│   │       │                                   │     │
│   │       ▼                                   │     │
│   │  ┌──────────┐  ┌────────────┐             │     │
│   │  │ Output   │  │ Audit      │             │     │
│   │  │ Sanitize │  │ Log Entry  │             │     │
│   │  └──────────┘  └────────────┘             │     │
│   └───────────────────────────────────────────┘     │
└─────────────────────────────────────────────────────┘

Security Controls

ControlImplementation
Argument validationJSON Schema validation before execution
Timeout enforcementHard kill after max_execution_time_ms
Output size limitTruncate at max_output_bytes
Network isolationOutbound allowlist per tool definition
Credential injectionSecrets injected at runtime, never visible to agent
Audit trailEvery invocation logged with args, result, duration

Approval Workflow

For tools marked requires_approval: true:

  1. Execution engine pauses at tool-call step.
  2. Approval request emitted via webhook / notification.
  3. Human approver reviews arguments and approves/rejects.
  4. On approval, execution resumes; on rejection, agent receives denial message.
  5. Approval timeout configurable (default: 24 hours → auto-reject).

5.3 Memory Management Layer

Purpose: Provide agents with both short-term (context window) and long-term (semantic/vector) memory, enabling contextual awareness across sessions.

Existing implementation: services/agent_memory.py implements MemoryManager with prepare_short_term_buffer() (token-aware windowing using tiktoken, system prompt preservation, most-recent-first selection) and save_episodic_memory() / save_entity() (pgvector-backed storage with embeddings via text-embedding-3-small). models/memory.py defines AgentMemoryDocument, AgentEpisodicMemory (with importance scoring), and AgentEntityMemory (with entity type classification).

Memory Architecture

graph LR
    subgraph "Short-Term Memory"
        CW["Context Window<br/>Token-Aware Buffer"]
        WM["Working Memory<br/>Current Execution State"]
    end

    subgraph "Long-Term Memory"
        EM["Episodic Memory<br/>Past Execution Summaries"]
        ENT["Entity Memory<br/>Extracted Entities & Facts"]
        DOC["Document Memory<br/>Ingested Knowledge Base"]
    end

    subgraph "Storage"
        VS["Vector Store<br/>(pgvector)"]
        RDB["Relational DB<br/>(Metadata)"]
        CACHE["Distributed Cache<br/>(Working Memory)"]
    end

    CW -->|"token window<br/>management"| WM
    WM -->|"promote important<br/>observations"| EM
    WM -->|"extract entities"| ENT
    
    EM --> VS
    ENT --> VS
    DOC --> VS

    EM --> RDB
    ENT --> RDB
    DOC --> RDB
    WM --> CACHE

Memory Types

TypeScopeStorageAccess Pattern
Context WindowSingle executionIn-memoryToken-bounded sliding window of recent messages
Working MemorySingle executionCacheScratchpad for intermediate results, tool outputs
Episodic MemoryCross-executionVector + RelationalSummaries of past conversations, retrieved by similarity
Entity MemoryCross-executionVector + RelationalStructured facts about people, projects, preferences
Document MemoryAgent-scopedVector + Object StoreIngested documents chunked and embedded for RAG

Context Window Management

The MemoryManager.prepare_short_term_buffer() algorithm:

  1. Reserve system prompt tokens (always included).
  2. Retrieve relevant episodic memories by similarity to current input.
  3. Retrieve relevant entity facts.
  4. Inject retrieved memories as system context.
  5. Fill remaining budget with most-recent conversation messages (reverse chronological).
  6. If budget exceeded, summarize oldest messages and replace with summary.

Memory Lifecycle

EventAction
Execution startsLoad context window from thread history
Tool result receivedStore in working memory
Execution completesGenerate episodic summary, extract entities
Memory document uploadedChunk → embed → index in vector store
Agent archivedEpisodic + entity memories retained for configurable TTL
Org deletedAll memories hard-deleted (GDPR compliance)

5.4 Thread Management

Purpose: Manage multi-turn conversation sessions with support for branching, forking, merging, and cross-session context.

Existing implementation: models/execution.py includes thread_id on the Execution model, enabling grouping of executions into conversational threads.

Thread Model

Thread {
    id: UUID
    agent_id: FK
    organization_id: FK
    user_id: String
    parent_thread_id: UUID | null      // for forked threads
    status: Enum[active, archived, merged]
    title: String | null               // auto-generated or user-set
    message_count: Integer
    last_activity_at: Timestamp
    created_at: Timestamp
    metadata: JSON
}

ThreadMessage {
    id: UUID
    thread_id: FK
    execution_id: FK | null
    role: Enum[user, assistant, system, tool]
    content: Text
    tool_calls: JSON | null
    token_count: Integer
    created_at: Timestamp
}

Thread Operations

OperationDescription
CreateNew thread with initial message
ContinueAppend message to existing thread
ForkCreate branch from a specific message — enables "what if" exploration
MergeCombine forked thread results back into parent
ArchiveMark thread inactive; messages retained for configured TTL
SummarizeCompress long thread into summary for context efficiency

Fork/Merge Semantics

Thread-A: [M1] → [M2] → [M3]
                           ├── Fork → Thread-B: [M3] → [M4b] → [M5b]
                           └── Fork → Thread-C: [M3] → [M4c] → [M5c]
                                                                  │
Thread-A (merged): [M1] → [M2] → [M3] → [M6 = merge(B, C)] ←────┘

The merge operation generates a synthesis message by invoking the agent with outputs from both branches, producing a unified response.


5.5 State Management

Purpose: Provide durable checkpointing, persistence, and crash recovery for agent executions.

Checkpoint Model

ExecutionCheckpoint {
    id: UUID
    execution_id: FK
    step_index: Integer                // which step in the reasoning loop
    state_snapshot: JSON {
        messages: [...]                // conversation so far
        working_memory: {...}          // intermediate results
        tool_results: [...]            // completed tool calls
        pending_actions: [...]         // queued but not yet executed
    }
    created_at: Timestamp
}

Checkpointing Strategy

EventCheckpoint Trigger
Execution startInitial state checkpoint
After each tool callPost-tool-result checkpoint
After model responsePost-reasoning checkpoint
Before human approval gatePre-pause checkpoint
Periodic intervalEvery N seconds during long executions

Recovery Protocol

  1. Worker crash detected (heartbeat timeout).
  2. Scheduler identifies incomplete executions with status = running and stale updated_at.
  3. Load latest ExecutionCheckpoint for the execution.
  4. Reassign to healthy worker.
  5. Worker resumes from checkpoint state — replays from last saved step.
  6. If no checkpoint exists, execution is marked failed with error_message = "worker_crash_no_checkpoint".

State Persistence Tiers

TierStorageDurabilityLatency
HotDistributed cacheReplicated, ephemeral< 1ms
WarmRelational DBPersistent, transactional< 10ms
ColdObject storeArchival, immutable< 100ms

Active executions use hot storage. On completion, state migrates to warm. After retention period, state moves to cold or is purged.


5.6 Sub-Agent Orchestrator

Purpose: Enable a primary agent to delegate subtasks to specialized sub-agents, with support for parallel execution, result aggregation, and DAG-based scheduling.

Delegation Model

SubAgentInvocation {
    id: UUID
    parent_execution_id: FK
    child_agent_id: FK
    child_execution_id: FK
    delegation_type: Enum[parallel, sequential, conditional]
    input: JSON
    output: JSON | null
    status: Enum[pending, running, completed, failed]
    timeout_ms: Integer
    created_at: Timestamp
}

Execution Patterns

Parallel fan-out:

Primary Agent
  ├── SubAgent-A (research)     ─── running in parallel
  ├── SubAgent-B (code review)  ─── running in parallel  
  └── SubAgent-C (testing)      ─── running in parallel
        │
        ▼
  Aggregation step (Primary Agent synthesizes results)

Sequential chain:

Primary Agent → SubAgent-A (extract) → SubAgent-B (transform) → SubAgent-C (validate)

Depth & Resource Controls

ControlDefaultDescription
max_delegation_depth3Prevent unbounded recursive sub-agent calls
max_parallel_sub_agents5Concurrency limit per parent execution
sub_agent_timeout_ms60000Hard timeout for each sub-agent invocation
inherit_policiestrueSub-agents inherit parent's policy bindings
aggregate_token_budgettrueSub-agent tokens count toward parent's budget

Communication

Sub-agents communicate via structured message passing — the parent defines input schema, the sub-agent returns structured output. No shared mutable state between agents; isolation is preserved.


5.7 RAG Pipeline

Purpose: Full document ingestion, embedding, indexing, and retrieval pipeline enabling agents to ground responses in organizational knowledge.

Pipeline Architecture

graph LR
    subgraph "Ingestion"
        UP["Document Upload"]
        PARSE["Parser<br/>(PDF, DOCX, HTML, MD)"]
        CHUNK["Chunker<br/>(Semantic / Fixed-Size)"]
        EMBED["Embedder<br/>(Model-Agnostic)"]
    end

    subgraph "Storage"
        VS["Vector Index"]
        META["Metadata Store"]
        OBJ["Object Store<br/>(Raw Documents)"]
    end

    subgraph "Retrieval"
        QUERY["Query Encoder"]
        SEARCH["Hybrid Search<br/>(Vector + Keyword)"]
        RERANK["Re-Ranker"]
        CTX["Context Assembly"]
    end

    UP --> PARSE --> CHUNK --> EMBED --> VS
    UP --> OBJ
    CHUNK --> META

    QUERY --> SEARCH
    VS --> SEARCH
    META --> SEARCH
    SEARCH --> RERANK --> CTX

Ingestion Pipeline

StageDescriptionConfiguration
ParseExtract text from source formatSupported: PDF, DOCX, HTML, Markdown, TXT, CSV
ChunkSplit into retrieval-sized segmentsStrategy: semantic (sentence boundaries) or fixed (token count)
EmbedGenerate vector embeddingsModel-agnostic; configurable embedding model per agent
IndexStore vectors with metadataVector store with HNSW index; metadata for filtering

Chunking Strategies

StrategyChunk SizeOverlapBest For
fixed_token512 tokens64 tokensUniform documents
semanticVariable (sentence boundaries)1 sentenceProse-heavy documents
hierarchicalSection → paragraph → sentenceParent ID linkageStructured documents
codeFunction/class boundariesNoneSource code files

Retrieval Pipeline

  1. Query encoding — Embed user query with same model used for document embedding.
  2. Hybrid search — Combine vector similarity (cosine/dot-product) with keyword search (BM25).
  3. Metadata filtering — Apply agent-scoped filters (document ID, tags, date range).
  4. Re-ranking — Cross-encoder re-ranker scores top-K candidates for relevance.
  5. Context assembly — Selected chunks assembled into prompt context with source citations.

Document Memory Model

Extends existing AgentMemoryDocument:

AgentMemoryDocument {
    id: UUID
    agent_id: FK
    filename: String
    file_type: String
    status: Enum[processing, ready, error]
    chunk_count: Integer
    embedding_model: String
    metadata_json: JSON
    created_at: Timestamp
    updated_at: Timestamp
}

DocumentChunk {
    id: UUID
    document_id: FK → AgentMemoryDocument
    chunk_index: Integer
    content: Text
    token_count: Integer
    embedding: Vector(dim)
    metadata: JSON {
        page_number: Integer
        section_title: String
        source_url: String
    }
}

5.8 Model Abstraction & Routing Layer

Purpose: Unified interface for invoking any LLM provider, with automatic routing, failover, streaming, and protocol normalization.

Existing implementation: services/model_router.py defines the ModelClient protocol with chat(), chat_stream(), capability queries, and concrete implementations AzureOpenAIClientImpl and AnthropicClientImpl. The get_model_client() factory resolves provider to implementation.

Protocol Interface

class ModelClient(Protocol):
    async def chat(
        self, 
        messages: List[Dict[str, str]], 
        tools: List[Dict] = None, 
        **kwargs
    ) -> ModelResponse: ...
    
    async def chat_stream(
        self, 
        messages: List[Dict[str, str]], 
        tools: List[Dict] = None, 
        **kwargs
    ) -> AsyncIterator[StreamChunk]: ...
    
    def supports_tools(self) -> bool: ...
    def supports_vision(self) -> bool: ...
    def get_context_window(self) -> int: ...

Normalized Response

All provider-specific response formats are normalized to a canonical schema:

{
  "id": "response-uuid",
  "model": "gpt-4o",
  "provider": "openai",
  "content": "The analysis shows...",
  "tool_calls": [
    {
      "id": "tc-1",
      "function": { "name": "query_db", "arguments": "{...}" }
    }
  ],
  "usage": {
    "prompt_tokens": 1523,
    "completion_tokens": 847,
    "total_tokens": 2370
  },
  "finish_reason": "stop",
  "latency_ms": 1892
}

Routing Engine

Request arrives
  → Resolve routing config for agent
  → Filter endpoints by capability requirements (tools? vision? context length?)
  → Apply routing strategy (fallback, cost-optimized, latency-optimized)
  → Select target endpoint
  → Normalize request to provider format
  → Invoke provider client
  → If failure:
      Check circuit breaker state
      If open → skip to next endpoint
      If half-open → probe
      Attempt next endpoint in fallback chain
  → Normalize response to canonical format
  → Emit token accounting event
  → Return

Provider Support Matrix

CapabilityOpenAI-compatibleAnthropicMistralSelf-hosted (OpenAI API compat)
Chat completionYesYesYesYes
StreamingYesYesYesYes
Tool callingYesYesYesVaries
VisionYesYesNoVaries
JSON modeYesYesYesVaries
Context window128K+200K+128KModel-dependent

6. Cross-Cutting Concerns

6.1 Security Boundaries & Tenant Isolation

Isolation Model

graph TB
    subgraph "Tenant Boundary (Organization)"
        direction TB
        subgraph "Network Isolation"
            NI["Dedicated namespace / network segment"]
        end
        
        subgraph "Data Isolation"
            DB["Row-level security<br/>(organization_id on every table)"]
            VS2["Tenant-scoped vector indices"]
            OBJ2["Tenant-prefixed object paths"]
            EK["Tenant-scoped encryption keys"]
        end
        
        subgraph "Compute Isolation"
            EX["Execution sandboxes<br/>(process isolation)"]
            TL["Per-tenant resource quotas"]
        end
        
        subgraph "Identity Boundary"
            AUTH["Org-scoped RBAC"]
            SA["Service account boundaries"]
            API_KEYS["API key scoping per org"]
        end
    end

    subgraph "Cross-Tenant Security Controls"
        WAF["API Gateway<br/>(WAF, DDoS, rate limit)"]
        AUDIT["Centralized Audit Log"]
        KMS["Key Management Service"]
        SCAN["Runtime Security Scanner"]
    end

    WAF --> NI
    KMS --> EK
    AUDIT --> DB
    SCAN --> EX

Defense Layers

LayerThreatControl
API GatewayUnauthenticated access, DDoSJWT validation, rate limiting, IP allowlists
AuthenticationIdentity spoofingOAuth 2.0 / OIDC, MFA enforcement
AuthorizationPrivilege escalationRBAC with org-scoped roles (admin, editor, viewer, agent_operator)
Data at restData breachTenant-scoped encryption keys, encrypted columns for credentials
Data in transitInterceptionTLS 1.3 minimum, certificate pinning for inter-service communication
Prompt injectionModel manipulationPromptGuard pattern matching + model-based detection (existing prompt_guard.py)
Output leakageCredential/PII exposurePolicyEngine.evaluate_output() with regex-based secret scanning (existing patterns)
Tool executionCode injection, SSRFSandbox isolation, network allowlists, argument schema validation
Cross-tenant data leakUnauthorized data accessorganization_id on every query, row-level security policies

Credential Management

  • Agent-attached credentials (API keys, database passwords) stored encrypted with envelope encryption.
  • Encryption keys rotated on configurable schedule (default: 90 days).
  • Credentials injected into tool execution environment at runtime — never serialized to logs or model context.
  • Credential access logged to audit trail.

6.2 Observability Architecture

Existing implementation: audit_logging.py provides AuditLogger with event types, severity levels, risk classification, correlation IDs, and alerting rules (brute-force detection, off-hours admin access, bulk export). models/audit.py defines AuditLogRecord with indexed event types and time-based partitioning.

Three Pillars + Token Accounting

┌────────────────────────────────────────────────────────────────┐
│                    Observability Pipeline                       │
│                                                                │
│   ┌──────────┐  ┌──────────┐  ┌──────────┐  ┌──────────────┐  │
│   │  Logs    │  │ Metrics  │  │ Traces   │  │ Token Acctg  │  │
│   ├──────────┤  ├──────────┤  ├──────────┤  ├──────────────┤  │
│   │Structured│  │Counters  │  │Distributed│  │Per-execution │  │
│   │JSON logs │  │Gauges    │  │trace ctx  │  │token counts  │  │
│   │Per-agent │  │Histograms│  │Span-based │  │Cost compute  │  │
│   │Correlation│ │SLO-driven│  │End-to-end │  │Budget checks │  │
│   │  ID      │  │          │  │           │  │              │  │
│   └────┬─────┘  └────┬─────┘  └────┬─────┘  └──────┬───────┘  │
│        │             │             │                │          │
│        ▼             ▼             ▼                ▼          │
│   ┌────────────────────────────────────────────────────────┐   │
│   │           Unified Telemetry Collector                  │   │
│   └────────────────────────────────────────────────────────┘   │
│        │             │             │                │          │
│        ▼             ▼             ▼                ▼          │
│   ┌──────────┐  ┌──────────┐  ┌──────────┐  ┌──────────────┐  │
│   │Log Store │  │Metrics DB│  │Trace Store│  │Cost Dashboard│  │
│   └──────────┘  └──────────┘  └──────────┘  └──────────────┘  │
└────────────────────────────────────────────────────────────────┘

Key Metrics

MetricTypeDescription
agent.execution.duration_msHistogramEnd-to-end execution time
agent.execution.statusCounterExecutions by status (completed, failed, timeout)
agent.model.latency_msHistogramPer-model-call latency
agent.model.tokens.inputCounterInput tokens consumed
agent.model.tokens.outputCounterOutput tokens generated
agent.tool.invocationsCounterTool calls by name and status
agent.tool.duration_msHistogramTool execution time
agent.memory.retrieval_msHistogramMemory lookup latency
agent.policy.violationsCounterPolicy violations by type and enforcement level
agent.cost.usdCounterDollar cost by agent, model, org
agent.queue.depthGaugePending executions in job queue
agent.queue.wait_msHistogramTime from enqueue to dequeue

Distributed Tracing

Every execution creates a root trace span. Child spans are created for:

  • Each model invocation (includes model, token counts, latency)
  • Each tool call (includes tool name, arguments hash, result status)
  • Memory retrieval operations
  • Policy evaluations
  • Sub-agent delegations (linked via parent trace ID)

Correlation ID propagated across all async boundaries (existing correlation_id_var pattern).


6.3 Fault Tolerance & Circuit Breaker Patterns

Circuit Breaker State Machine

      ┌─────────┐
      │  CLOSED  │ ← Normal operation
      └────┬─────┘
           │ failure_count >= threshold
           ▼
      ┌─────────┐
      │  OPEN    │ ← All requests fail-fast
      └────┬─────┘
           │ timeout expires
           ▼
      ┌──────────┐
      │HALF-OPEN │ ← Probe with single request
      └────┬─────┘
           │
    ┌──────┴──────┐
    │ Success     │ Failure
    ▼             ▼
 CLOSED         OPEN

Circuit Breaker Applications

ComponentFailure SignalThresholdRecovery
Model provider5xx response, timeout5 failures in 30s60s half-open probe
Tool executionTimeout, crash3 failures in 60s120s probe
Data source connectorConnection refused3 failures in 60s30s probe
Vector storeQuery timeout5 failures in 30s30s probe

Additional Resilience Patterns

PatternApplication
Retry with exponential backoffModel API calls (429 rate limit), transient DB errors
BulkheadSeparate thread/connection pools per tenant to prevent noisy-neighbor
Timeout cascadeExecution timeout > model timeout > tool timeout — prevents child from outliving parent
Dead letter queueFailed executions moved to DLQ for investigation; not silently dropped
Graceful degradationIf memory retrieval fails, execute without memory context (reduced quality, not failure)

6.4 Scalability Considerations

Horizontal Scaling Architecture

┌──────────────────────────────────────────────────────┐
│                    Load Balancer                      │
└────────────────────────┬─────────────────────────────┘
                         │
          ┌──────────────┼──────────────┐
          ▼              ▼              ▼
   ┌──────────┐   ┌──────────┐   ┌──────────┐
   │ API Node │   │ API Node │   │ API Node │
   │ (Control)│   │ (Control)│   │ (Control)│
   └────┬─────┘   └────┬─────┘   └────┬─────┘
        │               │               │
        └───────────────┼───────────────┘
                        │
                   ┌────┴────┐
                   │  Queue  │
                   └────┬────┘
                        │
          ┌─────────────┼─────────────┐
          ▼             ▼             ▼
   ┌────────────┐ ┌────────────┐ ┌────────────┐
   │  Worker    │ │  Worker    │ │  Worker    │
   │ (Runtime)  │ │ (Runtime)  │ │ (Runtime)  │
   │ Pool      │ │ Pool      │ │ Pool      │
   └────────────┘ └────────────┘ └────────────┘

Queue-Based Load Leveling

All agent executions are enqueued, not executed synchronously:

  1. API handler validates request + policies → enqueues execution job → returns 202 Accepted.
  2. Worker pool pulls jobs from queue at their own pace.
  3. Queue depth drives auto-scaling decisions.
  4. Backpressure: if queue depth exceeds threshold, new requests receive 429 Too Many Requests.

Scaling triggers:

MetricScale-Out TriggerScale-In Trigger
Queue depth> 100 pending for > 30s< 10 pending for > 5min
Worker CPU> 70% for > 2min< 20% for > 10min
p99 latency> SLO for > 1minWithin SLO for > 10min

Scalability by Component

ComponentScaling StrategyBottleneck Mitigation
API GatewayHorizontal (stateless)Connection pooling, request coalescing
Control Plane APIsHorizontal (stateless)Read replicas for catalog queries
Execution WorkersHorizontal (queue-based)Worker auto-scaling on queue depth
Relational DBVertical + read replicasPartition by organization_id, connection pooling
Vector StoreHorizontal (sharded by agent)Index per agent/collection, ANN tuning
CacheHorizontal (consistent hashing)Eviction policies, key partitioning
Model invocationsRate-limited by providerMulti-provider routing, request queuing

6.5 Governance Controls & Compliance Hooks

Governance Framework

ControlImplementation
Data residencyExecution routed to region-specific workers; model endpoints filtered by region
Data retentionConfigurable per org: execution history, thread messages, memory TTLs
Right to deletionAPI to purge all data for a user/thread/agent — cascading delete across all stores
Audit completenessEvery state mutation emits audit event; audit log is append-only, tamper-evident
Policy versioningPolicies are versioned; enforcement changes require audit entry
Export controlsGoverning which model providers are permitted (e.g., no cross-border model calls)
PII handlingConfigurable PII detection + redaction in policy engine before model invocation

Compliance Hooks Architecture

┌──────────────────────────────────────────────────┐
│               Compliance Hook Points             │
│                                                  │
│  PRE-EXECUTION                                   │
│  ├─ Input PII scan → redact / block             │
│  ├─ Data residency check → route or reject      │
│  ├─ User consent verification                   │
│  └─ Rate / budget pre-check                     │
│                                                  │
│  DURING EXECUTION                                │
│  ├─ Tool access authorization                   │
│  ├─ Data source query logging                   │
│  └─ Real-time cost metering                     │
│                                                  │
│  POST-EXECUTION                                  │
│  ├─ Output content filtering                    │
│  ├─ Secret / credential scan                    │
│  ├─ Audit event emission                        │
│  └─ Retention policy enforcement                │
│                                                  │
│  PERIODIC                                        │
│  ├─ Data retention sweep                        │
│  ├─ Credential rotation                         │
│  ├─ Compliance report generation                │
│  └─ SLO adherence calculation                   │
└──────────────────────────────────────────────────┘

Regulatory Mapping

RegulationPlatform Controls
GDPRData residency routing, right to deletion API, consent hooks, DPA support
SOC 2Audit logging, access controls, encryption, change management
HIPAABAA support, PHI detection/redaction, minimum necessary access
ISO 27001Risk assessment hooks, ISMS integration, incident response workflows

6.6 Data Flows & Integration Points

Primary Data Flows

graph LR
    subgraph "Inbound"
        USER["User Message"]
        DOC["Document Upload"]
        WEBHOOK["External Webhook"]
        SCHEDULE["Scheduled Trigger"]
    end

    subgraph "Processing"
        INGEST["Ingestion Pipeline"]
        EXEC["Execution Engine"]
        EVAL["Evaluation Engine"]
    end

    subgraph "Outbound"
        RESPONSE["Agent Response"]
        TOOL_OUT["Tool Side Effects"]
        EVENT["Event Publication"]
        REPORT["Evaluation Reports"]
    end

    subgraph "Persistence"
        STATE["State Store"]
        VECTOR["Vector Store"]
        AUDIT2["Audit Log"]
        METRICS["Metrics Pipeline"]
    end

    USER -->|"API / WebSocket"| EXEC
    DOC -->|"Upload API"| INGEST
    WEBHOOK -->|"Webhook Handler"| EXEC
    SCHEDULE -->|"Cron Scheduler"| EXEC

    INGEST -->|"chunks + embeddings"| VECTOR
    EXEC -->|"execution record"| STATE
    EXEC -->|"model calls, tool calls"| AUDIT2
    EXEC -->|"telemetry events"| METRICS
    EXEC --> RESPONSE
    EXEC --> TOOL_OUT
    EXEC -->|"completion event"| EVENT

    EVAL -->|"scoring metrics"| STATE
    EVAL --> REPORT

Integration Points

IntegrationProtocolDirectionDescription
Client SDKREST + WebSocket + SSEInboundAgent execution, thread management, streaming
Model providersHTTPS (provider-specific)OutboundLLM inference calls
Webhook destinationsHTTPS POSTOutboundEvent notifications, tool results
Identity providerOAuth 2.0 / OIDCInboundAuthentication, SSO
Object storageS3-compatible APIBidirectionalDocument upload/download
Telemetry collectorOTLP (OpenTelemetry)OutboundTraces, metrics, logs
External toolsHTTPS / gRPC / MCPOutboundTool execution for external APIs
Event busPub/sub protocolOutboundExecution lifecycle events

Event Schema (Pub/Sub)

All lifecycle events follow a standard envelope:

{
  "event_id": "evt-uuid",
  "event_type": "agent.execution.completed",
  "timestamp": "2026-03-22T10:00:00Z",
  "organization_id": "org-uuid",
  "subject": {
    "agent_id": "agent-uuid",
    "execution_id": "exec-uuid",
    "thread_id": "thread-uuid"
  },
  "data": {
    "status": "completed",
    "duration_ms": 3421,
    "total_tokens": 2370,
    "total_cost_usd": 0.0142
  },
  "correlation_id": "corr-uuid"
}

7. Data Model Summary

Entity Relationship Overview

Organization (tenant root)
  ├── Agent
  │     ├── AgentVersion (immutable snapshots)
  │     ├── AgentPolicyBinding → AgentPolicy
  │     ├── Execution
  │     │     ├── ExecutionCheckpoint
  │     │     ├── SubAgentInvocation
  │     │     └── TokenEvent
  │     ├── Thread
  │     │     └── ThreadMessage
  │     ├── AgentMemoryDocument
  │     │     └── DocumentChunk (with embeddings)
  │     ├── AgentEpisodicMemory (with embeddings)
  │     └── AgentEntityMemory (with embeddings)
  ├── Tool
  │     └── ToolVersion
  ├── DataSourceConnector
  ├── ModelEndpoint
  ├── Workflow
  │     └── WorkflowRun
  ├── EvaluationDataset
  │     └── EvaluationRun
  │           └── EvaluationSample
  ├── MarketplaceItem
  ├── Budget
  └── AuditLogRecord

Table Count by Domain

DomainTablesNotes
Agent lifecycle2agents, agent_versions (migration 003)
Execution3executions, execution_checkpoints, sub_agent_invocations
Memory4agent_memory_documents, document_chunks, agent_episodic_memories, agent_entity_memories (migration 005)
Policy2agent_policies, agent_policy_bindings (migration 006)
Threading2threads, thread_messages
Model registry1model_endpoints
Tool registry2tools, tool_attachments
Data sources1data_source_connectors
Workflow2workflows, workflow_runs
Evaluation3evaluation_datasets, evaluation_runs, evaluation_samples
Marketplace1marketplace_items
Cost2token_events, budgets
Audit1audit_log
Total~26

8. Deployment Topology

Production Reference Architecture

┌─────────────────────────────────────────────────────────────┐
│                        Region A (Primary)                    │
│                                                             │
│  ┌─────────────┐     ┌─────────────────────────────────┐   │
│  │Load Balancer│────▶│  API Tier (3+ replicas)         │   │
│  │  + WAF      │     │  Control Plane + Runtime APIs   │   │
│  └─────────────┘     └──────────────┬──────────────────┘   │
│                                      │                      │
│                              ┌───────┴───────┐              │
│                              │  Job Queue    │              │
│                              │  (HA cluster) │              │
│                              └───────┬───────┘              │
│                                      │                      │
│                      ┌───────────────┼───────────────┐      │
│                      ▼               ▼               ▼      │
│              ┌──────────────┐ ┌──────────┐ ┌──────────────┐ │
│              │ Worker Pool  │ │ Worker   │ │ Worker Pool  │ │
│              │ (auto-scale) │ │ Pool     │ │ (auto-scale) │ │
│              └──────────────┘ └──────────┘ └──────────────┘ │
│                                                             │
│  ┌──────────────────────────────────────────────────────┐   │
│  │ Data Tier                                            │   │
│  │ ┌────────────┐ ┌────────────┐ ┌────────────────────┐ │   │
│  │ │Relational  │ │Vector Store│ │Distributed Cache   │ │   │
│  │ │DB (HA)     │ │(Replicated)│ │(Clustered)         │ │   │
│  │ └────────────┘ └────────────┘ └────────────────────┘ │   │
│  │ ┌────────────┐ ┌─────────────────────────────────┐   │   │
│  │ │Object Store│ │Telemetry Collector + Storage    │   │   │
│  │ └────────────┘ └─────────────────────────────────┘   │   │
│  └──────────────────────────────────────────────────────┘   │
└─────────────────────────────────────────────────────────────┘

┌─────────────────────────────────────────────────────────────┐
│                    Region B (DR Standby)                      │
│  ┌──────────────────────────────────────────────────────┐   │
│  │ Read-replica DB │ Replicated Vector Store │ Cold API  │   │
│  └──────────────────────────────────────────────────────┘   │
└─────────────────────────────────────────────────────────────┘

HA/DR Specifications

ParameterValue
RTO15 minutes (regional failover)
RPO< 1 minute (synchronous replication within region, async cross-region)
Availability target99.95% (control plane), 99.9% (runtime plane)
Multi-AZAll stateful components deployed across ≥ 2 availability zones
Cross-regionWarm standby in secondary region; async replication
BackupDaily full + continuous WAL archiving; 30-day retention

9. Risks & Mitigations

#RiskImpactLikelihoodMitigation
1Runaway agent costHighMediumPer-execution and per-org cost caps with block_at_limit enforcement
2Prompt injection bypasses controlsHighMediumLayered defense: PromptGuard regex + model-based detection + output scanning
3Model provider outageHighLowMulti-provider fallback chain with circuit breakers
4Noisy neighbor (one tenant starves others)MediumMediumBulkhead pattern: per-tenant worker pools, queue priorities, resource quotas
5Vector store scalability ceilingMediumLowAgent-scoped indices with configurable embedding dimensions; tiered storage
6Sensitive data in model contextHighMediumPII detection in policy engine; tenant-scoped encryption; data masking
7Sub-agent infinite recursionMediumLowHard depth limit (max_delegation_depth=3), aggregate token budget
8State loss during worker crashMediumLowCheckpoint-based recovery; idempotent execution steps
9Evaluation metric gamingLowLowHold-out test sets, human review sampling, A/B statistical validation
10Tool supply chain attack (marketplace)HighLowSecurity scanning, manual review for public tools, sandboxed execution

10. Open Questions

#QuestionDecision OwnerImpact
1Should sub-agent communication support shared mutable state or only message passing?Platform ArchitectAffects isolation guarantees and debugging complexity
2What is the maximum thread history depth before mandatory summarization?ProductAffects memory costs and context quality
3Should the marketplace support paid third-party tools?Product / LegalAffects billing integration and liability model
4What is the minimum evaluation dataset size for regression gating pre-activation?ML EngineeringAffects agent deployment velocity
5Should the platform support fine-tuning model endpoints, or only inference?Platform ArchitectAffects compute requirements and cost model

11. Appendix: Existing Implementation References

The following table maps HLD subsystems to existing Archmorph codebase artifacts:

SubsystemExisting CodeMigrationStatus
Agent Registrymodels/agent.py (Agent, AgentVersion), routers/agents.py003_agent_paasCRUD + versioning implemented
Tool Registryservices/tool_registry.py (ToolRegistry)Registry + execution implemented
Policy Engineservices/policy_engine.py (PolicyEngine), models/policy.py006_policyInput/output/tool policies implemented
Prompt Guardprompt_guard.py (injection patterns, response leak patterns, PROMPT_ARMOR)Production-hardened
Memory Managerservices/agent_memory.py (MemoryManager), models/memory.py005_memoryToken windowing + episodic/entity memory implemented
Model Routerservices/model_router.py (ModelClient protocol, AzureOpenAI + Anthropic impls)Protocol + 2 providers implemented
Model Registrymodels/model_registry.py (ModelEndpoint), routers/models.pyCRUD implemented
Execution Modelmodels/execution.py (Execution with token accounting)004_executionsStep tracking + cost tracking implemented
Audit Loggingaudit_logging.py (AuditLogger, AuditEvent), models/audit.pyFull audit pipeline with alerting
Job Queuejob_queue.py (JobManager with SSE streaming)In-memory with SSE; Redis upgrade path noted
Performanceperformance_config.py (multi-worker, connection pooling)Gunicorn config, DB pool tuning

Document prepared for Track 2 — STU-MSFT review.
Architecture decisions are vendor-neutral and implementation-ready.

Related Documents