Loop Engine

Examples

LLM Commerce Gateway Integration

Overview

LLM Commerce Gateway exposes inventory, pricing, suppliers, and order operations as structured tool interfaces for LLM-driven workflows. Loop Engine governs the workflow state around those tools so recommendations and actions happen through explicit transitions.

The boundary is strict: Commerce Gateway handles commerce data access and operation execution, while Loop Engine handles state, actor attribution, guard enforcement, approval gates, and audit records.

Architecture

1Demand Spike Signal
2 |
3 v
4Loop Engine -------------------- engine.start('procurement.loop')
5 | state: GATEWAY_QUERY
6 |
7 v
8AI Actor ----------------------- GET /inventory/:sku
9 | GET /demand-forecast/:sku Commerce
10 | GET /suppliers/:sku ----> Gateway API
11 | (read only)
12 |
13 v
14Loop Engine -------------------- guard: confidence_threshold >= 0.80
15 | state: PENDING_APPROVAL
16 |
17 v
18Human Approves ----------------- engine.transition('approve_order', humanActor)
19 |
20 v
21Automation Actor --------------- POST /purchase-orders Commerce
22 | (write post-approval only) ---> Gateway API
23 |
24 v
25Loop Engine -------------------- transition('place_order') -> engine.close(outcome: 'po_created')

Three governed workflows

| Loop | Trigger | AI queries | Guard | Write operation | |------|---------|------------|-------|-----------------| | Procurement | Demand spike / low stock | Inventory, forecast, suppliers | confidence >= 0.80 + human approval | createPurchaseOrder | | Price change | Competitor price, margin alert | Price history, demand elasticity | confidence >= 0.85 + human approval | updatePrice | | Inventory adjustment | Expiry, damage, shrink | Current lots, expiry dates | human approval | writeDownInventory |

Using @loop-engine/adapter-commerce-gateway

1import { createLoopSystem } from '@loop-engine/sdk'
2import { CommerceGatewayClient, buildProcurementActor } from '@loop-engine/adapter-commerce-gateway'
3 
4const gatewayClient = new CommerceGatewayClient({
5 baseUrl: process.env.COMMERCE_GATEWAY_URL!,
6 apiKey: process.env.COMMERCE_GATEWAY_API_KEY!
7})
8 
9const procurementActor = buildProcurementActor({
10 gatewayClient,
11 llmProvider: 'claude', "cmt">// switch to 'openai' for GPT-4o
12 apiKey: process.env.ANTHROPIC_API_KEY!,
13 confidenceThreshold: 0.8
14})
15 
16const { engine } = await createLoopSystem({ loops: [procurementLoop] })
17const evidence = await procurementActor({ instance: { data: { sku: 'LMB-BRS-001' } } })

The governance boundary

Commerce Gateway owns structured commerce data and operation APIs. It executes reads and writes for inventory, demand, suppliers, pricing, and orders.

Loop Engine owns workflow progression, actor attribution, guard outcomes, human approval transitions, transition history, and learning feedback from outcomes.

Full procurement walkthrough

  1. DEMAND_SPIKE starts procurement.loop and moves to GATEWAY_QUERY.
  2. AI actor reads inventory, demand-forecast, and suppliers from Commerce Gateway.
  3. LLM emits recommendation evidence (recommendedQty, supplierId, confidence, rationale).
  4. confidence_threshold passes and loop moves to PENDING_APPROVAL.
  5. Human approves; automation actor calls createPurchaseOrder; loop executes place_order and closes with po_created.

Resources