packages
@loop-engine/adapter-openai
Overview
@loop-engine/adapter-openai wraps the OpenAI API as a Loop Engine AI actor using response_format: { type: "json_object" } for structured output. Same governance model as adapter-anthropic — identical guard enforcement, same audit trail, drop-in pattern for multi-model loops.
Installation
1npm install @loop-engine/adapter-openai openaiPeer dependencies
1openai ^4.0.0Basic usage
1import OpenAI from 'openai'2import { createOpenAIActorAdapter } from '@loop-engine/adapter-openai'3import { createLoopSystem } from '@loop-engine/sdk'4 5const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY })6 7const adapter = createOpenAIActorAdapter(openai, {8 modelId: 'gpt-4o',9 confidenceThreshold: 0.75,10})11 12const loopSystem = await createLoopSystem({ loops: [] })13const aggregateId = 'procurement-1'14 15"cmt">// Get the AI actor's decision for the current loop state16const { actor, decision } = await adapter.createSubmission({17 loopId: 'procurement',18 loopName: 'SCM Procurement',19 currentState: 'pending_analysis',20 availableSignals: [21 {22 signalId: 'submit_recommendation',23 name: 'Submit Recommendation',24 description: 'Submit a purchase order recommendation',25 allowedActors: ['ai-agent', 'automation'],26 },27 ],28 instruction: 'Analyze the demand data and recommend whether to issue a purchase order.',29 evidence: { demandForecast: 0.89, currentStock: 42, reorderPoint: 50 },30})31 32"cmt">// Submit to the runtime — guards evaluate after this point33const result = await loopSystem.transition({34 aggregateId,35 signal: decision.signalId,36 actor,37 evidence: decision,38})Configuration reference
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| modelId | string | gpt-4o | OpenAI model to use |
| maxTokens | number | 1024 | Max tokens in response |
| systemPrompt | string | — | Optional system prompt prepended to loop context |
| confidenceThreshold | number | 0.7 | Minimum confidence required (0–1) |
What the adapter produces
The adapter returns an AIAgentActor with:
type: "ai-agent"provider: "openai"modelId— the model usedconfidence— extracted from the model responsepromptHash— SHA-256 of the prompt sent (for audit trail)
Guard enforcement note
The confidence guard runs at the runtime level — not inside the adapter. If you configure a confidence-threshold guard on the transition, the runtime will block the transition if the model's confidence falls below the threshold regardless of what the adapter returns.