async-bulkhead-llm - v3.19.1
    Preparing search index...

    Streaming and reconciliation

    A reservation made at admission is a guess about a request that has not run yet. Streaming responses let you correct it while the call is still in flight.

    Report cumulative usage as stream events arrive. Input over-estimates are refunded to the budget immediately; output overruns expand the hold (blocking new admissions until the runaway stream releases). The returned snapshot lets a gateway decide to abort a stream, and since v3.18 the bulkhead can do it for you (see Stopping on overrun):

    await bulkhead.run(request, async (signal, ctx) => {
    for await (const event of providerStream(request, signal)) {
    const snap = ctx!.reportUsage({
    input: event.usage.input_tokens,
    output: event.usage.output_tokens, // cumulative
    });
    if (snap.outputRemaining === 0) {
    // stream has consumed its entire output reservation — abort policy here
    }
    }
    return final;
    });

    If release() is called without usage, the last reported usage drives the final refund. Reports are clamped monotonically non-decreasing per field.

    Gateways that receive cumulative streaming usage may opt into shrinking the active hold before request completion:

    const report = ctx!.reportUsage(
    { input: actualInput, output: cumulativeOutput },
    {
    remainingOutputTokens: Math.max(0, maxOutput - cumulativeOutput),
    safetyMarginTokens: 128,
    },
    );

    The progressive hold is:

    min(remainingOutputTokens + safetyMarginTokens, reservation) + output overrun
    

    The safety margin keeps back tokens the call already reserved, so the first two terms together never exceed the call's reservation. A fixed margin that is larger than a small request's reservation holds that whole reservation and no more; it is not counted as overrun and cannot trigger an overrun stop. Only output beyond the reserved output cap raises a progressive hold above the reservation.

    This lets processed input and already-generated output stop occupying the in-flight budget. The caller must use the option only after prefill is known to be complete and must derive remainingOutputTokens from an authoritative provider output cap. Omitting the second argument preserves the behavior of retaining the full output reservation until release. Final release() settlement remains authoritative.

    Progressive updates share the existing sequence-numbered usage event. Events include progressive, remainingOutputTokens, and safetyMarginTokens when the progressive mode is used.

    By default an overrun only widens the hold. A limits snapshot with stop.overrunAllowanceTokens bounds it instead:

    bulkhead.applyLimits({
    ...bulkhead.limits(),
    revision: bulkhead.limits().revision + 1,
    stop: { overrunAllowanceTokens: 500 },
    });

    A call whose hold grows more than 500 tokens above its reservation is stopped: its callback signal is aborted and run() rejects with LLMAdmissionStoppedError, cause "overrun". The same setting stops calls when a report leaves a class above its maxInFlightTokens or the pool above its budget, until the holds of the calls still running fit.

    The report that causes a stop, and every later report for that admission, carries stopped: true. The tokens stay charged until the callback settles. Details and the order in which calls are picked are in Stopping admitted work.

    Effective cumulative reportUsage() updates emit a sequence-numbered event. Stale or duplicate reports are clamped as before and do not emit:

    bulkhead.on('usage', (event) => {
    // External stores can ignore an update whose sequence is not newer.
    distributedLedger.adjust({
    admissionId: event.admissionId,
    sequence: event.sequence,
    heldTokens: event.heldTokens,
    });
    });

    The event includes previous/current hold, delta, cumulative usage, priority, output-cap state, and over-reservation status. Listeners remain synchronous; enqueue network or storage work rather than blocking inside the callback.

    For admission-critical distributed enforcement, use the returned report and await the external update in the gateway's stream loop:

    const report = ctx!.reportUsage(cumulativeUsage);
    await distributedLedger.setHold({
    admissionId: report.admissionId,
    sequence: report.sequence,
    heldTokens: report.held,
    });

    Streaming results cannot be shared by reference between deduplicated callers. See Deduplication for shareResult and the per-call dedup: false opt-out.