Repository navigation
Count each CombinedLlmArchetype call's cost once - #359
Merged
Merged
Conversation
CombinedLlmArchetype.invoke added response.cost to the active cost managers after its direct call. That direct call goes through GeneralLlm._mockable_direct_call_to_model, which already adds the same cost, so calls through the deprecated model classes (Gpt4o, Gpt4oVision, Perplexity, DeepSeekR1, ...) were counted twice. - Remove the extra addition from invoke. The hard-limit check before the call stays. - The hard-limit tests in test_models_incurring_cost.py mocked the archetype's direct call, which also skips the cost counting inside GeneralLlm. For archetypes they now mock GeneralLlm's litellm call instead (AiModelMockManager.mock_general_llm_litellm_call_with_value), so the cost is counted by the real code path. - Add a unit test that invoke counts one call's cost once. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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
CombinedLlmArchetype.invokeaddedresponse.costto the activeMonetaryCostManagers after its direct call. That direct call goes throughGeneralLlm._mockable_direct_call_to_model, which already adds the same cost, so every call through the deprecated model classes (Gpt4o,Gpt4oVision,Perplexity,DeepSeekR1, ...) was counted twice.This is separate from the litellm-callback double count addressed in #358. The two PRs touch different files.
Changes
invokeno longer adds the cost a second time. Theraise_error_if_limit_would_be_reached()check before the call stays. It is the only pre-call limit check on this path, because litellm catches and logs exceptions raised from a callback'slog_pre_api_call.test_models_incurring_cost.pymock one level lower for archetypes. They mocked the archetype's direct call, which also skips the cost counting inside GeneralLlm. For archetypes they now mock GeneralLlm's litellm call instead, via the newAiModelMockManager.mock_general_llm_litellm_call_with_value. The cost is then counted by the real code path. Other models (ExaSearcher) keep the direct-call mock.test_invoke_counts_call_cost_once. It invokesGpt4owith a mocked litellm call and checks the manager total equals one call's cost. Before this change it was double (0.001 vs 0.0005).Testing
code_tests/unit_tests: the new test passes. The only failures are 14 order-dependent tests that also fail onmainand pass when run alone.test_models_incurring_cost.py(-k error_thrown) pass for all 5 models (15 cases). Removing the line without moving the mocks made the 12 archetype cases fail.🤖 Generated with Claude Code