Skip to content

fix(sso): keep CLI SSO and Claude Code gateway team lists in the user record's order - #49033

Merged
mateo-berri merged 15 commits into
BerriAI:mainfrom
zoveki:litellm_cli_sso_team_order
Oct 30, 2026
Merged

mateo-berri merged 15 commits into
BerriAI:mainfrom
zoveki:litellm_cli_sso_team_order

Conversation

@zoveki

@zoveki zoveki commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

For a developer on several teams, the proxy listed their teams in Postgres's row order, which moves on every team write. So lite login's skip, the --pkce preselect, and the gateway's team dropdown (#49001) changed between sign-ins

This PR keeps the user record's order, and user create, group sync, SCIM, and JWT sync add new teams in the order sent

Original

TLDR

Problem this solves:

  • Team lists follow Postgres's row order, which moves on any team write
  • Paths that add teams save them out of the order sent

How it solves it:

  • Resolved teams follow the order on the user record
  • Those paths add teams in the order sent

User Flow

Before: a developer on two teams gets a different default team from one sign-in to the next

  1. The admin adds dev1 to team-alpha and then team-zeta, and GET https://litellm-domain/user/info?user_id=dev1 shows "teams": ["team-alpha", "team-zeta"]
  2. The admin sends POST https://litellm-domain/team/update for team-alpha, or dev1's spend on team-alpha gets recorded
  3. dev1 types /login in Claude Code, and the browser's "Complete CLI Login" page lists team-zeta first in its team dropdown
  4. skip in lite login and Approve on its --pkce page sign dev1 in on team-zeta
  5. Teams added at create (POST https://litellm-domain/user/new) or by the IdP land on /user/info in a random order, and steps 3 and 4 follow it

After: the same developer sees their teams in their user record's order, and every default is the first one

  1. The admin adds dev1 to team-alpha and then team-zeta, and GET https://litellm-domain/user/info?user_id=dev1 shows "teams": ["team-alpha", "team-zeta"]
  2. The admin sends POST https://litellm-domain/team/update for team-alpha, or dev1's spend on team-alpha gets recorded
  3. dev1 types /login in Claude Code, and the browser's "Complete CLI Login" page lists team-alpha first in its team dropdown
  4. skip in lite login and Approve on its --pkce page sign dev1 in on team-alpha
  5. Teams dev1 has keep their place on /user/info, new teams follow in the order sent, and steps 3 and 4 follow it
Decisions
  1. The team order is the user record's, skipping teams whose row is gone, since /user/info shows that order and the gateway docs promise the developer's first team as the default. Sorting by alias then id (fix: order CLI SSO team choices by alias then id for a stable consent default #37666) is stable but ignores the record, so a developer whose record lists zeta first would still land on alpha. An ORDER BY array_position in SQL needs raw SQL, and reordering at each of the four readers of the list is four places to forget
  2. A team listed twice on the user record keeps its first position, and a team whose row is gone is skipped, as before
  3. Group sync adds teams one at a time in claim order, after one lookup that drops claim groups with no LiteLLM team. Concurrent adds each append to the user's teams when they finish, so they land in completion order, and a reordering write after them races other writers and would also move teams the admin placed. The one lookup keeps a claim full of non-team groups from costing one failed add per group on every sign-in. When the lookup fails, every missing group is tried as before. The lookup reads the primary database, so a replica that lags a new team does not drop it. Creating a user with several teams joins them the same way, and a team that fails to join does not stop the rest. A side-by-side run with alternating requests on a busy shared box found one-at-a-time joins no slower (rows). With 30 teams, POST /user/new took a median 2.049 s on this PR and 2.679 s before, and SCIM create took 2.523 s and 3.740 s. Only this PR kept the order sent
  4. The claim is deduped in its own order and teams the user already holds are skipped, the same rule the old set difference applied
  5. The try around the adds is gone, since each add already catches its own errors
  6. Group sync's add and team lookup are injected as keyword-only parameters, so its tests pass recording fakes instead of patching the module. The user create tests patch the team join like the tests around them, since injecting it would break the /user/new test that already patches it
  7. The gateway's dropdown still starts on "Choose a team", as feat(proxy): let a Claude Code gateway user choose the session team #49001 chose, so this PR changes only the order it lists. Preselecting the record's first team there was the alternative, and that call belongs with that page. The teamless first sign-in when group sync adds the teams in that same sign-in (LIT-9413) stays out of scope
  8. There was no base PR to build on. fix: order CLI SSO team choices by alias then id for a stable consent default #37666 sorts by alias, which this replaces, so I'll close it as superseded once this merges
  9. A SCIM POST for an existing email, SCIM PATCH, SCIM PUT, and JWT team sync keep the teams the user already has at their place on the record and add new teams after them in the order the IdP sent, since the first team on the record is the default those sign-ins fall back to. So a POST, PATCH, or PUT that only reorders groups changes nothing, and a team removed and re-added in one PATCH keeps its place. The alternative was letting a full PUT or POST set the order. I rejected it because SCIM gives a multi-valued attribute's order no meaning (RFC 7643 marks a preferred value with primary) and an IdP's group order is not a default the admin chose.
  10. A SCIM PATCH that replaces groups still ignores the group adds and removes after it, as before. RFC 7644 applies them in order, but then a later add of a team that does not exist answers 404 and leaves a user the PATCH deactivates active, where it answered 200. So that change waits on a fix for missing teams

Relevant issues

Fixes #37642 with the user record's order as the stable order (see Decisions)

Affected release

lite login regressed in v1.98.0 (#36826), and the gateway has done this since v1.103.0

Linear ticket

Resolves LIT-9412

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: af22af0 (test types only). QA ran on f11cf98 for A to C, 2ceceae for D to G, 69604c9 for H, 50c4368 for I, and 7c616ad for J. Later commits change group sync's lookup, user create, and SCIM writes. Proof remains valid because A to C add teams with /team/member_add and sign in with no IdP groups, D to G send no SCIM POST with an email, a successful SCIM write saves the same ordered teams, and J re-ran G's PATCH, the one replace step. Verification: unit tests pass, and D to J prove them live

Both legs used Claude Code 2.1.312's TUI, lite login, Keycloak, and curl, on 2 uvicorn workers with Redis and Postgres and real Anthropic spend

Case Before (71babd9) After
A. Claude Code billed team-zeta billed team-alpha
B. lite login skip bound team-zeta skip bound team-alpha
C. --pkce page team-zeta preselected team-alpha preselected
D. Group sync records off claim order records in claim order
E. SCIM PATCH zeta, then kappa billed team-alpha kept its place
E-PUT. SCIM PUT record took PUT order record order unchanged
F. JWT sync boot 5 saved mu before zeta claim order on both boots
G. User create 0 of 13 in order 13 of 13 in order
H. SCIM POST, known email record took POST order record order kept
I. SCIM PATCH, missing team joins in random order joins in PATCH order
J. Deactivate, replace, missing team 200, deactivated 200, deactivated

Case A

After a write to team-alpha's row, dev1's record still lists team-alpha first. Sign-in 2 takes the dropdown's first entry

Setup requests, the same on both legs
$ curl -sS -X POST $P/team/new -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"team_id":"team-zeta"}' | jq -c '{team_id, max_budget, models}'
{"team_id":"team-zeta","max_budget":null,"models":[]}
$ curl -sS -X POST $P/team/new -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"team_id":"team-alpha"}' | jq -c '{team_id, max_budget, models}'
{"team_id":"team-alpha","max_budget":null,"models":[]}
$ curl -sS -X POST $P/user/new -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"user_id":"dev1","user_role":"internal_user","auto_create_key":false}' | jq -c '{user_id, user_role, teams}'
{"user_id":"dev1","user_role":"internal_user","teams":null}
$ curl -sS -X POST $P/team/member_add -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"team_id":"team-alpha","member":{"user_id":"dev1","role":"user"}}' | jq -c '{team_id, members: [.members_with_roles[].user_id]}'
{"team_id":"team-alpha","members":["default_user_id","dev1"]}
$ curl -sS -X POST $P/team/member_add -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"team_id":"team-zeta","member":{"user_id":"dev1","role":"user"}}' | jq -c '{team_id, members: [.members_with_roles[].user_id]}'
{"team_id":"team-zeta","members":["default_user_id","dev1"]}
$ curl -sS "$P/user/info?user_id=dev1" -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '{user_id: .user_id, teams: .user_info.teams}'
{"user_id":"dev1","teams":["team-alpha","team-zeta"]}
$ curl -sS -X POST $P/team/update -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"team_id":"team-alpha","max_budget":100}' | jq -c '{team_id: .data.team_id, max_budget: .data.max_budget}'
{"team_id":"team-alpha","max_budget":100.0}
$ curl -sS "$P/user/info?user_id=dev1" -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '{user_id: .user_id, teams: .user_info.teams}'
{"user_id":"dev1","teams":["team-alpha","team-zeta"]}

Every step is in the before and after gists

Before (71babd9)

options in the order the page lists them:
  1. team-zeta
  2. team-alpha

pr49033-71babd931b-A-signin2-picked.png

pr49033-71babd931b-A-signin2-ok.png

Spend logs
$ curl -sS "$P/spend/logs?user_id=dev1" -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c 'sort_by(.startTime) | .[] | {startTime, user, team_id, model, spend}'
{"startTime":"2026-10-29T11:27:05.630000Z","user":"dev1","team_id":"team-alpha","model":"anthropic/claude-haiku-4-5-20251001","spend":0.000877}
{"startTime":"2026-10-29T11:27:05.966000Z","user":"dev1","team_id":"team-alpha","model":"anthropic/claude-opus-4-7","spend":0.19619875}
{"startTime":"2026-10-29T11:29:17.463000Z","user":"dev1","team_id":"team-zeta","model":"anthropic/claude-opus-4-7","spend":0.017074}

After (f11cf98)

options in the order the page lists them:
  1. team-alpha
  2. team-zeta

pr49033-f11cf98a4e-A-signin2-picked.png

pr49033-f11cf98a4e-A-signin2-ok.png

Spend logs
$ curl -sS "$P/spend/logs?user_id=dev1" -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c 'sort_by(.startTime) | .[] | {startTime, user, team_id, model, spend}'
{"startTime":"2026-10-29T12:22:24.724000Z","user":"dev1","team_id":"team-alpha","model":"anthropic/claude-haiku-4-5-20251001","spend":0.000877}
{"startTime":"2026-10-29T12:22:25.042000Z","user":"dev1","team_id":"team-alpha","model":"anthropic/claude-opus-4-7","spend":0.19619875}
{"startTime":"2026-10-29T12:23:57.606000Z","user":"dev1","team_id":"team-alpha","model":"anthropic/claude-opus-4-7","spend":0.017074}

Case B

Before (71babd9)

The lite login list put team-zeta first, and skip bound and billed team-zeta

$ curl -sS -X POST $P/team/update -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"team_id":"team-alpha","max_budget":200}' | jq -c '{team_id: .data.team_id, max_budget: .data.max_budget}'
{"team_id":"team-alpha","max_budget":200.0}
$ curl -sS "$P/user/info?user_id=dev1" -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '{user_id: .user_id, teams: .user_info.teams}'
{"user_id":"dev1","teams":["team-alpha","team-zeta"]}
$ lite --base-url http://localhost:27954 login 
Opening browser to: http://localhost:27954/sso/key/generate?source=litellm-cli&key=cli-<login id>
Please complete the SSO authentication in your browser...
Verification code: 36ZY-HJ38
Session ID: cli-<login id>
Waiting for authentication...
Still waiting for authentication...
============================================================
Select a team for your CLI session...
          Available Teams          
┏━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━┓
┃ Index ┃ Team Name  ┃ Team ID    ┃
┡━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━━━┩
│ 1     │ team-zeta  │ team-zeta  │
│ 2     │ team-alpha │ team-alpha │
└───────┴────────────┴────────────┘
Select a team by entering the index number (or 'skip' to use first team): 
Select a team by entering the index number (or 'skip' to use first team): skip
Generating JWT for team: team-zeta
Successfully generated JWT for team: team-zeta
Login successful!
JWT Token: litellm_login_<cut>...
No OS keychain available. Credential stored in <qa dir>/qa/r3-before/lite-home/.litellm/token.json (owner-only).
You can now use the CLI without specifying --api-key

pr49033-71babd931b-B-lite-skip.png

$ curl -sS "$P/spend/logs?user_id=dev1" -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c 'sort_by(.startTime) | .[] | {startTime, user, team_id, model, spend}'
{"startTime":"2026-10-29T11:27:05.630000Z","user":"dev1","team_id":"team-alpha","model":"anthropic/claude-haiku-4-5-20251001","spend":0.000877}
{"startTime":"2026-10-29T11:27:05.966000Z","user":"dev1","team_id":"team-alpha","model":"anthropic/claude-opus-4-7","spend":0.19619875}
{"startTime":"2026-10-29T11:29:17.463000Z","user":"dev1","team_id":"team-zeta","model":"anthropic/claude-opus-4-7","spend":0.017074}
{"startTime":"2026-10-29T11:33:09.191000Z","user":"dev1","team_id":"team-zeta","model":"anthropic/claude-haiku-5-5","spend":0.0000052}

Every step

After (f11cf98)

The lite login list followed dev1's record, and skip bound and billed team-alpha

$ curl -sS -X POST $P/team/update -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"team_id":"team-alpha","max_budget":200}' | jq -c '{team_id: .data.team_id, max_budget: .data.max_budget}'
{"team_id":"team-alpha","max_budget":200.0}
$ curl -sS "$P/user/info?user_id=dev1" -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '{user_id: .user_id, teams: .user_info.teams}'
{"user_id":"dev1","teams":["team-alpha","team-zeta"]}
$ lite --base-url http://localhost:24871 login 
Opening browser to: http://localhost:24871/sso/key/generate?source=litellm-cli&key=cli-<login id>
Please complete the SSO authentication in your browser...
Verification code: YRSS-FQTM
Session ID: cli-<login id>
Waiting for authentication...
Still waiting for authentication...
Still waiting for authentication...
============================================================
Select a team for your CLI session...
          Available Teams          
┏━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━┓
┃ Index ┃ Team Name  ┃ Team ID    ┃
┡━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━━━┩
│ 1     │ team-alpha │ team-alpha │
│ 2     │ team-zeta  │ team-zeta  │
└───────┴────────────┴────────────┘
Select a team by entering the index number (or 'skip' to use first team): 
Select a team by entering the index number (or 'skip' to use first team): skip
Generating JWT for team: team-alpha
Successfully generated JWT for team: team-alpha
Login successful!
JWT Token: litellm_login_<cut>...
No OS keychain available. Credential stored in <qa dir>/qa/r3-after/lite-home/.litellm/token.json (owner-only).
You can now use the CLI without specifying --api-key

pr49033-f11cf98a4e-B-lite-skip.png

$ curl -sS "$P/spend/logs?user_id=dev1" -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c 'sort_by(.startTime) | .[] | {startTime, user, team_id, model, spend}'
{"startTime":"2026-10-29T12:22:24.724000Z","user":"dev1","team_id":"team-alpha","model":"anthropic/claude-haiku-4-5-20251001","spend":0.000877}
{"startTime":"2026-10-29T12:22:25.042000Z","user":"dev1","team_id":"team-alpha","model":"anthropic/claude-opus-4-7","spend":0.19619875}
{"startTime":"2026-10-29T12:23:57.606000Z","user":"dev1","team_id":"team-alpha","model":"anthropic/claude-opus-4-7","spend":0.017074}
{"startTime":"2026-10-29T12:27:59.908000Z","user":"dev1","team_id":"team-alpha","model":"anthropic/claude-haiku-5-5","spend":0.0000052}

Every step

Case C

Before (71babd9)

The consent page preselected team-zeta, and Approve with the dropdown untouched bound team-zeta

$ curl -sS -X POST $P/team/update -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"team_id":"team-alpha","max_budget":250}' | jq -c '{team_id: .data.team_id, max_budget: .data.max_budget}'
{"team_id":"team-alpha","max_budget":250.0}
$ lite --base-url http://localhost:27954 login --pkce
Opening browser to: http://localhost:27954/authorize?response_type=code&client_id=<x>&redirect_uri=http%3A%2F%2F127.0.0.1%3A37703%2Fcallback&state=<x>&code_challenge=<x>&code_challenge_method=S256&resource=http%3A%2F%2Flocalhost%3A27954
Approve the sign-in in your browser. Waiting...
idp page: http://localhost:21857/realms/litellm-qa-cc-gateway-team-order/protocol/openid-connect/auth
after IdP: /authorize | h1: Authorize CLI access
team dropdown options in order: ["team-zeta","team-alpha"]
preselected team: team-zeta
page text: Authorize CLI access A command-line client at http://127.0.0.1:37703 wants to call LiteLLM as dev1. Approving issues it a personal credential that expires within 24 hours. lite logout stops it from being renewed. Only approve if you started this sign-in yourself. Attribute requests to team team-zeta team-alpha Deny Approve
after approve: http://127.0.0.1:37703/callback | Signed in to LiteLLM. You can close this window and return to the terminal.

pr49033-71babd931b-C-pkce-consent.png

$ lite --base-url http://localhost:27954 whoami
Authenticated
User Email: unknown
User ID: dev1
User Role: cli
Team ID: team-zeta
Token age: 0.0 hours
Key expires in: 24.0 hours, renewed on next use
[lite exited with 0]

pr49033-71babd931b-C-lite-whoami.png

Every step

After (f11cf98)

The consent page preselected team-alpha, dev1's first team, and Approve bound team-alpha

$ curl -sS -X POST $P/team/update -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"team_id":"team-alpha","max_budget":250}' | jq -c '{team_id: .data.team_id, max_budget: .data.max_budget}'
{"team_id":"team-alpha","max_budget":250.0}
$ lite --base-url http://localhost:24871 login --pkce
Opening browser to: http://localhost:24871/authorize?response_type=code&client_id=<x>&redirect_uri=http%3A%2F%2F127.0.0.1%3A37479%2Fcallback&state=<x>&code_challenge=<x>&code_challenge_method=S256&resource=http%3A%2F%2Flocalhost%3A24871
Approve the sign-in in your browser. Waiting...
idp page: http://localhost:21857/realms/litellm-qa-cc-gateway-team-order/protocol/openid-connect/auth
after IdP: /authorize | h1: Authorize CLI access
team dropdown options in order: ["team-alpha","team-zeta"]
preselected team: team-alpha
page text: Authorize CLI access A command-line client at http://127.0.0.1:37479 wants to call LiteLLM as dev1. Approving issues it a personal credential that expires within 24 hours. lite logout stops it from being renewed. Only approve if you started this sign-in yourself. Attribute requests to team team-alpha team-zeta Deny Approve
after approve: http://127.0.0.1:37479/callback | Signed in to LiteLLM. You can close this window and return to the terminal.

pr49033-f11cf98a4e-C-pkce-consent.png

$ lite --base-url http://localhost:24871 whoami
Authenticated
User Email: unknown
User ID: dev1
User Role: cli
Team ID: team-alpha
Token age: 0.0 hours
Key expires in: 24.0 hours, renewed on next use
[lite exited with 0]

pr49033-f11cf98a4e-C-lite-whoami.png

Every step

Case D

Before (71babd9)

Both users got the same team_order claim, yet group sync saved two different orders, and after the team-zeta write orderdev1's dropdown listed team-mu first and billed it

$ for u in orderdev1 groupdev1; do curl -sS "$P/user/info?user_id=$u" -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '{user_id: .user_id, teams: .user_info.teams}'; done; scripts/kc_userinfo_team_order.sh orderdev1
{"user_id":"orderdev1","teams":["team-zeta","team-alpha","team-mu"]}
{"user_id":"groupdev1","teams":["team-mu","team-zeta","team-alpha"]}
{"preferred_username":"orderdev1","groups":null,"team_order":["team-zeta","grp-vpn","team-mu","team-alpha"]}
label: Attribute requests to team
select shows on load: Choose a team
options in the order the page lists them:
  0. Choose a team (disabled placeholder) (selected)
  1. team-mu
  2. team-alpha
  3. team-zeta
picked team: team-mu (first entry listed)
$ curl -sS "$P/spend/logs?user_id=orderdev1" -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c 'sort_by(.startTime) | .[] | {startTime, user, team_id, model, spend}'
{"startTime":"2026-10-29T17:16:34.178000Z","user":"orderdev1","team_id":"team-mu","model":"anthropic/claude-haiku-4-5-20251001","spend":0.000896}
{"startTime":"2026-10-29T17:16:34.419000Z","user":"orderdev1","team_id":"team-mu","model":"anthropic/claude-opus-4-7","spend":0.19858}

pr49033-71babd931b-r5-Dp-orderdev1-second-picked.png

Every step

After (2ceceae)

Group sync saved both records in claim order without grp-vpn, and after the team-zeta write both dropdowns listed team-zeta first and billed it

$ for u in orderdev1 groupdev1; do curl -sS "$P/user/info?user_id=$u" -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '{user_id: .user_id, teams: .user_info.teams}'; done; scripts/kc_userinfo_team_order.sh orderdev1
{"user_id":"orderdev1","teams":["team-zeta","team-mu","team-alpha"]}
{"user_id":"groupdev1","teams":["team-zeta","team-mu","team-alpha"]}
{"preferred_username":"orderdev1","groups":null,"team_order":["team-zeta","grp-vpn","team-mu","team-alpha"]}
label: Attribute requests to team
select shows on load: Choose a team
options in the order the page lists them:
  0. Choose a team (disabled placeholder) (selected)
  1. team-zeta
  2. team-mu
  3. team-alpha
picked team: team-zeta (first entry listed)
$ curl -sS "$P/spend/logs?user_id=orderdev1" -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c 'sort_by(.startTime) | .[] | {startTime, user, team_id, model, spend}'
{"startTime":"2026-10-29T20:29:30.060000Z","user":"orderdev1","team_id":"team-zeta","model":"anthropic/claude-haiku-4-5-20251001","spend":0.000891}
{"startTime":"2026-10-29T20:29:30.931000Z","user":"orderdev1","team_id":"team-zeta","model":"anthropic/claude-opus-4-7","spend":0.048183}

pr49033-2ceceae7be-Dp-orderdev1-second-picked.png

Every step

Case E

Before (71babd9)

The PATCH put team-kappa first on dev1's record. skip in lite login billed team-zeta, and after that request the next sign-in's dropdown listed team-zeta last and billed team-kappa

$ curl -sS "$P/user/info?user_id=dev1" -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '{user_id: .user_id, teams: .user_info.teams}'
{"user_id":"dev1","teams":["team-alpha","team-zeta"]}
$ curl -sS -X PATCH $P/scim/v2/Users/dev1 -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/scim+json' -d '{"schemas":["urn:ietf:params:scim:api:messages:2.0:PatchOp"],"Operations":[{"op":"remove","path":"groups[value eq \"team-alpha\"]"},{"op":"add","path":"groups","value":[{"value":"team-mu"},{"value":"team-kappa"}]},{"op":"add","path":"groups","value":[{"value":"team-alpha"},{"value":"team-beta"}]}]}' | tee case_E_patch_response.json | jq -c '{id, userName, groups}'
{"id":"dev1","userName":"Unknown Display Name","groups":[{"value":"team-kappa","display":null,"type":"direct"},{"value":"team-alpha","display":null,"type":"direct"},{"value":"team-zeta","display":null,"type":"direct"},{"value":"team-beta","display":null,"type":"direct"},{"value":"team-mu","display":null,"type":"direct"}]}
$ curl -sS "$P/user/info?user_id=dev1" -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '{user_id: .user_id, teams: .user_info.teams}'
{"user_id":"dev1","teams":["team-kappa","team-alpha","team-zeta","team-beta","team-mu"]}
$ lite --base-url http://localhost:27954 login 
Opening browser to: http://localhost:27954/sso/key/generate?source=litellm-cli&key=cli-<login id>
Please complete the SSO authentication in your browser...
Verification code: 2SSY-RBE4
Session ID: cli-<login id>
Waiting for authentication...
Still waiting for authentication...
============================================================
Select a team for your CLI session...
          Available Teams          
┏━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━┓
┃ Index ┃ Team Name  ┃ Team ID    ┃
┡━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━━━┩
│ 1     │ team-zeta  │ team-zeta  │
│ 2     │ team-kappa │ team-kappa │
│ 3     │ team-beta  │ team-beta  │
│ 4     │ team-mu    │ team-mu    │
│ 5     │ team-alpha │ team-alpha │
└───────┴────────────┴────────────┘
Select a team by entering the index number (or 'skip' to use first team): 
label: Attribute requests to team
select shows on load: Choose a team
options in the order the page lists them:
  0. Choose a team (disabled placeholder) (selected)
  1. team-kappa
  2. team-beta
  3. team-mu
  4. team-alpha
  5. team-zeta
picked team: team-kappa (first entry listed)
$ curl -sS "$P/spend/logs?user_id=dev1" -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c 'sort_by(.startTime) | .[] | {startTime, user, team_id, model, spend}'
{"startTime":"2026-10-29T17:06:41.668000Z","user":"dev1","team_id":"team-zeta","model":"anthropic/claude-haiku-5-5","spend":0.0000127}
{"startTime":"2026-10-29T17:08:44.146000Z","user":"dev1","team_id":"team-kappa","model":"anthropic/claude-haiku-4-5-20251001","spend":0.0008709999999999999}
{"startTime":"2026-10-29T17:08:44.372000Z","user":"dev1","team_id":"team-kappa","model":"anthropic/claude-opus-4-7","spend":0.1961925}

pr49033-71babd931b-r5-E-lite-skip.png

pr49033-71babd931b-r5-E-cc-picked.png

Every step

After (2ceceae)

The PATCH kept team-alpha first and added the new teams in the order sent, and lite login and the Claude Code dropdown both followed that order and billed team-alpha

$ curl -sS "$P/user/info?user_id=dev1" -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c "{user_id: .user_id, teams: .user_info.teams}"
{"user_id":"dev1","teams":["team-alpha","team-zeta"]}
$ curl -sS -X PATCH $P/scim/v2/Users/dev1 -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/scim+json' -d '{"schemas":["urn:ietf:params:scim:api:messages:2.0:PatchOp"],"Operations":[{"op":"remove","path":"groups[value eq \"team-alpha\"]"},{"op":"add","path":"groups","value":[{"value":"team-mu"},{"value":"team-kappa"}]},{"op":"add","path":"groups","value":[{"value":"team-alpha"},{"value":"team-beta"}]}]}' | tee case_E_patch_response.json | jq -c '{id, userName, groups}'
{"id":"dev1","userName":"Unknown Display Name","groups":[{"value":"team-alpha","display":null,"type":"direct"},{"value":"team-zeta","display":null,"type":"direct"},{"value":"team-mu","display":null,"type":"direct"},{"value":"team-kappa","display":null,"type":"direct"},{"value":"team-beta","display":null,"type":"direct"}]}
$ curl -sS "$P/user/info?user_id=dev1" -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c "{user_id: .user_id, teams: .user_info.teams}"
{"user_id":"dev1","teams":["team-alpha","team-zeta","team-mu","team-kappa","team-beta"]}
$ lite --base-url http://localhost:24871 login 
Opening browser to: http://localhost:24871/sso/key/generate?source=litellm-cli&key=cli-<login id>
Please complete the SSO authentication in your browser...
Verification code: 49VC-FAFL
Session ID: cli-<login id>
Waiting for authentication...
Still waiting for authentication...
============================================================
Select a team for your CLI session...
          Available Teams          
┏━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━┓
┃ Index ┃ Team Name  ┃ Team ID    ┃
┡━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━━━┩
│ 1     │ team-alpha │ team-alpha │
│ 2     │ team-zeta  │ team-zeta  │
│ 3     │ team-mu    │ team-mu    │
│ 4     │ team-kappa │ team-kappa │
│ 5     │ team-beta  │ team-beta  │
└───────┴────────────┴────────────┘
Select a team by entering the index number (or 'skip' to use first team): 
label: Attribute requests to team
select shows on load: Choose a team
options in the order the page lists them:
  0. Choose a team (disabled placeholder) (selected)
  1. team-alpha
  2. team-zeta
  3. team-mu
  4. team-kappa
  5. team-beta
picked team: team-alpha (first entry listed)
$ curl -sS "$P/spend/logs?user_id=dev1" -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c 'sort_by(.startTime) | .[] | {startTime, user, team_id, model, spend}'
{"startTime":"2026-10-29T20:20:59.719000Z","user":"dev1","team_id":"team-alpha","model":"anthropic/claude-haiku-5-5","spend":0.0000057}
{"startTime":"2026-10-29T20:22:37.445000Z","user":"dev1","team_id":"team-alpha","model":"anthropic/claude-haiku-4-5-20251001","spend":0.0008759999999999999}
{"startTime":"2026-10-29T20:22:38.402000Z","user":"dev1","team_id":"team-alpha","model":"anthropic/claude-opus-4-7","spend":0.1961925}

pr49033-2ceceae7be-E-lite-skip.png

pr49033-2ceceae7be-E-cc-picked.png

Every step

Case E-PUT

Before (71babd9)

A PUT that only reordered the same three teams rewrote the record to the PUT's order, and the dropdown then listed team-alpha first, matching neither order, and billed it

$ curl -sS "$P/user/info?user_id=orderdev2" -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '{user_id: .user_id, teams: .user_info.teams}'
{"user_id":"orderdev2","teams":["team-alpha","team-zeta","team-mu"]}
$ curl -sS -X PUT $P/scim/v2/Users/orderdev2 -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/scim+json' -d '{"schemas":["urn:ietf:params:scim:schemas:core:2.0:User"],"userName":"orderdev2","emails":[{"value":"orderdev2@example.com","primary":true}],"active":true,"groups":[{"value":"team-mu"},{"value":"team-zeta"},{"value":"team-alpha"}]}' | tee case_E-PUT_put_response.json | jq -c '{id, userName, groups}'
{"id":"orderdev2","userName":"orderdev2@example.com","groups":[{"value":"team-mu","display":null,"type":"direct"},{"value":"team-zeta","display":null,"type":"direct"},{"value":"team-alpha","display":null,"type":"direct"}]}
$ curl -sS "$P/user/info?user_id=orderdev2" -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '{user_id: .user_id, teams: .user_info.teams}'
{"user_id":"orderdev2","teams":["team-mu","team-zeta","team-alpha"]}
label: Attribute requests to team
select shows on load: Choose a team
options in the order the page lists them:
  0. Choose a team (disabled placeholder) (selected)
  1. team-alpha
  2. team-mu
  3. team-zeta
picked team: team-alpha (first entry listed)
$ curl -sS "$P/spend/logs?user_id=orderdev2" -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c 'sort_by(.startTime) | .[] | {startTime, user, team_id, model, spend}'
{"startTime":"2026-10-29T17:11:10.097000Z","user":"orderdev2","team_id":"team-alpha","model":"anthropic/claude-haiku-4-5-20251001","spend":0.000883}
{"startTime":"2026-10-29T17:11:10.498000Z","user":"orderdev2","team_id":"team-alpha","model":"anthropic/claude-opus-4-7","spend":0.04658925}

pr49033-71babd931b-r5-E-PUT-cc-picked.png

Every step

After (2ceceae)

A PUT that only reordered the same three teams left the record at team-alpha, team-zeta, team-mu, and the dropdown listed that order and billed team-alpha

$ curl -sS "$P/user/info?user_id=orderdev2" -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '{user_id: .user_id, teams: .user_info.teams}'
{"user_id":"orderdev2","teams":["team-alpha","team-zeta","team-mu"]}
$ curl -sS -X PUT $P/scim/v2/Users/orderdev2 -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/scim+json' -d '{"schemas":["urn:ietf:params:scim:schemas:core:2.0:User"],"userName":"orderdev2","emails":[{"value":"orderdev2@example.com","primary":true}],"active":true,"groups":[{"value":"team-mu"},{"value":"team-zeta"},{"value":"team-alpha"}]}' | tee case_E-PUT_put_response.json | jq -c '{id, userName, groups}'
{"id":"orderdev2","userName":"orderdev2@example.com","groups":[{"value":"team-alpha","display":null,"type":"direct"},{"value":"team-zeta","display":null,"type":"direct"},{"value":"team-mu","display":null,"type":"direct"}]}
$ curl -sS "$P/user/info?user_id=orderdev2" -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '{user_id: .user_id, teams: .user_info.teams}'
{"user_id":"orderdev2","teams":["team-alpha","team-zeta","team-mu"]}
label: Attribute requests to team
select shows on load: Choose a team
options in the order the page lists them:
  0. Choose a team (disabled placeholder) (selected)
  1. team-alpha
  2. team-zeta
  3. team-mu
picked team: team-alpha (first entry listed)
$ curl -sS "$P/spend/logs?user_id=orderdev2" -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c 'sort_by(.startTime) | .[] | {startTime, user, team_id, model, spend}'
{"startTime":"2026-10-29T20:24:52.056000Z","user":"orderdev2","team_id":"team-alpha","model":"anthropic/claude-haiku-4-5-20251001","spend":0.000883}
{"startTime":"2026-10-29T20:24:52.472000Z","user":"orderdev2","team_id":"team-alpha","model":"anthropic/claude-opus-4-7","spend":0.04658925}

pr49033-2ceceae7be-E-PUT-cc-picked.png

Every step

Case F

Before (71babd9)

Boots 1 to 4 saved team-alpha, team-zeta, team-mu by chance, but boot 5 saved team-mu before team-zeta from the same claim, and its dropdown listed team-mu first and billed it

$ curl -sS "$P/user/info?user_id=orderdev2" -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c "{user_id: .user_id, teams: .user_info.teams}"; scripts/kc_userinfo_team_order.sh orderdev2
{"user_id":"orderdev2","teams":["team-alpha","team-mu","team-zeta"]}
{"preferred_username":"orderdev2","groups":null,"team_order":["team-zeta","grp-vpn","team-mu","team-alpha"]}
label: Attribute requests to team
select shows on load: Choose a team
options in the order the page lists them:
  0. Choose a team (disabled placeholder) (selected)
  1. team-mu
  2. team-zeta
  3. team-alpha
picked team: team-mu (first entry listed)

pr49033-71babd931b-r5-Fp-boot5-cc-picked.png

Every step

After (2ceceae)

On boots 3 and 3b the JWT request kept team-alpha first, added team-zeta and team-mu in claim order, and dropped team-kappa, and the dropdown billed team-alpha

$ curl -sS "$P/user/info?user_id=orderdev2" -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '{user_id: .user_id, teams: .user_info.teams}'
{"user_id":"orderdev2","teams":["team-alpha","team-kappa"]}
$ curl -sS "$P/user/info?user_id=orderdev2" -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '{user_id: .user_id, teams: .user_info.teams}'
{"user_id":"orderdev2","teams":["team-alpha","team-zeta","team-mu"]}
label: Attribute requests to team
select shows on load: Choose a team
options in the order the page lists them:
  0. Choose a team (disabled placeholder) (selected)
  1. team-alpha
  2. team-zeta
  3. team-mu
picked team: team-alpha (first entry listed)

pr49033-2ceceae7be-Fp-boot3-cc-picked.png

Every step

Case G

Before (71babd9)

Every user created on five teams saved them out of the order sent. Case J re-ran the PATCH step

user teams on the record time_total (s)
newdev1 zeta, kappa, mu, alpha, beta 0.564
newdev2 zeta, mu, beta, kappa, alpha 0.393
newdev3 mu, zeta, beta, kappa, alpha 0.604
newdev4 beta, zeta, kappa, mu, alpha 0.421
newdev5 kappa, alpha, mu, zeta, beta 0.393
scimdev1 zeta, kappa, mu, alpha, beta 1.712
scimdev2 mu, zeta, kappa, beta, alpha 1.220
scimdev3 beta, zeta, mu, kappa, alpha 0.775
newdev6 to newdev10 none in order 0.737 to 0.919

Every step

After (2ceceae)

All 13 users saved their five teams in the order sent. Case J re-ran the PATCH step

user teams on the record time_total (s)
newdev1 to newdev5 zeta, mu, kappa, alpha, beta 0.433 to 0.535
scimdev1 to scimdev3 zeta, mu, kappa, alpha, beta 0.484 to 0.716
newdev6 to newdev10 zeta, mu, kappa, alpha, beta 0.434 to 0.752

Every step

Case H

Before (71babd9)

A SCIM POST for orderdev1's email kept its id but rewrote the record to the POST's order, although team-zeta and team-kappa were there first

$ curl -sS "$P/user/info?user_id=orderdev1" -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '{user_id: .user_id, teams: .user_info.teams}'
{"user_id":"orderdev1","teams":["team-zeta","team-alpha","team-kappa"]}
$ curl -sS -w '\nHTTP %{http_code}\n' -X POST $P/scim/v2/Users -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/scim+json' -d '{"schemas":["urn:ietf:params:scim:schemas:core:2.0:User"],"userName":"orderdev1@example.com","emails":[{"value":"orderdev1@example.com","primary":true}],"active":true,"groups":[{"value":"team-mu"},{"value":"team-kappa"},{"value":"team-beta"},{"value":"team-zeta"}]}' > case_H_post_raw.txt; head -1 case_H_post_raw.txt | jq -c '{id, userName, groups}'; tail -1 case_H_post_raw.txt
{"id":"orderdev1","userName":"orderdev1@example.com","groups":[{"value":"team-mu","display":null,"type":"direct"},{"value":"team-kappa","display":null,"type":"direct"},{"value":"team-beta","display":null,"type":"direct"},{"value":"team-zeta","display":null,"type":"direct"}]}
HTTP 201
$ curl -sS "$P/user/info?user_id=orderdev1" -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '{user_id: .user_id, teams: .user_info.teams}'
{"user_id":"orderdev1","teams":["team-mu","team-kappa","team-beta","team-zeta"]}
options in the order the page lists them:
  0. Choose a team (disabled placeholder) (selected)
  1. team-zeta
  2. team-kappa
  3. team-beta
  4. team-mu
picked team: team-zeta (first entry listed)
$ curl -sS "$P/spend/logs?user_id=orderdev1" -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c 'sort_by(.startTime) | .[] | {startTime, user, team_id, model, spend}'
{"startTime":"2026-10-29T22:59:27.928000Z","user":"orderdev1","team_id":"team-zeta","model":"anthropic/claude-haiku-4-5-20251001","spend":0.0008759999999999999}
{"startTime":"2026-10-29T22:59:28.450000Z","user":"orderdev1","team_id":"team-zeta","model":"anthropic/claude-opus-4-7","spend":0.1961925}

The dropdown follows Postgres's row order here, as in case A, so its first entry matches the fixed order by chance

pr49033-71babd931b-r6-H-cc-picked-before.png

Every step

After (69604c9)

The same POST kept team-zeta and team-kappa in their place, dropped team-alpha, and added team-mu and team-beta after them in the order sent, and the dropdown listed the record's order

$ curl -sS "$P/user/info?user_id=orderdev1" -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '{user_id: .user_id, teams: .user_info.teams}'
{"user_id":"orderdev1","teams":["team-zeta","team-alpha","team-kappa"]}
$ curl -sS -o case_H_post_response.json -w 'HTTP %{http_code}\n' -X POST $P/scim/v2/Users -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/scim+json' -d '{"schemas":["urn:ietf:params:scim:schemas:core:2.0:User"],"userName":"orderdev1@example.com","emails":[{"value":"orderdev1@example.com","primary":true}],"active":true,"groups":[{"value":"team-mu"},{"value":"team-kappa"},{"value":"team-beta"},{"value":"team-zeta"}]}'; jq -c '{id, userName, groups}' case_H_post_response.json
HTTP 201
{"id":"orderdev1","userName":"orderdev1@example.com","groups":[{"value":"team-zeta","display":null,"type":"direct"},{"value":"team-kappa","display":null,"type":"direct"},{"value":"team-mu","display":null,"type":"direct"},{"value":"team-beta","display":null,"type":"direct"}]}
$ curl -sS "$P/user/info?user_id=orderdev1" -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '{user_id: .user_id, teams: .user_info.teams}'
{"user_id":"orderdev1","teams":["team-zeta","team-kappa","team-mu","team-beta"]}
options in the order the page lists them:
  0. Choose a team (disabled placeholder) (selected)
  1. team-zeta
  2. team-kappa
  3. team-mu
  4. team-beta
picked team: team-zeta (first entry listed)
$ curl -sS "$P/spend/logs?user_id=orderdev1" -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c 'sort_by(.startTime) | .[] | {startTime, user, team_id, model, spend}'
{"startTime":"2026-10-29T23:39:27.979000Z","user":"orderdev1","team_id":"team-zeta","model":"anthropic/claude-haiku-4-5-20251001","spend":0.0008709999999999999}
{"startTime":"2026-10-29T23:39:28.067000Z","user":"orderdev1","team_id":"team-zeta","model":"anthropic/claude-opus-4-7","spend":0.19612375}

pr49033-69604c98f1-r6-H-cc-picked.png

Every step

Case I

Before (71babd9)

The PATCH answered 404 for team-nu and left the seven teams it joined in a random order. The retry after team-nu was created reordered the whole record and moved team-zeta last

$ curl -sS "$P/user/info?user_id=orderdev1" -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '{user_id: .user_id, teams: .user_info.teams}'
{"user_id":"orderdev1","teams":["team-zeta"]}
$ curl -sS -w '\nHTTP %{http_code}\n' -X PATCH $P/scim/v2/Users/orderdev1 -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/scim+json' -d '{"schemas":["urn:ietf:params:scim:api:messages:2.0:PatchOp"],"Operations":[{"op":"add","path":"groups","value":[{"value":"team-alpha"},{"value":"team-kappa"},{"value":"team-nu"},{"value":"team-mu"},{"value":"team-beta"},{"value":"team-omega"},{"value":"team-delta"},{"value":"team-sigma"}]}]}' | tee case_I_patch1_raw.txt
{"error":{"message":"SCIM roster sync failed on 1 of 8 team membership writes, leaving the roster partially updated. Retry the push to reconcile it. Failed writes: add orderdev1 to team-nu: 404: {'error': \"Team doesn't exist in db. Team=team-nu. Create team via `/team/new` call.\"}","type":"internal_server_error","param":null,"code":"404"}}
HTTP 404
$ curl -sS "$P/user/info?user_id=orderdev1" -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '{user_id: .user_id, teams: .user_info.teams}'
{"user_id":"orderdev1","teams":["team-zeta","team-kappa","team-delta","team-alpha","team-beta","team-omega","team-sigma","team-mu"]}
$ curl -sS -w '\nHTTP %{http_code}\n' -X PATCH $P/scim/v2/Users/orderdev1 -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/scim+json' -d '{"schemas":["urn:ietf:params:scim:api:messages:2.0:PatchOp"],"Operations":[{"op":"add","path":"groups","value":[{"value":"team-alpha"},{"value":"team-kappa"},{"value":"team-nu"},{"value":"team-mu"},{"value":"team-beta"},{"value":"team-omega"},{"value":"team-delta"},{"value":"team-sigma"}]}]}' > case_I_patch2_raw.txt; tail -1 case_I_patch2_raw.txt; sed '$d' case_I_patch2_raw.txt | sed '/^$/d' > case_I_patch2_response.json; jq -c '{id, userName, groups: [.groups[].value]}' case_I_patch2_response.json
HTTP 200
{"id":"orderdev1","userName":"orderdev1@example.com","groups":["team-beta","team-sigma","team-alpha","team-omega","team-mu","team-delta","team-kappa","team-nu","team-zeta"]}
$ curl -sS "$P/user/info?user_id=orderdev1" -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '{user_id: .user_id, teams: .user_info.teams}'
{"user_id":"orderdev1","teams":["team-beta","team-sigma","team-alpha","team-omega","team-mu","team-delta","team-kappa","team-nu","team-zeta"]}
options in the order the page lists them:
  0. Choose a team (disabled placeholder) (selected)
  1. team-zeta
  2. team-kappa
  3. team-delta
  4. team-alpha
  5. team-beta
  6. team-omega
  7. team-sigma
  8. team-mu
  9. team-nu
picked team: team-zeta (first entry listed)
$ curl -sS "$P/spend/logs?user_id=orderdev1" -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c 'sort_by(.startTime) | .[] | {startTime, user, team_id, model, spend}'
{"startTime":"2026-10-30T01:11:46.054000Z","user":"orderdev1","team_id":"team-zeta","model":"anthropic/claude-haiku-4-5-20251001","spend":0.0008709999999999999}
{"startTime":"2026-10-30T01:11:46.445000Z","user":"orderdev1","team_id":"team-zeta","model":"anthropic/claude-opus-4-7","spend":0.1961925}

The dropdown follows Postgres's row order here, as in case A, so it matches neither the record nor the PATCH

pr49033-71babd931b-r7-I-cc-picked-before.png

Every step

After (50c4368)

The same PATCH answered 404 for team-nu and left the seven teams it joined in the order sent. The retry kept that order and appended team-nu, and the dropdown listed the record's order

$ curl -sS "$P/user/info?user_id=orderdev1" -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '{user_id: .user_id, teams: .user_info.teams}'
{"user_id":"orderdev1","teams":["team-zeta"]}
$ curl -sS -w '\nHTTP %{http_code}\n' -X PATCH $P/scim/v2/Users/orderdev1 -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/scim+json' -d '{"schemas":["urn:ietf:params:scim:api:messages:2.0:PatchOp"],"Operations":[{"op":"add","path":"groups","value":[{"value":"team-alpha"},{"value":"team-kappa"},{"value":"team-nu"},{"value":"team-mu"},{"value":"team-beta"},{"value":"team-omega"},{"value":"team-delta"},{"value":"team-sigma"}]}]}' | tee case_I_patch1_raw.txt
{"error":{"message":"SCIM roster sync failed on 1 of 8 team membership writes, leaving the roster partially updated. Retry the push to reconcile it. Failed writes: add orderdev1 to team-nu: 404: {'error': \"Team doesn't exist in db. Team=team-nu. Create team via `/team/new` call.\"}","type":"internal_server_error","param":null,"code":"404"}}
HTTP 404
$ curl -sS "$P/user/info?user_id=orderdev1" -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '{user_id: .user_id, teams: .user_info.teams}'
{"user_id":"orderdev1","teams":["team-zeta","team-alpha","team-kappa","team-mu","team-beta","team-omega","team-delta","team-sigma"]}
$ curl -sS -o case_I_patch2_response.json -w 'HTTP %{http_code}\n' -X PATCH $P/scim/v2/Users/orderdev1 -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/scim+json' -d '{"schemas":["urn:ietf:params:scim:api:messages:2.0:PatchOp"],"Operations":[{"op":"add","path":"groups","value":[{"value":"team-alpha"},{"value":"team-kappa"},{"value":"team-nu"},{"value":"team-mu"},{"value":"team-beta"},{"value":"team-omega"},{"value":"team-delta"},{"value":"team-sigma"}]}]}'; jq -c '{id, userName, groups: [.groups[].value]}' case_I_patch2_response.json
HTTP 200
{"id":"orderdev1","userName":"orderdev1@example.com","groups":["team-zeta","team-alpha","team-kappa","team-mu","team-beta","team-omega","team-delta","team-sigma","team-nu"]}
$ curl -sS "$P/user/info?user_id=orderdev1" -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c '{user_id: .user_id, teams: .user_info.teams}'
{"user_id":"orderdev1","teams":["team-zeta","team-alpha","team-kappa","team-mu","team-beta","team-omega","team-delta","team-sigma","team-nu"]}
options in the order the page lists them:
  0. Choose a team (disabled placeholder) (selected)
  1. team-zeta
  2. team-alpha
  3. team-kappa
  4. team-mu
  5. team-beta
  6. team-omega
  7. team-delta
  8. team-sigma
  9. team-nu
picked team: team-zeta (first entry listed)
$ curl -sS "$P/spend/logs?user_id=orderdev1" -H "Authorization: Bearer $LITELLM_MASTER_KEY" | jq -c 'sort_by(.startTime) | .[] | {startTime, user, team_id, model, spend}'
{"startTime":"2026-10-30T01:28:26.346000Z","user":"orderdev1","team_id":"team-zeta","model":"anthropic/claude-haiku-4-5-20251001","spend":0.0008709999999999999}
{"startTime":"2026-10-30T01:28:26.591000Z","user":"orderdev1","team_id":"team-zeta","model":"anthropic/claude-opus-4-7","spend":0.19612375}

pr49033-50c4368d25-r7-I-cc-picked.png

Every step

Case J

Before (71babd9)

scimdev1's PATCH kept the replace's three teams in a random order and dropped the add and the remove. scimdev2's PATCH answered 200, and scimdev2 ended inactive on team-alpha

$ curl -sS "$P/user/info?user_id=scimdev1" ... # after the PATCH replace, add, remove
["team-beta","team-kappa","team-alpha"]
$ curl -sS -w '\nHTTP %{http_code}\n' -X PATCH $P/scim/v2/Users/scimdev2 -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/scim+json" -d '{"schemas":["urn:ietf:params:scim:api:messages:2.0:PatchOp"],"Operations":[{"op":"replace","path":"active","value":false},{"op":"replace","path":"groups","value":[{"value":"team-alpha"}]},{"op":"add","path":"groups","value":[{"value":"team-nope"}]}]}'
{"schemas":["urn:ietf:params:scim:schemas:core:2.0:User"],"id":"scimdev2","externalId":null,"meta":{"resourceType":"User","created":"2026-10-30T02:51:24.517000+00:00","lastModified":"2026-10-30T02:51:25.325000+00:00"},"userName":"scimdev2@example.com","name":{"familyName":"Unknown Family Name","givenName":"Unknown User","formatted":null,"middleName":null,"honorificPrefix":null,"honorificSuffix":null},"displayName":"scimdev2@example.com","active":false,"emails":[{"value":"scimdev2@example.com","type":null,"primary":true}],"groups":[{"value":"team-alpha","display":"team-alpha","type":"direct"}]}
HTTP 200
$ curl -sS $P/scim/v2/Users/scimdev2 -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/scim+json" | jq -c '{active, groups: [.groups[]?.value]}'; curl -sS "$P/user/info?user_id=scimdev2" -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/scim+json" | jq -c '.user_info.teams'
{"active":false,"groups":["team-alpha"]}
["team-alpha"]

Every step

After (7c616ad)

scimdev1's PATCH kept the replace's three teams in record order and dropped the add and the remove. scimdev2's PATCH answered 200, and scimdev2 ended inactive on team-alpha

$ curl -sS "$P/user/info?user_id=scimdev1" ... # after the PATCH replace, add, remove
["team-kappa","team-alpha","team-beta"]
$ curl -sS -w '\nHTTP %{http_code}\n' -X PATCH $P/scim/v2/Users/scimdev2 -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/scim+json" -d '{"schemas":["urn:ietf:params:scim:api:messages:2.0:PatchOp"],"Operations":[{"op":"replace","path":"active","value":false},{"op":"replace","path":"groups","value":[{"value":"team-alpha"}]},{"op":"add","path":"groups","value":[{"value":"team-nope"}]}]}'
{"schemas":["urn:ietf:params:scim:schemas:core:2.0:User"],"id":"scimdev2","externalId":null,"meta":{"resourceType":"User","created":"2026-10-30T03:20:20.511000+00:00","lastModified":"2026-10-30T03:20:20.936000+00:00"},"userName":"scimdev2@example.com","name":{"familyName":"Unknown Family Name","givenName":"Unknown User","formatted":null,"middleName":null,"honorificPrefix":null,"honorificSuffix":null},"displayName":"scimdev2@example.com","active":false,"emails":[{"value":"scimdev2@example.com","type":null,"primary":true}],"groups":[{"value":"team-alpha","display":"team-alpha","type":"direct"}]}
HTTP 200
$ curl -sS $P/scim/v2/Users/scimdev2 -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/scim+json" | jq -c '{active, groups: [.groups[]?.value]}'; curl -sS "$P/user/info?user_id=scimdev2" -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/scim+json" | jq -c '.user_info.teams'
{"active":false,"groups":["team-alpha"]}
["team-alpha"]

Every step

Surprises
  • First gateway sign-in shows no team field (leaves it alone)
  • SCIM create answers userName "Unknown Display Name" (leaves it alone)
  • SCIM PUT rewrites userName to the email (leaves it alone)
  • JWT sync drops team-kappa, which the claim lacks (leaves it alone)
  • JWT requests bill team-alpha, not claim-first team-zeta (leaves it alone)
  • haiku-5-5 once spent all 20 tokens thinking (leaves it alone)
  • Host load reached 29 on 8 cores (leaves it alone)
  • Classic lite whoami prints no Team ID (leaves it alone)
  • CLI sessions get 404 from /key/info (leaves it alone)
  • SCIM POST for a known email answers 201 (leaves it alone)
  • A partly failed SCIM PATCH answers 404 typed internal_server_error (leaves it alone)
  • A missing team added after a replace is ignored (leaves it alone)

Type

🐛 Bug Fix

Caveats (if any)

Medium

  • An old record saved out of order keeps that first team as the default, and reordering it by removing and re-adding a team drops the member's budget

Low

  • No API reorders a user's teams once they are added, and a SCIM PUT or POST no longer does
  • Upgrading moves a developer's default to their record's first team, so the release note should say so
5 more low caveats
  • lite login --pkce still preselects the first team, where [Bug]: native CLI login consent page lists teams in database order and preselects the first one #37642 also suggested a forced choice
  • Group adds and removes after a SCIM replace are still ignored, as before
  • The primary-database lookup is unit-tested only, since QA ran one database
  • find_existing_team_ids reads the module's global prisma_client, like its sibling
  • The one-at-a-time joins in group sync and JWT sign-in were not timed, and the timing runs used a local database

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
/live-pr-risk backward incompatible changes

Default team. Every reader of a user's first team now follows the user record instead of Postgres row order. The Expected section of LIT-9412, which Shivam filed, asks for this

SSO group sync. It adds new teams in claim order, and it skips a claim group with no LiteLLM team instead of trying to add it. The Suggested fix in LIT-9412 asks for the claim order, and the skip has no separate sign-off

SCIM PATCH, PUT and known-email POST, user create, and JWT team sync. These keep the record's order and add new teams in the order sent. LIT-9412 does not name these paths, and this PR adds them so one order holds everywhere, with no separate sign-off

SCIM PUT with a repeated team. It now stores one copy, so the single-team JWT fallback applies to that user. This has no separate sign-off

Bugbot summary

[!NOTE]
Medium Risk
Changes how team membership arrays are written and read across JWT sync, SCIM, and SSO login paths, which affects default team selection and billing attribution but not core token verification.

Overview
Stabilizes user.teams ordering so CLI SSO, Claude Code gateway flows, and lite login “skip” bind to the first team on the user record, not whatever order Postgres happens to return after unrelated team writes.

Membership writers now keep teams the user already has in place and append newly added teams in the order the IdP or API sent them, instead of rebuilding the list from unordered sets. That logic is applied on JWT team sync, SCIM (POST/PATCH/PUT and roster adds), /user/new default-team joins, and SSO group sync (add_missing_team_member).

SSO group sync also adds missing teams one at a time in claim order (replacing concurrent adds), filters claim groups to existing LiteLLM teams with a single writer DB lookup, and returns CLI team details in user-record order so dropdowns and defaults stay aligned.

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

@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

@greptile-apps

greptile-apps Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge, with the previous numbered finding addressed and no new blocking issues

Summary

The PR keeps CLI SSO and gateway team lists in the user record's order. User creation, SSO group sync, SCIM, and JWT sync append new teams in the order sent

The latest change adds types and Final annotations to the regression test, addressing the previous numbered finding. No new issues were found

In the earlier SCIM thread, mateo-berri explained that ignoring group changes after a replace preserves existing behavior and avoids blocking deactivation when a later add names a missing team. Greptile conceded that this was not a regression

  • CLI and gateway sign-in lists follow the user's saved team order.
  • SSO group sync adds new teams in the order the identity provider sends them.
  • New users join requested teams in the order they were provided.
  • JWT team sync keeps existing teams in place and appends new teams in claim order.
  • SCIM updates keep existing teams in place and append new teams in request order.

Reviews (9) · Last reviewed commit: "test(sso): type the CLI team detail row ..." · Reviewed by Greptile

@codspeed

codspeed Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

1 warning

⚠️ 1 benchmark is dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 43 untouched benchmarks


Comparing zoveki:litellm_cli_sso_team_order (af22af0) with main (4c8b909)

Open in CodSpeed

@codecov

codecov Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.38710% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...xy/management_endpoints/internal_user_endpoints.py 83.33% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

…SSO groups once

SCIM PATCH and JWT team sync saved a user's teams from a set, so the first
team on the record, the one a Claude Code gateway sign-in binds, could change
on any sync. Both now keep the record's order and append new teams in the
order the IdP sent them.

SSO group sync now drops claim groups that are not LiteLLM teams with one
lookup before adding the missing teams one at a time, so a claim full of
non-team groups no longer costs one failed add per group on every sign-in.
@mateo-berri mateo-berri changed the title fix(sso): bind CLI SSO and Claude Code gateway logins to the first team on the user record fix(sso): keep CLI SSO and Claude Code gateway team lists in the user record's order Oct 29, 2026
@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

…gain

Applying group adds and removes after a replace made a PATCH that deactivates a user
and adds a team that does not exist answer 404 and leave the user active, where it
answered 200 before. Keep the record's team order, but ignore group ops after the last
replace as the base did.
@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

@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!

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

Reviewed by Cursor Bugbot for commit af22af0. 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

@mateo-berri
mateo-berri merged commit 87fec54 into BerriAI:main Oct 30, 2026
90 checks passed
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]: native CLI login consent page lists teams in database order and preselects the first one

2 participants