Repository navigation
fix(proxy)!: honor caller litellm_session_id in spend log session_id - #44199
Conversation
Added a SQLite-backed database class for testing and a new test to ensure session isolation between different API keys.
Updated docstring to clarify session ID resolution logic for spend log rows.
Merging this PR will not alter performance
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
|
@litereview code-review |
|
bugbot run |
✅ code-review5/5 · Ready for human reviewScope: full · Full PR review. Completed · Some checks not run. No correctness findings. The PR is marked breaking ( Checks and execution details
View review · Reviewed |
|
@litereview code-review |
|
bugbot run |
…ssion headers win
|
bugbot run |
|
@litereview code-review |
# Conflicts: # tests/unit/proxy/spend_tracking/test_spend_tracking_utils.py
…session headers win beside a trace header
|
bugbot run |
|
@litereview code-review |
|
bugbot run |
|
@litereview code-review |
|
bugbot run |
|
@litereview code-review |
|
bugbot run |
| assert response.status_code == 200, response.text | ||
| assert f"answer {marker}" in response.text | ||
| assert len(rig.upstream_bodies(marker)) == 1 | ||
| assert rows == [{"session_id": session}] |
There was a problem hiding this comment.
Pass-through test asserts metadata session
Medium Severity
The new pass-through case expects metadata.session_id to become the spend-log session beside x-litellm-trace-id. Custom pass-through never stamps litellm_caller_session_id, and caller_session_id with empty TraceControls only reads litellm_session_id, so that parameterization lands on the trace id instead.
Reviewed by Cursor Bugbot for commit 6a672ad. Configure here.
There was a problem hiding this comment.
Refuted by a run. Pass-through params carry no litellm_trace_id, so the spend row falls back to metadata.session_id, and this cell passed on base and head
|
bugbot run |
|
@litereview code-review |
|
@deepanshupal this is ready to merge on our side. Could you mark the PR ready for review so we can land it |
|
@litellm-stampbot |
litellm-stampbotthe head branch lives on |
[Critical impact] The PR appears safe to merge, with no outstanding findings SummaryThe PR makes spend logs honor caller sessions when request correlation is enabled. It also limits bridged Responses history to rows with the same owner The latest commit only refactors test helpers and adds types. No new actionable issues were found
Reviews (3) · Last reviewed commit: "test(spend): type the SQLite fake and sp..." · Reviewed by Greptile |
… the caller session tests
|
@litereview code-review |
1 similar comment
|
bugbot run |
There was a problem hiding this comment.
✅ Bugbot reviewed your changes and found no new issues!
1 issue from previous review remains unresolved.
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit 082cd28. Configure here.
mateo-berri
left a comment
There was a problem hiding this comment.
LGTM. Thanks for the contribution!


TLDR
Problem this solves:
request_correlation_in_logs: true, spend logs ignore the caller's session idHow it solves it:
session_iduses the caller'slitellm_session_idormetadata.session_idon every route, also beside anx-litellm-trace-idheader, where a session header wins over the body. Rate limits keep the trace id, as on mainBreaking change: bridged
/v1/responseshistory now replays only the current owner's rows, the same user and team, or the same key when it has neither, with the flag on or off. A chain continued by another user or team, or by a rotated key with no user or team, loses the earlier turnsUser Flow
Before: with request correlation on, one conversation shows up as several single-request sessions in spend logs
litellm_settings.request_correlation_in_logs: true"litellm_session_id": "convo-42", one call with atraceparentheadersession_idon each row, neverconvo-42After: the same three calls all land under the caller's session
litellm_settings.request_correlation_in_logs: truesession_id: "convo-42"on every row,traceparentor not, and the same formetadata.session_idRelevant issues
Fixes #40851
Supersedes #41004, rebased onto current main
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
uv run pytest tests/unit/<your_test_file>.py -v. Leave the suites (make test-unit-*,make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or moreDelays in PR merge?
If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).
Screenshots / Proof of Fix
Last updated: 6a672ad. QA and /live-pr-risk ran on 22bd48a, the last commit with a product diff
Each leg runs a 2-worker proxy on Postgres calling real OpenAI and Anthropic
Config
The
generatecase runs on a second pair of proxies whose config addsgeneral_settings.missing_session_id: generatetraceparentmetadata.session_idx-litellm-trace-idx-litellm-session-idmax_iterations: 2, six calls/v1/messages, native/v1/responses/v1/responsesgeneratemetadata.trace_idmetadata.session_idBefore (46913be)
Chat session
Details
"litellm_session_id": "base8-convo-42", the second one with atraceparentheaderEvery route
Details
litellm_session_id, chat with atraceparentheader too, chat withmetadata.session_id,/v1/messages, native and bridged/v1/responses, and a chat call with no session as the controlTwo keys
Details
/v1/responsesconversation, then key B starts one with the same session idChains and headers
Details
/v1/responsesturns, each with a newx-litellm-trace-id, the last asking for turn 1's codenamemetadata.trace_id, and the second follows up"litellm_session_id"plus a newx-litellm-trace-ideach, then the session view counts its rowsmetadata.session_idinstead of"litellm_session_id""litellm_session_id"plus anx-litellm-session-idheader, then the session view counts rows for the header session and for the body sessionmissing_session_id: generatemetadata.max_iterations: 2sends six chat calls with one"litellm_session_id"and a newx-litellm-trace-ideachx-litellm-trace-id,x-litellm-session-idand a body"litellm_session_id", then the session view counts rows for eachLate and shared
Details
/v1/responsesconversations send the samemetadata.session_id, then the first one asks for every codename"litellm_session_id", then turn 3 asks for every codenameAfter (22bd48a)
Chat session
"litellm_session_id": "head11-convo-42", the second one with atraceparentheaderEvery route
Details
litellm_session_id, chat with atraceparentheader too, chat withmetadata.session_id,/v1/messages, native and bridged/v1/responses, and a chat call with no session as the controlTwo keys
Details
/v1/responsesconversation, then key B starts one with the same session idChains and headers
Details
/v1/responsesturns, each with a newx-litellm-trace-id, the last asking for turn 1's codenamemetadata.trace_id, and the second follows up"litellm_session_id"plus a newx-litellm-trace-ideach, then the session view counts its rowsmetadata.session_idinstead of"litellm_session_id""litellm_session_id"plus anx-litellm-session-idheader, then the session view counts rows for the header session and for the body sessionmissing_session_id: generatemetadata.max_iterations: 2sends six chat calls with one"litellm_session_id"and a newx-litellm-trace-ideachx-litellm-trace-id,x-litellm-session-idand a body"litellm_session_id", then the session view counts rows for eachLate and shared
Details
/v1/responsesconversations send the samemetadata.session_id, then the first one asks for every codename"litellm_session_id", then turn 3 asks for every codenameDependency check, 22bd48a against 46913be
Outbound requests and Langfuse exports match main. Cases, breaking, backward incompatible, regression risk, graph, not verified
Recording proxies sat in front of OpenAI, Anthropic and Vertex, and Langfuse was read back through its API
Both sides ran 2-worker proxies on Postgres with the flag on
litellm_caller_session_idinmetadatalitellm_metadata, trace headerx-litellm-session-id/v1/messages, body session, trace headerx-litellm-trace-idheaders, body sessionsession-idheader, trace headermetadata.session_idBreaking
None found at 22bd48a. At 23e9829 a caller session with a NUL byte made Postgres reject the spend row, so the row was lost while its batch neighbors were written, and c646318 fixed it by keeping the trace id for that session, as on main. At c646318 a bridged chain whose session first appeared on turn 2 forgot turn 1, and 22bd48a fixed it by logging continued turns under the chain's first session
Backward incompatible
/v1/responseshistory follows the caller session, as it already does with the flag off. Two chains that share onemetadata.session_idansweredMAPLEon main andMAPLE, OAKhere, the same answer main gives with the flag off, the default. mateo-berri approved shipping this as the reviewer, since it matches the defaultsession_idbeside anx-litellm-trace-idheader is the caller session instead of the trace id. This is the change the PR is forRegression risk
metadata, such as some guardrails and the ClickHouse and Helicone loggers, now also see the internallitellm_caller_session_idkey next tosession_id. Exercising them needs each vendor's accountDependency graph
session_id/spend/logs/session/uimax_iterationslimiterNot verified
Audit, 22bd48a against 46913be
42 cells on a base and a head rig, the head twice with one seed, plus 65 control cells
Each rig ran its own 2-worker proxy, Postgres, Redis and scripted upstream. Every cell lives in
tests/integration/spend/test_spend_log_caller_session.pyand asserts bylitellm_call_idor response id what the caller got, what the upstream received, and what landed in Postgres or the OTLP sink. Rows match the audit inventorytest_spend_log_session_is_the_body_session_beside_a_trace_header[chat-False]test_spend_log_session_is_the_body_session_beside_a_trace_header[chat-True]test_spend_log_session_is_the_body_session_beside_a_trace_header[messages-False]test_spend_log_session_is_the_body_session_beside_a_trace_header[messages-True]test_spend_log_session_is_the_body_session_beside_a_trace_header[responses-False]test_spend_log_session_is_the_body_session_beside_a_trace_header[responses-True]test_sdk_clients_record_the_body_session_beside_a_trace_headertest_conversation_span_keeps_the_trace_header_and_no_internal_keytest_flag_off_keeps_the_trace_id_beside_a_body_session[chat]test_flag_off_keeps_the_trace_id_beside_a_body_session[messages]test_flag_off_keeps_the_trace_id_beside_a_body_session[responses]test_response_cache_hit_records_the_body_session_beside_a_trace_header[chat]test_response_cache_hit_records_the_body_session_beside_a_trace_header[messages]test_response_cache_hit_records_the_body_session_beside_a_trace_header[responses]test_metadata_session_beside_a_new_trace_per_turn_groups_both_turnstest_spend_log_session_for_each_session_shape[session-header-wins]test_spend_log_session_for_each_session_shape[trace-header-only]test_spend_log_session_for_each_session_shape[body-session-only]test_spend_log_session_for_each_session_shape[int-session]test_spend_log_session_for_each_session_shape[list-session]test_spend_log_session_for_each_session_shape[empty-session]test_spend_log_session_for_each_session_shape[null-session]test_spend_log_session_for_each_session_shape[nul-session]test_spend_log_session_for_each_session_shape[two-trace-headers]test_spend_log_session_for_each_session_shape[5kb-session]test_client_sent_internal_session_key_is_ignored[chat]test_client_sent_internal_session_key_is_ignored[messages]test_identical_requests_each_log_once_with_the_body_sessiontest_bridged_chain_history[True-session-every-turn-True]test_bridged_chain_history[True-session-from-turn-2-True]test_bridged_chain_history[True-new-session-per-turn-True]test_bridged_chain_history[False-session-from-turn-2-False]test_bridged_chains_sharing_a_metadata_session_replay_each_others_turnstest_bridged_history_follows_the_previous_responses_owner[other-user-False]test_bridged_history_follows_the_previous_responses_owner[same-user-True]test_bridged_history_follows_the_previous_responses_owner[same-team-True]test_bridged_history_follows_the_previous_responses_owner[ownerless-keys-False]test_a_continued_chains_conversation_spans_keep_each_turns_trace_headertest_concurrent_burst_across_endpoints_logs_each_session_oncetest_killing_one_worker_mid_burst_keeps_logging_the_body_sessiontest_pass_through_spend_log_session_is_the_body_session_beside_a_trace_header[litellm_session_id]test_pass_through_spend_log_session_is_the_body_session_beside_a_trace_header[metadata]Rows 26 and 29 are the owner scoping fix for bridged history. Row 35 is a vendor payload change with the flag on, and main already answers the same with the flag off, the default. mateo-berri approved shipping it as the reviewer
The 65 control cells from
test_session_total_spend.py,test_spend_log_read_scope.pyandtest_autorouter_session_rollup.pypassed on the head. Row 36 covers configured pass-through routes with a URL target, with the flag on. A route with an adapter target answers 500 on the base and the head once the proxy reloads its routes, so its rows went unverified. The provider routes such as/openai/*and/anthropic/*build their logging params with the same helper as the URL targetType
🐛 Bug Fix
Caveats (if any)
Medium
Low
langfuse_session_idheader that the OTel session honors, as on maingeneratechains per turn, as on mainmetadataalso see the internallitellm_caller_session_idkeyFinal Attestation
Bugbot summary