File size: 147,374 Bytes
bf565ba
b7df2fe
bf565ba
f835300
 
62ed41b
2683513
b0569d9
d51ae99
bf565ba
dbb8b44
21dcb11
bf565ba
467f294
3f1021f
bf565ba
225c46e
4caa52c
 
 
d51ae99
 
522249e
467f294
225c46e
 
8ed4f52
225c46e
 
 
7cb148b
 
 
 
225c46e
522249e
225c46e
dcaf0eb
00b549c
d28e7c5
 
99c2167
467f294
bf565ba
225c46e
dbb8b44
 
225c46e
dbb8b44
 
 
 
225c46e
bf565ba
 
 
 
 
225c46e
b1631e5
 
8a28cd0
 
b1631e5
467f294
b1631e5
 
 
7785a1f
2ed5bb6
7785a1f
225c46e
79e83ae
f35e0e7
467f294
b1631e5
f35e0e7
 
a1cc875
f35e0e7
 
 
 
467f294
f35e0e7
 
 
 
 
 
 
 
 
 
 
 
467f294
 
f35e0e7
467f294
 
 
 
 
 
 
 
 
aa25dd9
 
 
15c96ab
4caa52c
 
15c96ab
4caa52c
 
 
 
 
 
 
8a28cd0
 
 
 
 
225c46e
 
 
8a28cd0
 
34e560e
 
225c46e
8a28cd0
bf565ba
bd8f638
 
 
 
 
467f294
 
 
bd8f638
d28e7c5
 
225c46e
d28e7c5
 
 
 
 
 
225c46e
bf565ba
62ed41b
da8d5f4
bd84d38
4bc7613
 
 
 
bd84d38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aefb706
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
467f294
 
 
 
 
 
f35e0e7
467f294
bd84d38
 
98f6823
aefb706
 
 
f35e0e7
21dcb11
d51ae99
27b342a
00b549c
d28e7c5
b1631e5
2ed5bb6
7785a1f
99c2167
b1631e5
79e83ae
 
 
 
 
a1cc875
 
 
79e83ae
 
b1631e5
 
 
b7df2fe
 
 
 
 
 
 
 
b1631e5
225c46e
3b4c51c
b1631e5
 
3b4c51c
225c46e
2ed5bb6
225c46e
3b4c51c
 
225c46e
3b4c51c
 
 
 
b7df2fe
 
 
 
 
b1631e5
bf565ba
2ed5bb6
 
 
b7df2fe
 
 
 
 
 
 
 
2ed5bb6
225c46e
3b4c51c
2ed5bb6
 
3b4c51c
b7df2fe
 
 
 
 
 
 
3b4c51c
b7df2fe
 
 
 
2ed5bb6
 
 
83934f3
 
3981e3a
 
83934f3
 
 
 
3981e3a
 
225c46e
 
 
 
 
 
 
 
3981e3a
225c46e
3981e3a
 
 
 
 
 
 
 
 
 
 
83934f3
3981e3a
225c46e
3981e3a
 
 
 
225c46e
3981e3a
 
 
 
83934f3
3981e3a
83934f3
 
 
 
 
 
f835300
83934f3
167dd9f
 
 
 
225c46e
f835300
 
 
 
225c46e
f835300
 
 
 
 
 
225c46e
 
 
f835300
225c46e
 
 
 
f835300
 
 
 
 
83934f3
3f1021f
 
 
 
 
 
 
 
225c46e
3f1021f
 
2fdaebd
 
 
 
 
 
 
 
 
 
225c46e
2fdaebd
 
 
225c46e
2fdaebd
 
39e01ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1cc875
 
 
adfcd18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1cc875
 
 
adfcd18
 
 
 
 
 
a1cc875
 
 
 
 
 
 
 
adfcd18
 
 
 
 
 
a1cc875
 
 
 
adfcd18
 
d28e7c5
 
 
225c46e
 
 
 
 
 
 
 
1e530bb
8d69bcd
 
 
a1cc875
8d69bcd
 
4dfb828
 
 
a1cc875
8d69bcd
 
 
4dfb828
 
 
 
 
 
 
 
8d69bcd
4dfb828
8d69bcd
 
 
 
a1cc875
1e530bb
 
225c46e
 
 
 
 
 
 
 
 
 
 
 
1e530bb
 
 
 
99c2167
 
 
a1cc875
99c2167
 
a1cc875
99c2167
 
 
a1cc875
99c2167
 
 
 
a1cc875
 
99c2167
 
a1cc875
99c2167
a1cc875
 
 
 
99c2167
 
 
a1cc875
99c2167
 
 
 
a1cc875
99c2167
 
 
225c46e
2be7535
 
 
 
 
 
225c46e
982e3aa
 
 
a1cc875
62e7cf3
 
 
 
 
982e3aa
79c9227
982e3aa
 
 
62e7cf3
 
982e3aa
 
 
 
225c46e
fce1762
225c46e
982e3aa
 
 
 
 
225c46e
 
 
982e3aa
225c46e
62e7cf3
 
 
 
 
 
 
a1cc875
62e7cf3
 
 
 
 
 
 
a1cc875
62e7cf3
 
 
 
a1cc875
62e7cf3
 
a1cc875
 
 
62e7cf3
a1cc875
62e7cf3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1cc875
62e7cf3
982e3aa
225c46e
62e7cf3
982e3aa
 
 
 
225c46e
 
 
79c9227
 
 
225c46e
 
 
79c9227
 
 
2be7535
982e3aa
 
8ed4f52
 
 
 
 
 
225c46e
 
 
 
 
fa51b1a
225c46e
fa51b1a
b079814
 
225c46e
 
 
b079814
 
982e3aa
 
f835300
 
 
 
 
 
225c46e
f835300
 
225c46e
 
 
 
 
 
f835300
225c46e
f835300
 
 
 
 
 
982e3aa
 
225c46e
f835300
225c46e
 
 
 
b079814
225c46e
 
 
f835300
982e3aa
f835300
225c46e
 
 
 
982e3aa
f835300
982e3aa
 
f835300
225c46e
 
 
 
 
 
83934f3
225c46e
982e3aa
 
 
 
 
 
 
63418af
225c46e
fce1762
225c46e
982e3aa
 
 
 
03558f6
 
982e3aa
225c46e
fce1762
225c46e
 
03558f6
 
225c46e
 
 
982e3aa
 
225c46e
 
 
 
 
 
 
231840d
bf565ba
 
 
 
225c46e
d28e7c5
225c46e
 
 
21dcb11
62ed41b
 
225c46e
aaab6f8
 
 
d28e7c5
 
a1cc875
372b6c5
 
a1cc875
372b6c5
a1cc875
 
372b6c5
 
 
 
 
 
225c46e
d28e7c5
27b342a
c68b148
d28e7c5
 
a1cc875
f00537a
 
 
 
 
 
 
a1cc875
b5da45c
 
a1cc875
b5da45c
 
 
 
 
 
 
a1cc875
b5da45c
a1cc875
b5da45c
 
 
 
 
 
 
 
 
 
 
 
 
a1cc875
b5da45c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1cc875
4dfb828
 
a1cc875
b5da45c
4dfb828
b5da45c
 
 
 
4dfb828
 
 
 
 
a1cc875
b5da45c
 
a1cc875
b5da45c
f00537a
7cb148b
 
 
 
 
225c46e
 
 
d28e7c5
3f1021f
27b342a
62ed41b
00b549c
225c46e
 
 
62ed41b
225c46e
62ed41b
 
225c46e
 
 
62ed41b
 
225c46e
 
 
62ed41b
00b549c
225c46e
 
 
d28e7c5
b5b51f2
27b342a
818ba14
 
 
 
 
 
 
 
 
225c46e
818ba14
225c46e
79e83ae
d28e7c5
a1cc875
 
 
b5da45c
a1cc875
4dfb828
62ed41b
3f1021f
d28e7c5
aa8035e
2786fc8
225c46e
d28e7c5
 
 
 
225c46e
d28e7c5
225c46e
d28e7c5
39e01ca
f00537a
 
99c2167
39e01ca
 
 
 
 
 
 
 
 
 
 
d28e7c5
225c46e
 
 
d28e7c5
225c46e
 
 
2ccd2a1
 
 
225c46e
 
 
2ccd2a1
 
225c46e
 
 
 
2ccd2a1
 
225c46e
 
 
 
 
a725feb
 
 
 
 
 
225c46e
a725feb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2be7535
a725feb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d28e7c5
225c46e
d28e7c5
225c46e
d28e7c5
 
225c46e
d28e7c5
225c46e
d28e7c5
 
adfcd18
 
a1cc875
 
 
adfcd18
 
a1cc875
 
 
adfcd18
d28e7c5
225c46e
 
 
 
 
d28e7c5
 
 
 
225c46e
d28e7c5
225c46e
 
 
8cf8bab
225c46e
 
 
aa8035e
d28e7c5
225c46e
 
 
 
 
 
 
d28e7c5
 
 
 
225c46e
fce1762
225c46e
 
d28e7c5
 
 
 
 
225c46e
 
 
d28e7c5
225c46e
 
 
 
39e01ca
 
 
 
 
d28e7c5
39e01ca
225c46e
 
 
 
 
d28e7c5
2be7535
d28e7c5
 
 
 
 
 
225c46e
 
 
 
 
 
 
 
 
bd84d38
225c46e
d28e7c5
225c46e
a1cc875
7cb148b
a1cc875
 
 
7cb148b
225c46e
7cb148b
225c46e
d28e7c5
0dd6d21
fa51b1a
 
 
 
d28e7c5
225c46e
 
 
 
 
 
 
 
62ed41b
225c46e
 
 
 
 
d28e7c5
225c46e
 
 
 
 
 
 
 
 
bd84d38
7cb148b
a1cc875
20944a5
225c46e
a1cc875
 
 
 
225c46e
978e4a8
d28e7c5
7cb148b
a1cc875
 
 
225c46e
7cb148b
225c46e
 
 
d28e7c5
225c46e
a1cc875
225c46e
d28e7c5
225c46e
d28e7c5
 
a1cc875
 
 
225c46e
7cb148b
225c46e
d28e7c5
 
225c46e
7cb148b
225c46e
d28e7c5
225c46e
d28e7c5
7cb148b
 
 
 
 
 
 
 
 
 
 
 
a1cc875
bd84d38
7cb148b
a1cc875
7cb148b
 
 
a1cc875
7cb148b
 
 
 
 
 
a1cc875
 
 
 
 
 
0dd6d21
 
7cb148b
 
 
 
a1cc875
7cb148b
a1cc875
 
 
 
 
 
 
7cb148b
 
 
 
 
 
 
a1cc875
7cb148b
 
 
 
a1cc875
 
 
7cb148b
 
d28e7c5
 
225c46e
 
 
 
 
 
 
 
 
 
d28e7c5
225c46e
7cb148b
225c46e
d28e7c5
 
bd84d38
225c46e
a1cc875
225c46e
7cb148b
225c46e
a1cc875
0dd6d21
a1cc875
fa51b1a
 
 
d28e7c5
225c46e
 
 
 
7cb148b
 
 
 
 
d28e7c5
 
a1cc875
 
 
 
 
225c46e
 
 
 
27b342a
d28e7c5
 
aa8035e
7cb148b
 
 
a1cc875
7cb148b
 
 
a1cc875
7cb148b
 
225c46e
7cb148b
a1cc875
 
 
 
62ed41b
21dcb11
225c46e
 
 
 
 
 
231840d
64e385a
225c46e
 
aaab6f8
d28e7c5
 
a1cc875
372b6c5
 
a1cc875
372b6c5
a1cc875
 
372b6c5
 
 
 
 
 
225c46e
62ed41b
d28e7c5
64e385a
c68b148
225c46e
856f4f3
 
f00537a
 
 
 
 
 
 
a1cc875
b5da45c
 
a1cc875
b5da45c
 
 
 
 
 
 
a1cc875
b5da45c
a1cc875
b5da45c
 
 
 
 
 
 
 
 
 
 
 
 
a1cc875
b5da45c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1cc875
4dfb828
 
a1cc875
b5da45c
4dfb828
b5da45c
 
 
 
4dfb828
 
 
 
 
a1cc875
b5da45c
 
a1cc875
b5da45c
f00537a
7cb148b
 
 
 
 
f8d59cd
225c46e
 
 
 
d28e7c5
f8d59cd
 
 
225c46e
 
 
62ed41b
 
225c46e
 
 
62ed41b
225c46e
 
 
62ed41b
f8d59cd
225c46e
 
 
d28e7c5
225c46e
 
 
f8d59cd
 
818ba14
 
 
 
 
 
 
 
 
225c46e
818ba14
225c46e
a1cc875
 
 
d28e7c5
a1cc875
 
 
b5da45c
a1cc875
4dfb828
62ed41b
f8d59cd
d28e7c5
f8d59cd
225c46e
 
 
63418af
 
2ccd2a1
63418af
225c46e
 
 
2ccd2a1
 
225c46e
 
 
 
2ccd2a1
 
225c46e
 
 
 
 
d28e7c5
 
 
 
225c46e
d28e7c5
 
 
39e01ca
f00537a
 
99c2167
39e01ca
 
 
 
 
 
 
 
 
 
 
 
d28e7c5
225c46e
 
 
d28e7c5
225c46e
 
 
 
79c9227
 
225c46e
fce1762
225c46e
fa51b1a
79c9227
 
225c46e
 
 
79c9227
 
225c46e
 
 
79c9227
225c46e
 
 
 
 
 
 
 
 
fce1762
225c46e
79c9227
 
225c46e
2be7535
 
 
 
 
225c46e
 
79c9227
 
 
 
225c46e
 
 
 
 
63418af
6aa3124
225c46e
bd84d38
 
 
7cb148b
a1cc875
7cb148b
 
 
 
 
 
 
 
 
a1cc875
7cb148b
a1cc875
 
 
 
7cb148b
 
 
 
 
 
a1cc875
0dd6d21
 
a1cc875
 
 
7cb148b
 
 
a1cc875
225c46e
 
 
 
7cb148b
225c46e
79c9227
 
225c46e
 
 
 
 
79c9227
225c46e
 
 
 
 
 
 
 
 
bd84d38
7cb148b
a1cc875
20944a5
225c46e
a1cc875
 
 
 
225c46e
79c9227
 
a1cc875
 
 
225c46e
7cb148b
225c46e
79c9227
225c46e
 
a1cc875
225c46e
79c9227
 
a1cc875
 
 
225c46e
7cb148b
225c46e
79c9227
225c46e
 
7cb148b
225c46e
79c9227
 
 
 
 
225c46e
 
 
 
 
 
 
 
 
bd84d38
7cb148b
a1cc875
7cb148b
a1cc875
 
 
 
225c46e
7cb148b
225c46e
a1cc875
7cb148b
 
 
 
 
79c9227
a1cc875
225c46e
 
 
63418af
225c46e
ad45c64
 
 
d28e7c5
225c46e
d28e7c5
225c46e
d28e7c5
 
225c46e
d28e7c5
 
f8d59cd
 
adfcd18
 
a1cc875
 
 
adfcd18
a1cc875
 
 
adfcd18
f8d59cd
225c46e
 
 
 
 
f8d59cd
 
 
 
225c46e
f8d59cd
225c46e
 
 
8cf8bab
225c46e
 
 
64e385a
f8d59cd
225c46e
 
 
 
 
 
 
f8d59cd
 
ad45c64
 
 
 
225c46e
ad45c64
f8d59cd
 
225c46e
fce1762
225c46e
 
da8d5f4
 
 
 
 
225c46e
 
 
da8d5f4
225c46e
 
 
 
 
39e01ca
 
 
 
 
83934f3
39e01ca
225c46e
 
 
 
fce1762
83934f3
b079814
f8d59cd
225c46e
2be7535
 
 
 
 
225c46e
 
f8d59cd
 
 
 
a1cc875
 
 
 
 
f8d59cd
225c46e
b079814
 
10dabd5
b079814
225c46e
bd84d38
 
 
a1cc875
7cb148b
 
 
 
 
 
b079814
 
 
225c46e
 
 
b079814
225c46e
 
 
b079814
 
 
225c46e
10dabd5
d28e7c5
10dabd5
 
 
225c46e
 
 
 
10dabd5
b079814
 
 
225c46e
7cb148b
225c46e
a1cc875
7cb148b
a1cc875
 
 
b079814
225c46e
 
 
 
856f4f3
a1cc875
b079814
 
225c46e
 
 
b079814
 
 
 
a1cc875
 
 
b079814
 
a1cc875
 
 
 
b079814
225c46e
 
 
 
856f4f3
 
7cb148b
856f4f3
7cb148b
 
 
 
 
856f4f3
 
 
 
 
225c46e
7cb148b
225c46e
856f4f3
225c46e
856f4f3
 
225c46e
7cb148b
225c46e
 
0dd6d21
a1cc875
 
 
225c46e
 
 
 
 
 
 
856f4f3
f8d59cd
225c46e
 
 
 
 
856f4f3
f8d59cd
225c46e
 
 
 
 
 
 
 
 
bd84d38
7cb148b
a1cc875
 
 
 
 
 
20944a5
225c46e
a1cc875
 
 
 
225c46e
f8d59cd
 
225c46e
fce1762
225c46e
856f4f3
f8d59cd
225c46e
 
a1cc875
225c46e
62ed41b
 
225c46e
 
 
62ed41b
225c46e
 
fce1762
225c46e
f8d59cd
 
 
 
856f4f3
f8d59cd
225c46e
 
 
 
 
 
 
 
 
bd84d38
7cb148b
a1cc875
7cb148b
a1cc875
 
 
5a4ba9e
225c46e
fce1762
225c46e
f8d59cd
0dd6d21
a1cc875
fa51b1a
 
 
f8d59cd
225c46e
 
 
 
7cb148b
225c46e
 
7cb148b
 
 
 
 
 
f8d59cd
225c46e
7cb148b
225c46e
 
 
a1cc875
 
 
f8d59cd
 
 
d28e7c5
 
225c46e
7cb148b
 
a1cc875
7cb148b
 
 
a1cc875
7cb148b
 
 
4ef7f43
7cb148b
a1cc875
 
 
7cb148b
a1cc875
 
 
7cb148b
 
 
4ef7f43
a1cc875
62ed41b
f8d59cd
231840d
f8d59cd
225c46e
 
 
f8d59cd
 
 
 
62ed41b
225c46e
 
 
 
 
 
 
 
 
 
231840d
 
 
225c46e
 
 
 
 
 
da8d5f4
 
 
 
 
 
 
 
 
 
 
 
 
c590f47
 
 
a1cc875
c590f47
a1cc875
 
 
c590f47
a1cc875
231840d
c590f47
 
 
 
 
 
a1cc875
225c46e
 
 
231840d
225c46e
 
 
 
 
 
 
 
 
 
 
 
 
da8d5f4
 
 
 
 
 
 
 
 
 
 
 
 
225c46e
 
 
 
 
 
64e385a
2fdaebd
27b342a
2fdaebd
 
 
 
 
3f1021f
 
21dcb11
 
 
 
 
 
 
 
27b342a
83934f3
21dcb11
b079814
21dcb11
 
3b4c51c
 
 
21dcb11
 
3b4c51c
 
 
ce13f06
1e530bb
 
3b4c51c
 
 
 
 
ce13f06
3b4c51c
fce1762
225c46e
fce1762
225c46e
 
 
 
 
 
 
b1631e5
2ed5bb6
 
 
 
 
 
 
 
 
 
 
 
 
 
225c46e
 
 
b1631e5
2ed5bb6
 
ce13f06
bd84d38
fce1762
225c46e
fce1762
225c46e
 
622b036
225c46e
 
 
ce13f06
21dcb11
225c46e
 
 
27b342a
d51ae99
225c46e
3b4c51c
 
1e530bb
 
21dcb11
3b4c51c
1e530bb
225c46e
 
 
3f1021f
1e530bb
 
225c46e
d51ae99
26c6a6e
 
 
 
b1631e5
 
 
8b4ff52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06b3f7d
 
 
 
 
8b4ff52
 
 
 
 
 
 
06b3f7d
 
 
 
 
 
 
 
 
90d4836
06b3f7d
90d4836
 
 
 
 
 
 
 
 
 
06b3f7d
 
 
1af1879
8b4ff52
06b3f7d
1af1879
 
06b3f7d
 
 
 
 
 
 
 
8b4ff52
1af1879
 
06b3f7d
 
 
 
 
 
 
1af1879
 
 
 
 
 
 
06b3f7d
 
 
1af1879
06b3f7d
8b4ff52
 
 
 
 
 
 
06b3f7d
 
 
 
 
 
 
 
 
 
8b4ff52
 
 
 
 
 
 
 
06b3f7d
90d4836
1af1879
90d4836
 
8b4ff52
06b3f7d
1af1879
 
06b3f7d
90d4836
 
 
 
 
 
 
 
 
06b3f7d
 
 
90d4836
06b3f7d
 
 
8b4ff52
1af1879
 
 
 
90d4836
 
 
 
1af1879
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90d4836
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b4ff52
 
 
 
 
 
 
 
 
1af1879
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b4ff52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
import asyncio
import fnmatch
import json
import re
import codecs
import time
import os
import random
import httpx
import litellm
from litellm.exceptions import APIConnectionError
from litellm.litellm_core_utils.token_counter import token_counter
import logging
from pathlib import Path
from typing import List, Dict, Any, AsyncGenerator, Optional, Union

lib_logger = logging.getLogger("rotator_library")
# Ensure the logger is configured to propagate to the root logger
# which is set up in main.py. This allows the main app to control
# log levels and handlers centrally.
lib_logger.propagate = False

from .usage_manager import UsageManager
from .failure_logger import log_failure, configure_failure_logger
from .error_handler import (
    PreRequestCallbackError,
    CredentialNeedsReauthError,
    classify_error,
    AllProviders,
    NoAvailableKeysError,
    should_rotate_on_error,
    should_retry_same_key,
    RequestErrorAccumulator,
    mask_credential,
)
from .providers import PROVIDER_PLUGINS
from .providers.openai_compatible_provider import OpenAICompatibleProvider
from .request_sanitizer import sanitize_request_payload
from .cooldown_manager import CooldownManager
from .credential_manager import CredentialManager
from .background_refresher import BackgroundRefresher
from .model_definitions import ModelDefinitions
from .utils.paths import get_default_root, get_logs_dir, get_oauth_dir, get_data_file


class StreamedAPIError(Exception):
    """Custom exception to signal an API error received over a stream."""

    def __init__(self, message, data=None):
        super().__init__(message)
        self.data = data


class RotatingClient:
    """
    A client that intelligently rotates and retries API keys using LiteLLM,
    with support for both streaming and non-streaming responses.
    """

    def __init__(
        self,
        api_keys: Optional[Dict[str, List[str]]] = None,
        oauth_credentials: Optional[Dict[str, List[str]]] = None,
        max_retries: int = 2,
        usage_file_path: Optional[Union[str, Path]] = None,
        configure_logging: bool = True,
        global_timeout: int = 30,
        abort_on_callback_error: bool = True,
        litellm_provider_params: Optional[Dict[str, Any]] = None,
        ignore_models: Optional[Dict[str, List[str]]] = None,
        whitelist_models: Optional[Dict[str, List[str]]] = None,
        enable_request_logging: bool = False,
        max_concurrent_requests_per_key: Optional[Dict[str, int]] = None,
        rotation_tolerance: float = 3.0,
        data_dir: Optional[Union[str, Path]] = None,
    ):
        """
        Initialize the RotatingClient with intelligent credential rotation.

        Args:
            api_keys: Dictionary mapping provider names to lists of API keys
            oauth_credentials: Dictionary mapping provider names to OAuth credential paths
            max_retries: Maximum number of retry attempts per credential
            usage_file_path: Path to store usage statistics. If None, uses data_dir/key_usage.json
            configure_logging: Whether to configure library logging
            global_timeout: Global timeout for requests in seconds
            abort_on_callback_error: Whether to abort on pre-request callback errors
            litellm_provider_params: Provider-specific parameters for LiteLLM
            ignore_models: Models to ignore/blacklist per provider
            whitelist_models: Models to explicitly whitelist per provider
            enable_request_logging: Whether to enable detailed request logging
            max_concurrent_requests_per_key: Max concurrent requests per key by provider
            rotation_tolerance: Tolerance for weighted random credential rotation.
                - 0.0: Deterministic, least-used credential always selected
                - 2.0 - 4.0 (default, recommended): Balanced randomness, can pick credentials within 2 uses of max
                - 5.0+: High randomness, more unpredictable selection patterns
            data_dir: Root directory for all data files (logs, cache, oauth_creds, key_usage.json).
                      If None, auto-detects: EXE directory if frozen, else current working directory.
        """
        # Resolve data_dir early - this becomes the root for all file operations
        if data_dir is not None:
            self.data_dir = Path(data_dir).resolve()
        else:
            self.data_dir = get_default_root()

        # Configure failure logger to use correct logs directory
        configure_failure_logger(get_logs_dir(self.data_dir))

        os.environ["LITELLM_LOG"] = "ERROR"
        litellm.set_verbose = False
        litellm.drop_params = True
        if configure_logging:
            # When True, this allows logs from this library to be handled
            # by the parent application's logging configuration.
            lib_logger.propagate = True
            # Remove any default handlers to prevent duplicate logging
            if lib_logger.hasHandlers():
                lib_logger.handlers.clear()
                lib_logger.addHandler(logging.NullHandler())
        else:
            lib_logger.propagate = False

        api_keys = api_keys or {}
        oauth_credentials = oauth_credentials or {}

        # Filter out providers with empty lists of credentials to ensure validity
        api_keys = {provider: keys for provider, keys in api_keys.items() if keys}
        oauth_credentials = {
            provider: paths for provider, paths in oauth_credentials.items() if paths
        }

        if not api_keys and not oauth_credentials:
            lib_logger.warning(
                "No provider credentials configured. The client will be unable to make any API requests."
            )

        self.api_keys = api_keys
        # Use provided oauth_credentials directly if available (already discovered by main.py)
        # Only call discover_and_prepare() if no credentials were passed
        if oauth_credentials:
            self.oauth_credentials = oauth_credentials
        else:
            self.credential_manager = CredentialManager(
                os.environ, oauth_dir=get_oauth_dir(self.data_dir)
            )
            self.oauth_credentials = self.credential_manager.discover_and_prepare()
        self.background_refresher = BackgroundRefresher(self)
        self.oauth_providers = set(self.oauth_credentials.keys())

        all_credentials = {}
        for provider, keys in api_keys.items():
            all_credentials.setdefault(provider, []).extend(keys)
        for provider, paths in self.oauth_credentials.items():
            all_credentials.setdefault(provider, []).extend(paths)
        self.all_credentials = all_credentials

        self.max_retries = max_retries
        self.global_timeout = global_timeout
        self.abort_on_callback_error = abort_on_callback_error

        # Initialize provider plugins early so they can be used for rotation mode detection
        self._provider_plugins = PROVIDER_PLUGINS
        self._provider_instances = {}

        # Build provider rotation modes map
        # Each provider can specify its preferred rotation mode ("balanced" or "sequential")
        provider_rotation_modes = {}
        for provider in self.all_credentials.keys():
            provider_class = self._provider_plugins.get(provider)
            if provider_class and hasattr(provider_class, "get_rotation_mode"):
                # Use class method to get rotation mode (checks env var + class default)
                mode = provider_class.get_rotation_mode(provider)
            else:
                # Fallback: check environment variable directly
                env_key = f"ROTATION_MODE_{provider.upper()}"
                mode = os.getenv(env_key, "balanced")

            provider_rotation_modes[provider] = mode
            if mode != "balanced":
                lib_logger.info(f"Provider '{provider}' using rotation mode: {mode}")

        # Build priority-based concurrency multiplier maps
        # These are universal multipliers based on credential tier/priority
        priority_multipliers: Dict[str, Dict[int, int]] = {}
        priority_multipliers_by_mode: Dict[str, Dict[str, Dict[int, int]]] = {}
        sequential_fallback_multipliers: Dict[str, int] = {}

        for provider in self.all_credentials.keys():
            provider_class = self._provider_plugins.get(provider)

            # Start with provider class defaults
            if provider_class:
                # Get default priority multipliers from provider class
                if hasattr(provider_class, "default_priority_multipliers"):
                    default_multipliers = provider_class.default_priority_multipliers
                    if default_multipliers:
                        priority_multipliers[provider] = dict(default_multipliers)

                # Get sequential fallback from provider class
                if hasattr(provider_class, "default_sequential_fallback_multiplier"):
                    fallback = provider_class.default_sequential_fallback_multiplier
                    if fallback != 1:  # Only store if different from global default
                        sequential_fallback_multipliers[provider] = fallback

            # Override with environment variables
            # Format: CONCURRENCY_MULTIPLIER_<PROVIDER>_PRIORITY_<N>=<multiplier>
            # Format: CONCURRENCY_MULTIPLIER_<PROVIDER>_PRIORITY_<N>_<MODE>=<multiplier>
            for key, value in os.environ.items():
                prefix = f"CONCURRENCY_MULTIPLIER_{provider.upper()}_PRIORITY_"
                if key.startswith(prefix):
                    remainder = key[len(prefix) :]
                    try:
                        multiplier = int(value)
                        if multiplier < 1:
                            lib_logger.warning(f"Invalid {key}: {value}. Must be >= 1.")
                            continue

                        # Check if mode-specific (e.g., _PRIORITY_1_SEQUENTIAL)
                        if "_" in remainder:
                            parts = remainder.rsplit("_", 1)
                            priority = int(parts[0])
                            mode = parts[1].lower()
                            if mode in ("sequential", "balanced"):
                                # Mode-specific override
                                if provider not in priority_multipliers_by_mode:
                                    priority_multipliers_by_mode[provider] = {}
                                if mode not in priority_multipliers_by_mode[provider]:
                                    priority_multipliers_by_mode[provider][mode] = {}
                                priority_multipliers_by_mode[provider][mode][
                                    priority
                                ] = multiplier
                                lib_logger.info(
                                    f"Provider '{provider}' priority {priority} ({mode} mode) multiplier: {multiplier}x"
                                )
                            else:
                                # Assume it's part of the priority number (unlikely but handle gracefully)
                                lib_logger.warning(f"Unknown mode in {key}: {mode}")
                        else:
                            # Universal priority multiplier
                            priority = int(remainder)
                            if provider not in priority_multipliers:
                                priority_multipliers[provider] = {}
                            priority_multipliers[provider][priority] = multiplier
                            lib_logger.info(
                                f"Provider '{provider}' priority {priority} multiplier: {multiplier}x"
                            )
                    except ValueError:
                        lib_logger.warning(
                            f"Invalid {key}: {value}. Could not parse priority or multiplier."
                        )

        # Log configured multipliers
        for provider, multipliers in priority_multipliers.items():
            if multipliers:
                lib_logger.info(
                    f"Provider '{provider}' priority multipliers: {multipliers}"
                )
        for provider, fallback in sequential_fallback_multipliers.items():
            lib_logger.info(
                f"Provider '{provider}' sequential fallback multiplier: {fallback}x"
            )

        # Resolve usage file path - use provided path or default to data_dir
        if usage_file_path is not None:
            resolved_usage_path = Path(usage_file_path)
        else:
            resolved_usage_path = self.data_dir / "key_usage.json"

        self.usage_manager = UsageManager(
            file_path=resolved_usage_path,
            rotation_tolerance=rotation_tolerance,
            provider_rotation_modes=provider_rotation_modes,
            provider_plugins=PROVIDER_PLUGINS,
            priority_multipliers=priority_multipliers,
            priority_multipliers_by_mode=priority_multipliers_by_mode,
            sequential_fallback_multipliers=sequential_fallback_multipliers,
        )
        self._model_list_cache = {}
        self.http_client = httpx.AsyncClient()
        self.all_providers = AllProviders()
        self.cooldown_manager = CooldownManager()
        self.litellm_provider_params = litellm_provider_params or {}
        self.ignore_models = ignore_models or {}
        self.whitelist_models = whitelist_models or {}
        self.enable_request_logging = enable_request_logging
        self.model_definitions = ModelDefinitions()

        # Store and validate max concurrent requests per key
        self.max_concurrent_requests_per_key = max_concurrent_requests_per_key or {}
        # Validate all values are >= 1
        for provider, max_val in self.max_concurrent_requests_per_key.items():
            if max_val < 1:
                lib_logger.warning(
                    f"Invalid max_concurrent for '{provider}': {max_val}. Setting to 1."
                )
                self.max_concurrent_requests_per_key[provider] = 1

    def _is_model_ignored(self, provider: str, model_id: str) -> bool:
        """
        Checks if a model should be ignored based on the ignore list.
        Supports full glob/fnmatch patterns for both full model IDs and model names.

        Pattern examples:
        - "gpt-4" - exact match
        - "gpt-4*" - prefix wildcard (matches gpt-4, gpt-4-turbo, etc.)
        - "*-preview" - suffix wildcard (matches gpt-4-preview, o1-preview, etc.)
        - "*-preview*" - contains wildcard (matches anything with -preview)
        - "*" - match all
        """
        model_provider = model_id.split("/")[0]
        if model_provider not in self.ignore_models:
            return False

        ignore_list = self.ignore_models[model_provider]
        if ignore_list == ["*"]:
            return True

        try:
            # This is the model name as the provider sees it (e.g., "gpt-4" or "google/gemma-7b")
            provider_model_name = model_id.split("/", 1)[1]
        except IndexError:
            provider_model_name = model_id

        for ignored_pattern in ignore_list:
            # Use fnmatch for full glob pattern support
            if fnmatch.fnmatch(provider_model_name, ignored_pattern) or fnmatch.fnmatch(
                model_id, ignored_pattern
            ):
                return True
        return False

    def _is_model_whitelisted(self, provider: str, model_id: str) -> bool:
        """
        Checks if a model is explicitly whitelisted.
        Supports full glob/fnmatch patterns for both full model IDs and model names.

        Pattern examples:
        - "gpt-4" - exact match
        - "gpt-4*" - prefix wildcard (matches gpt-4, gpt-4-turbo, etc.)
        - "*-preview" - suffix wildcard (matches gpt-4-preview, o1-preview, etc.)
        - "*-preview*" - contains wildcard (matches anything with -preview)
        - "*" - match all
        """
        model_provider = model_id.split("/")[0]
        if model_provider not in self.whitelist_models:
            return False

        whitelist = self.whitelist_models[model_provider]

        try:
            # This is the model name as the provider sees it (e.g., "gpt-4" or "google/gemma-7b")
            provider_model_name = model_id.split("/", 1)[1]
        except IndexError:
            provider_model_name = model_id

        for whitelisted_pattern in whitelist:
            # Use fnmatch for full glob pattern support
            if fnmatch.fnmatch(
                provider_model_name, whitelisted_pattern
            ) or fnmatch.fnmatch(model_id, whitelisted_pattern):
                return True
        return False

    def _sanitize_litellm_log(self, log_data: dict) -> dict:
        """
        Recursively removes large data fields and sensitive information from litellm log
        dictionaries to keep debug logs clean and secure.
        """
        if not isinstance(log_data, dict):
            return log_data

        # Keys to remove at any level of the dictionary
        keys_to_pop = [
            "messages",
            "input",
            "response",
            "data",
            "api_key",
            "api_base",
            "original_response",
            "additional_args",
        ]

        # Keys that might contain nested dictionaries to clean
        nested_keys = ["kwargs", "litellm_params", "model_info", "proxy_server_request"]

        # Create a deep copy to avoid modifying the original log object in memory
        clean_data = json.loads(json.dumps(log_data, default=str))

        def clean_recursively(data_dict):
            if not isinstance(data_dict, dict):
                return

            # Remove sensitive/large keys
            for key in keys_to_pop:
                data_dict.pop(key, None)

            # Recursively clean nested dictionaries
            for key in nested_keys:
                if key in data_dict and isinstance(data_dict[key], dict):
                    clean_recursively(data_dict[key])

            # Also iterate through all values to find any other nested dicts
            for key, value in list(data_dict.items()):
                if isinstance(value, dict):
                    clean_recursively(value)

        clean_recursively(clean_data)
        return clean_data

    def _litellm_logger_callback(self, log_data: dict):
        """
        Callback function to redirect litellm's logs to the library's logger.
        This allows us to control the log level and destination of litellm's output.
        It also cleans up error logs for better readability in debug files.
        """
        # Filter out verbose pre_api_call and post_api_call logs
        log_event_type = log_data.get("log_event_type")
        if log_event_type in ["pre_api_call", "post_api_call"]:
            return  # Skip these verbose logs entirely

        # For successful calls or pre-call logs, a simple debug message is enough.
        if not log_data.get("exception"):
            sanitized_log = self._sanitize_litellm_log(log_data)
            # We log it at the DEBUG level to ensure it goes to the debug file
            # and not the console, based on the main.py configuration.
            lib_logger.debug(f"LiteLLM Log: {sanitized_log}")
            return

        # For failures, extract key info to make debug logs more readable.
        model = log_data.get("model", "N/A")
        call_id = log_data.get("litellm_call_id", "N/A")
        error_info = log_data.get("standard_logging_object", {}).get(
            "error_information", {}
        )
        error_class = error_info.get("error_class", "UnknownError")
        error_message = error_info.get(
            "error_message", str(log_data.get("exception", ""))
        )
        error_message = " ".join(error_message.split())  # Sanitize

        lib_logger.debug(
            f"LiteLLM Callback Handled Error: Model={model} | "
            f"Type={error_class} | Message='{error_message}'"
        )

    async def __aenter__(self):
        return self

    async def __aexit__(self, exc_type, exc_val, exc_tb):
        await self.close()

    async def close(self):
        """Close the HTTP client to prevent resource leaks."""
        if hasattr(self, "http_client") and self.http_client:
            await self.http_client.aclose()

    def _convert_model_params(self, **kwargs) -> Dict[str, Any]:
        """
        Converts model parameters for specific providers.
        For example, the 'chutes' provider requires the model name to be prepended
        with 'openai/' and a specific 'api_base'.
        """
        model = kwargs.get("model")
        if not model:
            return kwargs

        provider = model.split("/")[0]
        if provider == "chutes":
            kwargs["model"] = f"openai/{model.split('/', 1)[1]}"
            kwargs["api_base"] = "https://llm.chutes.ai/v1"

        return kwargs

    def _convert_model_params_for_litellm(self, **kwargs) -> Dict[str, Any]:
        """
        Converts model parameters specifically for LiteLLM calls.
        This is called right before calling LiteLLM to handle custom providers.
        """
        model = kwargs.get("model")
        if not model:
            return kwargs

        provider = model.split("/")[0]

        # Handle custom OpenAI-compatible providers
        # Check if this is a custom provider by looking for API_BASE environment variable
        import os

        api_base_env = f"{provider.upper()}_API_BASE"
        if os.getenv(api_base_env):
            # For custom providers, tell LiteLLM to use openai provider with custom model name
            # This preserves original model name in logs but converts for LiteLLM
            kwargs = kwargs.copy()  # Don't modify original
            kwargs["model"] = f"openai/{model.split('/', 1)[1]}"
            kwargs["api_base"] = os.getenv(api_base_env).rstrip("/")
            kwargs["custom_llm_provider"] = "openai"

        return kwargs

    def _apply_default_safety_settings(
        self, litellm_kwargs: Dict[str, Any], provider: str
    ):
        """
        Ensure default Gemini safety settings are present when calling the Gemini provider.
        This will not override any explicit settings provided by the request. It accepts
        either OpenAI-compatible generic `safety_settings` (dict) or direct Gemini-style
        `safetySettings` (list of dicts). Missing categories will be added with safe defaults.
        """
        if provider != "gemini":
            return

        # Generic defaults (openai-compatible style)
        default_generic = {
            "harassment": "OFF",
            "hate_speech": "OFF",
            "sexually_explicit": "OFF",
            "dangerous_content": "OFF",
            "civic_integrity": "BLOCK_NONE",
        }

        # Gemini defaults (direct Gemini format)
        default_gemini = [
            {"category": "HARM_CATEGORY_HARASSMENT", "threshold": "OFF"},
            {"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "OFF"},
            {"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "OFF"},
            {"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "OFF"},
            {"category": "HARM_CATEGORY_CIVIC_INTEGRITY", "threshold": "BLOCK_NONE"},
        ]

        # If generic form is present, ensure missing generic keys are filled in
        if "safety_settings" in litellm_kwargs and isinstance(
            litellm_kwargs["safety_settings"], dict
        ):
            for k, v in default_generic.items():
                if k not in litellm_kwargs["safety_settings"]:
                    litellm_kwargs["safety_settings"][k] = v
            return

        # If Gemini form is present, ensure missing gemini categories are appended
        if "safetySettings" in litellm_kwargs and isinstance(
            litellm_kwargs["safetySettings"], list
        ):
            present = {
                item.get("category")
                for item in litellm_kwargs["safetySettings"]
                if isinstance(item, dict)
            }
            for d in default_gemini:
                if d["category"] not in present:
                    litellm_kwargs["safetySettings"].append(d)
            return

        # Neither present: set generic defaults so provider conversion will translate them
        if (
            "safety_settings" not in litellm_kwargs
            and "safetySettings" not in litellm_kwargs
        ):
            litellm_kwargs["safety_settings"] = default_generic.copy()

    def get_oauth_credentials(self) -> Dict[str, List[str]]:
        return self.oauth_credentials

    def _is_custom_openai_compatible_provider(self, provider_name: str) -> bool:
        """Checks if a provider is a custom OpenAI-compatible provider."""
        import os

        # Check if the provider has an API_BASE environment variable
        api_base_env = f"{provider_name.upper()}_API_BASE"
        return os.getenv(api_base_env) is not None

    def _get_provider_instance(self, provider_name: str):
        """
        Lazily initializes and returns a provider instance.
        Only initializes providers that have configured credentials.

        Args:
            provider_name: The name of the provider to get an instance for.
                          For OAuth providers, this may include "_oauth" suffix
                          (e.g., "antigravity_oauth"), but credentials are stored
                          under the base name (e.g., "antigravity").

        Returns:
            Provider instance if credentials exist, None otherwise.
        """
        # For OAuth providers, credentials are stored under base name (without _oauth suffix)
        # e.g., "antigravity_oauth" plugin → credentials under "antigravity"
        credential_key = provider_name
        if provider_name.endswith("_oauth"):
            base_name = provider_name[:-6]  # Remove "_oauth"
            if base_name in self.oauth_providers:
                credential_key = base_name

        # Only initialize providers for which we have credentials
        if credential_key not in self.all_credentials:
            lib_logger.debug(
                f"Skipping provider '{provider_name}' initialization: no credentials configured"
            )
            return None

        if provider_name not in self._provider_instances:
            if provider_name in self._provider_plugins:
                self._provider_instances[provider_name] = self._provider_plugins[
                    provider_name
                ]()
            elif self._is_custom_openai_compatible_provider(provider_name):
                # Create a generic OpenAI-compatible provider for custom providers
                try:
                    self._provider_instances[provider_name] = OpenAICompatibleProvider(
                        provider_name
                    )
                except ValueError:
                    # If the provider doesn't have the required environment variables, treat it as a standard provider
                    return None
            else:
                return None
        return self._provider_instances[provider_name]

    def _resolve_model_id(self, model: str, provider: str) -> str:
        """
        Resolves the actual model ID to send to the provider.

        For custom models with name/ID mappings, returns the ID.
        Otherwise, returns the model name unchanged.

        Args:
            model: Full model string with provider (e.g., "iflow/DS-v3.2")
            provider: Provider name (e.g., "iflow")

        Returns:
            Full model string with ID (e.g., "iflow/deepseek-v3.2")
        """
        # Extract model name from "provider/model_name" format
        model_name = model.split("/")[-1] if "/" in model else model

        # Try to get provider instance to check for model definitions
        provider_plugin = self._get_provider_instance(provider)

        # Check if provider has model definitions
        if provider_plugin and hasattr(provider_plugin, "model_definitions"):
            model_id = provider_plugin.model_definitions.get_model_id(
                provider, model_name
            )
            if model_id and model_id != model_name:
                # Return with provider prefix
                return f"{provider}/{model_id}"

        # Fallback: use client's own model definitions
        model_id = self.model_definitions.get_model_id(provider, model_name)
        if model_id and model_id != model_name:
            return f"{provider}/{model_id}"

        # No conversion needed, return original
        return model

    async def _safe_streaming_wrapper(
        self,
        stream: Any,
        key: str,
        model: str,
        request: Optional[Any] = None,
        provider_plugin: Optional[Any] = None,
    ) -> AsyncGenerator[Any, None]:
        """
        A hybrid wrapper for streaming that buffers fragmented JSON, handles client disconnections gracefully,
        and distinguishes between content and streamed errors.

        FINISH_REASON HANDLING:
        Providers just translate chunks - this wrapper handles ALL finish_reason logic:
        1. Strip finish_reason from intermediate chunks (litellm defaults to "stop")
        2. Track accumulated_finish_reason with priority: tool_calls > length/content_filter > stop
        3. Only emit finish_reason on final chunk (detected by usage.completion_tokens > 0)
        """
        last_usage = None
        stream_completed = False
        stream_iterator = stream.__aiter__()
        json_buffer = ""
        accumulated_finish_reason = None  # Track strongest finish_reason across chunks
        has_tool_calls = False  # Track if ANY tool calls were seen in stream

        try:
            while True:
                if request and await request.is_disconnected():
                    lib_logger.info(
                        f"Client disconnected. Aborting stream for credential {mask_credential(key)}."
                    )
                    break

                try:
                    chunk = await stream_iterator.__anext__()
                    if json_buffer:
                        lib_logger.warning(
                            f"Discarding incomplete JSON buffer from previous chunk: {json_buffer}"
                        )
                        json_buffer = ""

                    # Convert chunk to dict, handling both litellm.ModelResponse and raw dicts
                    if hasattr(chunk, "dict"):
                        chunk_dict = chunk.dict()
                    elif hasattr(chunk, "model_dump"):
                        chunk_dict = chunk.model_dump()
                    else:
                        chunk_dict = chunk

                    # === FINISH_REASON LOGIC ===
                    # Providers send raw chunks without finish_reason logic.
                    # This wrapper determines finish_reason based on accumulated state.
                    if "choices" in chunk_dict and chunk_dict["choices"]:
                        choice = chunk_dict["choices"][0]
                        delta = choice.get("delta", {})
                        usage = chunk_dict.get("usage", {})

                        # Track tool_calls across ALL chunks - if we ever see one, finish_reason must be tool_calls
                        if delta.get("tool_calls"):
                            has_tool_calls = True
                            accumulated_finish_reason = "tool_calls"

                        # Detect final chunk: has usage with completion_tokens > 0
                        has_completion_tokens = (
                            usage
                            and isinstance(usage, dict)
                            and usage.get("completion_tokens", 0) > 0
                        )

                        if has_completion_tokens:
                            # FINAL CHUNK: Determine correct finish_reason
                            if has_tool_calls:
                                # Tool calls always win
                                choice["finish_reason"] = "tool_calls"
                            elif accumulated_finish_reason:
                                # Use accumulated reason (length, content_filter, etc.)
                                choice["finish_reason"] = accumulated_finish_reason
                            else:
                                # Default to stop
                                choice["finish_reason"] = "stop"
                        else:
                            # INTERMEDIATE CHUNK: Never emit finish_reason
                            # (litellm.ModelResponse defaults to "stop" which is wrong)
                            choice["finish_reason"] = None

                    yield f"data: {json.dumps(chunk_dict)}\n\n"

                    if hasattr(chunk, "usage") and chunk.usage:
                        last_usage = chunk.usage

                except StopAsyncIteration:
                    stream_completed = True
                    if json_buffer:
                        lib_logger.info(
                            f"Stream ended with incomplete data in buffer: {json_buffer}"
                        )
                    if last_usage:
                        # Create a dummy ModelResponse for recording (only usage matters)
                        dummy_response = litellm.ModelResponse(usage=last_usage)
                        await self.usage_manager.record_success(
                            key, model, dummy_response
                        )
                    else:
                        # If no usage seen (rare), record success without tokens/cost
                        await self.usage_manager.record_success(key, model)

                    break

                except CredentialNeedsReauthError as e:
                    # This credential needs re-authentication but re-auth is already queued.
                    # Wrap it so the outer retry loop can rotate to the next credential.
                    # No scary traceback needed - this is an expected recovery scenario.
                    raise StreamedAPIError("Credential needs re-authentication", data=e)

                except (
                    litellm.RateLimitError,
                    litellm.ServiceUnavailableError,
                    litellm.InternalServerError,
                    APIConnectionError,
                    httpx.HTTPStatusError,
                ) as e:
                    # This is a critical, typed error from litellm or httpx that signals a key failure.
                    # We do not try to parse it here. We wrap it and raise it immediately
                    # for the outer retry loop to handle.
                    lib_logger.warning(
                        f"Caught a critical API error mid-stream: {type(e).__name__}. Signaling for credential rotation."
                    )
                    raise StreamedAPIError("Provider error received in stream", data=e)

                except Exception as e:
                    try:
                        raw_chunk = ""
                        # Google streams errors inside a bytes representation (b'{...}').
                        # We use regex to extract the content, which is more reliable than splitting.
                        match = re.search(r"b'(\{.*\})'", str(e), re.DOTALL)
                        if match:
                            # The extracted string is unicode-escaped (e.g., '\\n'). We must decode it.
                            raw_chunk = codecs.decode(match.group(1), "unicode_escape")
                        else:
                            # Fallback for other potential error formats that use "Received chunk:".
                            chunk_from_split = (
                                str(e).split("Received chunk:")[-1].strip()
                            )
                            if chunk_from_split != str(
                                e
                            ):  # Ensure the split actually did something
                                raw_chunk = chunk_from_split

                        if not raw_chunk:
                            # If we could not extract a valid chunk, we cannot proceed with reassembly.
                            # This indicates a different, unexpected error type. Re-raise it.
                            raise e

                        # Append the clean chunk to the buffer and try to parse.
                        json_buffer += raw_chunk
                        parsed_data = json.loads(json_buffer)

                        # If parsing succeeds, we have the complete object.
                        lib_logger.info(
                            f"Successfully reassembled JSON from stream: {json_buffer}"
                        )

                        # Wrap the complete error object and raise it. The outer function will decide how to handle it.
                        raise StreamedAPIError(
                            "Provider error received in stream", data=parsed_data
                        )

                    except json.JSONDecodeError:
                        # This is the expected outcome if the JSON in the buffer is not yet complete.
                        lib_logger.info(
                            f"Buffer still incomplete. Waiting for more chunks: {json_buffer}"
                        )
                        continue  # Continue to the next loop to get the next chunk.
                    except StreamedAPIError:
                        # Re-raise to be caught by the outer retry handler.
                        raise
                    except Exception as buffer_exc:
                        # If the error was not a JSONDecodeError, it's an unexpected internal error.
                        lib_logger.error(
                            f"Error during stream buffering logic: {buffer_exc}. Discarding buffer."
                        )
                        json_buffer = (
                            ""  # Clear the corrupted buffer to prevent further issues.
                        )
                        raise buffer_exc

        except StreamedAPIError:
            # This is caught by the acompletion retry logic.
            # We re-raise it to ensure it's not caught by the generic 'except Exception'.
            raise

        except Exception as e:
            # Catch any other unexpected errors during streaming.
            lib_logger.error(f"Caught unexpected exception of type: {type(e).__name__}")
            lib_logger.error(
                f"An unexpected error occurred during the stream for credential {mask_credential(key)}: {e}"
            )
            # We still need to raise it so the client knows something went wrong.
            raise

        finally:
            # This block now runs regardless of how the stream terminates (completion, client disconnect, etc.).
            # The primary goal is to ensure usage is always logged internally.
            await self.usage_manager.release_key(key, model)
            lib_logger.info(
                f"STREAM FINISHED and lock released for credential {mask_credential(key)}."
            )

            # Only send [DONE] if the stream completed naturally and the client is still there.
            # This prevents sending [DONE] to a disconnected client or after an error.
            if stream_completed and (
                not request or not await request.is_disconnected()
            ):
                yield "data: [DONE]\n\n"

    async def _execute_with_retry(
        self,
        api_call: callable,
        request: Optional[Any],
        pre_request_callback: Optional[callable] = None,
        **kwargs,
    ) -> Any:
        """A generic retry mechanism for non-streaming API calls."""
        model = kwargs.get("model")
        if not model:
            raise ValueError("'model' is a required parameter.")

        provider = model.split("/")[0]
        if provider not in self.all_credentials:
            raise ValueError(
                f"No API keys or OAuth credentials configured for provider: {provider}"
            )

        # Establish a global deadline for the entire request lifecycle.
        deadline = time.time() + self.global_timeout

        # Create a mutable copy of the keys and shuffle it to ensure
        # that the key selection is randomized, which is crucial when
        # multiple keys have the same usage stats.
        credentials_for_provider = list(self.all_credentials[provider])
        random.shuffle(credentials_for_provider)

        # Filter out credentials that are unavailable (queued for re-auth)
        provider_plugin = self._get_provider_instance(provider)
        if provider_plugin and hasattr(provider_plugin, "is_credential_available"):
            available_creds = [
                cred
                for cred in credentials_for_provider
                if provider_plugin.is_credential_available(cred)
            ]
            if available_creds:
                credentials_for_provider = available_creds
            # If all credentials are unavailable, keep the original list
            # (better to try unavailable creds than fail immediately)

        tried_creds = set()
        last_exception = None
        kwargs = self._convert_model_params(**kwargs)

        # The main rotation loop. It continues as long as there are untried credentials and the global deadline has not been exceeded.

        # Resolve model ID early, before any credential operations
        # This ensures consistent model ID usage for acquisition, release, and tracking
        resolved_model = self._resolve_model_id(model, provider)
        if resolved_model != model:
            lib_logger.info(f"Resolved model '{model}' to '{resolved_model}'")
            model = resolved_model
            kwargs["model"] = model  # Ensure kwargs has the resolved model for litellm

        # [NEW] Filter by model tier requirement and build priority map
        credential_priorities = None
        if provider_plugin and hasattr(provider_plugin, "get_model_tier_requirement"):
            required_tier = provider_plugin.get_model_tier_requirement(model)
            if required_tier is not None:
                # Filter OUT only credentials we KNOW are too low priority
                # Keep credentials with unknown priority (None) - they might be high priority
                incompatible_creds = []
                compatible_creds = []
                unknown_creds = []

                for cred in credentials_for_provider:
                    if hasattr(provider_plugin, "get_credential_priority"):
                        priority = provider_plugin.get_credential_priority(cred)
                        if priority is None:
                            # Unknown priority - keep it, will be discovered on first use
                            unknown_creds.append(cred)
                        elif priority <= required_tier:
                            # Known compatible priority
                            compatible_creds.append(cred)
                        else:
                            # Known incompatible priority (too low)
                            incompatible_creds.append(cred)
                    else:
                        # Provider doesn't support priorities - keep all
                        unknown_creds.append(cred)

                # If we have any known-compatible or unknown credentials, use them
                tier_compatible_creds = compatible_creds + unknown_creds
                if tier_compatible_creds:
                    credentials_for_provider = tier_compatible_creds
                    if compatible_creds and unknown_creds:
                        lib_logger.info(
                            f"Model {model} requires priority <= {required_tier}. "
                            f"Using {len(compatible_creds)} known-compatible + {len(unknown_creds)} unknown-tier credentials."
                        )
                    elif compatible_creds:
                        lib_logger.info(
                            f"Model {model} requires priority <= {required_tier}. "
                            f"Using {len(compatible_creds)} known-compatible credentials."
                        )
                    else:
                        lib_logger.info(
                            f"Model {model} requires priority <= {required_tier}. "
                            f"Using {len(unknown_creds)} unknown-tier credentials (will discover on use)."
                        )
                elif incompatible_creds:
                    # Only known-incompatible credentials remain
                    lib_logger.warning(
                        f"Model {model} requires priority <= {required_tier} credentials, "
                        f"but all {len(incompatible_creds)} known credentials have priority > {required_tier}. "
                        f"Request will likely fail."
                    )

        # Build priority map and tier names map for usage_manager
        credential_tier_names = None
        if provider_plugin and hasattr(provider_plugin, "get_credential_priority"):
            credential_priorities = {}
            credential_tier_names = {}
            for cred in credentials_for_provider:
                priority = provider_plugin.get_credential_priority(cred)
                if priority is not None:
                    credential_priorities[cred] = priority
                # Also get tier name for logging
                if hasattr(provider_plugin, "get_credential_tier_name"):
                    tier_name = provider_plugin.get_credential_tier_name(cred)
                    if tier_name:
                        credential_tier_names[cred] = tier_name

            if credential_priorities:
                lib_logger.debug(
                    f"Credential priorities for {provider}: {', '.join(f'P{p}={len([c for c in credentials_for_provider if credential_priorities.get(c) == p])}' for p in sorted(set(credential_priorities.values())))}"
                )

        # Initialize error accumulator for tracking errors across credential rotation
        error_accumulator = RequestErrorAccumulator()
        error_accumulator.model = model
        error_accumulator.provider = provider

        while (
            len(tried_creds) < len(credentials_for_provider) and time.time() < deadline
        ):
            current_cred = None
            key_acquired = False
            try:
                # Check for a provider-wide cooldown first.
                if await self.cooldown_manager.is_cooling_down(provider):
                    remaining_cooldown = (
                        await self.cooldown_manager.get_cooldown_remaining(provider)
                    )
                    remaining_budget = deadline - time.time()

                    # If the cooldown is longer than the remaining time budget, fail fast.
                    if remaining_cooldown > remaining_budget:
                        lib_logger.warning(
                            f"Provider {provider} cooldown ({remaining_cooldown:.2f}s) exceeds remaining request budget ({remaining_budget:.2f}s). Failing early."
                        )
                        break

                    lib_logger.warning(
                        f"Provider {provider} is in cooldown. Waiting for {remaining_cooldown:.2f} seconds."
                    )
                    await asyncio.sleep(remaining_cooldown)

                creds_to_try = [
                    c for c in credentials_for_provider if c not in tried_creds
                ]
                if not creds_to_try:
                    break

                # Get count of credentials not on cooldown for this model
                available_creds = (
                    await self.usage_manager.get_available_credentials_for_model(
                        creds_to_try, model
                    )
                )
                available_count = len(available_creds)
                total_count = len(credentials_for_provider)

                lib_logger.info(
                    f"Acquiring key for model {model}. Tried keys: {len(tried_creds)}/{available_count}({total_count})"
                )
                max_concurrent = self.max_concurrent_requests_per_key.get(provider, 1)
                current_cred = await self.usage_manager.acquire_key(
                    available_keys=creds_to_try,
                    model=model,
                    deadline=deadline,
                    max_concurrent=max_concurrent,
                    credential_priorities=credential_priorities,
                    credential_tier_names=credential_tier_names,
                )
                key_acquired = True
                tried_creds.add(current_cred)

                litellm_kwargs = self.all_providers.get_provider_kwargs(**kwargs.copy())

                # [NEW] Merge provider-specific params
                if provider in self.litellm_provider_params:
                    litellm_kwargs["litellm_params"] = {
                        **self.litellm_provider_params[provider],
                        **litellm_kwargs.get("litellm_params", {}),
                    }

                provider_plugin = self._get_provider_instance(provider)

                # Model ID is already resolved before the loop, and kwargs['model'] is updated.
                # No further resolution needed here.

                # Apply model-specific options for custom providers
                if provider_plugin and hasattr(provider_plugin, "get_model_options"):
                    model_options = provider_plugin.get_model_options(model)
                    if model_options:
                        # Merge model options into litellm_kwargs
                        for key, value in model_options.items():
                            if key == "reasoning_effort":
                                litellm_kwargs["reasoning_effort"] = value
                            elif key not in litellm_kwargs:
                                litellm_kwargs[key] = value

                if provider_plugin and provider_plugin.has_custom_logic():
                    lib_logger.debug(
                        f"Provider '{provider}' has custom logic. Delegating call."
                    )
                    litellm_kwargs["credential_identifier"] = current_cred
                    litellm_kwargs["enable_request_logging"] = (
                        self.enable_request_logging
                    )

                    # Check body first for custom_reasoning_budget
                    if "custom_reasoning_budget" in kwargs:
                        litellm_kwargs["custom_reasoning_budget"] = kwargs[
                            "custom_reasoning_budget"
                        ]
                    else:
                        custom_budget_header = None
                        if request and hasattr(request, "headers"):
                            custom_budget_header = request.headers.get(
                                "custom_reasoning_budget"
                            )

                        if custom_budget_header is not None:
                            is_budget_enabled = custom_budget_header.lower() == "true"
                            litellm_kwargs["custom_reasoning_budget"] = (
                                is_budget_enabled
                            )

                    # Retry loop for custom providers - mirrors streaming path error handling
                    for attempt in range(self.max_retries):
                        try:
                            lib_logger.info(
                                f"Attempting call with credential {mask_credential(current_cred)} (Attempt {attempt + 1}/{self.max_retries})"
                            )

                            if pre_request_callback:
                                try:
                                    await pre_request_callback(request, litellm_kwargs)
                                except Exception as e:
                                    if self.abort_on_callback_error:
                                        raise PreRequestCallbackError(
                                            f"Pre-request callback failed: {e}"
                                        ) from e
                                    else:
                                        lib_logger.warning(
                                            f"Pre-request callback failed but abort_on_callback_error is False. Proceeding with request. Error: {e}"
                                        )

                            response = await provider_plugin.acompletion(
                                self.http_client, **litellm_kwargs
                            )

                            # For non-streaming, success is immediate
                            await self.usage_manager.record_success(
                                current_cred, model, response
                            )

                            await self.usage_manager.release_key(current_cred, model)
                            key_acquired = False
                            return response

                        except (
                            litellm.RateLimitError,
                            httpx.HTTPStatusError,
                        ) as e:
                            last_exception = e
                            classified_error = classify_error(e, provider=provider)
                            error_message = str(e).split("\n")[0]

                            log_failure(
                                api_key=current_cred,
                                model=model,
                                attempt=attempt + 1,
                                error=e,
                                request_headers=dict(request.headers)
                                if request
                                else {},
                            )

                            # Record in accumulator for client reporting
                            error_accumulator.record_error(
                                current_cred, classified_error, error_message
                            )

                            # Check if this error should trigger rotation
                            if not should_rotate_on_error(classified_error):
                                lib_logger.error(
                                    f"Non-recoverable error ({classified_error.error_type}) during custom provider call. Failing."
                                )
                                raise last_exception

                            # Handle rate limits with cooldown (exclude quota_exceeded)
                            if classified_error.error_type == "rate_limit":
                                cooldown_duration = classified_error.retry_after or 60
                                await self.cooldown_manager.start_cooldown(
                                    provider, cooldown_duration
                                )

                            await self.usage_manager.record_failure(
                                current_cred, model, classified_error
                            )
                            lib_logger.warning(
                                f"Cred {mask_credential(current_cred)} {classified_error.error_type} (HTTP {classified_error.status_code}). Rotating."
                            )
                            break  # Rotate to next credential

                        except (
                            APIConnectionError,
                            litellm.InternalServerError,
                            litellm.ServiceUnavailableError,
                        ) as e:
                            last_exception = e
                            log_failure(
                                api_key=current_cred,
                                model=model,
                                attempt=attempt + 1,
                                error=e,
                                request_headers=dict(request.headers)
                                if request
                                else {},
                            )
                            classified_error = classify_error(e, provider=provider)
                            error_message = str(e).split("\n")[0]

                            # Provider-level error: don't increment consecutive failures
                            await self.usage_manager.record_failure(
                                current_cred,
                                model,
                                classified_error,
                                increment_consecutive_failures=False,
                            )

                            if attempt >= self.max_retries - 1:
                                error_accumulator.record_error(
                                    current_cred, classified_error, error_message
                                )
                                lib_logger.warning(
                                    f"Cred {mask_credential(current_cred)} failed after max retries. Rotating."
                                )
                                break

                            wait_time = classified_error.retry_after or (
                                2**attempt
                            ) + random.uniform(0, 1)
                            remaining_budget = deadline - time.time()
                            if wait_time > remaining_budget:
                                error_accumulator.record_error(
                                    current_cred, classified_error, error_message
                                )
                                lib_logger.warning(
                                    f"Retry wait ({wait_time:.2f}s) exceeds budget. Rotating."
                                )
                                break

                            lib_logger.warning(
                                f"Cred {mask_credential(current_cred)} server error. Retrying in {wait_time:.2f}s."
                            )
                            await asyncio.sleep(wait_time)
                            continue

                        except Exception as e:
                            last_exception = e
                            log_failure(
                                api_key=current_cred,
                                model=model,
                                attempt=attempt + 1,
                                error=e,
                                request_headers=dict(request.headers)
                                if request
                                else {},
                            )
                            classified_error = classify_error(e, provider=provider)
                            error_message = str(e).split("\n")[0]

                            # Record in accumulator
                            error_accumulator.record_error(
                                current_cred, classified_error, error_message
                            )

                            lib_logger.warning(
                                f"Cred {mask_credential(current_cred)} {classified_error.error_type} (HTTP {classified_error.status_code})."
                            )

                            # Check if this error should trigger rotation
                            if not should_rotate_on_error(classified_error):
                                lib_logger.error(
                                    f"Non-recoverable error ({classified_error.error_type}). Failing."
                                )
                                raise last_exception

                            # Handle rate limits with cooldown (exclude quota_exceeded)
                            if (
                                classified_error.status_code == 429
                                and classified_error.error_type != "quota_exceeded"
                            ) or classified_error.error_type == "rate_limit":
                                cooldown_duration = classified_error.retry_after or 60
                                await self.cooldown_manager.start_cooldown(
                                    provider, cooldown_duration
                                )

                            await self.usage_manager.record_failure(
                                current_cred, model, classified_error
                            )
                            break  # Rotate to next credential

                    # If the inner loop breaks, it means the key failed and we need to rotate.
                    # Continue to the next iteration of the outer while loop to pick a new key.
                    continue

                else:  # This is the standard API Key / litellm-handled provider logic
                    is_oauth = provider in self.oauth_providers
                    if is_oauth:  # Standard OAuth provider (not custom)
                        # ... (logic to set headers) ...
                        pass
                    else:  # API Key
                        litellm_kwargs["api_key"] = current_cred

                    provider_instance = self._get_provider_instance(provider)
                    if provider_instance:
                        # Ensure default Gemini safety settings are present (without overriding request)
                        try:
                            self._apply_default_safety_settings(
                                litellm_kwargs, provider
                            )
                        except Exception:
                            # If anything goes wrong here, avoid breaking the request flow.
                            lib_logger.debug(
                                "Could not apply default safety settings; continuing."
                            )

                        if "safety_settings" in litellm_kwargs:
                            converted_settings = (
                                provider_instance.convert_safety_settings(
                                    litellm_kwargs["safety_settings"]
                                )
                            )
                            if converted_settings is not None:
                                litellm_kwargs["safety_settings"] = converted_settings
                            else:
                                del litellm_kwargs["safety_settings"]

                    if provider == "gemini" and provider_instance:
                        provider_instance.handle_thinking_parameter(
                            litellm_kwargs, model
                        )
                    if provider == "nvidia_nim" and provider_instance:
                        provider_instance.handle_thinking_parameter(
                            litellm_kwargs, model
                        )

                    if "gemma-3" in model and "messages" in litellm_kwargs:
                        litellm_kwargs["messages"] = [
                            {"role": "user", "content": m["content"]}
                            if m.get("role") == "system"
                            else m
                            for m in litellm_kwargs["messages"]
                        ]

                    litellm_kwargs = sanitize_request_payload(litellm_kwargs, model)

                    for attempt in range(self.max_retries):
                        try:
                            lib_logger.info(
                                f"Attempting call with credential {mask_credential(current_cred)} (Attempt {attempt + 1}/{self.max_retries})"
                            )

                            if pre_request_callback:
                                try:
                                    await pre_request_callback(request, litellm_kwargs)
                                except Exception as e:
                                    if self.abort_on_callback_error:
                                        raise PreRequestCallbackError(
                                            f"Pre-request callback failed: {e}"
                                        ) from e
                                    else:
                                        lib_logger.warning(
                                            f"Pre-request callback failed but abort_on_callback_error is False. Proceeding with request. Error: {e}"
                                        )

                            # Convert model parameters for custom providers right before LiteLLM call
                            final_kwargs = self._convert_model_params_for_litellm(
                                **litellm_kwargs
                            )

                            response = await api_call(
                                **final_kwargs,
                                logger_fn=self._litellm_logger_callback,
                            )

                            await self.usage_manager.record_success(
                                current_cred, model, response
                            )

                            await self.usage_manager.release_key(current_cred, model)
                            key_acquired = False
                            return response

                        except litellm.RateLimitError as e:
                            last_exception = e
                            log_failure(
                                api_key=current_cred,
                                model=model,
                                attempt=attempt + 1,
                                error=e,
                                request_headers=dict(request.headers)
                                if request
                                else {},
                            )
                            classified_error = classify_error(e, provider=provider)

                            # Extract a clean error message for the user-facing log
                            error_message = str(e).split("\n")[0]

                            # Record in accumulator for client reporting
                            error_accumulator.record_error(
                                current_cred, classified_error, error_message
                            )

                            lib_logger.info(
                                f"Key {mask_credential(current_cred)} hit rate limit for {model}. Rotating key."
                            )

                            # Only trigger provider-wide cooldown for rate limits, not quota issues
                            if (
                                classified_error.status_code == 429
                                and classified_error.error_type != "quota_exceeded"
                            ):
                                cooldown_duration = classified_error.retry_after or 60
                                await self.cooldown_manager.start_cooldown(
                                    provider, cooldown_duration
                                )

                            await self.usage_manager.record_failure(
                                current_cred, model, classified_error
                            )
                            break  # Move to the next key

                        except (
                            APIConnectionError,
                            litellm.InternalServerError,
                            litellm.ServiceUnavailableError,
                        ) as e:
                            last_exception = e
                            log_failure(
                                api_key=current_cred,
                                model=model,
                                attempt=attempt + 1,
                                error=e,
                                request_headers=dict(request.headers)
                                if request
                                else {},
                            )
                            classified_error = classify_error(e, provider=provider)
                            error_message = str(e).split("\n")[0]

                            # Provider-level error: don't increment consecutive failures
                            await self.usage_manager.record_failure(
                                current_cred,
                                model,
                                classified_error,
                                increment_consecutive_failures=False,
                            )

                            if attempt >= self.max_retries - 1:
                                # Record in accumulator only on final failure for this key
                                error_accumulator.record_error(
                                    current_cred, classified_error, error_message
                                )
                                lib_logger.warning(
                                    f"Key {mask_credential(current_cred)} failed after max retries due to server error. Rotating."
                                )
                                break  # Move to the next key

                            # For temporary errors, wait before retrying with the same key.
                            wait_time = classified_error.retry_after or (
                                2**attempt
                            ) + random.uniform(0, 1)
                            remaining_budget = deadline - time.time()

                            # If the required wait time exceeds the budget, don't wait; rotate to the next key immediately.
                            if wait_time > remaining_budget:
                                error_accumulator.record_error(
                                    current_cred, classified_error, error_message
                                )
                                lib_logger.warning(
                                    f"Retry wait ({wait_time:.2f}s) exceeds budget ({remaining_budget:.2f}s). Rotating key."
                                )
                                break

                            lib_logger.warning(
                                f"Key {mask_credential(current_cred)} server error. Retrying in {wait_time:.2f}s."
                            )
                            await asyncio.sleep(wait_time)
                            continue  # Retry with the same key

                        except httpx.HTTPStatusError as e:
                            # Handle HTTP errors from httpx (e.g., from custom providers like Antigravity)
                            last_exception = e
                            log_failure(
                                api_key=current_cred,
                                model=model,
                                attempt=attempt + 1,
                                error=e,
                                request_headers=dict(request.headers)
                                if request
                                else {},
                            )

                            classified_error = classify_error(e, provider=provider)
                            error_message = str(e).split("\n")[0]

                            lib_logger.warning(
                                f"Key {mask_credential(current_cred)} HTTP {e.response.status_code} ({classified_error.error_type})."
                            )

                            # Check if this error should trigger rotation
                            if not should_rotate_on_error(classified_error):
                                lib_logger.error(
                                    f"Non-recoverable error ({classified_error.error_type}). Failing request."
                                )
                                raise last_exception

                            # Record in accumulator after confirming it's a rotatable error
                            error_accumulator.record_error(
                                current_cred, classified_error, error_message
                            )

                            # Handle rate limits with cooldown (exclude quota_exceeded from provider-wide cooldown)
                            if classified_error.error_type == "rate_limit":
                                cooldown_duration = classified_error.retry_after or 60
                                await self.cooldown_manager.start_cooldown(
                                    provider, cooldown_duration
                                )

                            # Check if we should retry same key (server errors with retries left)
                            if (
                                should_retry_same_key(classified_error)
                                and attempt < self.max_retries - 1
                            ):
                                wait_time = classified_error.retry_after or (
                                    2**attempt
                                ) + random.uniform(0, 1)
                                remaining_budget = deadline - time.time()
                                if wait_time <= remaining_budget:
                                    lib_logger.warning(
                                        f"Server error, retrying same key in {wait_time:.2f}s."
                                    )
                                    await asyncio.sleep(wait_time)
                                    continue

                            # Record failure and rotate to next key
                            await self.usage_manager.record_failure(
                                current_cred, model, classified_error
                            )
                            lib_logger.info(
                                f"Rotating to next key after {classified_error.error_type} error."
                            )
                            break

                        except Exception as e:
                            last_exception = e
                            log_failure(
                                api_key=current_cred,
                                model=model,
                                attempt=attempt + 1,
                                error=e,
                                request_headers=dict(request.headers)
                                if request
                                else {},
                            )

                            if request and await request.is_disconnected():
                                lib_logger.warning(
                                    f"Client disconnected. Aborting retries for {mask_credential(current_cred)}."
                                )
                                raise last_exception

                            classified_error = classify_error(e, provider=provider)
                            error_message = str(e).split("\n")[0]

                            lib_logger.warning(
                                f"Key {mask_credential(current_cred)} {classified_error.error_type} (HTTP {classified_error.status_code})."
                            )

                            # Handle rate limits with cooldown (exclude quota_exceeded from provider-wide cooldown)
                            if (
                                classified_error.status_code == 429
                                and classified_error.error_type != "quota_exceeded"
                            ) or classified_error.error_type == "rate_limit":
                                cooldown_duration = classified_error.retry_after or 60
                                await self.cooldown_manager.start_cooldown(
                                    provider, cooldown_duration
                                )

                            # Check if this error should trigger rotation
                            if not should_rotate_on_error(classified_error):
                                lib_logger.error(
                                    f"Non-recoverable error ({classified_error.error_type}). Failing request."
                                )
                                raise last_exception

                            # Record in accumulator after confirming it's a rotatable error
                            error_accumulator.record_error(
                                current_cred, classified_error, error_message
                            )

                            await self.usage_manager.record_failure(
                                current_cred, model, classified_error
                            )
                            break  # Try next key for other errors
            finally:
                if key_acquired and current_cred:
                    await self.usage_manager.release_key(current_cred, model)

        # Check if we exhausted all credentials or timed out
        if time.time() >= deadline:
            error_accumulator.timeout_occurred = True

        if error_accumulator.has_errors():
            # Log concise summary for server logs
            lib_logger.error(error_accumulator.build_log_message())

            # Return the structured error response for the client
            return error_accumulator.build_client_error_response()

        # Return None to indicate failure without error details (shouldn't normally happen)
        lib_logger.warning(
            "Unexpected state: request failed with no recorded errors. "
            "This may indicate a logic error in error tracking."
        )
        return None

    async def _streaming_acompletion_with_retry(
        self,
        request: Optional[Any],
        pre_request_callback: Optional[callable] = None,
        **kwargs,
    ) -> AsyncGenerator[str, None]:
        """A dedicated generator for retrying streaming completions with full request preparation and per-key retries."""
        model = kwargs.get("model")
        provider = model.split("/")[0]

        # Create a mutable copy of the keys and shuffle it.
        credentials_for_provider = list(self.all_credentials[provider])
        random.shuffle(credentials_for_provider)

        # Filter out credentials that are unavailable (queued for re-auth)
        provider_plugin = self._get_provider_instance(provider)
        if provider_plugin and hasattr(provider_plugin, "is_credential_available"):
            available_creds = [
                cred
                for cred in credentials_for_provider
                if provider_plugin.is_credential_available(cred)
            ]
            if available_creds:
                credentials_for_provider = available_creds
            # If all credentials are unavailable, keep the original list
            # (better to try unavailable creds than fail immediately)

        deadline = time.time() + self.global_timeout
        tried_creds = set()
        last_exception = None
        kwargs = self._convert_model_params(**kwargs)

        consecutive_quota_failures = 0

        # Resolve model ID early, before any credential operations
        # This ensures consistent model ID usage for acquisition, release, and tracking
        resolved_model = self._resolve_model_id(model, provider)
        if resolved_model != model:
            lib_logger.info(f"Resolved model '{model}' to '{resolved_model}'")
            model = resolved_model
            kwargs["model"] = model  # Ensure kwargs has the resolved model for litellm

        # [NEW] Filter by model tier requirement and build priority map
        credential_priorities = None
        if provider_plugin and hasattr(provider_plugin, "get_model_tier_requirement"):
            required_tier = provider_plugin.get_model_tier_requirement(model)
            if required_tier is not None:
                # Filter OUT only credentials we KNOW are too low priority
                # Keep credentials with unknown priority (None) - they might be high priority
                incompatible_creds = []
                compatible_creds = []
                unknown_creds = []

                for cred in credentials_for_provider:
                    if hasattr(provider_plugin, "get_credential_priority"):
                        priority = provider_plugin.get_credential_priority(cred)
                        if priority is None:
                            # Unknown priority - keep it, will be discovered on first use
                            unknown_creds.append(cred)
                        elif priority <= required_tier:
                            # Known compatible priority
                            compatible_creds.append(cred)
                        else:
                            # Known incompatible priority (too low)
                            incompatible_creds.append(cred)
                    else:
                        # Provider doesn't support priorities - keep all
                        unknown_creds.append(cred)

                # If we have any known-compatible or unknown credentials, use them
                tier_compatible_creds = compatible_creds + unknown_creds
                if tier_compatible_creds:
                    credentials_for_provider = tier_compatible_creds
                    if compatible_creds and unknown_creds:
                        lib_logger.info(
                            f"Model {model} requires priority <= {required_tier}. "
                            f"Using {len(compatible_creds)} known-compatible + {len(unknown_creds)} unknown-tier credentials."
                        )
                    elif compatible_creds:
                        lib_logger.info(
                            f"Model {model} requires priority <= {required_tier}. "
                            f"Using {len(compatible_creds)} known-compatible credentials."
                        )
                    else:
                        lib_logger.info(
                            f"Model {model} requires priority <= {required_tier}. "
                            f"Using {len(unknown_creds)} unknown-tier credentials (will discover on use)."
                        )
                elif incompatible_creds:
                    # Only known-incompatible credentials remain
                    lib_logger.warning(
                        f"Model {model} requires priority <= {required_tier} credentials, "
                        f"but all {len(incompatible_creds)} known credentials have priority > {required_tier}. "
                        f"Request will likely fail."
                    )

        # Build priority map and tier names map for usage_manager
        credential_tier_names = None
        if provider_plugin and hasattr(provider_plugin, "get_credential_priority"):
            credential_priorities = {}
            credential_tier_names = {}
            for cred in credentials_for_provider:
                priority = provider_plugin.get_credential_priority(cred)
                if priority is not None:
                    credential_priorities[cred] = priority
                # Also get tier name for logging
                if hasattr(provider_plugin, "get_credential_tier_name"):
                    tier_name = provider_plugin.get_credential_tier_name(cred)
                    if tier_name:
                        credential_tier_names[cred] = tier_name

            if credential_priorities:
                lib_logger.debug(
                    f"Credential priorities for {provider}: {', '.join(f'P{p}={len([c for c in credentials_for_provider if credential_priorities.get(c) == p])}' for p in sorted(set(credential_priorities.values())))}"
                )

        # Initialize error accumulator for tracking errors across credential rotation
        error_accumulator = RequestErrorAccumulator()
        error_accumulator.model = model
        error_accumulator.provider = provider

        try:
            while (
                len(tried_creds) < len(credentials_for_provider)
                and time.time() < deadline
            ):
                current_cred = None
                key_acquired = False
                try:
                    if await self.cooldown_manager.is_cooling_down(provider):
                        remaining_cooldown = (
                            await self.cooldown_manager.get_cooldown_remaining(provider)
                        )
                        remaining_budget = deadline - time.time()
                        if remaining_cooldown > remaining_budget:
                            lib_logger.warning(
                                f"Provider {provider} cooldown ({remaining_cooldown:.2f}s) exceeds remaining request budget ({remaining_budget:.2f}s). Failing early."
                            )
                            break
                        lib_logger.warning(
                            f"Provider {provider} is in a global cooldown. All requests to this provider will be paused for {remaining_cooldown:.2f} seconds."
                        )
                        await asyncio.sleep(remaining_cooldown)

                    creds_to_try = [
                        c for c in credentials_for_provider if c not in tried_creds
                    ]
                    if not creds_to_try:
                        lib_logger.warning(
                            f"All credentials for provider {provider} have been tried. No more credentials to rotate to."
                        )
                        break

                    # Get count of credentials not on cooldown for this model
                    available_creds = (
                        await self.usage_manager.get_available_credentials_for_model(
                            creds_to_try, model
                        )
                    )
                    available_count = len(available_creds)
                    total_count = len(credentials_for_provider)

                    lib_logger.info(
                        f"Acquiring credential for model {model}. Tried credentials: {len(tried_creds)}/{available_count}({total_count})"
                    )
                    max_concurrent = self.max_concurrent_requests_per_key.get(
                        provider, 1
                    )
                    current_cred = await self.usage_manager.acquire_key(
                        available_keys=creds_to_try,
                        model=model,
                        deadline=deadline,
                        max_concurrent=max_concurrent,
                        credential_priorities=credential_priorities,
                        credential_tier_names=credential_tier_names,
                    )
                    key_acquired = True
                    tried_creds.add(current_cred)

                    litellm_kwargs = self.all_providers.get_provider_kwargs(
                        **kwargs.copy()
                    )
                    if "reasoning_effort" in kwargs:
                        litellm_kwargs["reasoning_effort"] = kwargs["reasoning_effort"]
                    # Check body first for custom_reasoning_budget
                    if "custom_reasoning_budget" in kwargs:
                        litellm_kwargs["custom_reasoning_budget"] = kwargs[
                            "custom_reasoning_budget"
                        ]
                    else:
                        custom_budget_header = None
                        if request and hasattr(request, "headers"):
                            custom_budget_header = request.headers.get(
                                "custom_reasoning_budget"
                            )

                        if custom_budget_header is not None:
                            is_budget_enabled = custom_budget_header.lower() == "true"
                            litellm_kwargs["custom_reasoning_budget"] = (
                                is_budget_enabled
                            )

                    # [NEW] Merge provider-specific params
                    if provider in self.litellm_provider_params:
                        litellm_kwargs["litellm_params"] = {
                            **self.litellm_provider_params[provider],
                            **litellm_kwargs.get("litellm_params", {}),
                        }

                    provider_plugin = self._get_provider_instance(provider)

                    # Model ID is already resolved before the loop, and kwargs['model'] is updated.
                    # No further resolution needed here.

                    # Apply model-specific options for custom providers
                    if provider_plugin and hasattr(
                        provider_plugin, "get_model_options"
                    ):
                        model_options = provider_plugin.get_model_options(model)
                        if model_options:
                            # Merge model options into litellm_kwargs
                            for key, value in model_options.items():
                                if key == "reasoning_effort":
                                    litellm_kwargs["reasoning_effort"] = value
                                elif key not in litellm_kwargs:
                                    litellm_kwargs[key] = value
                    if provider_plugin and provider_plugin.has_custom_logic():
                        lib_logger.debug(
                            f"Provider '{provider}' has custom logic. Delegating call."
                        )
                        litellm_kwargs["credential_identifier"] = current_cred
                        litellm_kwargs["enable_request_logging"] = (
                            self.enable_request_logging
                        )

                        for attempt in range(self.max_retries):
                            try:
                                lib_logger.info(
                                    f"Attempting stream with credential {mask_credential(current_cred)} (Attempt {attempt + 1}/{self.max_retries})"
                                )

                                if pre_request_callback:
                                    try:
                                        await pre_request_callback(
                                            request, litellm_kwargs
                                        )
                                    except Exception as e:
                                        if self.abort_on_callback_error:
                                            raise PreRequestCallbackError(
                                                f"Pre-request callback failed: {e}"
                                            ) from e
                                        else:
                                            lib_logger.warning(
                                                f"Pre-request callback failed but abort_on_callback_error is False. Proceeding with request. Error: {e}"
                                            )

                                response = await provider_plugin.acompletion(
                                    self.http_client, **litellm_kwargs
                                )

                                lib_logger.info(
                                    f"Stream connection established for credential {mask_credential(current_cred)}. Processing response."
                                )

                                key_acquired = False
                                stream_generator = self._safe_streaming_wrapper(
                                    response,
                                    current_cred,
                                    model,
                                    request,
                                    provider_plugin,
                                )

                                async for chunk in stream_generator:
                                    yield chunk
                                return

                            except (
                                StreamedAPIError,
                                litellm.RateLimitError,
                                httpx.HTTPStatusError,
                            ) as e:
                                last_exception = e
                                # If the exception is our custom wrapper, unwrap the original error
                                original_exc = getattr(e, "data", e)
                                classified_error = classify_error(
                                    original_exc, provider=provider
                                )
                                error_message = str(original_exc).split("\n")[0]

                                log_failure(
                                    api_key=current_cred,
                                    model=model,
                                    attempt=attempt + 1,
                                    error=e,
                                    request_headers=dict(request.headers)
                                    if request
                                    else {},
                                )

                                # Record in accumulator for client reporting
                                error_accumulator.record_error(
                                    current_cred, classified_error, error_message
                                )

                                # Check if this error should trigger rotation
                                if not should_rotate_on_error(classified_error):
                                    lib_logger.error(
                                        f"Non-recoverable error ({classified_error.error_type}) during custom stream. Failing."
                                    )
                                    raise last_exception

                                # Handle rate limits with cooldown (exclude quota_exceeded)
                                if classified_error.error_type == "rate_limit":
                                    cooldown_duration = (
                                        classified_error.retry_after or 60
                                    )
                                    await self.cooldown_manager.start_cooldown(
                                        provider, cooldown_duration
                                    )

                                await self.usage_manager.record_failure(
                                    current_cred, model, classified_error
                                )
                                lib_logger.warning(
                                    f"Cred {mask_credential(current_cred)} {classified_error.error_type} (HTTP {classified_error.status_code}). Rotating."
                                )
                                break

                            except (
                                APIConnectionError,
                                litellm.InternalServerError,
                                litellm.ServiceUnavailableError,
                            ) as e:
                                last_exception = e
                                log_failure(
                                    api_key=current_cred,
                                    model=model,
                                    attempt=attempt + 1,
                                    error=e,
                                    request_headers=dict(request.headers)
                                    if request
                                    else {},
                                )
                                classified_error = classify_error(e, provider=provider)
                                error_message = str(e).split("\n")[0]

                                # Provider-level error: don't increment consecutive failures
                                await self.usage_manager.record_failure(
                                    current_cred,
                                    model,
                                    classified_error,
                                    increment_consecutive_failures=False,
                                )

                                if attempt >= self.max_retries - 1:
                                    error_accumulator.record_error(
                                        current_cred, classified_error, error_message
                                    )
                                    lib_logger.warning(
                                        f"Cred {mask_credential(current_cred)} failed after max retries. Rotating."
                                    )
                                    break

                                wait_time = classified_error.retry_after or (
                                    2**attempt
                                ) + random.uniform(0, 1)
                                remaining_budget = deadline - time.time()
                                if wait_time > remaining_budget:
                                    error_accumulator.record_error(
                                        current_cred, classified_error, error_message
                                    )
                                    lib_logger.warning(
                                        f"Retry wait ({wait_time:.2f}s) exceeds budget. Rotating."
                                    )
                                    break

                                lib_logger.warning(
                                    f"Cred {mask_credential(current_cred)} server error. Retrying in {wait_time:.2f}s."
                                )
                                await asyncio.sleep(wait_time)
                                continue

                            except Exception as e:
                                last_exception = e
                                log_failure(
                                    api_key=current_cred,
                                    model=model,
                                    attempt=attempt + 1,
                                    error=e,
                                    request_headers=dict(request.headers)
                                    if request
                                    else {},
                                )
                                classified_error = classify_error(e, provider=provider)
                                error_message = str(e).split("\n")[0]

                                # Record in accumulator
                                error_accumulator.record_error(
                                    current_cred, classified_error, error_message
                                )

                                lib_logger.warning(
                                    f"Cred {mask_credential(current_cred)} {classified_error.error_type} (HTTP {classified_error.status_code})."
                                )

                                # Check if this error should trigger rotation
                                if not should_rotate_on_error(classified_error):
                                    lib_logger.error(
                                        f"Non-recoverable error ({classified_error.error_type}). Failing."
                                    )
                                    raise last_exception

                                await self.usage_manager.record_failure(
                                    current_cred, model, classified_error
                                )
                                break

                        # If the inner loop breaks, it means the key failed and we need to rotate.
                        # Continue to the next iteration of the outer while loop to pick a new key.
                        continue

                    else:  # This is the standard API Key / litellm-handled provider logic
                        is_oauth = provider in self.oauth_providers
                        if is_oauth:  # Standard OAuth provider (not custom)
                            # ... (logic to set headers) ...
                            pass
                        else:  # API Key
                            litellm_kwargs["api_key"] = current_cred

                    provider_instance = self._get_provider_instance(provider)
                    if provider_instance:
                        # Ensure default Gemini safety settings are present (without overriding request)
                        try:
                            self._apply_default_safety_settings(
                                litellm_kwargs, provider
                            )
                        except Exception:
                            lib_logger.debug(
                                "Could not apply default safety settings for streaming path; continuing."
                            )

                        if "safety_settings" in litellm_kwargs:
                            converted_settings = (
                                provider_instance.convert_safety_settings(
                                    litellm_kwargs["safety_settings"]
                                )
                            )
                            if converted_settings is not None:
                                litellm_kwargs["safety_settings"] = converted_settings
                            else:
                                del litellm_kwargs["safety_settings"]

                    if provider == "gemini" and provider_instance:
                        provider_instance.handle_thinking_parameter(
                            litellm_kwargs, model
                        )
                    if provider == "nvidia_nim" and provider_instance:
                        provider_instance.handle_thinking_parameter(
                            litellm_kwargs, model
                        )

                    if "gemma-3" in model and "messages" in litellm_kwargs:
                        litellm_kwargs["messages"] = [
                            {"role": "user", "content": m["content"]}
                            if m.get("role") == "system"
                            else m
                            for m in litellm_kwargs["messages"]
                        ]

                    litellm_kwargs = sanitize_request_payload(litellm_kwargs, model)

                    # If the provider is 'qwen_code', set the custom provider to 'qwen'
                    # and strip the prefix from the model name for LiteLLM.
                    if provider == "qwen_code":
                        litellm_kwargs["custom_llm_provider"] = "qwen"
                        litellm_kwargs["model"] = model.split("/", 1)[1]

                    for attempt in range(self.max_retries):
                        try:
                            lib_logger.info(
                                f"Attempting stream with credential {mask_credential(current_cred)} (Attempt {attempt + 1}/{self.max_retries})"
                            )

                            if pre_request_callback:
                                try:
                                    await pre_request_callback(request, litellm_kwargs)
                                except Exception as e:
                                    if self.abort_on_callback_error:
                                        raise PreRequestCallbackError(
                                            f"Pre-request callback failed: {e}"
                                        ) from e
                                    else:
                                        lib_logger.warning(
                                            f"Pre-request callback failed but abort_on_callback_error is False. Proceeding with request. Error: {e}"
                                        )

                            # lib_logger.info(f"DEBUG: litellm.acompletion kwargs: {litellm_kwargs}")
                            # Convert model parameters for custom providers right before LiteLLM call
                            final_kwargs = self._convert_model_params_for_litellm(
                                **litellm_kwargs
                            )

                            response = await litellm.acompletion(
                                **final_kwargs,
                                logger_fn=self._litellm_logger_callback,
                            )

                            lib_logger.info(
                                f"Stream connection established for credential {mask_credential(current_cred)}. Processing response."
                            )

                            key_acquired = False
                            stream_generator = self._safe_streaming_wrapper(
                                response,
                                current_cred,
                                model,
                                request,
                                provider_instance,
                            )

                            async for chunk in stream_generator:
                                yield chunk
                            return

                        except (
                            StreamedAPIError,
                            litellm.RateLimitError,
                            httpx.HTTPStatusError,
                        ) as e:
                            last_exception = e

                            # This is the final, robust handler for streamed errors.
                            error_payload = {}
                            cleaned_str = None
                            # The actual exception might be wrapped in our StreamedAPIError.
                            original_exc = getattr(e, "data", e)
                            classified_error = classify_error(
                                original_exc, provider=provider
                            )

                            # Check if this error should trigger rotation
                            if not should_rotate_on_error(classified_error):
                                lib_logger.error(
                                    f"Non-recoverable error ({classified_error.error_type}) during litellm stream. Failing."
                                )
                                raise last_exception

                            try:
                                # The full error JSON is in the string representation of the exception.
                                json_str_match = re.search(
                                    r"(\{.*\})", str(original_exc), re.DOTALL
                                )
                                if json_str_match:
                                    cleaned_str = codecs.decode(
                                        json_str_match.group(1), "unicode_escape"
                                    )
                                    error_payload = json.loads(cleaned_str)
                            except (json.JSONDecodeError, TypeError):
                                error_payload = {}

                            log_failure(
                                api_key=current_cred,
                                model=model,
                                attempt=attempt + 1,
                                error=e,
                                request_headers=dict(request.headers)
                                if request
                                else {},
                                raw_response_text=cleaned_str,
                            )

                            error_details = error_payload.get("error", {})
                            error_status = error_details.get("status", "")
                            error_message_text = error_details.get(
                                "message", str(original_exc).split("\n")[0]
                            )

                            # Record in accumulator for client reporting
                            error_accumulator.record_error(
                                current_cred, classified_error, error_message_text
                            )

                            if (
                                "quota" in error_message_text.lower()
                                or "resource_exhausted" in error_status.lower()
                            ):
                                consecutive_quota_failures += 1

                                quota_value = "N/A"
                                quota_id = "N/A"
                                if "details" in error_details and isinstance(
                                    error_details.get("details"), list
                                ):
                                    for detail in error_details["details"]:
                                        if isinstance(detail.get("violations"), list):
                                            for violation in detail["violations"]:
                                                if "quotaValue" in violation:
                                                    quota_value = violation[
                                                        "quotaValue"
                                                    ]
                                                if "quotaId" in violation:
                                                    quota_id = violation["quotaId"]
                                                if (
                                                    quota_value != "N/A"
                                                    and quota_id != "N/A"
                                                ):
                                                    break

                                await self.usage_manager.record_failure(
                                    current_cred, model, classified_error
                                )

                                if consecutive_quota_failures >= 3:
                                    # Fatal: likely input data too large
                                    client_error_message = (
                                        f"Request failed after 3 consecutive quota errors (input may be too large). "
                                        f"Limit: {quota_value} (Quota ID: {quota_id})"
                                    )
                                    lib_logger.error(
                                        f"Fatal quota error for {mask_credential(current_cred)}. ID: {quota_id}, Limit: {quota_value}"
                                    )
                                    yield f"data: {json.dumps({'error': {'message': client_error_message, 'type': 'proxy_fatal_quota_error'}})}\n\n"
                                    yield "data: [DONE]\n\n"
                                    return
                                else:
                                    lib_logger.warning(
                                        f"Cred {mask_credential(current_cred)} quota error ({consecutive_quota_failures}/3). Rotating."
                                    )
                                    break

                            else:
                                consecutive_quota_failures = 0
                                lib_logger.warning(
                                    f"Cred {mask_credential(current_cred)} {classified_error.error_type}. Rotating."
                                )

                                if classified_error.error_type == "rate_limit":
                                    cooldown_duration = (
                                        classified_error.retry_after or 60
                                    )
                                    await self.cooldown_manager.start_cooldown(
                                        provider, cooldown_duration
                                    )

                                await self.usage_manager.record_failure(
                                    current_cred, model, classified_error
                                )
                                break

                        except (
                            APIConnectionError,
                            litellm.InternalServerError,
                            litellm.ServiceUnavailableError,
                        ) as e:
                            consecutive_quota_failures = 0
                            last_exception = e
                            log_failure(
                                api_key=current_cred,
                                model=model,
                                attempt=attempt + 1,
                                error=e,
                                request_headers=dict(request.headers)
                                if request
                                else {},
                            )
                            classified_error = classify_error(e, provider=provider)
                            error_message_text = str(e).split("\n")[0]

                            # Record error in accumulator (server errors are transient, not abnormal)
                            error_accumulator.record_error(
                                current_cred, classified_error, error_message_text
                            )

                            # Provider-level error: don't increment consecutive failures
                            await self.usage_manager.record_failure(
                                current_cred,
                                model,
                                classified_error,
                                increment_consecutive_failures=False,
                            )

                            if attempt >= self.max_retries - 1:
                                lib_logger.warning(
                                    f"Credential {mask_credential(current_cred)} failed after max retries for model {model} due to a server error. Rotating key silently."
                                )
                                # [MODIFIED] Do not yield to the client here.
                                break

                            wait_time = classified_error.retry_after or (
                                2**attempt
                            ) + random.uniform(0, 1)
                            remaining_budget = deadline - time.time()
                            if wait_time > remaining_budget:
                                lib_logger.warning(
                                    f"Required retry wait time ({wait_time:.2f}s) exceeds remaining budget ({remaining_budget:.2f}s). Rotating key early."
                                )
                                break

                            lib_logger.warning(
                                f"Credential {mask_credential(current_cred)} encountered a server error for model {model}. Reason: '{error_message_text}'. Retrying in {wait_time:.2f}s."
                            )
                            await asyncio.sleep(wait_time)
                            continue

                        except Exception as e:
                            consecutive_quota_failures = 0
                            last_exception = e
                            log_failure(
                                api_key=current_cred,
                                model=model,
                                attempt=attempt + 1,
                                error=e,
                                request_headers=dict(request.headers)
                                if request
                                else {},
                            )
                            classified_error = classify_error(e, provider=provider)
                            error_message_text = str(e).split("\n")[0]

                            # Record error in accumulator
                            error_accumulator.record_error(
                                current_cred, classified_error, error_message_text
                            )

                            lib_logger.warning(
                                f"Credential {mask_credential(current_cred)} failed with {classified_error.error_type} (Status: {classified_error.status_code}). Error: {error_message_text}."
                            )

                            # Handle rate limits with cooldown (exclude quota_exceeded)
                            if (
                                classified_error.status_code == 429
                                and classified_error.error_type != "quota_exceeded"
                            ) or classified_error.error_type == "rate_limit":
                                cooldown_duration = classified_error.retry_after or 60
                                await self.cooldown_manager.start_cooldown(
                                    provider, cooldown_duration
                                )
                                lib_logger.warning(
                                    f"Rate limit detected for {provider}. Starting {cooldown_duration}s cooldown."
                                )

                            # Check if this error should trigger rotation
                            if not should_rotate_on_error(classified_error):
                                # Non-rotatable errors - fail immediately
                                lib_logger.error(
                                    f"Non-recoverable error ({classified_error.error_type}). Failing request."
                                )
                                raise last_exception

                            # Record failure and rotate to next key
                            await self.usage_manager.record_failure(
                                current_cred, model, classified_error
                            )
                            lib_logger.info(
                                f"Rotating to next key after {classified_error.error_type} error."
                            )
                            break

                finally:
                    if key_acquired and current_cred:
                        await self.usage_manager.release_key(current_cred, model)

            # Build detailed error response using error accumulator
            error_accumulator.timeout_occurred = time.time() >= deadline

            if error_accumulator.has_errors():
                # Log concise summary for server logs
                lib_logger.error(error_accumulator.build_log_message())

                # Build structured error response for client
                error_response = error_accumulator.build_client_error_response()
                error_data = error_response
            else:
                # Fallback if no errors were recorded (shouldn't happen)
                final_error_message = (
                    "Request failed: No available API keys after rotation or timeout."
                )
                if last_exception:
                    final_error_message = (
                        f"Request failed. Last error: {str(last_exception)}"
                    )
                error_data = {
                    "error": {"message": final_error_message, "type": "proxy_error"}
                }
                lib_logger.error(final_error_message)

            yield f"data: {json.dumps(error_data)}\n\n"
            yield "data: [DONE]\n\n"

        except NoAvailableKeysError as e:
            lib_logger.error(
                f"A streaming request failed because no keys were available within the time budget: {e}"
            )
            error_data = {"error": {"message": str(e), "type": "proxy_busy"}}
            yield f"data: {json.dumps(error_data)}\n\n"
            yield "data: [DONE]\n\n"
        except Exception as e:
            # This will now only catch fatal errors that should be raised, like invalid requests.
            lib_logger.error(
                f"An unhandled exception occurred in streaming retry logic: {e}",
                exc_info=True,
            )
            error_data = {
                "error": {
                    "message": f"An unexpected error occurred: {str(e)}",
                    "type": "proxy_internal_error",
                }
            }
            yield f"data: {json.dumps(error_data)}\n\n"
            yield "data: [DONE]\n\n"

    def acompletion(
        self,
        request: Optional[Any] = None,
        pre_request_callback: Optional[callable] = None,
        **kwargs,
    ) -> Union[Any, AsyncGenerator[str, None]]:
        """
        Dispatcher for completion requests.

        Args:
            request: Optional request object, used for client disconnect checks and logging.
            pre_request_callback: Optional async callback function to be called before each API request attempt.
                The callback will receive the `request` object and the prepared request `kwargs` as arguments.
                This can be used for custom logic such as request validation, logging, or rate limiting.
                If the callback raises an exception, the completion request will be aborted and the exception will propagate.

        Returns:
            The completion response object, or an async generator for streaming responses, or None if all retries fail.
        """
        # Handle iflow provider: remove stream_options to avoid HTTP 406
        model = kwargs.get("model", "")
        provider = model.split("/")[0] if "/" in model else ""

        if provider == "iflow" and "stream_options" in kwargs:
            lib_logger.debug(
                "Removing stream_options for iflow provider to avoid HTTP 406"
            )
            kwargs.pop("stream_options", None)

        if kwargs.get("stream"):
            # Only add stream_options for providers that support it (excluding iflow)
            if provider != "iflow":
                if "stream_options" not in kwargs:
                    kwargs["stream_options"] = {}
                if "include_usage" not in kwargs["stream_options"]:
                    kwargs["stream_options"]["include_usage"] = True

            return self._streaming_acompletion_with_retry(
                request=request, pre_request_callback=pre_request_callback, **kwargs
            )
        else:
            return self._execute_with_retry(
                litellm.acompletion,
                request=request,
                pre_request_callback=pre_request_callback,
                **kwargs,
            )

    def aembedding(
        self,
        request: Optional[Any] = None,
        pre_request_callback: Optional[callable] = None,
        **kwargs,
    ) -> Any:
        """
        Executes an embedding request with retry logic.

        Args:
            request: Optional request object, used for client disconnect checks and logging.
            pre_request_callback: Optional async callback function to be called before each API request attempt.
                The callback will receive the `request` object and the prepared request `kwargs` as arguments.
                This can be used for custom logic such as request validation, logging, or rate limiting.
                If the callback raises an exception, the embedding request will be aborted and the exception will propagate.

        Returns:
            The embedding response object, or None if all retries fail.
        """
        return self._execute_with_retry(
            litellm.aembedding,
            request=request,
            pre_request_callback=pre_request_callback,
            **kwargs,
        )

    def token_count(self, **kwargs) -> int:
        """Calculates the number of tokens for a given text or list of messages."""
        kwargs = self._convert_model_params(**kwargs)
        model = kwargs.get("model")
        text = kwargs.get("text")
        messages = kwargs.get("messages")

        if not model:
            raise ValueError("'model' is a required parameter.")
        if messages:
            return token_counter(model=model, messages=messages)
        elif text:
            return token_counter(model=model, text=text)
        else:
            raise ValueError("Either 'text' or 'messages' must be provided.")

    async def get_available_models(self, provider: str) -> List[str]:
        """Returns a list of available models for a specific provider, with caching."""
        lib_logger.info(f"Getting available models for provider: {provider}")
        if provider in self._model_list_cache:
            lib_logger.debug(f"Returning cached models for provider: {provider}")
            return self._model_list_cache[provider]

        credentials_for_provider = self.all_credentials.get(provider)
        if not credentials_for_provider:
            lib_logger.warning(f"No credentials for provider: {provider}")
            return []

        # Create a copy and shuffle it to randomize the starting credential
        shuffled_credentials = list(credentials_for_provider)
        random.shuffle(shuffled_credentials)

        provider_instance = self._get_provider_instance(provider)
        if provider_instance:
            # For providers with hardcoded models (like gemini_cli), we only need to call once.
            # For others, we might need to try multiple keys if one is invalid.
            # The current logic of iterating works for both, as the credential is not
            # always used in get_models.
            for credential in shuffled_credentials:
                try:
                    # Display last 6 chars for API keys, or the filename for OAuth paths
                    cred_display = mask_credential(credential)
                    lib_logger.debug(
                        f"Attempting to get models for {provider} with credential {cred_display}"
                    )
                    models = await provider_instance.get_models(
                        credential, self.http_client
                    )
                    lib_logger.info(
                        f"Got {len(models)} models for provider: {provider}"
                    )

                    # Whitelist and blacklist logic
                    final_models = []
                    for m in models:
                        is_whitelisted = self._is_model_whitelisted(provider, m)
                        is_blacklisted = self._is_model_ignored(provider, m)

                        if is_whitelisted:
                            final_models.append(m)
                            continue

                        if not is_blacklisted:
                            final_models.append(m)

                    if len(final_models) != len(models):
                        lib_logger.info(
                            f"Filtered out {len(models) - len(final_models)} models for provider {provider}."
                        )

                    self._model_list_cache[provider] = final_models
                    return final_models
                except Exception as e:
                    classified_error = classify_error(e, provider=provider)
                    cred_display = mask_credential(credential)
                    lib_logger.debug(
                        f"Failed to get models for provider {provider} with credential {cred_display}: {classified_error.error_type}. Trying next credential."
                    )
                    continue  # Try the next credential

        lib_logger.error(
            f"Failed to get models for provider {provider} after trying all credentials."
        )
        return []

    async def get_all_available_models(
        self, grouped: bool = True
    ) -> Union[Dict[str, List[str]], List[str]]:
        """Returns a list of all available models, either grouped by provider or as a flat list."""
        lib_logger.info("Getting all available models...")

        all_providers = list(self.all_credentials.keys())
        tasks = [self.get_available_models(provider) for provider in all_providers]
        results = await asyncio.gather(*tasks, return_exceptions=True)

        all_provider_models = {}
        for provider, result in zip(all_providers, results):
            if isinstance(result, Exception):
                lib_logger.error(
                    f"Failed to get models for provider {provider}: {result}"
                )
                all_provider_models[provider] = []
            else:
                all_provider_models[provider] = result

        lib_logger.info("Finished getting all available models.")
        if grouped:
            return all_provider_models
        else:
            flat_models = []
            for models in all_provider_models.values():
                flat_models.extend(models)
            return flat_models

    async def get_quota_stats(
        self,
        provider_filter: Optional[str] = None,
    ) -> Dict[str, Any]:
        """
        Get quota and usage stats for all credentials.

        This returns cached/disk data aggregated by provider.
        For provider-specific quota info (e.g., Antigravity quota groups),
        it enriches the data from provider plugins.

        Args:
            provider_filter: If provided, only return stats for this provider

        Returns:
            Complete stats dict ready for the /v1/quota-stats endpoint
        """
        # Get base stats from usage manager
        stats = await self.usage_manager.get_stats_for_endpoint(provider_filter)

        # Enrich with provider-specific quota data
        for provider, prov_stats in stats.get("providers", {}).items():
            provider_class = self._provider_plugins.get(provider)
            if not provider_class:
                continue

            # Get or create provider instance
            if provider not in self._provider_instances:
                self._provider_instances[provider] = provider_class()
            provider_instance = self._provider_instances[provider]

            # Check if provider has quota tracking (like Antigravity)
            if hasattr(provider_instance, "_get_effective_quota_groups"):
                # Add quota group summary
                quota_groups = provider_instance._get_effective_quota_groups()
                prov_stats["quota_groups"] = {}

                for group_name, group_models in quota_groups.items():
                    group_stats = {
                        "models": group_models,
                        "credentials_total": 0,
                        "credentials_exhausted": 0,
                        "avg_remaining_pct": 0,
                        "total_remaining_pcts": [],
                        # Total requests tracking across all credentials
                        "total_requests_used": 0,
                        "total_requests_max": 0,
                        # Tier breakdown: tier_name -> {"total": N, "active": M}
                        "tiers": {},
                    }

                    # Calculate per-credential quota for this group
                    for cred in prov_stats.get("credentials", []):
                        models_data = cred.get("models", {})
                        group_stats["credentials_total"] += 1

                        # Track tier - get directly from provider cache since cred["tier"] not set yet
                        tier = cred.get("tier")
                        if not tier and hasattr(
                            provider_instance, "project_tier_cache"
                        ):
                            cred_path = cred.get("full_path", "")
                            tier = provider_instance.project_tier_cache.get(cred_path)
                        tier = tier or "unknown"

                        # Initialize tier entry if needed with priority for sorting
                        if tier not in group_stats["tiers"]:
                            priority = 10  # default
                            if hasattr(provider_instance, "_resolve_tier_priority"):
                                priority = provider_instance._resolve_tier_priority(
                                    tier
                                )
                            group_stats["tiers"][tier] = {
                                "total": 0,
                                "active": 0,
                                "priority": priority,
                            }
                        group_stats["tiers"][tier]["total"] += 1

                        # Find model with VALID baseline (not just any model with stats)
                        model_stats = None
                        for model in group_models:
                            candidate = self._find_model_stats_in_data(
                                models_data, model, provider, provider_instance
                            )
                            if candidate:
                                baseline = candidate.get("baseline_remaining_fraction")
                                if baseline is not None:
                                    model_stats = candidate
                                    break
                                # Keep first found as fallback (for request counts)
                                if model_stats is None:
                                    model_stats = candidate

                        if model_stats:
                            baseline = model_stats.get("baseline_remaining_fraction")
                            req_count = model_stats.get("request_count", 0)
                            max_req = model_stats.get("quota_max_requests") or 0

                            # Accumulate totals (one model per group per credential)
                            group_stats["total_requests_used"] += req_count
                            group_stats["total_requests_max"] += max_req

                            if baseline is not None:
                                remaining_pct = int(baseline * 100)
                                group_stats["total_remaining_pcts"].append(
                                    remaining_pct
                                )
                                if baseline <= 0:
                                    group_stats["credentials_exhausted"] += 1
                                else:
                                    # Credential is active (has quota remaining)
                                    group_stats["tiers"][tier]["active"] += 1

                    # Calculate average remaining percentage (per-credential average)
                    if group_stats["total_remaining_pcts"]:
                        group_stats["avg_remaining_pct"] = int(
                            sum(group_stats["total_remaining_pcts"])
                            / len(group_stats["total_remaining_pcts"])
                        )
                    del group_stats["total_remaining_pcts"]

                    # Calculate total remaining percentage (global)
                    if group_stats["total_requests_max"] > 0:
                        used = group_stats["total_requests_used"]
                        max_r = group_stats["total_requests_max"]
                        group_stats["total_remaining_pct"] = max(
                            0, int((1 - used / max_r) * 100)
                        )
                    else:
                        group_stats["total_remaining_pct"] = None

                    prov_stats["quota_groups"][group_name] = group_stats

                # Also enrich each credential with formatted quota group info
                for cred in prov_stats.get("credentials", []):
                    cred["model_groups"] = {}
                    models_data = cred.get("models", {})

                    for group_name, group_models in quota_groups.items():
                        # Find model with VALID baseline (prefer over any model with stats)
                        # Also track the best reset_ts across all models in the group
                        model_stats = None
                        best_reset_ts = None

                        for model in group_models:
                            candidate = self._find_model_stats_in_data(
                                models_data, model, provider, provider_instance
                            )
                            if candidate:
                                # Track the best (latest) reset_ts from any model in group
                                candidate_reset_ts = candidate.get("quota_reset_ts")
                                if candidate_reset_ts:
                                    if (
                                        best_reset_ts is None
                                        or candidate_reset_ts > best_reset_ts
                                    ):
                                        best_reset_ts = candidate_reset_ts

                                baseline = candidate.get("baseline_remaining_fraction")
                                if baseline is not None:
                                    model_stats = candidate
                                    # Don't break - continue to find best reset_ts
                                # Keep first found as fallback
                                if model_stats is None:
                                    model_stats = candidate

                        if model_stats:
                            baseline = model_stats.get("baseline_remaining_fraction")
                            max_req = model_stats.get("quota_max_requests")
                            req_count = model_stats.get("request_count", 0)
                            # Use best_reset_ts from any model in the group
                            reset_ts = best_reset_ts or model_stats.get(
                                "quota_reset_ts"
                            )

                            remaining_pct = (
                                int(baseline * 100) if baseline is not None else None
                            )
                            is_exhausted = baseline is not None and baseline <= 0

                            # Format reset time
                            reset_iso = None
                            if reset_ts:
                                try:
                                    from datetime import datetime, timezone

                                    reset_iso = datetime.fromtimestamp(
                                        reset_ts, tz=timezone.utc
                                    ).isoformat()
                                except (ValueError, OSError):
                                    pass

                            cred["model_groups"][group_name] = {
                                "remaining_pct": remaining_pct,
                                "requests_used": req_count,
                                "requests_max": max_req,
                                "display": f"{req_count}/{max_req}"
                                if max_req
                                else f"{req_count}/?",
                                "is_exhausted": is_exhausted,
                                "reset_time_iso": reset_iso,
                                "models": group_models,
                                "confidence": self._get_baseline_confidence(
                                    model_stats
                                ),
                            }

                    # Recalculate credential's requests from model_groups
                    # This fixes double-counting when models share quota groups
                    if cred.get("model_groups"):
                        group_requests = sum(
                            g.get("requests_used", 0)
                            for g in cred["model_groups"].values()
                        )
                        cred["requests"] = group_requests

                        # HACK: Fix global requests if present
                        # This is a simplified fix that sets global.requests = current group_requests.
                        # TODO: Properly track archived requests per quota group in usage_manager.py
                        # so that global stats correctly sum: current_period + archived_periods
                        # without double-counting models that share quota groups.
                        # See: usage_manager.py lines 2388-2404 where global stats are built
                        # by iterating all models (causing double-counting for grouped models).
                        if cred.get("global"):
                            cred["global"]["requests"] = group_requests

                    # Try to get email from provider's cache
                    cred_path = cred.get("full_path", "")
                    if hasattr(provider_instance, "project_tier_cache"):
                        tier = provider_instance.project_tier_cache.get(cred_path)
                        if tier:
                            cred["tier"] = tier

        return stats

    def _find_model_stats_in_data(
        self,
        models_data: Dict[str, Any],
        model: str,
        provider: str,
        provider_instance: Any,
    ) -> Optional[Dict[str, Any]]:
        """
        Find model stats in models_data, trying various name variants.

        Handles aliased model names (e.g., gemini-3-pro-preview -> gemini-3-pro-high)
        by using the provider's _user_to_api_model() mapping.

        Args:
            models_data: Dict of model_name -> stats from credential
            model: Model name to look up (user-facing name)
            provider: Provider name for prefixing
            provider_instance: Provider instance for alias methods

        Returns:
            Model stats dict if found, None otherwise
        """
        # Try direct match with and without provider prefix
        prefixed_model = f"{provider}/{model}"
        model_stats = models_data.get(prefixed_model) or models_data.get(model)

        if model_stats:
            return model_stats

        # Try with API model name (e.g., gemini-3-pro-preview -> gemini-3-pro-high)
        if hasattr(provider_instance, "_user_to_api_model"):
            api_model = provider_instance._user_to_api_model(model)
            if api_model != model:
                prefixed_api = f"{provider}/{api_model}"
                model_stats = models_data.get(prefixed_api) or models_data.get(
                    api_model
                )

        return model_stats

    def _get_baseline_confidence(self, model_stats: Dict) -> str:
        """
        Determine confidence level based on baseline age.

        Args:
            model_stats: Model statistics dict with baseline_fetched_at

        Returns:
            "high" | "medium" | "low"
        """
        baseline_fetched_at = model_stats.get("baseline_fetched_at")
        if not baseline_fetched_at:
            return "low"

        age_seconds = time.time() - baseline_fetched_at
        if age_seconds < 300:  # 5 minutes
            return "high"
        elif age_seconds < 1800:  # 30 minutes
            return "medium"
        return "low"

    async def reload_usage_from_disk(self) -> None:
        """
        Force reload usage data from disk.

        Useful when wanting fresh stats without making external API calls.
        """
        await self.usage_manager.reload_from_disk()

    async def force_refresh_quota(
        self,
        provider: Optional[str] = None,
        credential: Optional[str] = None,
    ) -> Dict[str, Any]:
        """
        Force refresh quota from external API.

        For Antigravity, this fetches live quota data from the API.
        For other providers, this is a no-op (just reloads from disk).

        Args:
            provider: If specified, only refresh this provider
            credential: If specified, only refresh this specific credential

        Returns:
            Refresh result dict with success/failure info
        """
        result = {
            "action": "force_refresh",
            "scope": "credential"
            if credential
            else ("provider" if provider else "all"),
            "provider": provider,
            "credential": credential,
            "credentials_refreshed": 0,
            "success_count": 0,
            "failed_count": 0,
            "duration_ms": 0,
            "errors": [],
        }

        start_time = time.time()

        # Determine which providers to refresh
        if provider:
            providers_to_refresh = (
                [provider] if provider in self.all_credentials else []
            )
        else:
            providers_to_refresh = list(self.all_credentials.keys())

        for prov in providers_to_refresh:
            provider_class = self._provider_plugins.get(prov)
            if not provider_class:
                continue

            # Get or create provider instance
            if prov not in self._provider_instances:
                self._provider_instances[prov] = provider_class()
            provider_instance = self._provider_instances[prov]

            # Check if provider supports quota refresh (like Antigravity)
            if hasattr(provider_instance, "fetch_initial_baselines"):
                # Get credentials to refresh
                if credential:
                    # Find full path for this credential
                    creds_to_refresh = []
                    for cred_path in self.all_credentials.get(prov, []):
                        if cred_path.endswith(credential) or cred_path == credential:
                            creds_to_refresh.append(cred_path)
                            break
                else:
                    creds_to_refresh = self.all_credentials.get(prov, [])

                if not creds_to_refresh:
                    continue

                try:
                    # Fetch live quota from API for ALL specified credentials
                    quota_results = await provider_instance.fetch_initial_baselines(
                        creds_to_refresh
                    )

                    # Store baselines in usage manager
                    if hasattr(provider_instance, "_store_baselines_to_usage_manager"):
                        stored = (
                            await provider_instance._store_baselines_to_usage_manager(
                                quota_results, self.usage_manager
                            )
                        )
                        result["success_count"] += stored

                    result["credentials_refreshed"] += len(creds_to_refresh)

                    # Count failures
                    for cred_path, data in quota_results.items():
                        if data.get("status") != "success":
                            result["failed_count"] += 1
                            result["errors"].append(
                                f"{Path(cred_path).name}: {data.get('error', 'Unknown error')}"
                            )

                except Exception as e:
                    lib_logger.error(f"Failed to refresh quota for {prov}: {e}")
                    result["errors"].append(f"{prov}: {str(e)}")
                    result["failed_count"] += len(creds_to_refresh)

        result["duration_ms"] = int((time.time() - start_time) * 1000)
        return result