Skip to content

Conversation

AlexsanderHamir
Copy link
Collaborator

Title

[Fix] Router: optimize provider tracking in _set_model_group_info (O(n*p) → O(n))

Relevant issues

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have Added testing in the tests/litellm/ directory, Adding at least 1 test is a hard requirement - see details
  • I have added a screenshot of my new test passing locally
  • My PR passes all unit tests on make test-unit
  • My PR's scope is as isolated as possible, it only solves 1 specific problem

Type

🧹 Refactoring

Changes

  • Use set for O(1) provider deduplication during iteration
  • Convert set to list after loop to maintain API contract

Performance Gains

  • Reduced CPU usage in 7%

Replace O(n×p) list-based provider deduplication with O(n) set-based
approach in _set_model_group_info().

Previously, the function checked `if llm_provider not in providers`
on each iteration, resulting in O(p) list scans per model.

Changes:
- Track providers in a set during iteration for O(1) deduplication
- Convert set to list after loop to maintain API contract
- Reduces time complexity from O(n×p) to O(n)

Impact: Improves performance for /model_group/info endpoint (which
iterates over all models without caching) and on LRU cache misses for
set_response_headers.
Copy link

vercel bot commented Oct 1, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
litellm Error Error Oct 1, 2025 5:15pm

):
model_group_info.supports_url_context = True

# Track provider in set for O(1) deduplication
Copy link
Collaborator Author

@AlexsanderHamir AlexsanderHamir Oct 1, 2025

Choose a reason for hiding this comment

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

Most changes are just indentation from removal of if statement.

Actual Changes:

  1. Use set for deduplication
  2. Add providers at the end

cast,
)

from fastapi import HTTPException
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

doc quality pointed this was missing.

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.

1 participant