Skip to content

fix(vertex-ai): reuse anthropic messages config instances - #28996

Closed
milan-berri wants to merge 1 commit into
BerriAI:release/v1.83.7-stablefrom
milan-berri:litellm_vertex_anthropic_config_cache_reuse
Closed

fix(vertex-ai): reuse anthropic messages config instances#28996
milan-berri wants to merge 1 commit into
BerriAI:release/v1.83.7-stablefrom
milan-berri:litellm_vertex_anthropic_config_cache_reuse

Conversation

@milan-berri

Copy link
Copy Markdown
Contributor

Summary

  • Cherry-pick of f4dd727b45 onto release/v1.83.7-stable.
  • Cache ProviderConfigManager.get_provider_anthropic_messages_config lookups via @lru_cache so repeated Vertex Claude /v1/messages requests reuse the same VertexAIPartnerModelsAnthropicMessagesConfig instance and preserve credential cache state.
  • Add regression test asserting config instance reuse for Vertex Anthropic messages.

Already merged to main via #26099; this backports the fix to the v1.83.7 stable line.

Test plan

  • Cherry-pick applies cleanly to release/v1.83.7-stable
  • pytest tests/test_litellm/llms/vertex_ai/vertex_ai_partner_models/anthropic/test_vertex_ai_partner_models_anthropic_messages_config.py::test_provider_config_manager_reuses_vertex_anthropic_messages_config_instance

Made with Cursor

Cache provider config lookups for Vertex Anthropic messages so repeated requests reuse the same config object and preserve credential cache state. Add a regression test to catch any future loss of config reuse.

Made-with: Cursor
@greptile-apps

greptile-apps Bot commented May 27, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR backports the lru_cache fix from main (#26099) to release/v1.83.7-stable, making ProviderConfigManager.get_provider_anthropic_messages_config reuse the same config instance across repeated calls so that Vertex AI Claude auth credential state is not discarded on every request.

  • Adds a private _get_provider_anthropic_messages_config_cached static method decorated with @lru_cache, keyed on (model, provider), and makes the existing public method delegate to it.
  • Includes a regression test that asserts identity (is) of the returned config instance across two successive lookups, with proper cache_clear() isolation in a try/finally.

Confidence Score: 4/5

The change is a narrow, well-scoped caching addition that matches existing patterns in the codebase; the main outstanding item is running the new regression test to confirm the fix behaves as expected.

The implementation is straightforward and mirrors patterns already used in the project. The only notable gap is that the PR's own regression test is explicitly marked as not yet run, so there is no confirmed signal that the backport behaves identically to the original main-branch fix. Additionally, the cache is keyed on model even for providers like ANTHROPIC and AZURE_AI where the model string has no effect on the returned instance, which wastes cache capacity but won't cause incorrect behavior.

The _get_provider_anthropic_messages_config_cached method in litellm/utils.py deserves a second look to confirm that all provider branches (especially BEDROCK) are safe to cache with a shared instance.

Important Files Changed

Filename Overview
litellm/utils.py Splits get_provider_anthropic_messages_config into a thin public wrapper and a new @staticmethod @lru_cache-decorated private method, ensuring Vertex (and all other) provider config instances are reused per (model, provider) pair. No breaking changes; the lru_cache key includes model even for providers that don't use it (ANTHROPIC, AZURE_AI), which wastes a few cache slots but is otherwise harmless.
tests/test_litellm/llms/vertex_ai/vertex_ai_partner_models/anthropic/test_vertex_ai_partner_models_anthropic_messages_config.py Adds regression test asserting config-instance identity across repeated lookups; correctly isolates itself with cache_clear() in a try/finally, but the test is shown as not yet run in the PR checklist.

Reviews (1): Last reviewed commit: "fix(vertex-ai): reuse anthropic messages..." | Re-trigger Greptile

Comment on lines +306 to +321
ProviderConfigManager._get_provider_anthropic_messages_config_cached.cache_clear()
try:
first_config = ProviderConfigManager.get_provider_anthropic_messages_config(
model="claude-opus-4-6",
provider=litellm.LlmProviders.VERTEX_AI,
)
second_config = ProviderConfigManager.get_provider_anthropic_messages_config(
model="claude-opus-4-6",
provider=litellm.LlmProviders.VERTEX_AI,
)

assert isinstance(first_config, VertexAIPartnerModelsAnthropicMessagesConfig)
assert isinstance(second_config, VertexAIPartnerModelsAnthropicMessagesConfig)
assert first_config is second_config
finally:
ProviderConfigManager._get_provider_anthropic_messages_config_cached.cache_clear()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Regression test left unchecked in PR description

The PR's test plan checkbox for test_provider_config_manager_reuses_vertex_anthropic_messages_config_instance is explicitly unchecked (- [ ]). Per the project rule requiring evidence of fix resolution, the test should be confirmed passing before merge. Without confirmation, there's no assurance the lru_cache correctly preserves the config instance across multiple invocations in the actual test environment.

Rule Used: What: Ensure that any PR claiming to fix an issue ... (source)

Comment thread litellm/utils.py
Comment on lines 8397 to 8398
if litellm.LlmProviders.ANTHROPIC == provider:
return litellm.AnthropicMessagesConfig()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 AnthropicMessagesConfig cached per model string unnecessarily

For provider=ANTHROPIC, the function returns litellm.AnthropicMessagesConfig() regardless of the model name. Because model is part of the lru_cache key, each distinct model string (e.g. "claude-3-5-sonnet", "claude-opus-4-6") gets its own AnthropicMessagesConfig instance in the cache, slowly filling the 64-entry LRU even though the config is model-agnostic. The same minor concern applies to AzureAnthropicMessagesConfig. This won't break anything but can evict useful Vertex AI entries from the cache under load.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants