LLM gateway: per-tier concurrency cap (phase 1 of #352)#355
Merged
rockfordlhotka merged 1 commit intomainfrom May 7, 2026
Merged
LLM gateway: per-tier concurrency cap (phase 1 of #352)#355rockfordlhotka merged 1 commit intomainfrom
rockfordlhotka merged 1 commit intomainfrom
Conversation
Adds the gateway core: a singleton ILlmGateway with per-tier SemaphoreSlim instances that all LLM calls flow through. Bursty parallel callers cannot overwhelm a tier; pending waiters cancel automatically when their ct fires, which is how user work effectively preempts dream-cycle work without an explicit priority queue. Phase 1 scope per design/llm-gateway.md: - LlmGateway singleton with per-tier semaphores (Low/Balanced/High) - LlmGatewayOptions with configurable concurrency caps (defaults: Low=8, Balanced=4, High=2) - LlmClient.CallTierAsync routes through the gateway, preserving existing tier-fallback and SDK-quirk-retry behavior - New metric: rockbot.llm.gateway.slot_wait.duration - Internal counters (Pending, InFlight) surfaced for diagnostics and tests - 8 unit tests covering cap enforcement, ct propagation, cross-tier independence, slot release on exception, and cancellation-while-waiting cleanup Phases 2-4 (retry-on-429, bounded queue, ct audit) land separately. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This was referenced May 7, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
First of four phases implementing
design/llm-gateway.md. Phase 1 lands the gateway core: a singletonILlmGatewaythat all LLM calls flow through, with per-tierSemaphoreSlimconcurrency caps. This unblocks parallelism work (notably the observation framework, #353) by ensuring bursty parallel callers can't overwhelm a tier.Summary
LlmGateway(singleton) andILlmGatewayinterfaceLlmGatewayOptionswith per-tier concurrency caps; defaults Low=8, Balanced=4, High=2LlmClient.CallTierAsyncnow routes the actual SDK invocation through the gateway, preserving the existing tier-fallback and SDK-quirk-retry behavior outside the gated sectionrockbot.llm.gateway.slot_wait.duration(histogram, ms) tagged by tierGetPendingCount/GetInFlightCountaccessors surfaced for diagnostics and testsLlmGatewayTests.cs) covering cap enforcement, ct propagation, per-tier independence, exception-releases-slot, and cancel-while-waiting cleanupUser-priority semantics emerge from cancellation: when dream's
ctfires, every pending waiter on the gateway throwsOperationCanceledExceptionand the user call'sWaitAsynccompletes promptly. No priority lanes needed.Out of scope (later phases)
Retry-Afterhonor + exponential fallback; disable provider SDK retryMaxPendingPerTier) with fail-fastctmandatory onILlmClientTest plan
dotnet build RockBot.slnx— cleandotnet test tests/RockBot.Host.Tests/RockBot.Host.Tests.csproj --filter "FullyQualifiedName~LlmGatewayTests"— 8/8 passLlmClientctor change)🤖 Generated with Claude Code