litellm - 💡(How to fix) Fix refactor: extract LiteLLM Data Access Layer (DAL) for control & inference nodes [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#25483Fetched 2026-04-11 06:14:02
View on GitHub
Comments
0
Participants
1
Timeline
0
Reactions
0
Participants
RAW_BUFFERClick to expand / collapse

Problem

Raw SQL access to LiteLLM database tables is scattered across 6+ files (auth.py, provisioning.py, invitations.py, budget_sync.py, consumer.py, worldclaw/service.py) with:

  • Duplicated queries (e.g., _find_user_by_sso in both provisioning.py and worldclaw/service.py)
  • Missing documentation on performance/correctness trade-offs (sorted keys for deadlock prevention, best-effort spend, LOWER() email matching, ON CONFLICT idempotency)
  • No clean data access boundary between business logic and LiteLLM table operations

Proposed Solution

  1. Create infer/litellm_integration/litellm_db.py — a single DAL module centralizing all ~18 raw SQL functions for LiteLLM tables, organized by table with WHY-level comments
  2. Refactor 6 source files to delegate SQL to the DAL (provisioning, auth, invitations, consumer, budget_sync, worldclaw)
  3. Document the inference auth boundary in custom_auth.py explaining which LiteLLM internal functions read which tables and their caching strategies
  4. Add DAL unit tests (tests/litellm_integration/test_litellm_db.py)

Design Details

See full plan: docs/plans/litellm-dal-refactoring.md

Key decisions:

  • Single module (not split read/write) — callers do read-then-write in same flow
  • DAL accepts explicit prisma_client — no connection management inside DAL
  • Inference node gets documentation only (must use LiteLLM internals for cache/budget contracts)
  • RawSQLLiteLLMTeamBudgetStore stays in budget_sync.py as protocol adapter, delegates SQL to DAL

Related

  • #243, #249, #250 (current branch context)
  • #266 (decouple provisioning from auth hot path)

extent analysis

TL;DR

Create a single Data Access Layer (DAL) module, litellm_db.py, to centralize raw SQL functions for LiteLLM tables and refactor existing files to delegate SQL operations to this module.

Guidance

  • Identify and consolidate duplicated queries across files (auth.py, provisioning.py, invitations.py, budget_sync.py, consumer.py, worldclaw/service.py) into the proposed litellm_db.py module.
  • Refactor the listed source files to use the new DAL module for SQL operations, ensuring a clean data access boundary between business logic and LiteLLM table operations.
  • Document performance and correctness trade-offs for SQL queries in the DAL module, including considerations for deadlock prevention, spend, and idempotency.
  • Implement unit tests for the DAL module to ensure its correctness and reliability.

Example

No specific code example is provided due to the lack of explicit code snippets in the issue body. However, the process would involve creating a litellm_db.py file with functions like _find_user_by_sso and then updating files like provisioning.py and worldclaw/service.py to import and use these centralized functions.

Notes

The proposed solution aims to improve code organization, reduce duplication, and enhance maintainability by introducing a clear data access layer. However, the success of this approach depends on correctly identifying all relevant SQL queries and ensuring that the new DAL module is properly tested and integrated into the existing codebase.

Recommendation

Apply the proposed workaround by creating the litellm_db.py module and refactoring the existing files to use it, as this approach addresses the identified issues of duplicated queries, missing documentation, and lack of a clean data access boundary.

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…

Still need to ship something?

×6

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

Back to top recommendations

TRENDING