Open Agent Loops
API Reference

withModelGate

@open-agent-loops/core


function withModelGate(model, gate): ModelClient;

Defined in: compose.ts:100

Wrap a ModelClient so a ModelGate runs before each stream — the "can I make this call?" seam.

Parameters

ParameterTypeDescription
modelModelClientThe model client to wrap.
gateModelGateThe admission gate awaited before each request.

Returns

ModelClient

A ModelClient that gates every stream on gate.

Remarks

The gate is awaited before model.stream is even invoked, so a gate that backpressures on a full concurrency budget holds the turn at the model boundary without converting messages or touching the wire. Once it resolves, the call proceeds and every StreamEvent is forwarded unchanged; if it rejects (e.g. the run was aborted while waiting), the stream rejects and the inner model is never called.

Composition, not a subclass — stack it with withModelObserver freely.

Example

// Park the call until the live Featherless budget has room; cancel-aware.
const gated = withModelGate(model, async (req) => {
  while (meter.limit !== null && meter.used >= meter.limit) {
    req.signal?.throwIfAborted();
    await meter.nextFrame();
  }
});

See

withModelObserver

On this page