Site search

Find architecture, research, and terms

Start typing to search the editorial index.

Production practiceAdvanced

Machine Intelligence Runtime Design Patterns

Map enterprise design patterns to MIR orchestration, policy, tools, model adapters, recovery, lifecycle, and evidence with compact C# contracts.

Pattern map

Patterns do not make a system safe by name. They provide recognizable structures for isolating providers, selecting behavior, controlling failure, preserving history, and managing side effects. The runtime contract still determines what each pattern is allowed to do.

Pattern MIR Use Example
Adapter Normalize models, tools, memory, evidence stores OpenAIAdapter, LocalVllmAdapter, SqlToolConnector
Strategy Select model, verifier, recovery, approval, routing behavior Fast model first, high-risk verifier, rule fallback
Factory Build model clients, tool connectors, pipelines from config RuntimeProfileFactory
Decorator Add logging, redaction, retry, timeout, metrics, policy wrappers EvidenceLoggingModelAdapter
Circuit Breaker Stop repeated failures from model or tool providers Suspend a route after a timeout threshold
Saga Manage multi-step side effects with compensation Stage, test, approve, publish, rollback
Event Sourcing Preserve runtime decisions and state changes as evidence Append-only EvidenceLedger
CQRS Separate live run commands from audit and reporting queries Runtime command store plus evidence read model
Dependency Injection Keep use cases independent from providers Inject IModelAdapter and IToolConnector
Specification Express policy, risk, schema, and approval rules RequiresApprovalSpecification
State Machine Manage run lifecycle explicitly created, provisioning, running, awaiting approval, recovering, completed, failed, terminated

These patterns are established software-engineering tools. Their application to model-driven execution is editorial synthesis and must be validated against the workload. Source: Azure pattern catalog

Pattern-to-layer placement

Model adapters · Tools · Memory · Evidence

Adapter and Decorator

Normalize external contracts, then add bounded cross-cutting controls such as redaction, telemetry, timeout, and retry.

Runtime orchestration · Policy · Recovery · Routing

Strategy, Factory, Specification

Select behavior from explicit runtime configuration and evaluate typed rules rather than branching on prompt text.

Evidence and telemetry

Event Sourcing and CQRS

Append facts on the command path; project them into review, audit, incident, and performance views.

Runtime orchestration · Tools and connectors

Saga

Coordinate side effects with idempotent steps, checkpoints, and compensating actions.

External models · Tools · Queues · Stores

Circuit Breaker

Contain repeated external failure and route to a safe fallback, suspension state, or operator review.

Runtime orchestration · Control plane

State Machine

Make run, approval, recovery, completion, failure, and termination transitions explicit and testable.

Anti-patterns

Structures that erase runtime control

  • One giant “agent loop” owns everything.
  • Provider SDK calls are embedded in controllers.
  • Prompt text decides permissions.
  • Tools receive broad write authority.
  • Logs are generic strings instead of typed evidence events.
  • Long-running work is hidden inside a synchronous request.
  • Human approval is stored only in chat text.
  • Memory has no provenance, TTL, owner, or deletion path.
  • Recovery means “try again” without checkpoint, idempotency, or compensation.

Small code-oriented contracts

These panels are conceptual C# contracts, not a shipped MiRuntime SDK. They show where typed authority and evidence can live.

IModelAdapter

/// <summary>Invokes a model through a provider-neutral boundary.</summary>
public interface IModelAdapter
{
    /// <summary>Returns a candidate without granting side-effect authority.</summary>
    /// <param name="request">Validated model request.</param>
    /// <param name="cancellationToken">Cancellation signal for the current run.</param>
    Task<ModelCandidate> GenerateAsync(
        ModelRequest request,
        CancellationToken cancellationToken);
}

IEvidenceLedger

/// <summary>Appends immutable, ordered runtime evidence.</summary>
public interface IEvidenceLedger
{
    /// <summary>Records one validated evidence event.</summary>
    /// <param name="evidenceEvent">Typed event with a UTC timestamp.</param>
    /// <param name="cancellationToken">Cancellation signal.</param>
    Task AppendAsync(
        EvidenceEvent evidenceEvent,
        CancellationToken cancellationToken);
}

IRuntimePolicy

/// <summary>Evaluates authority for a proposed runtime action.</summary>
public interface IRuntimePolicy
{
    /// <summary>Returns allow, deny, transform, or require-approval.</summary>
    /// <param name="context">Identity, risk, tenant, and action context.</param>
    PolicyDecision Evaluate(PolicyContext context);
}

RunLifecycleState

/// <summary>Represents explicit run lifecycle states.</summary>
public enum RunLifecycleState
{
    Created,
    Provisioning,
    Running,
    AwaitingApproval,
    Recovering,
    Completed,
    Failed,
    Terminated
}

ToolActionEnvelope

/// <summary>Contains a proposed tool action before execution.</summary>
public sealed class ToolActionEnvelope
{
    [Display(Name = "Run")]
    public required Guid RunId { get; init; }

    [Display(Name = "Tool")]
    public required string ToolId { get; init; }

    [Display(Name = "Proposed At UTC")]
    public required DateTimeOffset ProposedAtUtc { get; init; }

    [Display(Name = "Idempotency Key")]
    public required string IdempotencyKey { get; init; }
}

CircuitBreakerModelAdapter

/// <summary>Stops calls after repeated provider failure.</summary>
public sealed class CircuitBreakerModelAdapter : IModelAdapter
{
    /// <summary>Invokes the inner adapter through a configured breaker.</summary>
    /// <param name="request">Validated model request.</param>
    /// <param name="cancellationToken">Cancellation signal.</param>
    public Task<ModelCandidate> GenerateAsync(
        ModelRequest request,
        CancellationToken cancellationToken) =>
        _breaker.ExecuteAsync(
            token => _inner.GenerateAsync(request, token),
            cancellationToken);
}

ApprovalRequiredPolicy

/// <summary>Requires review for consequential action classes.</summary>
public sealed class ApprovalRequiredPolicy : IRuntimePolicy
{
    /// <summary>Evaluates the proposed action and its risk class.</summary>
    /// <param name="context">Validated runtime policy context.</param>
    public PolicyDecision Evaluate(PolicyContext context) =>
        context.Action.IsIrreversible || context.Risk.IsHigh
            ? PolicyDecision.RequireApproval("Consequential side effect")
            : PolicyDecision.Allow();
}
Practice status

Adapter, circuit breaker, state machine, dependency injection, and transactional patterns are production practices. Event-sourced evidence and agent-specific policy specifications are emerging implementations whose storage, privacy, and replay semantics require local design.

Source record

References

Suggest a correction
  1. Microsoft. Azure Architecture Center. Published Current documentation; last reviewed 2026-06-23 UTC. Official architecture guidance.

  2. Microsoft. Microsoft Learn. Published Current documentation; last reviewed 2026-06-23 UTC. Official architecture guidance.

  3. OpenTelemetry project. Cloud Native Computing Foundation. Published Current specification repository; last reviewed 2026-06-24 UTC. Official specification.