Skip to content

fix(proxy): honor caller litellm_session_id in spend log session_id - #41004

Closed
deepanshupal wants to merge 4 commits into
BerriAI:mainfrom
deepanshupal:fix/spendlog-session-id
Closed

deepanshupal wants to merge 4 commits into
BerriAI:mainfrom
deepanshupal:fix/spendlog-session-id

Conversation

@deepanshupal

Copy link
Copy Markdown
Contributor

Summary

Fixes #40851. Supersedes #40856 (auto-closed when litellm_internal_staging was merged into main and deleted).

When a caller passes litellm_session_id, the spend log's session_id was still taken from the per-call trace id (standard_logging_payload.trace_id, then litellm_trace_id, then a fresh uuid), so LiteLLM_SpendLogs.session_id never reflected the caller's session and grouping/filtering by session was broken.

The default path of _get_session_id_for_spend_log never consulted kwargs["litellm_session_id"]. Preferring it is safe: when the caller sends no session, get_litellm_params only derives it from session_id/trace_id, so the returned value equals the trace id that won before - sessionless requests are unchanged, and a real caller session now reaches the spend log.

Deliberate behavior preserved: the omit policy still honors only metadata.session_id (the Langfuse key), and the batch lifecycle's deterministic batch-id session still outranks everything so the create row and the poller's cost row stay grouped.

Testing

  • tests/test_litellm/proxy/spend_tracking/test_spend_tracking_utils.py: 227 passed, including 2 new tests (caller session wins over the per-call trace id; batch session still outranks a caller session).

Prepared with AI assistance; testing and review by me.

The default path of _get_session_id_for_spend_log never consulted
kwargs['litellm_session_id'], so a caller-supplied session was
overwritten by the per-call trace id and LiteLLM_SpendLogs.session_id
could never group by litellm_session_id. Prefer it after the batch
lifecycle's deterministic session: when the caller sent no session the
value is only the derived trace-id copy, so sessionless requests are
unchanged.

Fixes BerriAI#40851
@deepanshupal
deepanshupal requested a review from a team September 13, 2026 12:11
@CLAassistant

CLAassistant commented Sep 13, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@greptile-apps

greptile-apps Bot commented Sep 13, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR updates spend-log session resolution so a supplied litellm_session_id takes precedence over request trace identifiers while preserving batch and omit-policy behavior

  • Adds focused coverage for caller-session and batch-session precedence
  • Leaves sessionless and trace-only fallback behavior unchanged
  • Introduces two redundant test docstrings that conflict with repository guidance

Confidence Score: 4/5

The behavior appears safe, but the redundant test docstrings must be removed to satisfy the repository’s explicit source-comment requirement

Session precedence and fallback behavior are adequately covered, with only a repository-guidance violation in the added test docstrings

Files Needing Attention: tests/test_litellm/proxy/spend_tracking/test_spend_tracking_utils.py

Important Files Changed

Filename Overview
litellm/proxy/spend_tracking/spend_tracking_utils.py Adds caller session precedence after omit and batch handling without changing trace fallbacks
tests/test_litellm/proxy/spend_tracking/test_spend_tracking_utils.py Adds meaningful precedence regressions, but includes two unnecessary explanatory docstrings

Reviews (1): Last reviewed commit: "fix(proxy): harden spend log session pre..." | Re-trigger Greptile

@codspeed

codspeed Bot commented Sep 13, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing DeepanshuPal:fix/spendlog-session-id (9e09a5e) with main (30f33a9)1

Open in CodSpeed

Footnotes

  1. No successful run was found on main (9e1ed40) during the generation of this report, so 30f33a9 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

assert session_id == "batch-uid-1"


def test_caller_litellm_session_id_wins_over_the_per_request_trace_id():

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 Redundant test comments These docstrings only repeat clear test names, violating the repository directive against unnecessary comments. Remove both before merging

Context Used: CLAUDE.md (source)

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed both in e1730f3.

@codecov

codecov Bot commented Sep 14, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@deepanshupal

Copy link
Copy Markdown
Contributor Author

#40856 was reopened against the release base, and this PR carries the same head against main. Which base do you want this fix on? I'll keep that one and close the duplicate.

@deepanshupal

Copy link
Copy Markdown
Contributor Author

Two current checks are unrelated to this spend-log diff. OSV reports the existing uv.lock entry for mlflow 3.15.0 (PYSEC-2026-3865 / GHSA-h7x2-h6g9-p789); the old branch's temporary ignore expired September 14, while main extends it. The dashboard type-sync job regenerates schema.d.ts with two soft_budget documentation lines removed. That file is identical on this PR head and its base; this PR changes only spend tracking and a test. I have not changed dependency/security policy or generated UI types on this branch.

@deepanshupal

Copy link
Copy Markdown
Contributor Author

Closing in favour of #44199, which has the same change rebased on main.

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.

[Bug]: LiteLLM_SpendLogs.session_id doesn't reflect litellm_session_id, breaks session grouping

2 participants