Commit 6486db3
authored
fix: improve streaming proxy throughput by fixing middleware and logging bottlenecks (#21501)
* fix(middleware): replace BaseHTTPMiddleware with pure ASGI middleware
BaseHTTPMiddleware wraps streaming responses with receive_or_disconnect
per chunk, blocking the event loop and causing severe throughput
degradation under concurrent streaming load (53% of CPU in profiling).
Converts PrometheusAuthMiddleware to a pure ASGI middleware using the
__call__(scope, receive, send) protocol.
* fix(streaming): remove expensive debug logging and optimize usage stripping
- Remove print_verbose calls that format chunk/response Pydantic objects,
triggering millions of __repr__ calls (8% of CPU in profiling)
- Guard remaining verbose_logger.debug with isEnabledFor(DEBUG) and use
lazy %s formatting instead of f-strings
- Replace usage stripping round-trip (model_dump + delete + reconstruct)
with a _usage_stripped flag, deferring exclusion to serialization time
* fix(proxy): remove per-chunk debug log and use _usage_stripped flag
- Remove verbose_proxy_logger.debug that formatted every streaming chunk
- Honor _usage_stripped flag from streaming handler to exclude usage
during model_dump_json serialization instead of reconstructing objects
* fix(proxy): remove per-chunk debug log in async_data_generator
Remove verbose_proxy_logger.debug that formatted every streaming chunk,
which triggered expensive Pydantic serialization on the hot path.
* fix indentation and add clarifying comment for usage stripping
* fix: guard calculate_total_usage against None usage in chunks
* fix: store chunk copy to preserve usage for calculate_total_usage1 parent a9058bb commit 6486db3
File tree
4 files changed
+70
-70
lines changed- litellm
- litellm_core_utils
- proxy
- middleware
- tests/test_litellm/proxy/middleware
4 files changed
+70
-70
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
| |||
435 | 436 | | |
436 | 437 | | |
437 | 438 | | |
438 | | - | |
| 439 | + | |
439 | 440 | | |
440 | 441 | | |
441 | 442 | | |
| |||
485 | 486 | | |
486 | 487 | | |
487 | 488 | | |
488 | | - | |
| 489 | + | |
489 | 490 | | |
490 | 491 | | |
491 | 492 | | |
| |||
506 | 507 | | |
507 | 508 | | |
508 | 509 | | |
509 | | - | |
| 510 | + | |
510 | 511 | | |
511 | 512 | | |
512 | 513 | | |
| |||
870 | 871 | | |
871 | 872 | | |
872 | 873 | | |
873 | | - | |
874 | | - | |
875 | | - | |
876 | 874 | | |
877 | 875 | | |
878 | 876 | | |
| |||
899 | 897 | | |
900 | 898 | | |
901 | 899 | | |
902 | | - | |
903 | 900 | | |
904 | 901 | | |
905 | 902 | | |
906 | | - | |
907 | 903 | | |
908 | 904 | | |
909 | 905 | | |
| |||
921 | 917 | | |
922 | 918 | | |
923 | 919 | | |
924 | | - | |
925 | | - | |
926 | | - | |
| 920 | + | |
| 921 | + | |
| 922 | + | |
| 923 | + | |
| 924 | + | |
927 | 925 | | |
928 | 926 | | |
929 | 927 | | |
| |||
1370 | 1368 | | |
1371 | 1369 | | |
1372 | 1370 | | |
1373 | | - | |
1374 | | - | |
1375 | | - | |
1376 | 1371 | | |
1377 | 1372 | | |
1378 | 1373 | | |
| |||
1432 | 1427 | | |
1433 | 1428 | | |
1434 | 1429 | | |
1435 | | - | |
1436 | 1430 | | |
1437 | 1431 | | |
1438 | 1432 | | |
| |||
1466 | 1460 | | |
1467 | 1461 | | |
1468 | 1462 | | |
1469 | | - | |
1470 | 1463 | | |
1471 | | - | |
1472 | | - | |
1473 | | - | |
1474 | 1464 | | |
1475 | 1465 | | |
1476 | 1466 | | |
| |||
1480 | 1470 | | |
1481 | 1471 | | |
1482 | 1472 | | |
1483 | | - | |
1484 | | - | |
1485 | | - | |
1486 | | - | |
1487 | | - | |
1488 | 1473 | | |
1489 | 1474 | | |
1490 | 1475 | | |
| |||
1915 | 1900 | | |
1916 | 1901 | | |
1917 | 1902 | | |
1918 | | - | |
1919 | | - | |
1920 | | - | |
1921 | | - | |
1922 | | - | |
1923 | | - | |
1924 | 1903 | | |
1925 | 1904 | | |
1926 | 1905 | | |
1927 | | - | |
1928 | | - | |
1929 | | - | |
1930 | 1906 | | |
1931 | 1907 | | |
1932 | 1908 | | |
| |||
1943 | 1919 | | |
1944 | 1920 | | |
1945 | 1921 | | |
1946 | | - | |
1947 | | - | |
| 1922 | + | |
| 1923 | + | |
| 1924 | + | |
| 1925 | + | |
1948 | 1926 | | |
1949 | 1927 | | |
1950 | 1928 | | |
1951 | 1929 | | |
1952 | | - | |
1953 | | - | |
1954 | | - | |
1955 | | - | |
1956 | | - | |
1957 | | - | |
1958 | | - | |
1959 | | - | |
1960 | | - | |
1961 | | - | |
1962 | | - | |
1963 | | - | |
| 1930 | + | |
| 1931 | + | |
| 1932 | + | |
| 1933 | + | |
| 1934 | + | |
| 1935 | + | |
| 1936 | + | |
| 1937 | + | |
| 1938 | + | |
1964 | 1939 | | |
1965 | 1940 | | |
1966 | 1941 | | |
1967 | | - | |
1968 | 1942 | | |
1969 | 1943 | | |
1970 | | - | |
1971 | 1944 | | |
1972 | 1945 | | |
1973 | 1946 | | |
| |||
1982 | 1955 | | |
1983 | 1956 | | |
1984 | 1957 | | |
1985 | | - | |
| 1958 | + | |
1986 | 1959 | | |
1987 | 1960 | | |
1988 | 1961 | | |
| |||
1996 | 1969 | | |
1997 | 1970 | | |
1998 | 1971 | | |
1999 | | - | |
2000 | 1972 | | |
2001 | 1973 | | |
2002 | 1974 | | |
2003 | | - | |
2004 | | - | |
2005 | | - | |
2006 | 1975 | | |
2007 | 1976 | | |
2008 | 1977 | | |
| |||
2193 | 2162 | | |
2194 | 2163 | | |
2195 | 2164 | | |
2196 | | - | |
| 2165 | + | |
2197 | 2166 | | |
2198 | 2167 | | |
2199 | 2168 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
3 | 8 | | |
4 | | - | |
5 | | - | |
6 | | - | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
7 | 12 | | |
8 | 13 | | |
9 | 14 | | |
10 | 15 | | |
11 | 16 | | |
12 | 17 | | |
13 | | - | |
| 18 | + | |
14 | 19 | | |
15 | 20 | | |
16 | 21 | | |
| |||
24 | 29 | | |
25 | 30 | | |
26 | 31 | | |
27 | | - | |
28 | | - | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
29 | 41 | | |
30 | 42 | | |
31 | 43 | | |
| |||
38 | 50 | | |
39 | 51 | | |
40 | 52 | | |
41 | | - | |
| 53 | + | |
42 | 54 | | |
43 | 55 | | |
44 | 56 | | |
| 57 | + | |
| 58 | + | |
45 | 59 | | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
| 60 | + | |
50 | 61 | | |
51 | 62 | | |
52 | 63 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5074 | 5074 | | |
5075 | 5075 | | |
5076 | 5076 | | |
5077 | | - | |
5078 | | - | |
5079 | | - | |
5080 | | - | |
5081 | 5077 | | |
5082 | 5078 | | |
5083 | 5079 | | |
| |||
Lines changed: 24 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
0 commit comments