litellm - ✅(Solved) Fix [Bug]: /user/list performs N+1 key count queries when returning key_count [1 pull requests, 1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
BerriAI/litellm#28221Fetched 2026-05-20 03:40:53
View on GitHub
Comments
0
Participants
1
Timeline
2
Reactions
0
Participants
Timeline (top)
cross-referenced ×1labeled ×1

Fix Action

Fixed

PR fix notes

PR #28222: fix(proxy): avoid N+1 key count queries in /user/list

Description (problem / solution / changelog)

Relevant issues

Fixes #28221

Changes

This PR updates /user/list key count loading to avoid an N+1 query pattern.

Previously, get_user_key_counts() called litellm_verificationtoken.count() once per returned user. With a page size of 50, this added 50 extra database queries.

This change replaces the per-user count loop with a single Prisma group_by query grouped by user_id.

The existing filtering behavior is preserved, including excluding UI session tokens.

Testing

  • Added unit coverage for empty user_ids
  • Added unit coverage for users with no keys returning 0
  • Added unit coverage for grouped counts being mapped to the correct user_id
  • Verified the existing UI session token exclusion filter is preserved
  • Ran uv run pytest tests/test_litellm/proxy/management_endpoints/test_internal_user_endpoints.py -q

Changed files

  • litellm/proxy/management_endpoints/internal_user_endpoints.py (modified, +19/-13)
  • tests/test_litellm/proxy/management_endpoints/test_internal_user_endpoints.py (modified, +50/-0)
RAW_BUFFERClick to expand / collapse

What happened?

The /user/list endpoint populates key_count by calling litellm_verificationtoken.count() once per returned user.

With a page size of 50 users, this adds 50 extra database queries just to populate key_count. This can slow down the Admin UI and increase database load as user and key counts grow.

The existing filtering behavior should be preserved, including excluding UI session tokens.

Expected behavior

/user/list should fetch key counts for all users in the current page with a single grouped query.

A Prisma group_by query grouped by user_id can preserve the existing response shape while avoiding per-user count queries.

Steps to Reproduce

  1. Create multiple internal users.
  2. Create one or more virtual keys for each user.
  3. Call /user/list with a page size greater than 1.
  4. Observe that get_user_key_counts() performs one count query per returned user.

What part of LiteLLM is this about?

Proxy

What LiteLLM version are you on?

main

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

FAQ

Expected behavior

/user/list should fetch key counts for all users in the current page with a single grouped query.

A Prisma group_by query grouped by user_id can preserve the existing response shape while avoiding per-user count queries.

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING