Repository navigation
Enforce MonetaryCostManager hard limits for GeneralLlm and AgentSdkLlm - #360
Open
CodexVeritas wants to merge 1 commit into
Open
CodexVeritas wants to merge 1 commit into
CodexVeritas wants to merge 1 commit into
Conversation
GeneralLlm and AgentSdkLlm relied on LitellmCostTracker's pre-call hook to raise HardLimitExceededError. litellm catches and logs exceptions raised in that hook (and never calls the async variant), so calls went ahead after a limit was exceeded. - GeneralLlm._mockable_direct_call_to_model checks the limit before every call, including exa/ and asknews/ models. - AgentSdkLlm checks the limit in get_response and stream_response. - RetryableModel no longer retries HardLimitExceededError, which previously waited 5-60s per retry before raising the same error. - Remove LitellmCostTracker's pre-call hooks, which could not stop a call, and document how limits are enforced. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
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.
Summary
MonetaryCostManagerhard limits did not stop calls made throughGeneralLlmorAgentSdkLlm. Neither class checked the limit itself. They relied onLitellmCostTracker.log_pre_api_callraisingHardLimitExceededError, but that check never stopped a call:Logging.pre_callwraps each callback'slog_pre_api_callintry/exceptand only logs the exception;async_log_pre_api_callis never called.Once usage passed a limit, calls kept going, and the only signal was a warning once usage passed the limit.
Changes
GeneralLlmchecks the limit before every call._mockable_direct_call_to_modelcallsMonetaryCostManager.raise_error_if_limit_would_be_reached()first, soexa/andasknews/models are covered too.AgentSdkLlmchecks the limit in bothget_responseandstream_response. The streamed path is used byAgentRunner.run_streamed.RetryableModeldoes not retryHardLimitExceededError. Retrying could not succeed and waited 5–60s per attempt before raising the same error.LitellmCostTracker's pre-call hooks are removed, since they could not stop a call. The class docstrings now say how limits are enforced. They also note that litellm calls made outside these classes are tracked but not limited.Semantics are unchanged otherwise. A call is refused once usage is already above the limit, and concurrent calls that start before then still run, as the class docstring describes.
Testing
test_hard_limit_enforcement.py(no network):HardLimitExceededErrorwithout calling litellm or retrying. With the retry exclusion removed, the same test made 3 attempts over 20s.AgentSdkLlm.get_responseandstream_responseraise without callingLitellmModel.mock_response(no network). With usage over the limit,GeneralLlm.invokeraisedHardLimitExceededErrorimmediately and usage did not change. Before this change the same call went through.code_tests/unit_tests: 969 passed. The only failures are 14 order-dependent tests that also fail onmainand pass when run alone.code_tests/integration_tests/test_ai_models/test_models_incurring_cost.py(-k error_thrown): 15 passed.🤖 Generated with Claude Code