@overengineered-solutions/ai-runtime
v0.1.0
Published
Tenant-scoped AI runtime primitives — spend-consent gate, kill switch, tool-loop detector, provider type contracts. 0.1.0 ships types + gate logic; concrete provider adapters land in 0.2.0.
Maintainers
Readme
@overengineered-solutions/ai-runtime
Tenant-scoped AI runtime primitives — spend-consent gate, kill switch, tool-loop detector, provider type contracts.
Status — 0.1.0: types + pure gate logic. Concrete provider adapters (Anthropic, OpenAI, Claude Code, Codex) land in 0.2.0.
Install
pnpm add @overengineered-solutions/ai-runtimeZero runtime dependencies. Bring your own provider SDKs.
Usage
import {
isApiRuntimeAllowed,
shouldShortCircuitToolLoop,
type EventSink,
} from '@overengineered-solutions/ai-runtime';
// 1) Gate every AI call behind the runtime decision.
const gate = isApiRuntimeAllowed({
tenant: { tenantId },
killSwitchOn: row.kill_switch_on,
pausedUntil: row.paused_until,
quietHours: row.quiet_hours,
nowIso: new Date().toISOString(),
});
if (!gate.allowed) {
throw new Error(`AI runtime gated: ${gate.reason}`);
}
// 2) Detect tool-use loops (same (toolName, inputHash) N consecutive times).
const { shortCircuit, nextState } = shouldShortCircuitToolLoop({
state: priorState,
latest: { toolName, inputHash },
tenantId,
threshold: 3,
sink,
});
if (shortCircuit) {
// surface to operator + halt the conversation loop
}Why pure functions
isApiRuntimeAllowed and detectToolLoop are pure functions over their inputs. Storage / I/O is the consumer's responsibility — fetch the kill switch, paused_until, quiet_hours from your tenant store, then call the gate. This keeps the package driver-agnostic (no Supabase, no specific ORM) and trivially testable.
License
MIT
