Skip to content

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

Merged
mateo-berri merged 19 commits into
BerriAI:mainfrom
deepanshupal:fix/spendlog-session-id-rebased
Oct 16, 2026
Merged

mateo-berri merged 19 commits into
BerriAI:mainfrom
deepanshupal:fix/spendlog-session-id-rebased

Conversation

@deepanshupal

@deepanshupal deepanshupal commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

  • With request_correlation_in_logs: true, spend logs ignore the caller's session id

How it solves it:

  • The spend log session_id uses the caller's litellm_session_id or metadata.session_id on every route, also beside an x-litellm-trace-id header, where a session header wins over the body. Rate limits keep the trace id, as on main
  • A minted session, or one copied from a trace id, yields to the trace id, and a continued bridged turn logs under its chain's first session
  • Clients cannot set the internal caller session key, and a session with a NUL byte keeps the trace id, as on main

Breaking change: bridged /v1/responses history 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 turns

User Flow

Before: with request correlation on, one conversation shows up as several single-request sessions in spend logs

  1. The proxy admin sets litellm_settings.request_correlation_in_logs: true
  2. A client sends POST https://litellm-domain/v1/chat/completions three times with "litellm_session_id": "convo-42", one call with a traceparent header
  3. GET https://litellm-domain/spend/logs?request_id= shows a different session_id on each row, never convo-42

After: the same three calls all land under the caller's session

  1. The proxy admin sets litellm_settings.request_correlation_in_logs: true
  2. A client sends the same three calls
  3. GET https://litellm-domain/spend/logs?request_id= shows session_id: "convo-42" on every row, traceparent or not, and the same for metadata.session_id

Relevant 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

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. 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 more
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • Every automated review on my PR's latest commit has finished, and I have addressed or replied to each of its findings before requesting a maintainer review

Delays 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
model_list:
  - model_name: gpt-mini
    litellm_params:
      model: openai/gpt-5.4-mini
      api_key: os.environ/OPENAI_API_KEY
  - model_name: haiku
    litellm_params:
      model: anthropic/claude-haiku-5-5
      api_key: os.environ/ANTHROPIC_API_KEY

litellm_settings:
  request_correlation_in_logs: true

general_settings:
  master_key: os.environ/LITELLM_MASTER_KEY
  proxy_batch_write_at: 2
  store_prompts_in_spend_logs: true

The generate case runs on a second pair of proxies whose config adds general_settings.missing_session_id: generate

Chat case unless named Before After
Body session random id caller session
Body session, traceparent trace id caller session
metadata.session_id random id caller session
Either session, x-litellm-trace-id trace id caller session
Body session, x-litellm-session-id header session header session
Trace and session headers trace id header session
max_iterations: 2, six calls all 200 all 200
/v1/messages, native /v1/responses caller session caller session
Bridged /v1/responses random id caller session
No session random id random id
Two keys, one session, each follows up own turns own turns
Bridged, trace header per turn or generate remembers remembers
Bridged, shared metadata.trace_id no history no history
Bridged, key regenerated remembers remembers
Bridged, session first sent on turn 2 remembers remembers
Bridged, two chains share metadata.session_id own chain both chains

Before (46913be)

Chat session

Details
  1. Send three chat calls with "litellm_session_id": "base8-convo-42", the second one with a traceparent header
$ curl -s http://localhost:$PORT/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"gpt-mini","messages":[{"role":"user","content":"reply with the word one"}],"litellm_session_id":"base8-convo-42"}' | jq -c '{id, content: .choices[0].message.content}'
{"id":"chatcmpl-EZNnTwKxnfz45VxgBjRATvCnmEjAO","content":"one"}
$ curl -s http://localhost:$PORT/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -H "traceparent: 00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01" -d '{"model":"gpt-mini","messages":[{"role":"user","content":"reply with the word two"}],"litellm_session_id":"base8-convo-42"}' | jq -c '{id, content: .choices[0].message.content}'
{"id":"chatcmpl-EZNnUE0qcDLzzETLmR4PGMJHcHrex","content":"two"}
$ curl -s http://localhost:$PORT/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"gpt-mini","messages":[{"role":"user","content":"reply with the word three"}],"litellm_session_id":"base8-convo-42"}' | jq -c '{id, content: .choices[0].message.content}'
{"id":"chatcmpl-EZNnUbrtwzoyeWXwMIVzz7WUDpESE","content":"three"}
  1. Read the three spend log rows back
$ for id in chatcmpl-EZNnTwKxnfz45VxgBjRATvCnmEjAO chatcmpl-EZNnUE0qcDLzzETLmR4PGMJHcHrex chatcmpl-EZNnUbrtwzoyeWXwMIVzz7WUDpESE; do curl -s -G http://localhost:$PORT/spend/logs -H "Authorization: Bearer $LITELLM_MASTER_KEY" --data-urlencode "request_id=$id" | jq -c '.[0] | {request_id, session_id}'; done
{"request_id":"chatcmpl-EZNnTwKxnfz45VxgBjRATvCnmEjAO","session_id":"4d9a0c1f-2912-4aac-a9bf-93a4b6a533ca"}
{"request_id":"chatcmpl-EZNnUE0qcDLzzETLmR4PGMJHcHrex","session_id":"4bf92f3577b34da6a3ce929d0e0e4736"}
{"request_id":"chatcmpl-EZNnUbrtwzoyeWXwMIVzz7WUDpESE","session_id":"2b888e33-e00f-4618-9637-f9129404206d"}

Every route

Details
  1. Send one call per route with the same session: chat with litellm_session_id, chat with a traceparent header too, chat with metadata.session_id, /v1/messages, native and bridged /v1/responses, and a chat call with no session as the control
$ curl -s http://localhost:$PORT/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"gpt-mini","messages":[{"role":"user","content":"reply with the word one"}],"litellm_session_id":"base8-convo"}' | jq -r .id
chatcmpl-EZNnTglfIWsOKWpt315sYQx4N6dsR
$ curl -s http://localhost:$PORT/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -H "traceparent: 00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01" -d '{"model":"gpt-mini","messages":[{"role":"user","content":"reply with the word two"}],"litellm_session_id":"base8-convo"}' | jq -r .id
chatcmpl-EZNnUGSTlOSH9zbOAyRJ0yugvykaw
$ curl -s http://localhost:$PORT/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"gpt-mini","messages":[{"role":"user","content":"reply with the word three"}],"metadata":{"session_id":"base8-convo"}}' | jq -r .id
chatcmpl-EZNnVqTfMrceG6IhO21UCOFxhPnS7
$ curl -s http://localhost:$PORT/v1/messages -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"haiku","max_tokens":20,"messages":[{"role":"user","content":"reply with the word four"}],"litellm_session_id":"base8-convo"}' | jq -r .id
msg_011Cg4mwwatU1VcXn2mWffLP
$ curl -s http://localhost:$PORT/v1/responses -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"gpt-mini","input":"reply with the word five","litellm_session_id":"base8-convo"}' | jq -r .id
resp_nj6fDlD93xkSo6Vk76nRS_8rUk7fVaHlLYb
$ curl -s http://localhost:$PORT/v1/responses -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"haiku","input":"reply with the word six","litellm_session_id":"base8-convo"}' | jq -r .id
resp_9_EmPOQR0qoaforMBKgwkrfBZZiF773s5bh
$ curl -s http://localhost:$PORT/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"gpt-mini","messages":[{"role":"user","content":"reply with the word seven"}]}' | jq -r .id
chatcmpl-EZNnYmrp4HdyoxzwkyOZfSMYX6Dv5
  1. Open the session the way the logs page does
$ curl -s -G http://localhost:$PORT/spend/logs/session/ui -H "Authorization: Bearer $LITELLM_MASTER_KEY" --data-urlencode "session_id=base8-convo" | jq -c '.data[] | {call_type, model: .model_group}'
{"call_type":"aresponses","model":"gpt-mini"}
{"call_type":"anthropic_messages","model":"haiku"}

Two keys

Details
  1. Key A starts a bridged /v1/responses conversation, then key B starts one with the same session id
  2. Each key follows up on its own response
$ KEY_A=$(curl -s http://localhost:$PORT/key/generate -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"key_alias":"b8tk-a"}' | jq -r .key)
$ KEY_B=$(curl -s http://localhost:$PORT/key/generate -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"key_alias":"b8tk-b"}' | jq -r .key)
$ RA=$(curl -s http://localhost:$PORT/v1/responses -H "Authorization: Bearer $KEY_A" -H "Content-Type: application/json" -d '{"model":"haiku","input":"Remember this: the codename is MAPLE. Reply only OK.","litellm_session_id":"b8tk-shared"}'); jq -r '.output[-1].content[0].text' <<< "$RA"
OK
$ RB=$(curl -s http://localhost:$PORT/v1/responses -H "Authorization: Bearer $KEY_B" -H "Content-Type: application/json" -d '{"model":"haiku","input":"Reply only OK.","litellm_session_id":"b8tk-shared"}'); jq -r '.output[-1].content[0].text' <<< "$RB"
OK
$ FB=$(curl -s http://localhost:$PORT/v1/responses -H "Authorization: Bearer $KEY_B" -H "Content-Type: application/json" -d '{"model":"haiku","previous_response_id":"'"$(jq -r .id <<< "$RB")"'","input":"What is the codename? Reply with just that word, or NONE."}'); jq -r '.output[-1].content[0].text' <<< "$FB"
NONE
$ FA=$(curl -s http://localhost:$PORT/v1/responses -H "Authorization: Bearer $KEY_A" -H "Content-Type: application/json" -d '{"model":"haiku","previous_response_id":"'"$(jq -r .id <<< "$RA")"'","input":"What is the codename? Reply with just that word, or NONE."}'); jq -r '.output[-1].content[0].text' <<< "$FA"
MAPLE

Chains and headers

Details
  1. A: three bridged /v1/responses turns, each with a new x-litellm-trace-id, the last asking for turn 1's codename
  2. C: two conversations send the same metadata.trace_id, and the second follows up
  3. D: two chat calls send "litellm_session_id" plus a new x-litellm-trace-id each, then the session view counts its rows
  4. F: the same with metadata.session_id instead of "litellm_session_id"
  5. G: one chat call sends "litellm_session_id" plus an x-litellm-session-id header, then the session view counts rows for the header session and for the body session
  6. E: a key owned by a user is regenerated between turn 1 and turn 2, and turn 3 asks for both facts
  7. B: three bridged turns with no session under missing_session_id: generate
  8. H: a key with metadata.max_iterations: 2 sends six chat calls with one "litellm_session_id" and a new x-litellm-trace-id each
  9. I: one chat call sends x-litellm-trace-id, x-litellm-session-id and a body "litellm_session_id", then the session view counts rows for each
== A: three bridged turns, a new x-litellm-trace-id per turn
$ R1=$(curl -s http://localhost:$PORT/v1/responses -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -H "x-litellm-trace-id: b8-tr-1" -d '{"model":"haiku","input":"Remember: the codename is MAPLE. Reply only OK."}'); jq -r '.output[-1].content[0].text' <<< "$R1"
OK
$ R2=$(curl -s http://localhost:$PORT/v1/responses -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -H "x-litellm-trace-id: b8-tr-2" -d '{"model":"haiku","previous_response_id":"'"$(jq -r .id <<< "$R1")"'","input":"Also remember: the color is BLUE. Reply only OK."}'); jq -r '.output[-1].content[0].text' <<< "$R2"
OK
$ R3=$(curl -s http://localhost:$PORT/v1/responses -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -H "x-litellm-trace-id: b8-tr-3" -d '{"model":"haiku","previous_response_id":"'"$(jq -r .id <<< "$R2")"'","input":"What is the codename? Reply with just that word, or NONE."}'); jq -r '.output[-1].content[0].text' <<< "$R3"
MAPLE
== C: two conversations send the same metadata.trace_id, B follows up
$ RA=$(curl -s http://localhost:$PORT/v1/responses -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"haiku","input":"Remember: the codename is MAPLE. Reply only OK.","metadata":{"trace_id":"b8-job"}}'); jq -r '.output[-1].content[0].text' <<< "$RA"
OK
$ RB=$(curl -s http://localhost:$PORT/v1/responses -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"haiku","input":"Reply only OK.","metadata":{"trace_id":"b8-job"}}'); jq -r '.output[-1].content[0].text' <<< "$RB"
OK
$ RC=$(curl -s http://localhost:$PORT/v1/responses -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"haiku","previous_response_id":"'"$(jq -r .id <<< "$RB")"'","input":"What is the codename? Reply with just that word, or NONE."}'); jq -r '.output[-1].content[0].text' <<< "$RC"
NONE
== D: chat calls send litellm_session_id plus a new x-litellm-trace-id each
$ curl -s http://localhost:$PORT/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -H "x-litellm-trace-id: b8-req-1" -d '{"model":"gpt-mini","messages":[{"role":"user","content":"reply with the word ok"}],"litellm_session_id":"b8-convo-d"}' | jq -r .id
chatcmpl-EZNlqQDFvMJykiumfIjPVjq10Vbf8
$ curl -s http://localhost:$PORT/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -H "x-litellm-trace-id: b8-req-2" -d '{"model":"gpt-mini","messages":[{"role":"user","content":"reply with the word ok"}],"litellm_session_id":"b8-convo-d"}' | jq -r .id
chatcmpl-EZNlr0CremqvjPboqSPwCSAKO5gOV
$ curl -s -G http://localhost:$PORT/spend/logs/session/ui -H "Authorization: Bearer $LITELLM_MASTER_KEY" --data-urlencode "session_id=b8-convo-d" | jq '.data | length'
0
== F: chat calls send metadata.session_id plus a new x-litellm-trace-id each
$ curl -s http://localhost:$PORT/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -H "x-litellm-trace-id: b8-mreq-1" -d '{"model":"gpt-mini","messages":[{"role":"user","content":"reply with the word ok"}],"metadata":{"session_id":"b8-convo-f"}}' | jq -r .id
chatcmpl-EZNm46mkSq34IaZTM6a5ODiVnDtnc
$ curl -s http://localhost:$PORT/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -H "x-litellm-trace-id: b8-mreq-2" -d '{"model":"gpt-mini","messages":[{"role":"user","content":"reply with the word ok"}],"metadata":{"session_id":"b8-convo-f"}}' | jq -r .id
chatcmpl-EZNm5KUAECf0FMZwSlQPswHbdIa7o
$ curl -s -G http://localhost:$PORT/spend/logs/session/ui -H "Authorization: Bearer $LITELLM_MASTER_KEY" --data-urlencode "session_id=b8-convo-f" | jq '.data | length'
0
== G: a chat call sends litellm_session_id plus an x-litellm-session-id header
$ curl -s http://localhost:$PORT/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -H "x-litellm-session-id: b8-hdr-g" -d '{"model":"gpt-mini","messages":[{"role":"user","content":"reply with the word ok"}],"litellm_session_id":"b8-body-g"}' | jq -r .id
chatcmpl-EZNmIdVJhIvF0m6tHd0TLI47JVYac
$ curl -s -G http://localhost:$PORT/spend/logs/session/ui -H "Authorization: Bearer $LITELLM_MASTER_KEY" --data-urlencode "session_id=b8-hdr-g" | jq '.data | length'
1
$ curl -s -G http://localhost:$PORT/spend/logs/session/ui -H "Authorization: Bearer $LITELLM_MASTER_KEY" --data-urlencode "session_id=b8-body-g" | jq '.data | length'
0
== E: a user's key is regenerated between turn 1 and turn 2
$ KEY_1=$(curl -s http://localhost:$PORT/key/generate -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"user_id":"b8-rot-user"}' | jq -r .key)
$ R1=$(curl -s http://localhost:$PORT/v1/responses -H "Authorization: Bearer $KEY_1" -H "Content-Type: application/json" -d '{"model":"haiku","input":"Remember: the codename is MAPLE. Reply only OK."}'); jq -r '.output[-1].content[0].text' <<< "$R1"
OK
$ KEY_2=$(curl -s -X POST http://localhost:$PORT/key/$KEY_1/regenerate -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -r .key)
$ R2=$(curl -s http://localhost:$PORT/v1/responses -H "Authorization: Bearer $KEY_2" -H "Content-Type: application/json" -d '{"model":"haiku","previous_response_id":"'"$(jq -r .id <<< "$R1")"'","input":"Also remember: the color is BLUE. Reply only OK."}'); jq -r '.output[-1].content[0].text' <<< "$R2"
OK
$ R3=$(curl -s http://localhost:$PORT/v1/responses -H "Authorization: Bearer $KEY_2" -H "Content-Type: application/json" -d '{"model":"haiku","previous_response_id":"'"$(jq -r .id <<< "$R2")"'","input":"What are the codename and the color? Reply with just those two words, or NONE."}'); jq -r '.output[-1].content[0].text' <<< "$R3"
MAPLE BLUE
== B: three bridged turns, no session sent, missing_session_id: generate
$ R1=$(curl -s http://localhost:$PORT/v1/responses -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"haiku","input":"Remember: the codename is MAPLE. Reply only OK."}'); jq -r '.output[-1].content[0].text' <<< "$R1"
OK
$ R2=$(curl -s http://localhost:$PORT/v1/responses -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"haiku","previous_response_id":"'"$(jq -r .id <<< "$R1")"'","input":"Also remember: the color is BLUE. Reply only OK."}'); jq -r '.output[-1].content[0].text' <<< "$R2"
OK
$ R3=$(curl -s http://localhost:$PORT/v1/responses -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"haiku","previous_response_id":"'"$(jq -r .id <<< "$R2")"'","input":"What is the codename? Reply with just that word, or NONE."}'); jq -r '.output[-1].content[0].text' <<< "$R3"
MAPLE
== H: a key with max_iterations 2 sends 6 chat calls with one litellm_session_id and a new x-litellm-trace-id each
$ KEY=$(curl -s http://localhost:$PORT/key/generate -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"metadata":{"max_iterations":2}}' | jq -r .key)
$ for n in 1 2 3 4 5 6; do curl -s -o /dev/null -w '%{http_code} ' http://localhost:$PORT/v1/chat/completions -H "Authorization: Bearer $KEY" -H "Content-Type: application/json" -H "x-litellm-trace-id: b8hi-iter-$n" -d '{"model":"gpt-mini","messages":[{"role":"user","content":"reply with the word ok"}],"litellm_session_id":"b8hi-convo-h"}'; done; echo
200 200 200 200 200 200 
== I: a chat call sends x-litellm-trace-id, x-litellm-session-id and a body litellm_session_id
$ curl -s http://localhost:$PORT/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -H "x-litellm-trace-id: b8hi-trace-i" -H "x-litellm-session-id: b8hi-hdr-i" -d '{"model":"gpt-mini","messages":[{"role":"user","content":"reply with the word ok"}],"litellm_session_id":"b8hi-body-i"}' | jq -r .id
chatcmpl-EZNn0cbFtun2l6bQCvBu6XziDybeO
$ curl -s -G http://localhost:$PORT/spend/logs/session/ui -H "Authorization: Bearer $LITELLM_MASTER_KEY" --data-urlencode "session_id=b8hi-hdr-i" | jq '.data | length'
0
$ curl -s -G http://localhost:$PORT/spend/logs/session/ui -H "Authorization: Bearer $LITELLM_MASTER_KEY" --data-urlencode "session_id=b8hi-trace-i" | jq '.data | length'
1
$ curl -s -G http://localhost:$PORT/spend/logs/session/ui -H "Authorization: Bearer $LITELLM_MASTER_KEY" --data-urlencode "session_id=b8hi-body-i" | jq '.data | length'
0

Late and shared

Details
  1. Merge: two /v1/responses conversations send the same metadata.session_id, then the first one asks for every codename
  2. Late: turn 1 sends no session, turn 2 sends "litellm_session_id", then turn 3 asks for every codename
== merge: two conversations share metadata.session_id ml9b-user
$ K=$(curl -s http://localhost:$PORT/key/generate -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"key_alias":"ml9b-ml"}' | jq -r .key)
$ RA=$(curl -s http://localhost:$PORT/v1/responses -H "Authorization: Bearer $K" -H "Content-Type: application/json" -d '{"model":"haiku","input":"Remember: the codename is MAPLE. Reply only OK.","metadata":{"session_id":"ml9b-user"}}' | jq -r .id)
$ RB=$(curl -s http://localhost:$PORT/v1/responses -H "Authorization: Bearer $K" -H "Content-Type: application/json" -d '{"model":"haiku","input":"Remember: the codename is OAK. Reply only OK.","metadata":{"session_id":"ml9b-user"}}' | jq -r .id)
$ sleep 12
$ curl -s http://localhost:$PORT/v1/responses -H "Authorization: Bearer $K" -H "Content-Type: application/json" -d '{"model":"haiku","previous_response_id":"'"$RA"'","input":"List every codename mentioned earlier in this conversation, comma separated, or NONE."}' | jq -c '{text: .output[-1].content[0].text}'
{"text":"MAPLE"}
== late: turn 1 no session, turn 2 sends litellm_session_id ml9b-late, turn 3 follows up
$ R1=$(curl -s http://localhost:$PORT/v1/responses -H "Authorization: Bearer $K" -H "Content-Type: application/json" -d '{"model":"haiku","input":"Remember: the codename is MAPLE. Reply only OK."}' | jq -r .id)
$ sleep 12
$ R2=$(curl -s http://localhost:$PORT/v1/responses -H "Authorization: Bearer $K" -H "Content-Type: application/json" -d '{"model":"haiku","previous_response_id":"'"$R1"'","input":"Also remember: the codename PURPLE. Reply only OK.","litellm_session_id":"ml9b-late"}' | jq -r .id)
$ sleep 12
$ curl -s http://localhost:$PORT/v1/responses -H "Authorization: Bearer $K" -H "Content-Type: application/json" -d '{"model":"haiku","previous_response_id":"'"$R2"'","input":"List every codename mentioned earlier in this conversation, comma separated, or NONE."}' | jq -c '{text: .output[-1].content[0].text}'
{"text":"MAPLE, PURPLE"}

After (22bd48a)

Chat session

  1. Send three chat calls with "litellm_session_id": "head11-convo-42", the second one with a traceparent header
$ curl -s http://localhost:$PORT/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"gpt-mini","messages":[{"role":"user","content":"reply with the word one"}],"litellm_session_id":"head11-convo-42"}' | jq -c '{id, content: .choices[0].message.content}'
{"id":"chatcmpl-EZQGnsS1UezkhmCPW9bGLn6MNGlmI","content":"one"}
$ curl -s http://localhost:$PORT/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -H "traceparent: 00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01" -d '{"model":"gpt-mini","messages":[{"role":"user","content":"reply with the word two"}],"litellm_session_id":"head11-convo-42"}' | jq -c '{id, content: .choices[0].message.content}'
{"id":"chatcmpl-EZQGoO6uODWGWWODlmACJCBoUU0EV","content":"two"}
$ curl -s http://localhost:$PORT/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"gpt-mini","messages":[{"role":"user","content":"reply with the word three"}],"litellm_session_id":"head11-convo-42"}' | jq -c '{id, content: .choices[0].message.content}'
{"id":"chatcmpl-EZQGqcEPEl4ymNhpmaD8kJz2ZhcI8","content":"three"}
  1. Read the three spend log rows back
$ for id in chatcmpl-EZQGnsS1UezkhmCPW9bGLn6MNGlmI chatcmpl-EZQGoO6uODWGWWODlmACJCBoUU0EV chatcmpl-EZQGqcEPEl4ymNhpmaD8kJz2ZhcI8; do curl -s -G http://localhost:$PORT/spend/logs -H "Authorization: Bearer $LITELLM_MASTER_KEY" --data-urlencode "request_id=$id" | jq -c '.[0] | {request_id, session_id}'; done
{"request_id":"chatcmpl-EZQGnsS1UezkhmCPW9bGLn6MNGlmI","session_id":"head11-convo-42"}
{"request_id":"chatcmpl-EZQGoO6uODWGWWODlmACJCBoUU0EV","session_id":"head11-convo-42"}
{"request_id":"chatcmpl-EZQGqcEPEl4ymNhpmaD8kJz2ZhcI8","session_id":"head11-convo-42"}

Every route

Details
  1. Send one call per route with the same session: chat with litellm_session_id, chat with a traceparent header too, chat with metadata.session_id, /v1/messages, native and bridged /v1/responses, and a chat call with no session as the control
$ curl -s http://localhost:$PORT/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"gpt-mini","messages":[{"role":"user","content":"reply with the word one"}],"litellm_session_id":"head11-convo"}' | jq -r .id
chatcmpl-EZQGmtsAcMnt55lANfqRrBW7RMsgr
$ curl -s http://localhost:$PORT/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -H "traceparent: 00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01" -d '{"model":"gpt-mini","messages":[{"role":"user","content":"reply with the word two"}],"litellm_session_id":"head11-convo"}' | jq -r .id
chatcmpl-EZQGnE6GI7BqwGkC3CuLwpcHEQ84X
$ curl -s http://localhost:$PORT/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"gpt-mini","messages":[{"role":"user","content":"reply with the word three"}],"metadata":{"session_id":"head11-convo"}}' | jq -r .id
chatcmpl-EZQGontP9nNrb3ju1X4zPSvPTcc5h
$ curl -s http://localhost:$PORT/v1/messages -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"haiku","max_tokens":20,"messages":[{"role":"user","content":"reply with the word four"}],"litellm_session_id":"head11-convo"}' | jq -r .id
msg_011Cg4z2jkn3WKbnMkxfbGnq
$ curl -s http://localhost:$PORT/v1/responses -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"gpt-mini","input":"reply with the word five","litellm_session_id":"head11-convo"}' | jq -r .id
resp_t8Lc6tO0Xjw5Vhu3NtwjZ60DCMTRe0xWxH-
$ curl -s http://localhost:$PORT/v1/responses -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"haiku","input":"reply with the word six","litellm_session_id":"head11-convo"}' | jq -r .id
resp_tvBasnGXKupUxHwxgBIvTQW3XQH-N2iTloA
$ curl -s http://localhost:$PORT/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"gpt-mini","messages":[{"role":"user","content":"reply with the word seven"}]}' | jq -r .id
chatcmpl-EZQGsmw4xcVt0meu7yd1z7iPf482W
  1. Open the session the way the logs page does
$ curl -s -G http://localhost:$PORT/spend/logs/session/ui -H "Authorization: Bearer $LITELLM_MASTER_KEY" --data-urlencode "session_id=head11-convo" | jq -c '.data[] | {call_type, model: .model_group}'
{"call_type":"aresponses","model":"haiku"}
{"call_type":"aresponses","model":"gpt-mini"}
{"call_type":"anthropic_messages","model":"haiku"}
{"call_type":"acompletion","model":"gpt-mini"}
{"call_type":"acompletion","model":"gpt-mini"}
{"call_type":"acompletion","model":"gpt-mini"}

Two keys

Details
  1. Key A starts a bridged /v1/responses conversation, then key B starts one with the same session id
  2. Each key follows up on its own response
$ KEY_A=$(curl -s http://localhost:$PORT/key/generate -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"key_alias":"r9k-a"}' | jq -r .key)
$ KEY_B=$(curl -s http://localhost:$PORT/key/generate -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"key_alias":"r9k-b"}' | jq -r .key)
$ RA=$(curl -s http://localhost:$PORT/v1/responses -H "Authorization: Bearer $KEY_A" -H "Content-Type: application/json" -d '{"model":"haiku","input":"Remember this: the codename is MAPLE. Reply only OK.","litellm_session_id":"r9k-shared"}'); jq -r '.output[-1].content[0].text' <<< "$RA"
OK
$ RB=$(curl -s http://localhost:$PORT/v1/responses -H "Authorization: Bearer $KEY_B" -H "Content-Type: application/json" -d '{"model":"haiku","input":"Reply only OK.","litellm_session_id":"r9k-shared"}'); jq -r '.output[-1].content[0].text' <<< "$RB"
OK
$ FB=$(curl -s http://localhost:$PORT/v1/responses -H "Authorization: Bearer $KEY_B" -H "Content-Type: application/json" -d '{"model":"haiku","previous_response_id":"'"$(jq -r .id <<< "$RB")"'","input":"What is the codename? Reply with just that word, or NONE."}'); jq -r '.output[-1].content[0].text' <<< "$FB"
NONE
$ FA=$(curl -s http://localhost:$PORT/v1/responses -H "Authorization: Bearer $KEY_A" -H "Content-Type: application/json" -d '{"model":"haiku","previous_response_id":"'"$(jq -r .id <<< "$RA")"'","input":"What is the codename? Reply with just that word, or NONE."}'); jq -r '.output[-1].content[0].text' <<< "$FA"
MAPLE

Chains and headers

Details
  1. A: three bridged /v1/responses turns, each with a new x-litellm-trace-id, the last asking for turn 1's codename
  2. C: two conversations send the same metadata.trace_id, and the second follows up
  3. D: two chat calls send "litellm_session_id" plus a new x-litellm-trace-id each, then the session view counts its rows
  4. F: the same with metadata.session_id instead of "litellm_session_id"
  5. G: one chat call sends "litellm_session_id" plus an x-litellm-session-id header, then the session view counts rows for the header session and for the body session
  6. E: a key owned by a user is regenerated between turn 1 and turn 2, and turn 3 asks for both facts
  7. B: three bridged turns with no session under missing_session_id: generate
  8. H: a key with metadata.max_iterations: 2 sends six chat calls with one "litellm_session_id" and a new x-litellm-trace-id each
  9. I: one chat call sends x-litellm-trace-id, x-litellm-session-id and a body "litellm_session_id", then the session view counts rows for each
== A: three bridged turns, a new x-litellm-trace-id per turn
$ R1=$(curl -s http://localhost:$PORT/v1/responses -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -H "x-litellm-trace-id: r9h-tr-1" -d '{"model":"haiku","input":"Remember: the codename is MAPLE. Reply only OK."}'); jq -r '.output[-1].content[0].text' <<< "$R1"
OK
$ R2=$(curl -s http://localhost:$PORT/v1/responses -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -H "x-litellm-trace-id: r9h-tr-2" -d '{"model":"haiku","previous_response_id":"'"$(jq -r .id <<< "$R1")"'","input":"Also remember: the color is BLUE. Reply only OK."}'); jq -r '.output[-1].content[0].text' <<< "$R2"
OK
$ R3=$(curl -s http://localhost:$PORT/v1/responses -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -H "x-litellm-trace-id: r9h-tr-3" -d '{"model":"haiku","previous_response_id":"'"$(jq -r .id <<< "$R2")"'","input":"What is the codename? Reply with just that word, or NONE."}'); jq -r '.output[-1].content[0].text' <<< "$R3"
MAPLE
== C: two conversations send the same metadata.trace_id, B follows up
$ RA=$(curl -s http://localhost:$PORT/v1/responses -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"haiku","input":"Remember: the codename is MAPLE. Reply only OK.","metadata":{"trace_id":"r9h-job"}}'); jq -r '.output[-1].content[0].text' <<< "$RA"
OK
$ RB=$(curl -s http://localhost:$PORT/v1/responses -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"haiku","input":"Reply only OK.","metadata":{"trace_id":"r9h-job"}}'); jq -r '.output[-1].content[0].text' <<< "$RB"
OK
$ RC=$(curl -s http://localhost:$PORT/v1/responses -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"haiku","previous_response_id":"'"$(jq -r .id <<< "$RB")"'","input":"What is the codename? Reply with just that word, or NONE."}'); jq -r '.output[-1].content[0].text' <<< "$RC"
NONE
== D: chat calls send litellm_session_id plus a new x-litellm-trace-id each
$ curl -s http://localhost:$PORT/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -H "x-litellm-trace-id: r9h-req-1" -d '{"model":"gpt-mini","messages":[{"role":"user","content":"reply with the word ok"}],"litellm_session_id":"r9h-convo-d"}' | jq -r .id
chatcmpl-EZQEx1WU8XOS8ng8mo3kt1nxr9Qys
$ curl -s http://localhost:$PORT/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -H "x-litellm-trace-id: r9h-req-2" -d '{"model":"gpt-mini","messages":[{"role":"user","content":"reply with the word ok"}],"litellm_session_id":"r9h-convo-d"}' | jq -r .id
chatcmpl-EZQF0t49Yi9SuNOGuMLQNOl24kPaH
$ curl -s -G http://localhost:$PORT/spend/logs/session/ui -H "Authorization: Bearer $LITELLM_MASTER_KEY" --data-urlencode "session_id=r9h-convo-d" | jq '.data | length'
2
== F: chat calls send metadata.session_id plus a new x-litellm-trace-id each
$ curl -s http://localhost:$PORT/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -H "x-litellm-trace-id: r9h-mreq-1" -d '{"model":"gpt-mini","messages":[{"role":"user","content":"reply with the word ok"}],"metadata":{"session_id":"r9h-convo-f"}}' | jq -r .id
chatcmpl-EZQFDydBLA0Bp76t7w72zBymLQpy1
$ curl -s http://localhost:$PORT/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -H "x-litellm-trace-id: r9h-mreq-2" -d '{"model":"gpt-mini","messages":[{"role":"user","content":"reply with the word ok"}],"metadata":{"session_id":"r9h-convo-f"}}' | jq -r .id
chatcmpl-EZQFDXZKHflLFrgcUBWJJaDOsDZdz
$ curl -s -G http://localhost:$PORT/spend/logs/session/ui -H "Authorization: Bearer $LITELLM_MASTER_KEY" --data-urlencode "session_id=r9h-convo-f" | jq '.data | length'
2
== G: a chat call sends litellm_session_id plus an x-litellm-session-id header
$ curl -s http://localhost:$PORT/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -H "x-litellm-session-id: r9h-hdr-g" -d '{"model":"gpt-mini","messages":[{"role":"user","content":"reply with the word ok"}],"litellm_session_id":"r9h-body-g"}' | jq -r .id
chatcmpl-EZQFS0P5teZAbwwM5bDr5YL6so4Nf
$ curl -s -G http://localhost:$PORT/spend/logs/session/ui -H "Authorization: Bearer $LITELLM_MASTER_KEY" --data-urlencode "session_id=r9h-hdr-g" | jq '.data | length'
1
$ curl -s -G http://localhost:$PORT/spend/logs/session/ui -H "Authorization: Bearer $LITELLM_MASTER_KEY" --data-urlencode "session_id=r9h-body-g" | jq '.data | length'
0
== E: a user's key is regenerated between turn 1 and turn 2
$ KEY_1=$(curl -s http://localhost:$PORT/key/generate -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"user_id":"r9h-rot-user"}' | jq -r .key)
$ R1=$(curl -s http://localhost:$PORT/v1/responses -H "Authorization: Bearer $KEY_1" -H "Content-Type: application/json" -d '{"model":"haiku","input":"Remember: the codename is MAPLE. Reply only OK."}'); jq -r '.output[-1].content[0].text' <<< "$R1"
OK
$ KEY_2=$(curl -s -X POST http://localhost:$PORT/key/$KEY_1/regenerate -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -r .key)
$ R2=$(curl -s http://localhost:$PORT/v1/responses -H "Authorization: Bearer $KEY_2" -H "Content-Type: application/json" -d '{"model":"haiku","previous_response_id":"'"$(jq -r .id <<< "$R1")"'","input":"Also remember: the color is BLUE. Reply only OK."}'); jq -r '.output[-1].content[0].text' <<< "$R2"
OK
$ R3=$(curl -s http://localhost:$PORT/v1/responses -H "Authorization: Bearer $KEY_2" -H "Content-Type: application/json" -d '{"model":"haiku","previous_response_id":"'"$(jq -r .id <<< "$R2")"'","input":"What are the codename and the color? Reply with just those two words, or NONE."}'); jq -r '.output[-1].content[0].text' <<< "$R3"
MAPLE BLUE
== B: three bridged turns, no session sent, missing_session_id: generate
$ R1=$(curl -s http://localhost:$PORT/v1/responses -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"haiku","input":"Remember: the codename is MAPLE. Reply only OK."}'); jq -r '.output[-1].content[0].text' <<< "$R1"
OK
$ R2=$(curl -s http://localhost:$PORT/v1/responses -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"haiku","previous_response_id":"'"$(jq -r .id <<< "$R1")"'","input":"Also remember: the color is BLUE. Reply only OK."}'); jq -r '.output[-1].content[0].text' <<< "$R2"
OK
$ R3=$(curl -s http://localhost:$PORT/v1/responses -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"haiku","previous_response_id":"'"$(jq -r .id <<< "$R2")"'","input":"What is the codename? Reply with just that word, or NONE."}'); jq -r '.output[-1].content[0].text' <<< "$R3"
MAPLE
== H: a key with max_iterations 2 sends 6 chat calls with one litellm_session_id and a new x-litellm-trace-id each
$ KEY=$(curl -s http://localhost:$PORT/key/generate -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"metadata":{"max_iterations":2}}' | jq -r .key)
$ for n in 1 2 3 4 5 6; do curl -s -o /dev/null -w '%{http_code} ' http://localhost:$PORT/v1/chat/completions -H "Authorization: Bearer $KEY" -H "Content-Type: application/json" -H "x-litellm-trace-id: r9h-iter-$n" -d '{"model":"gpt-mini","messages":[{"role":"user","content":"reply with the word ok"}],"litellm_session_id":"r9h-convo-h"}'; done; echo
200 200 200 200 200 200 
== I: a chat call sends x-litellm-trace-id, x-litellm-session-id and a body litellm_session_id
$ curl -s http://localhost:$PORT/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -H "x-litellm-trace-id: r9h-trace-i" -H "x-litellm-session-id: r9h-hdr-i" -d '{"model":"gpt-mini","messages":[{"role":"user","content":"reply with the word ok"}],"litellm_session_id":"r9h-body-i"}' | jq -r .id
chatcmpl-EZQGDJg0wVq3jhx0C0ooRV0yulgvj
$ curl -s -G http://localhost:$PORT/spend/logs/session/ui -H "Authorization: Bearer $LITELLM_MASTER_KEY" --data-urlencode "session_id=r9h-hdr-i" | jq '.data | length'
1
$ curl -s -G http://localhost:$PORT/spend/logs/session/ui -H "Authorization: Bearer $LITELLM_MASTER_KEY" --data-urlencode "session_id=r9h-trace-i" | jq '.data | length'
0
$ curl -s -G http://localhost:$PORT/spend/logs/session/ui -H "Authorization: Bearer $LITELLM_MASTER_KEY" --data-urlencode "session_id=r9h-body-i" | jq '.data | length'
0

Late and shared

Details
  1. Merge: two /v1/responses conversations send the same metadata.session_id, then the first one asks for every codename
  2. Late: turn 1 sends no session, turn 2 sends "litellm_session_id", then turn 3 asks for every codename
== merge: two conversations share metadata.session_id ml9h-user
$ K=$(curl -s http://localhost:$PORT/key/generate -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"key_alias":"ml9h-ml"}' | jq -r .key)
$ RA=$(curl -s http://localhost:$PORT/v1/responses -H "Authorization: Bearer $K" -H "Content-Type: application/json" -d '{"model":"haiku","input":"Remember: the codename is MAPLE. Reply only OK.","metadata":{"session_id":"ml9h-user"}}' | jq -r .id)
$ RB=$(curl -s http://localhost:$PORT/v1/responses -H "Authorization: Bearer $K" -H "Content-Type: application/json" -d '{"model":"haiku","input":"Remember: the codename is OAK. Reply only OK.","metadata":{"session_id":"ml9h-user"}}' | jq -r .id)
$ sleep 12
$ curl -s http://localhost:$PORT/v1/responses -H "Authorization: Bearer $K" -H "Content-Type: application/json" -d '{"model":"haiku","previous_response_id":"'"$RA"'","input":"List every codename mentioned earlier in this conversation, comma separated, or NONE."}' | jq -c '{text: .output[-1].content[0].text}'
{"text":"MAPLE, OAK"}
== late: turn 1 no session, turn 2 sends litellm_session_id ml9h-late, turn 3 follows up
$ R1=$(curl -s http://localhost:$PORT/v1/responses -H "Authorization: Bearer $K" -H "Content-Type: application/json" -d '{"model":"haiku","input":"Remember: the codename is MAPLE. Reply only OK."}' | jq -r .id)
$ sleep 12
$ R2=$(curl -s http://localhost:$PORT/v1/responses -H "Authorization: Bearer $K" -H "Content-Type: application/json" -d '{"model":"haiku","previous_response_id":"'"$R1"'","input":"Also remember: the codename PURPLE. Reply only OK.","litellm_session_id":"ml9h-late"}' | jq -r .id)
$ sleep 12
$ curl -s http://localhost:$PORT/v1/responses -H "Authorization: Bearer $K" -H "Content-Type: application/json" -d '{"model":"haiku","previous_response_id":"'"$R2"'","input":"List every codename mentioned earlier in this conversation, comma separated, or NONE."}' | jq -c '{text: .output[-1].content[0].text}'
{"text":"MAPLE, PURPLE"}

Dependency 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

Chat case Before After
Forged litellm_caller_session_id in metadata random id random id
Forged key in litellm_metadata, trace header trace id trace id
Forged key, x-litellm-session-id header session header session
Vertex Gemini, body session, trace header trace id caller session
/v1/messages, body session, trace header trace id caller session
Session with a NUL byte, trace header trace id trace id
3,000-character session, trace header trace id caller session
Two x-litellm-trace-id headers, body session second trace id caller session
Codex session-id header, trace header trace id Codex session
Responses chain, session first sent on turn 2 remembers both turns remembers both turns
Two Responses chains share metadata.session_id own chain both chains

Breaking

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

  • With the flag on, bridged /v1/responses history follows the caller session, as it already does with the flag off. Two chains that share one metadata.session_id answered MAPLE on main and MAPLE, OAK here, the same answer main gives with the flag off, the default. mateo-berri approved shipping this as the reviewer, since it matches the default
  • With the flag on or off, a bridged chain that changes owner replays only the current owner's turns. 2d163e6 and d95df44 ship this on purpose
  • With the flag on, the spend log session_id beside an x-litellm-trace-id header is the caller session instead of the trace id. This is the change the PR is for

Regression risk

  • Callbacks that read the raw request metadata, such as some guardrails and the ClickHouse and Helicone loggers, now also see the internal litellm_caller_session_id key next to session_id. Exercising them needs each vendor's account

Dependency graph

Dependent Status
Spend log session_id verified live
/spend/logs/session/ui verified live
Bridged Responses history verified live
Continued bridged turn metadata verified live
Vertex request labels verified live
OpenAI and Anthropic requests verified live
Langfuse OTel session verified live
max_iterations limiter verified live
OpenAI preview metadata tested indirectly
Straiker guardrail tested indirectly
Auto-router session benchmark untested
ClickHouse spend rows untested
Raw-metadata guardrails untested
Configured pass-through spend rows tested by /audit
Adapter pass-through unreachable

Not verified

  • The adapter pass-through, which answers 500 on main and here after a reload
  • The Conduct, Noma, Alice, Onyx and Gray Swan guardrails, and the ClickHouse and Helicone loggers, for lack of vendor accounts here
  • Sandbox routes

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.py and asserts by litellm_call_id or response id what the caller got, what the upstream received, and what landed in Postgres or the OTLP sink. Rows match the audit inventory

Row Cell Base Head, twice Result Ran at
1, 14 test_spend_log_session_is_the_body_session_beside_a_trace_header[chat-False] red green, green PASS 22bd48a
2, 14 test_spend_log_session_is_the_body_session_beside_a_trace_header[chat-True] red green, green PASS 22bd48a
3, 14 test_spend_log_session_is_the_body_session_beside_a_trace_header[messages-False] red green, green PASS 22bd48a
4, 14 test_spend_log_session_is_the_body_session_beside_a_trace_header[messages-True] red green, green PASS 22bd48a
5, 14 test_spend_log_session_is_the_body_session_beside_a_trace_header[responses-False] red green, green PASS 22bd48a
6, 14 test_spend_log_session_is_the_body_session_beside_a_trace_header[responses-True] red green, green PASS 22bd48a
2, 3, 6 test_sdk_clients_record_the_body_session_beside_a_trace_header red green, green PASS 22bd48a
30 test_conversation_span_keeps_the_trace_header_and_no_internal_key green green, green PASS 082cd28
11 test_flag_off_keeps_the_trace_id_beside_a_body_session[chat] green green, green PASS 22bd48a
11 test_flag_off_keeps_the_trace_id_beside_a_body_session[messages] green green, green PASS 22bd48a
11 test_flag_off_keeps_the_trace_id_beside_a_body_session[responses] green green, green PASS 22bd48a
15 test_response_cache_hit_records_the_body_session_beside_a_trace_header[chat] red green, green PASS 22bd48a
15 test_response_cache_hit_records_the_body_session_beside_a_trace_header[messages] red green, green PASS 22bd48a
15 test_response_cache_hit_records_the_body_session_beside_a_trace_header[responses] red green, green PASS 22bd48a
7, 31 test_metadata_session_beside_a_new_trace_per_turn_groups_both_turns red green, green PASS 22bd48a
8 test_spend_log_session_for_each_session_shape[session-header-wins] green green, green PASS 22bd48a
9 test_spend_log_session_for_each_session_shape[trace-header-only] green green, green PASS 22bd48a
10 test_spend_log_session_for_each_session_shape[body-session-only] red green, green PASS 22bd48a
16 test_spend_log_session_for_each_session_shape[int-session] green green, green PASS 22bd48a
16 test_spend_log_session_for_each_session_shape[list-session] green green, green PASS 22bd48a
16 test_spend_log_session_for_each_session_shape[empty-session] green green, green PASS 22bd48a
20 test_spend_log_session_for_each_session_shape[null-session] green green, green PASS 22bd48a
19 test_spend_log_session_for_each_session_shape[nul-session] green green, green PASS 22bd48a
18 test_spend_log_session_for_each_session_shape[two-trace-headers] red green, green PASS 22bd48a
17 test_spend_log_session_for_each_session_shape[5kb-session] red green, green PASS 22bd48a
12 test_client_sent_internal_session_key_is_ignored[chat] green green, green PASS 22bd48a
13 test_client_sent_internal_session_key_is_ignored[messages] green green, green PASS 22bd48a
21 test_identical_requests_each_log_once_with_the_body_session red green, green PASS 22bd48a
22 test_bridged_chain_history[True-session-every-turn-True] green green, green PASS 22bd48a
23 test_bridged_chain_history[True-session-from-turn-2-True] green green, green PASS 22bd48a
24 test_bridged_chain_history[True-new-session-per-turn-True] green green, green PASS 22bd48a
25 test_bridged_chain_history[False-session-from-turn-2-False] green green, green PASS 22bd48a
35 test_bridged_chains_sharing_a_metadata_session_replay_each_others_turns red green, green FAIL, approved 04e4d9b
26 test_bridged_history_follows_the_previous_responses_owner[other-user-False] red green, green FAIL, fixed: 2d163e6 22bd48a
27 test_bridged_history_follows_the_previous_responses_owner[same-user-True] green green, green PASS 22bd48a
28 test_bridged_history_follows_the_previous_responses_owner[same-team-True] green green, green PASS 22bd48a
29 test_bridged_history_follows_the_previous_responses_owner[ownerless-keys-False] red green, green FAIL, fixed: d95df44 22bd48a
34 test_a_continued_chains_conversation_spans_keep_each_turns_trace_header green green, green PASS 082cd28
32 test_concurrent_burst_across_endpoints_logs_each_session_once red green, green PASS 22bd48a
33 test_killing_one_worker_mid_burst_keeps_logging_the_body_session red green, green PASS 22bd48a
36 test_pass_through_spend_log_session_is_the_body_session_beside_a_trace_header[litellm_session_id] green green, green PASS 6a672ad
36 test_pass_through_spend_log_session_is_the_body_session_beside_a_trace_header[metadata] green green, green PASS 6a672ad

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.py and test_autorouter_session_rollup.py passed 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 target

Type

🐛 Bug Fix

Caveats (if any)

Medium

  • With the flag on, bridged history follows the caller session, as with the flag off, so chains sharing one session replay into each other. Send one session per conversation

Low

  • Scoping tests run the lookup SQL on SQLite, and QA ran it on Postgres
  • Spend logs ignore a langfuse_session_id header that the OTel session honors, as on main
  • With the flag on, auto-router session benchmarks group by the caller session, as intended
  • ClickHouse spend rows still split bridged generate chains per turn, as on main
  • Callbacks reading the raw request metadata also see the internal litellm_caller_session_id key
  • Adapter pass-through routes answer 500 on main and here after a reload, so they went untested

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR
Bugbot summary

[!NOTE]
Medium Risk
Changes spend-log session resolution and bridged Responses history SQL (owner scoping), with intentional breaking behavior when session ids collide across owners or chains.

Overview
With request_correlation_in_logs: true, spend log session_id now prefers the caller’s conversation id (litellm_session_id / metadata.session_id) even when x-litellm-trace-id overwrites the request’s active session/trace fields. The proxy records that id in internal metadata litellm_caller_session_id (clients cannot forge it; it stays out of requester_metadata and upstream payloads). Minted or trace-copied sessions still log under the trace id; continued bridged /v1/responses turns stamp the chain session for spend logging.

Breaking: bridged Responses history loaded from spend logs is scoped to the same owner (user, team, or api key when neither is set), so shared session strings no longer pull another key’s turns. With the flag on, two chains sharing one metadata.session_id can replay each other’s history (aligned with flag-off behavior).

Reviewed by Cursor Bugbot for commit 082cd28. Bugbot is set up for automated code reviews on this repo. Configure here.

@codspeed

codspeed Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

⚠️ 1 benchmark spent significant time in system calls

System calls cannot be consistently instrumented, so they are not included in the measure, which understates the real cost. Please switch to the Walltime instrument to accurately measure system calls.

Measurement and system calls

✅ 32 untouched benchmarks


Comparing deepanshupal:fix/spendlog-session-id-rebased (082cd28) with main (4e2dd65)

Open in CodSpeed

@codecov

codecov Bot commented Oct 2, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.75000% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
litellm/proxy/litellm_pre_call_utils.py 90.90% 2 Missing ⚠️
...itellm_completion_transformation/transformation.py 90.90% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@mateo-berri

Copy link
Copy Markdown
Contributor

@litereview code-review

@mateo-berri

Copy link
Copy Markdown
Contributor

bugbot run

@litereview

litereview Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

✅ code-review

5/5 · Ready for human review

Scope: full · Full PR review.

Completed · Some checks not run.

No correctness findings.

The PR is marked breaking (fix(proxy)!:). Its description explains the one incompatibility: bridged /v1/responses history now replays only rows that belong to the same owner as the referenced response. "Same owner" means the same user and team, or the same key when the row has neither.

Checks and execution details
  • Passed · Real testing evidence (required): Not applicable: no LLM translation changes in the scoped paths. The changed files are litellm/constants.py, litellm/proxy/litellm_pre_call_utils.py, litellm/proxy/spend_tracking/spend_tracking_utils.py, litellm/responses/litellm_completion_transformation/{session_handler,transformation}.py and tests. None of them is under litellm/llms/** or litellm/litellm_core_utils/prompt_templates/**.
  • Passed · Unit tests for touched modules (optional): .venv/bin/python -m pytest -q tests/unit/responses/litellm_completion_transformation/test_session_handler.py tests/unit/proxy/spend_tracking/test_spend_tracking_utils.py tests/unit/proxy/test_litellm_pre_call_utils.py tests/unit/responses/litellm_completion_transformation/test_litellm_completion_responses.py: 1251 passed in 42s. The run includes the new SQLite-backed owner-scoping tests for get_all_spend_logs_for_previous_response_id.
  • Not run · Integration suite tests/integration/spend/test_spend_log_caller_session.py (optional): Not run locally. CircleCI owns the suite through run_integration.sh, which needs an owned Postgres, Redis and a two-worker proxy. I inspected the file: it covers body, metadata and header session shapes, streaming, cache hits, pass-through, bridged chains, owner scoping and client-forged internal keys against a scripted upstream.
  • Passed · Breaking-change marker (optional): The title uses fix(proxy)!:. The description names the incompatibility: a bridged chain continued by another user or team, or by a rotated key with no owner, loses earlier turns. That matches the new EXISTS owner filter in session_handler.py.
  • Passed · Code-path review (optional): I traced the request path. add_litellm_data_to_request calls add_litellm_metadata_from_request_headers, which drops any client-sent litellm_caller_session_id from metadata and litellm_metadata, then stamps the body session only when x-litellm-trace-id won get_chain_id_from_headers. _get_session_id_for_spend_log prefers that stamp, then caller_session_id, and rejects NUL bytes. requester_metadata omits the internal key. Bridged continuations override the stamp with the first session of the chain.

View review · Reviewed 082cd285e804

@cursor cursor Bot left a comment •

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.

Stale Bugbot comment from a previous run.

@mateo-berri

Copy link
Copy Markdown
Contributor

@litereview code-review

@mateo-berri

Copy link
Copy Markdown
Contributor

bugbot run

@cursor cursor Bot left a comment •

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.

Stale Bugbot comment from a previous run.

Comment thread litellm/proxy/litellm_pre_call_utils.py Resolved
@mateo-berri

Copy link
Copy Markdown
Contributor

bugbot run

@mateo-berri

Copy link
Copy Markdown
Contributor

@litereview code-review

@cursor cursor Bot left a comment •

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.

Stale Bugbot comment from a previous run.

# Conflicts:
#	tests/unit/proxy/spend_tracking/test_spend_tracking_utils.py
@mateo-berri

Copy link
Copy Markdown
Contributor

bugbot run

@mateo-berri

Copy link
Copy Markdown
Contributor

@litereview code-review

@cursor cursor Bot left a comment •

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.

Stale Bugbot comment from a previous run.

@mateo-berri

Copy link
Copy Markdown
Contributor

bugbot run

@mateo-berri

Copy link
Copy Markdown
Contributor

@litereview code-review

@mateo-berri

Copy link
Copy Markdown
Contributor

bugbot run

@mateo-berri

Copy link
Copy Markdown
Contributor

@litereview code-review

@cursor cursor Bot left a comment •

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.

Stale Bugbot comment from a previous run.

@mateo-berri

Copy link
Copy Markdown
Contributor

bugbot run

@cursor cursor Bot left a comment •

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.

Stale Bugbot comment from a previous 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}]

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.

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.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 6a672ad. Configure here.

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.

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

@mateo-berri

Copy link
Copy Markdown
Contributor

bugbot run

@cursor cursor Bot left a comment •

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.

Stale Bugbot comment from a previous run.

@mateo-berri

Copy link
Copy Markdown
Contributor

@litereview code-review

@mateo-berri

Copy link
Copy Markdown
Contributor

@deepanshupal this is ready to merge on our side. Could you mark the PR ready for review so we can land it

@mateo-berri

Copy link
Copy Markdown
Contributor

@litellm-stampbot

@litellm-stampbot

Copy link
Copy Markdown

litellm-stampbot

the head branch lives on deepanshupal/litellm, and the stampbot does not run on fork pull requests.

@kerry-berri

Copy link
Copy Markdown
Contributor

@greptileai

@greptile-apps

greptile-apps Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Critical impact] The PR appears safe to merge, with no outstanding findings

Summary

The 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

mateo-berri accepted keeping the in-place metadata removal because the request is an out-param and the surrounding code follows that pattern. The earlier upgrade-notice concern is addressed by the breaking title and description note

  • Spend logs use the caller’s session while trace IDs stay separate.
  • Bridged Responses replay history only from the same owner.

Reviews (3) · Last reviewed commit: "test(spend): type the SQLite fake and sp..." · Reviewed by Greptile

Comment thread litellm/proxy/litellm_pre_call_utils.py Resolved
@mateo-berri mateo-berri changed the title fix(proxy): honor caller litellm_session_id in spend log session_id fix(proxy)!: honor caller litellm_session_id in spend log session_id Oct 16, 2026
@mateo-berri

Copy link
Copy Markdown
Contributor

@litereview code-review

@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

1 similar comment
@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

@mateo-berri

Copy link
Copy Markdown
Contributor

bugbot run

@cursor cursor Bot left a comment

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.

✅ Bugbot reviewed your changes and found no new issues!

1 issue from previous review remains unresolved.

Fix All in Cursor

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 082cd28. Configure here.

@mateo-berri mateo-berri left a comment

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.

LGTM. Thanks for the contribution!

@mateo-berri
mateo-berri merged commit d5ce7db into BerriAI:main Oct 16, 2026
93 checks passed
@mateo-berri
mateo-berri removed the request for review from yucheng-berri October 16, 2026 13:18
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

3 participants