async-bulkhead-llm 3.17.0 → 3.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (201) hide show
  1. package/CHANGELOG.md +181 -0
  2. package/README.md +46 -0
  3. package/dist/cjs/bulkhead.cjs +140 -1817
  4. package/dist/cjs/bulkhead.cjs.map +1 -1
  5. package/dist/cjs/engine/acquire.cjs +144 -0
  6. package/dist/cjs/engine/acquire.cjs.map +1 -0
  7. package/dist/cjs/engine/admission-counts.cjs +111 -0
  8. package/dist/cjs/engine/admission-counts.cjs.map +1 -0
  9. package/dist/cjs/engine/admission-request.cjs +28 -0
  10. package/dist/cjs/engine/admission-request.cjs.map +1 -0
  11. package/dist/cjs/engine/admitted-run.cjs +143 -0
  12. package/dist/cjs/engine/admitted-run.cjs.map +1 -0
  13. package/dist/cjs/engine/admitted-token.cjs +283 -0
  14. package/dist/cjs/engine/admitted-token.cjs.map +1 -0
  15. package/dist/cjs/engine/bypass.cjs +162 -0
  16. package/dist/cjs/engine/bypass.cjs.map +1 -0
  17. package/dist/cjs/engine/capacity.cjs +89 -0
  18. package/dist/cjs/engine/capacity.cjs.map +1 -0
  19. package/dist/cjs/engine/class-limits.cjs +101 -0
  20. package/dist/cjs/engine/class-limits.cjs.map +1 -0
  21. package/dist/cjs/engine/class-slots.cjs +49 -0
  22. package/dist/cjs/engine/class-slots.cjs.map +1 -0
  23. package/dist/cjs/engine/dedup-flow.cjs +207 -0
  24. package/dist/cjs/engine/dedup-flow.cjs.map +1 -0
  25. package/dist/cjs/engine/events.cjs +36 -0
  26. package/dist/cjs/engine/events.cjs.map +1 -0
  27. package/dist/cjs/engine/limit-fit.cjs +108 -0
  28. package/dist/cjs/engine/limit-fit.cjs.map +1 -0
  29. package/dist/cjs/engine/limits.cjs +334 -0
  30. package/dist/cjs/engine/limits.cjs.map +1 -0
  31. package/dist/cjs/engine/observe-stats.cjs +43 -0
  32. package/dist/cjs/engine/observe-stats.cjs.map +1 -0
  33. package/dist/cjs/engine/options.cjs +79 -0
  34. package/dist/cjs/engine/options.cjs.map +1 -0
  35. package/dist/cjs/engine/reject-detail.cjs +103 -0
  36. package/dist/cjs/engine/reject-detail.cjs.map +1 -0
  37. package/dist/cjs/engine/reservation.cjs +55 -0
  38. package/dist/cjs/engine/reservation.cjs.map +1 -0
  39. package/dist/cjs/engine/run-options.cjs +59 -0
  40. package/dist/cjs/engine/run-options.cjs.map +1 -0
  41. package/dist/cjs/engine/run.cjs +76 -0
  42. package/dist/cjs/engine/run.cjs.map +1 -0
  43. package/dist/cjs/engine/running-calls.cjs +79 -0
  44. package/dist/cjs/engine/running-calls.cjs.map +1 -0
  45. package/dist/cjs/engine/shared-usage.cjs +70 -0
  46. package/dist/cjs/engine/shared-usage.cjs.map +1 -0
  47. package/dist/cjs/engine/shared.cjs +23 -0
  48. package/dist/cjs/engine/shared.cjs.map +1 -0
  49. package/dist/cjs/engine/shutdown.cjs +76 -0
  50. package/dist/cjs/engine/shutdown.cjs.map +1 -0
  51. package/dist/cjs/engine/stats.cjs +94 -0
  52. package/dist/cjs/engine/stats.cjs.map +1 -0
  53. package/dist/cjs/engine/stop-policy.cjs +40 -0
  54. package/dist/cjs/engine/stop-policy.cjs.map +1 -0
  55. package/dist/cjs/engine/stop.cjs +91 -0
  56. package/dist/cjs/engine/stop.cjs.map +1 -0
  57. package/dist/cjs/engine/token-ledger.cjs +117 -0
  58. package/dist/cjs/engine/token-ledger.cjs.map +1 -0
  59. package/dist/cjs/engine/usage-report.cjs +96 -0
  60. package/dist/cjs/engine/usage-report.cjs.map +1 -0
  61. package/dist/cjs/engine/would-admit.cjs +37 -0
  62. package/dist/cjs/engine/would-admit.cjs.map +1 -0
  63. package/dist/cjs/errors.cjs +25 -1
  64. package/dist/cjs/errors.cjs.map +1 -1
  65. package/dist/cjs/index.cjs +14 -3
  66. package/dist/cjs/index.cjs.map +1 -1
  67. package/dist/cjs/profiles 2.cjs +16 -0
  68. package/dist/cjs/types 2.cjs +3 -0
  69. package/dist/cjs/types.cjs 2.map +1 -0
  70. package/dist/cjs/validation.cjs 2.map +1 -0
  71. package/dist/esm/bulkhead.js +140 -1817
  72. package/dist/esm/bulkhead.js.map +1 -1
  73. package/dist/esm/engine/acquire.js +141 -0
  74. package/dist/esm/engine/acquire.js.map +1 -0
  75. package/dist/esm/engine/admission-counts.js +108 -0
  76. package/dist/esm/engine/admission-counts.js.map +1 -0
  77. package/dist/esm/engine/admission-request.js +25 -0
  78. package/dist/esm/engine/admission-request.js.map +1 -0
  79. package/dist/esm/engine/admitted-run.js +140 -0
  80. package/dist/esm/engine/admitted-run.js.map +1 -0
  81. package/dist/esm/engine/admitted-token.js +280 -0
  82. package/dist/esm/engine/admitted-token.js.map +1 -0
  83. package/dist/esm/engine/bypass.js +159 -0
  84. package/dist/esm/engine/bypass.js.map +1 -0
  85. package/dist/esm/engine/capacity.js +86 -0
  86. package/dist/esm/engine/capacity.js.map +1 -0
  87. package/dist/esm/engine/class-limits.js +95 -0
  88. package/dist/esm/engine/class-limits.js.map +1 -0
  89. package/dist/esm/engine/class-slots.js +46 -0
  90. package/dist/esm/engine/class-slots.js.map +1 -0
  91. package/dist/esm/engine/dedup-flow.js +204 -0
  92. package/dist/esm/engine/dedup-flow.js.map +1 -0
  93. package/dist/esm/engine/events.js +33 -0
  94. package/dist/esm/engine/events.js.map +1 -0
  95. package/dist/esm/engine/limit-fit.js +105 -0
  96. package/dist/esm/engine/limit-fit.js.map +1 -0
  97. package/dist/esm/engine/limits.js +331 -0
  98. package/dist/esm/engine/limits.js.map +1 -0
  99. package/dist/esm/engine/observe-stats.js +40 -0
  100. package/dist/esm/engine/observe-stats.js.map +1 -0
  101. package/dist/esm/engine/options.js +76 -0
  102. package/dist/esm/engine/options.js.map +1 -0
  103. package/dist/esm/engine/reject-detail.js +100 -0
  104. package/dist/esm/engine/reject-detail.js.map +1 -0
  105. package/dist/esm/engine/reservation.js +52 -0
  106. package/dist/esm/engine/reservation.js.map +1 -0
  107. package/dist/esm/engine/run-options.js +55 -0
  108. package/dist/esm/engine/run-options.js.map +1 -0
  109. package/dist/esm/engine/run.js +73 -0
  110. package/dist/esm/engine/run.js.map +1 -0
  111. package/dist/esm/engine/running-calls.js +76 -0
  112. package/dist/esm/engine/running-calls.js.map +1 -0
  113. package/dist/esm/engine/shared-usage.js +67 -0
  114. package/dist/esm/engine/shared-usage.js.map +1 -0
  115. package/dist/esm/engine/shared.js +20 -0
  116. package/dist/esm/engine/shared.js.map +1 -0
  117. package/dist/esm/engine/shutdown.js +73 -0
  118. package/dist/esm/engine/shutdown.js.map +1 -0
  119. package/dist/esm/engine/stats.js +91 -0
  120. package/dist/esm/engine/stats.js.map +1 -0
  121. package/dist/esm/engine/stop-policy.js +37 -0
  122. package/dist/esm/engine/stop-policy.js.map +1 -0
  123. package/dist/esm/engine/stop.js +88 -0
  124. package/dist/esm/engine/stop.js.map +1 -0
  125. package/dist/esm/engine/token-ledger.js +114 -0
  126. package/dist/esm/engine/token-ledger.js.map +1 -0
  127. package/dist/esm/engine/usage-report.js +87 -0
  128. package/dist/esm/engine/usage-report.js.map +1 -0
  129. package/dist/esm/engine/would-admit.js +34 -0
  130. package/dist/esm/engine/would-admit.js.map +1 -0
  131. package/dist/esm/errors.js +23 -0
  132. package/dist/esm/errors.js.map +1 -1
  133. package/dist/esm/index.js +13 -3
  134. package/dist/esm/index.js.map +1 -1
  135. package/dist/types/bulkhead.d.ts +38 -6
  136. package/dist/types/bulkhead.d.ts.map +1 -1
  137. package/dist/types/engine/acquire.d.ts +25 -0
  138. package/dist/types/engine/acquire.d.ts.map +1 -0
  139. package/dist/types/engine/admission-counts.d.ts +34 -0
  140. package/dist/types/engine/admission-counts.d.ts.map +1 -0
  141. package/dist/types/engine/admission-request.d.ts +12 -0
  142. package/dist/types/engine/admission-request.d.ts.map +1 -0
  143. package/dist/types/engine/admitted-run.d.ts +25 -0
  144. package/dist/types/engine/admitted-run.d.ts.map +1 -0
  145. package/dist/types/engine/admitted-token.d.ts +49 -0
  146. package/dist/types/engine/admitted-token.d.ts.map +1 -0
  147. package/dist/types/engine/bypass.d.ts +17 -0
  148. package/dist/types/engine/bypass.d.ts.map +1 -0
  149. package/dist/types/engine/capacity.d.ts +36 -0
  150. package/dist/types/engine/capacity.d.ts.map +1 -0
  151. package/dist/types/engine/class-limits.d.ts +20 -0
  152. package/dist/types/engine/class-limits.d.ts.map +1 -0
  153. package/dist/types/engine/class-slots.d.ts +20 -0
  154. package/dist/types/engine/class-slots.d.ts.map +1 -0
  155. package/dist/types/engine/dedup-flow.d.ts +41 -0
  156. package/dist/types/engine/dedup-flow.d.ts.map +1 -0
  157. package/dist/types/engine/events.d.ts +13 -0
  158. package/dist/types/engine/events.d.ts.map +1 -0
  159. package/dist/types/engine/limit-fit.d.ts +30 -0
  160. package/dist/types/engine/limit-fit.d.ts.map +1 -0
  161. package/dist/types/engine/limits.d.ts +47 -0
  162. package/dist/types/engine/limits.d.ts.map +1 -0
  163. package/dist/types/engine/observe-stats.d.ts +14 -0
  164. package/dist/types/engine/observe-stats.d.ts.map +1 -0
  165. package/dist/types/engine/options.d.ts +21 -0
  166. package/dist/types/engine/options.d.ts.map +1 -0
  167. package/dist/types/engine/reject-detail.d.ts +25 -0
  168. package/dist/types/engine/reject-detail.d.ts.map +1 -0
  169. package/dist/types/engine/reservation.d.ts +13 -0
  170. package/dist/types/engine/reservation.d.ts.map +1 -0
  171. package/dist/types/engine/run-options.d.ts +25 -0
  172. package/dist/types/engine/run-options.d.ts.map +1 -0
  173. package/dist/types/engine/run.d.ts +28 -0
  174. package/dist/types/engine/run.d.ts.map +1 -0
  175. package/dist/types/engine/running-calls.d.ts +55 -0
  176. package/dist/types/engine/running-calls.d.ts.map +1 -0
  177. package/dist/types/engine/shared-usage.d.ts +28 -0
  178. package/dist/types/engine/shared-usage.d.ts.map +1 -0
  179. package/dist/types/engine/shared.d.ts +76 -0
  180. package/dist/types/engine/shared.d.ts.map +1 -0
  181. package/dist/types/engine/shutdown.d.ts +23 -0
  182. package/dist/types/engine/shutdown.d.ts.map +1 -0
  183. package/dist/types/engine/stats.d.ts +28 -0
  184. package/dist/types/engine/stats.d.ts.map +1 -0
  185. package/dist/types/engine/stop-policy.d.ts +15 -0
  186. package/dist/types/engine/stop-policy.d.ts.map +1 -0
  187. package/dist/types/engine/stop.d.ts +44 -0
  188. package/dist/types/engine/stop.d.ts.map +1 -0
  189. package/dist/types/engine/token-ledger.d.ts +40 -0
  190. package/dist/types/engine/token-ledger.d.ts.map +1 -0
  191. package/dist/types/engine/usage-report.d.ts +65 -0
  192. package/dist/types/engine/usage-report.d.ts.map +1 -0
  193. package/dist/types/engine/would-admit.d.ts +16 -0
  194. package/dist/types/engine/would-admit.d.ts.map +1 -0
  195. package/dist/types/errors.d.ts +21 -2
  196. package/dist/types/errors.d.ts.map +1 -1
  197. package/dist/types/index.d.ts +14 -4
  198. package/dist/types/index.d.ts.map +1 -1
  199. package/dist/types/types.d.ts +103 -2
  200. package/dist/types/types.d.ts.map +1 -1
  201. package/package.json +1 -1
package/CHANGELOG.md CHANGED
@@ -7,6 +7,187 @@ and adheres to [Semantic Versioning](https://semver.org/).
7
7
 
8
8
  ---
9
9
 
10
+ ## [3.18.0] - 2026-11-23
11
+
12
+ Adds an opt-in way to stop work the bulkhead already admitted. Nothing is
13
+ stopped unless a limits snapshot carries a `stop` policy or a drain passes
14
+ `stopOnTimeout`; without them this release behaves as 3.17.1.
15
+
16
+ ### Added
17
+
18
+ * **`LLMAdmissionStoppedError` and one meaning of "stop".** Whatever the
19
+ trigger, stopping a call aborts the signal passed to its `run()` callback,
20
+ rejects the caller with `LLMAdmissionStoppedError` (`admissionId`, `cause`,
21
+ `resources`, `admissionClass`), and returns the concurrency slot at once.
22
+ The token hold stays charged until the callback settles, because a local
23
+ abort does not prove the provider stopped work; the borrowed-slot deadline
24
+ already followed this rule. `cause` is `"overrun"`, `"drain_deadline"`, or
25
+ `"limits"`.
26
+
27
+ * **Stop policy in the limits snapshot.** `LLMAdmissionLimits` gains optional
28
+ `stop: { overrunAllowanceTokens?, takeBackGraceMs? }`. It is validated with
29
+ the rest of the snapshot and returned by `limits()`, `stats().limits`, and
30
+ the `reconfigure` event while it is applied. Like every other limit it is
31
+ not inherited: a snapshot without `stop` has no policy. `setBudget()`
32
+ carries the applied policy into its new revision.
33
+
34
+ * **Overrun trigger.** `stop.overrunAllowanceTokens` lets a call's hold grow
35
+ that many tokens above its reservation; one more token stops it with cause
36
+ `"overrun"`. With the allowance set, a usage report that raises a hold and
37
+ leaves a class above `maxInFlightTokens`, or the pool above its budget,
38
+ also stops calls until the holds of the calls still running fit. A class
39
+ above its own ceiling gives up only its own calls. If a snapshot had
40
+ already lowered a ceiling below what is running, a report undoes only its
41
+ own growth. Requires a token budget.
42
+
43
+ * **Drain trigger.** `drain({ timeoutMs, stopOnTimeout: true })` stops every
44
+ call still running at the deadline with cause `"drain_deadline"`. Its
45
+ result gains `stopped`, the number of calls it stopped. Stopped calls stay
46
+ in `inFlight` until their callbacks settle.
47
+
48
+ * **Take-back trigger.** `stop.takeBackGraceMs` bounds how long running work
49
+ may stay above the limits of a new snapshot: a lower `maxConcurrent` or
50
+ token budget, lower class ceilings, or a restored class floor. Nothing is
51
+ stopped during the grace period and calls that finish inside it are left
52
+ alone; then enough calls are stopped to fit, with cause `"limits"`. Any
53
+ later applied snapshot replaces a take-back that is still waiting and, if
54
+ it still leaves work above the limits, starts its own grace period.
55
+
56
+ * **A fixed stop order.** Whenever the engine picks, calls admitted on
57
+ borrowed capacity go first, then normal priority before high, and on a tie
58
+ the call admitted last. A stopped call is never counted or picked again.
59
+ Deduplication followers receive their leader's error and are never picked;
60
+ observe-mode bypasses and queue waiters are never stopped.
61
+
62
+ * **`stop` event and counters.** A `stop` event fires once per stopped call
63
+ with `cause`, `resources`, and `heldTokens`. `stats().llm` gains `stopped`
64
+ and `stoppedByCause`, and each admission class gains `stopped`. All three
65
+ are absent until the first stop.
66
+
67
+ * **`stopped: true` on usage reports.** An `acquire()` token has no signal.
68
+ It learns of a stop from the event and from `stopped: true` on its usage
69
+ reports, starting with the report that caused the stop. Its tokens stay
70
+ charged until `release()`.
71
+
72
+ * **Tests.** Five new files cover each trigger, the stop order, what is never
73
+ stopped, and the default of never stopping: `test/stop_overrun.test.ts`,
74
+ `test/stop_drain.test.ts`, `test/stop_take_back.test.ts`,
75
+ `test/stop_order.test.ts`, and `test/stop_default.test.ts`.
76
+
77
+ * **Guide.** [Stopping admitted work](guides/stopping-admitted-work.md), with
78
+ matching sections in the README and the runtime-limits, streaming,
79
+ admission-classes, gateway-integration, telemetry, concepts, and migration
80
+ guides.
81
+
82
+ ### Contract
83
+
84
+ * A stop is a local decision. It returns the local slot and asks the callback
85
+ to end. It does not prove that a provider account, model queue, or
86
+ accelerator stopped work, and a callback that ignores its signal keeps its
87
+ tokens charged for as long as it runs.
88
+
89
+ * `run()` gives the callback a signal the engine can abort only when a stop
90
+ policy was applied at the moment the call was admitted (`stop: {}` is
91
+ enough). A call admitted without a policy can still be stopped later, by a
92
+ drain or by a policy that arrives afterward: its caller is rejected and its
93
+ slot returns, but its callback is not aborted.
94
+
95
+ * The grace period of a take-back starts again with every newer snapshot that
96
+ still leaves work above the limits. Revisions that arrive faster than the
97
+ grace period keep postponing it.
98
+
99
+ ### Internal
100
+
101
+ * Four new engine modules. `src/engine/running-calls.ts` owns the set of
102
+ admitted calls still running and the stop order; the set is a linked
103
+ list threaded through the calls, so registering one costs a few pointer
104
+ writes. `src/engine/stop.ts`
105
+ holds the three triggers and the timer of a waiting take-back.
106
+ `src/engine/limit-fit.ts` answers whether running work fits the applied
107
+ limits and which call to stop next. `src/engine/stop-policy.ts` validates
108
+ the `stop` part of a snapshot. The admitted token registers its call at
109
+ admission and removes it at stop or final release, and the stop policy
110
+ itself lives with the other limits in `limits.ts`.
111
+
112
+ * The promise `run()` returns for an admitted call is now built by hand
113
+ instead of by an `async` function, so that a stop can reject it early. It
114
+ settles on the same microtask as before.
115
+
116
+ * Every admission now registers its call as running. The median
117
+ `decisionDurationNs` measured on `admit` events stayed within one clock
118
+ tick (42 ns) of 3.17.1.
119
+
120
+ ### Compatibility
121
+
122
+ * Existing exports, methods, and event names are unchanged, and every new
123
+ field is optional. The 13 existing test files pass unedited.
124
+ * Without a stop policy, a `run()` callback still receives exactly the signal
125
+ its caller passed: the same object, or `undefined`. Under a policy it
126
+ receives an engine-owned signal that the caller's abort is forwarded to.
127
+ * Behavior without a stop policy was checked against 3.17.1 by driving both
128
+ builds with the same 1,000 seeded random scenarios, covering classes,
129
+ token budgets, queues, deduplication, observe mode, limit changes, and
130
+ listeners, and comparing every result, error, event, and stats snapshot,
131
+ and the microtask on which each `run()` settled.
132
+ * Code that switches exhaustively on `LLMEventType`, or maps every error
133
+ other than `LLMBulkheadRejectedError` to an upstream failure, should
134
+ account for the `stop` event and `LLMAdmissionStoppedError` before opting
135
+ in.
136
+ * The runtime dependency remains `async-bulkhead-ts@^1.0.1`.
137
+
138
+ ## [3.17.1] - 2026-11-22
139
+
140
+ Internal restructuring release. No public API, runtime behavior, or
141
+ dependency changed; no upgrade action is required.
142
+
143
+ ### Internal
144
+
145
+ * **`createLLMBulkhead` split into focused engine modules.** The former
146
+ 2,690-line `src/bulkhead.ts` is now a short entry point that validates
147
+ options and wires together 25 modules under `src/engine/`, one concern
148
+ each: limits, token ledger, class slots, admission counts, capacity checks,
149
+ the admitted token, `run()`, observe mode, deduplication, stats, events,
150
+ and shutdown. No file under `src/engine/` is longer than 500 lines and no
151
+ function is longer than 150.
152
+
153
+ * **One owner per counter.** Every limit and counter is now written in
154
+ exactly one module, and a global counter and its per-class twin are always
155
+ moved by the same function. Token counters live in `token-ledger.ts`;
156
+ admitted, released, rejected, and abandoned counts in
157
+ `admission-counts.ts`; per-class slot counts in `class-slots.ts`; and the
158
+ revision, budget, high-priority reserve, and class limits in `limits.ts`.
159
+ The per-class state object that functions used to pass around is gone: a
160
+ class travels as its id. The global slot count stays inside
161
+ `reconfigurable-bulkhead.ts`, which is unchanged.
162
+
163
+ * **`rename-cjs.mjs` handles nested output folders.** The CommonJS rename
164
+ step now walks `dist/cjs` recursively, so the `.cjs` renames, `require()`
165
+ rewrites, `sourceMappingURL` trailers, and source-map `file` fields are
166
+ also correct for `dist/cjs/engine/*`.
167
+
168
+ ### Added
169
+
170
+ * **Class-versus-global counter tests.**
171
+ `test/class_counter_invariants.test.ts` takes admissions through a
172
+ reserve, growing and shrinking usage reports, abandonment, limit updates,
173
+ and release, and checks after every step that each per-class counter adds
174
+ up to its global counterpart. The tests read `stats()` only. Before this,
175
+ nothing in the suite could see a class counter drift from its global twin.
176
+
177
+ ### Compatibility
178
+
179
+ * The package entry point exports the identical surface, and the declared
180
+ type of `createLLMBulkhead` and of each of its methods is unchanged.
181
+ * Results, errors, event payloads, rejection reasons and constraints,
182
+ `stats()` snapshots, and the order of counter updates relative to events
183
+ match 3.17.0. This was checked by driving the 3.17.0 build and this build
184
+ with the same 30,000 seeded random scenarios, including listeners that call
185
+ back into the bulkhead, and comparing everything they returned and emitted.
186
+ * `dist/` gains an `engine/` folder for ESM, CJS, and declarations. Deep
187
+ imports of internal modules remain unsupported; the `exports` map still
188
+ exposes only the entry point.
189
+ * The runtime dependency remains `async-bulkhead-ts@^1.0.1`.
190
+
10
191
  ## [3.17.0] - 2026-09-03
11
192
 
12
193
  ### Added
package/README.md CHANGED
@@ -10,6 +10,17 @@
10
10
 
11
11
  ---
12
12
 
13
+ ## The problem
14
+
15
+ LLM calls are slow and uneven. One finishes in two seconds on 500 tokens; the
16
+ next runs for a minute on 100,000. A service that forwards every request it
17
+ receives has no defense when traffic bursts: all of them reach the provider
18
+ at once, the provider starts answering 429 or slowing down, retries add to
19
+ the pile, and every caller waits a long time to fail.
20
+
21
+ Counting requests is not enough to prevent it, because the load is the work
22
+ in flight, and a request's size is part of that work.
23
+
13
24
  ## What it does
14
25
 
15
26
  You wrap your LLM calls in a bulkhead. Before each call runs, the bulkhead
@@ -181,6 +192,32 @@ capacity that client cancellation cannot authoritatively release.
181
192
 
182
193
  → [Stats and events](guides/telemetry.md)
183
194
 
195
+ ## Stopping admitted work
196
+
197
+ By default an admitted call is never touched: a stream that overruns its
198
+ reservation only widens its hold, and a lowered limit waits for running calls
199
+ to finish. A limits snapshot can opt in to stopping them instead:
200
+
201
+ ```ts
202
+ bulkhead.applyLimits({
203
+ ...bulkhead.limits(),
204
+ revision: bulkhead.limits().revision + 1,
205
+ stop: {
206
+ overrunAllowanceTokens: 500, // a hold may grow this far past its reservation
207
+ takeBackGraceMs: 2_000, // then work above lowered limits is stopped
208
+ },
209
+ });
210
+ ```
211
+
212
+ A stopped call has its callback signal aborted, its caller rejected with
213
+ `LLMAdmissionStoppedError` (`cause` is `"overrun"`, `"drain_deadline"`, or
214
+ `"limits"`), and its concurrency slot returned at once. Its tokens stay
215
+ charged until the callback settles, because a local abort does not prove the
216
+ provider stopped. Calls on borrowed capacity are stopped first, then normal
217
+ priority before high, newest first.
218
+
219
+ → [Stopping admitted work](guides/stopping-admitted-work.md)
220
+
184
221
  ## Shutting down cleanly
185
222
 
186
223
  ```ts
@@ -193,6 +230,14 @@ await bulkhead.drain({ timeoutMs: 10_000 }); // wait, but not forever
193
230
  it does not abandon or release that work. One stuck upstream stream therefore
194
231
  cannot hold the shutdown decision open indefinitely.
195
232
 
233
+ Add `stopOnTimeout: true` to stop whatever is still running at the deadline.
234
+ The result then also reports `stopped`, the number of calls it stopped:
235
+
236
+ ```ts
237
+ bulkhead.close();
238
+ const result = await bulkhead.drain({ timeoutMs: 10_000, stopOnTimeout: true });
239
+ ```
240
+
196
241
  Without `timeoutMs`, `drain()` waits for final token settlement as well as base
197
242
  concurrency. In particular, returning a borrowed slot with
198
243
  `abandonBorrowedConcurrency()` does not complete the admission: a manual
@@ -211,6 +256,7 @@ settling the resource accounting for the work behind it.
211
256
  | [Streaming and reconciliation](guides/streaming.md) | `reportUsage()`, progressive holds, ordered usage events |
212
257
  | [Admission classes](guides/admission-classes.md) | Per-class floors and ceilings, shared capacity, borrowing |
213
258
  | [Runtime limits and reconfiguration](guides/runtime-limits.md) | `applyLimits()`, revisions, fail-closed startup, kill switch |
259
+ | [Stopping admitted work](guides/stopping-admitted-work.md) | Opt-in stops on overrun, at a drain deadline, and after a limit change |
214
260
  | [Observe mode](guides/observe-mode.md) | Measure a policy before you enforce it |
215
261
  | [Deduplication](guides/deduplication.md) | In-flight dedup, tenant scoping, streaming results |
216
262
  | [Gateway integration](guides/gateway-integration.md) | `wouldAdmit()`, admission IDs, external leases, control planes |