Loop Engine

Examples

AI Replenishment

AI replenishment shows model-swappable actor wiring with identical loop constraints and deterministic approval policy.

Loop behavior

  • States: SIGNAL_DETECTED -> AI_ANALYSIS -> PENDING_BUYER_APPROVAL -> PO_TRIGGERED
  • AI actor transition: recommend_replenishment
  • Hard guards: confidence_threshold, approval_obtained
  • Human-only transition: approve_replenishment

Scenario

Lumebondé Bond Repair Serum (LMB-BRS-001) spikes +89% at DC-East with currentStock=142, reorderPoint=280, forecastedDemand=527, and leadTimeDays=14.

Loop definition

1import { LoopBuilder } from "@loop-engine/sdk"
2 
3const replenishmentLoop = LoopBuilder
4 .create("scm.replenishment", "scm")
5 .version("1.0.0")
6 .description("Triggered replenishment from demand signal")
7 .state("SIGNAL_DETECTED")
8 .state("AI_ANALYSIS")
9 .state("PENDING_BUYER_APPROVAL")
10 .state("PO_TRIGGERED", { isTerminal: true })
11 .state("DEFERRED", { isTerminal: true })
12 .initialState("SIGNAL_DETECTED")
13 .transition({ id: "start_analysis", from: "SIGNAL_DETECTED", to: "AI_ANALYSIS", actors: ["automation"] })
14 .transition({
15 id: "recommend_replenishment",
16 from: "AI_ANALYSIS",
17 to: "PENDING_BUYER_APPROVAL",
18 actors: ["ai-agent", "automation"],
19 guards: [
20 {
21 id: "confidence_threshold" as never,
22 description: "AI confidence >= 0.75",
23 failureMessage: "Confidence below threshold",
24 severity: "hard",
25 evaluatedBy: "external"
26 }
27 ]
28 })
29 .transition({ id: "approve_replenishment", from: "PENDING_BUYER_APPROVAL", to: "PO_TRIGGERED", actors: ["human"] })
30 .outcome({ id: "replenishment_triggered", description: "PO submitted", valueUnit: "replenishment_triggered" })
31 .build()

Anthropic and OpenAI actors

Both providers create the same AIAgentActor shape and submit evidence with identical keys.

1import { actorId } from "@loop-engine/core"
2import type { AIAgentActor } from "@loop-engine/actors"
3 
4const claudeActor: AIAgentActor = {
5 type: "ai-agent",
6 id: actorId("agent:demand-forecaster"),
7 agentId: "claude-sonnet-4-20250514",
8 gatewaySessionId: "claude-session-001"
9}
10 
11const openAiActor: AIAgentActor = {
12 type: "ai-agent",
13 id: actorId("agent:demand-forecaster"),
14 agentId: "gpt-4o",
15 gatewaySessionId: "openai-session-001"
16}

Guard outcomes

1"cmt">// confidence 0.58 -> held at AI_ANALYSIS
2const lowConfidence = await engine.transition({
3 aggregateId,
4 transitionId: transitionId("recommend_replenishment"),
5 actor: claudeActor,
6 evidence: { ai_confidence: 0.58, ai_reasoning: "Insufficient certainty" }
7})
8"cmt">// lowConfidence.status === "guard_failed"
9 
10"cmt">// confidence 0.82 -> advances to PENDING_BUYER_APPROVAL
11const highConfidence = await engine.transition({
12 aggregateId,
13 transitionId: transitionId("recommend_replenishment"),
14 actor: openAiActor,
15 evidence: { ai_confidence: 0.82, ai_reasoning: "Demand spike sustained" }
16})
17"cmt">// highConfidence.status === "executed"

Human approval gate

1"cmt">// AI tries to execute human-only transition
2const rejected = await engine.transition({
3 aggregateId,
4 transitionId: transitionId("approve_replenishment"),
5 actor: claudeActor,
6 evidence: { approval_obtained: true }
7})
8"cmt">// rejected.status === "rejected"
9 
10"cmt">// Human executes approval path
11await engine.transition({
12 aggregateId,
13 transitionId: transitionId("approve_replenishment"),
14 actor: { type: "human", id: "buyer@lumebonde.com" },
15 evidence: { approved: true, approval_obtained: true }
16})

Provider parity

The loop definition, transition IDs, guards, and state progression remain identical across Anthropic and OpenAI implementations.

  • https://github.com/loopengine/loop-examples/tree/main/ai-actors/claude
  • https://github.com/loopengine/loop-examples/tree/main/ai-actors/openai