From 51eaad657baf790379ed7d047d45c08364f2de40 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Mon, 21 Sep 2026 14:42:23 -0700 Subject: [PATCH] fix(proxy): look the entity spend table up lazily so only the selected batcher table is touched --- litellm/proxy/db/db_spend_update_writer.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/litellm/proxy/db/db_spend_update_writer.py b/litellm/proxy/db/db_spend_update_writer.py index 5381071172f..d9c8b271646 100644 --- a/litellm/proxy/db/db_spend_update_writer.py +++ b/litellm/proxy/db/db_spend_update_writer.py @@ -141,16 +141,18 @@ _EntitySpendTable: TypeAlias = Literal[ ] +_ENTITY_SPEND_TABLES: Final[Mapping[_EntitySpendTable, Callable[[_SpendBatch], BatchTable]]] = MappingProxyType( + { + "litellm_tagtable": lambda batcher: batcher.litellm_tagtable, + "litellm_agentstable": lambda batcher: batcher.litellm_agentstable, + "litellm_modelaccessgroupbudgettable": lambda batcher: batcher.litellm_modelaccessgroupbudgettable, + "litellm_projecttable": lambda batcher: batcher.litellm_projecttable, + } +) + + def _entity_spend_table(batcher: _SpendBatch, table_accessor: _EntitySpendTable) -> BatchTable: - tables: Final[Mapping[_EntitySpendTable, BatchTable]] = MappingProxyType( - { - "litellm_tagtable": batcher.litellm_tagtable, - "litellm_agentstable": batcher.litellm_agentstable, - "litellm_modelaccessgroupbudgettable": batcher.litellm_modelaccessgroupbudgettable, - "litellm_projecttable": batcher.litellm_projecttable, - } - ) - return tables[table_accessor] + return _ENTITY_SPEND_TABLES[table_accessor](batcher) class _SpendBatchManager(Protocol):