Skip to content

OpenAI Chat Completions model

OpenAI ChatCompletions model -- backward-compatible module.

The utilities that used to live here have been refactored into the chatcompletions/ sub-package. This file re-exports them so that all existing from cai.sdk.agents.models.openai_chatcompletions import X statements continue to work.

OpenAIChatCompletionsModel

Bases: Model

OpenAI Chat Completions Model

Source code in src/cai/sdk/agents/models/openai_chatcompletions.py
 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
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
class OpenAIChatCompletionsModel(Model):
    """OpenAI Chat Completions Model"""

    INTERMEDIATE_LOG_INTERVAL = 5

    def __init__(
        self,
        model: str | ChatModel,
        openai_client: AsyncOpenAI,
        agent_name: str = "CTF agent",  # Default to CTF agent instead of generic "Agent"
        agent_id: str | None = None,
        agent_type: str | None = None,  # The type of agent (e.g., "red_teamer")
    ) -> None:
        self.model = model
        self._client = openai_client
        # Check if we're using OLLAMA models
        self.is_ollama = os.getenv("OLLAMA") is not None and os.getenv("OLLAMA").lower() != "false"
        # Detect alias models for direct httpx bypass (skip LiteLLM overhead)
        _m = str(model).lower()
        self._is_alias_model = (
            "alias" in _m and "alias1.5" not in _m
        ) or _m == "alias2-mini"
        self.empty_content_error_shown = False

        # Track interaction counter and token totals for cli display
        self.interaction_counter = 0
        self.total_input_tokens = 0
        self.total_output_tokens = 0
        self.total_reasoning_tokens = 0
        self.total_cost = 0.0
        # Per-interaction token tracking
        self.interaction_input_tokens = 0
        self.interaction_output_tokens = 0
        self.interaction_reasoning_tokens = 0
        # Cache token tracking
        self.cache_read_tokens = 0
        self.cache_creation_tokens = 0
        self.agent_name = agent_name
        self.agent_type = agent_type or agent_name.lower().replace(" ", "_")  # For registry tracking
        self.uses_unified_context = False  # Flag to indicate if using shared message history

        # For SimpleAgentManager, we don't auto-register
        # The agent will be registered when explicitly created by cli.py
        self.agent_id = agent_id or AGENT_MANAGER.get_agent_id()
        self._display_name = self.agent_name

        # Instance-based message history
        # Check if we have an isolated history for this agent (parallel mode)
        if agent_id and PARALLEL_ISOLATION.is_parallel_mode():
            isolated_history = PARALLEL_ISOLATION.get_isolated_history(agent_id)
            if isolated_history is not None:
                self.message_history = isolated_history
            else:
                self.message_history = []
        else:
            # Get or create history from AGENT_MANAGER to ensure we share the same list reference
            # This is critical for proper history clearing to work
            existing_history = AGENT_MANAGER.get_message_history(self.agent_name)
            if existing_history is not None and isinstance(existing_history, list):
                # Use the existing list reference from AGENT_MANAGER
                self.message_history = existing_history
            else:
                # Create new history and ensure AGENT_MANAGER has it too
                self.message_history = []
                if self.agent_name not in AGENT_MANAGER._message_history:
                    AGENT_MANAGER._message_history[self.agent_name] = self.message_history

        # NOTE: Models should NOT register themselves with AGENT_MANAGER
        # The agent that owns this model will handle registration
        # This prevents duplicate registrations with agent keys

        # CRITICAL: Ensure AGENT_MANAGER uses the same list reference as the model
        # This is necessary for proper history clearing to work
        if agent_id is not None and not PARALLEL_ISOLATION.is_parallel_mode():
            if self.agent_name in AGENT_MANAGER._message_history:
                # Share the same list reference
                self.message_history = AGENT_MANAGER._message_history[self.agent_name]

        # Instance-based converter
        self._converter = _Converter()

        # Flags for CLI integration
        self.disable_rich_streaming = False  # Prevents creating a rich panel in the model
        self.suppress_final_output = False  # Prevents duplicate output at end of streaming

        # Initialize the session logger
        self.logger = get_session_recorder()

        # DEPRECATED: Still maintain backward compatibility with ACTIVE_MODEL_INSTANCES
        # TODO: Remove this after updating all dependent code
        ACTIVE_MODEL_INSTANCES[(self._display_name, self.agent_id)] = weakref.ref(self)

    def _shallow_copy_history_messages(self) -> list[dict]:
        """Shallow-copy ``message_history`` for API requests; keep ``cache_control`` when present."""
        converted: list[dict] = []
        if self.message_history:
            for msg in self.message_history:
                msg_copy = msg.copy()
                if "cache_control" in msg:
                    msg_copy["cache_control"] = msg["cache_control"]
                converted.append(msg_copy)
        return converted

    def _messages_for_token_count_after_history_mutation(
        self,
        *,
        system_instructions: str | None,
        input: str | list[TResponseInputItem],
    ) -> list[dict]:
        """Rebuild the message list the API will send (same rules as ``_fetch_response``).

        After auto-compaction mutates ``message_history`` in place, token estimates must not
        rebuild from ``input`` alone — that drops history and makes ``CAI_CONTEXT_USAGE`` look
        near zero while the model still receives the full thread.
        """
        converted_messages = self._shallow_copy_history_messages()
        if not self.message_history:
            converted_messages.extend(self._converter.items_to_messages(input, model_instance=self))
        if system_instructions:
            has_system = any(msg.get("role") == "system" for msg in converted_messages)
            if not has_system:
                converted_messages.insert(
                    0,
                    {"role": "system", "content": system_instructions},
                )
            else:
                for msg in converted_messages:
                    if msg.get("role") == "system":
                        msg["content"] = system_instructions
                        break
        try:
            from cai.util import fix_message_list

            return fix_message_list(converted_messages)
        except Exception:
            return converted_messages

    # ------------------------------------------------------------------
    # Retry helper – exponential backoff with jitter
    # ------------------------------------------------------------------
    @staticmethod
    def _backoff_delay(attempt: int, base: float = 5.0, cap: float = 120.0) -> float:
        """Return seconds to wait: min(cap, base * 2^attempt) + jitter."""
        delay = min(cap, base * (2 ** attempt)) + random.uniform(0, 3)
        return delay

    async def _retry_with_backoff(self, attempt: int, kind: str) -> None:
        """Wait with exponential backoff. Console noise only if CAI_VERBOSE_LLM_RETRY is set."""
        delay = self._backoff_delay(attempt)
        msg = f"{kind} (attempt {attempt + 1}/3) — retrying in {delay:.0f}s..."
        self.logger.warning(f"LLM backoff: {msg}")
        if verbose_http_retries():
            OUTPUT.emit(StatusEvent(message=msg, level="warning", agent_id=self.agent_name))
            from rich.console import Console
            console = Console()
            console.print(f"\n[yellow]⚠️  {msg}[/yellow]")
            await sleep_with_retry_backoff_hint(delay)
            console.print("[green]↻ Retrying now...[/green]\n")
        else:
            await sleep_with_retry_backoff_hint(delay)

    async def _recover_after_empty_completion(
        self,
        *,
        empty_streak: int,
        estimated_input_tokens: int,
        input: str | list[TResponseInputItem],
        system_instructions: str | None,
    ) -> tuple[str | list[TResponseInputItem], str | None, int]:
        """Backoff and optionally force compact before retrying after an empty provider response."""
        model_max = self._get_model_max_tokens(str(self.model))
        will_force_compact = _should_force_compact_on_empty_streak(
            empty_streak, estimated_input_tokens, model_max
        )
        await self._retry_with_backoff(empty_streak - 1, "Empty assistant completion")
        if not will_force_compact:
            return input, system_instructions, estimated_input_tokens
        try:
            force_est = max(
                estimated_input_tokens,
                int(model_max * EMPTY_COMPLETION_FORCE_COMPACT_CONTEXT_FRACTION),
            )
            input, system_instructions, compacted = await self._auto_compact_if_needed(
                force_est,
                input,
                system_instructions,
            )
            if compacted:
                converted_messages = self._messages_for_token_count_after_history_mutation(
                    system_instructions=system_instructions,
                    input=input,
                )
                estimated_input_tokens, _ = count_tokens_with_tiktoken(converted_messages)
                if model_max > 0:
                    os.environ["CAI_CONTEXT_USAGE"] = str(
                        min(1.0, max(0.0, estimated_input_tokens / model_max))
                    )
        except Exception as e:
            self.logger.warning("Forced compaction after empty completion failed: %s", e)
        return input, system_instructions, estimated_input_tokens

    def get_full_display_name(self) -> str:
        """Get the full display name including ID."""
        return f"{self._display_name} [{self.agent_id}]"

    def __del__(self):
        """Clean up when the model instance is destroyed."""
        try:
            # DEPRECATED: Remove from old registry for backward compatibility
            if hasattr(self, '_display_name') and hasattr(self, 'agent_id'):
                key = (self._display_name, self.agent_id)
                if key in ACTIVE_MODEL_INSTANCES:
                    del ACTIVE_MODEL_INSTANCES[key]

            # SimpleAgentManager handles history persistence
            # No need to save to PERSISTENT_MESSAGE_HISTORIES

        except Exception:
            # Ignore any errors during cleanup
            pass

    def add_to_message_history(self, msg, skip_deduplication: bool = False):
        """Add a message to this instance's history.

        Args:
            msg: The message dictionary to add
            skip_deduplication: If True, skip all duplicate checking and just append.
                              Use this when loading session history where messages
                              are already in correct order and deduplication would
                              cause reordering issues.

        Now only adds to the instance's local history, no global registry.
        """
        # When loading session history, skip all deduplication to preserve order
        if skip_deduplication:
            self.message_history.append(msg)
            manager_history = AGENT_MANAGER.get_message_history(self.agent_name)
            if manager_history is not self.message_history:
                AGENT_MANAGER.add_to_history(self.agent_name, msg)
            if PARALLEL_ISOLATION.is_parallel_mode() and self.agent_id:
                PARALLEL_ISOLATION.update_isolated_history(self.agent_id, msg)
            return

        is_duplicate = False

        if self.message_history:
            if msg.get("role") in ["system", "user"]:
                is_duplicate = any(
                    existing.get("role") == msg.get("role")
                    and existing.get("content") == msg.get("content")
                    for existing in self.message_history
                )
            elif msg.get("role") == "assistant" and msg.get("tool_calls"):
                # For tool calls, check if message with same tool call ID already exists
                # If it does, UPDATE IN PLACE to preserve message order
                tool_call_id = msg["tool_calls"][0].get("id") if msg.get("tool_calls") else None
                if tool_call_id:
                    existing_idx = None
                    for i, existing in enumerate(self.message_history):
                        if (existing.get("role") == "assistant"
                            and existing.get("tool_calls")
                            and existing["tool_calls"][0].get("id") == tool_call_id):
                            existing_idx = i
                            break

                    if existing_idx is not None:
                        # UPDATE IN PLACE to preserve order (don't remove and re-add!)
                        self.message_history[existing_idx] = msg
                        is_duplicate = True  # Mark as duplicate so we don't append again
                    else:
                        is_duplicate = False
            elif msg.get("role") == "tool":
                is_duplicate = any(
                    existing.get("role") == "tool"
                    and existing.get("tool_call_id") == msg.get("tool_call_id")
                    for existing in self.message_history
                )

        if not is_duplicate:
            self.message_history.append(msg)
            # Also update SimpleAgentManager ONLY if they're not the same list reference
            # This avoids double-adding when they share the same list
            manager_history = AGENT_MANAGER.get_message_history(self.agent_name)
            if manager_history is not self.message_history:
                AGENT_MANAGER.add_to_history(self.agent_name, msg)
            # Update isolated history if in parallel mode
            if PARALLEL_ISOLATION.is_parallel_mode() and self.agent_id:
                PARALLEL_ISOLATION.update_isolated_history(self.agent_id, msg)

    def set_agent_name(self, name: str) -> None:
        """Set the agent name for CLI display purposes."""
        self.agent_name = name

    def _non_null_or_not_given(self, value: Any) -> Any:
        return value if value is not None else NOT_GIVEN

    async def get_response(
        self,
        system_instructions: str | None,
        input: str | list[TResponseInputItem],
        model_settings: ModelSettings,
        tools: list[Tool],
        output_schema: AgentOutputSchema | None,
        handoffs: list[Handoff],
        tracing: ModelTracing,
    ) -> ModelResponse:
        # Close any open streaming panels from the previous cycle
        # This ensures panels don't stay open when the model starts a new inference
        try:
            from cai.util import close_all_streaming_panels
            close_all_streaming_panels()
        except ImportError:
            pass

        # Increment the interaction counter for CLI display
        self.interaction_counter += 1
        self._intermediate_logs()

        # Set this as the current active model for tool execution context
        set_current_active_model(self)

        # Stop idle timer and start active timer to track LLM processing time
        stop_idle_timer()
        start_active_timer()

        with generation_span(
            model=str(self.model),
            model_config=dataclasses.asdict(model_settings)
            | {"base_url": str(self._get_client().base_url)},
            disabled=tracing.is_disabled(),
        ) as span_generation:
            # Prepare the messages for consistent token counting
            # IMPORTANT: Include existing message history for context
            converted_messages = self._shallow_copy_history_messages()

            # Then convert and add the new input
            new_messages = self._converter.items_to_messages(input, model_instance=self)
            converted_messages.extend(new_messages)

            if system_instructions:
                # Check if we already have a system message
                has_system = any(msg.get("role") == "system" for msg in converted_messages)
                if not has_system:
                    converted_messages.insert(
                        0,
                        {
                            "content": system_instructions,
                            "role": "system",
                        },
                    )

            # # --- Add to message_history: user, system, and assistant tool call messages ---
            # # Add system prompt to message_history
            # if system_instructions:
            #     sys_msg = {
            #         "role": "system",
            #         "content": system_instructions
            #     }
            #     self.add_to_message_history(sys_msg)

            # Add user messages to message_history.
            # add_to_message_history() has built-in dedup (same role+content),
            # so repeated calls with the same message within a Runner loop are safe.
            if isinstance(input, str):
                self.add_to_message_history({"role": "user", "content": input})
                self.logger.log_user_message(input)
            elif isinstance(input, list):
                for item in input:
                    if isinstance(item, dict) and item.get("role") == "user":
                        self.add_to_message_history(
                            {"role": "user", "content": item.get("content", "")}
                        )

            # IMPORTANT: Ensure the message list has valid tool call/result pairs
            # This needs to happen before the API call AND before applying cache_control
            try:
                from cai.util import fix_message_list

                converted_messages = fix_message_list(converted_messages)
            except Exception:
                pass

            # Request-path message normalization/cache-control is applied in _fetch_response().
            # Keep startup estimation lightweight to avoid duplicate per-turn preprocessing work.

            # Get token count estimate before API call for consistent counting
            estimated_input_tokens, _ = count_tokens_with_tiktoken(converted_messages)

            # Calculate and set context usage for toolbar
            max_tokens = self._get_model_max_tokens(str(self.model))
            context_usage = estimated_input_tokens / max_tokens if max_tokens > 0 else 0.0
            os.environ['CAI_CONTEXT_USAGE'] = str(context_usage)

            # Check if auto-compaction is needed
            input, system_instructions, compacted = await self._auto_compact_if_needed(estimated_input_tokens, input, system_instructions)

            # If compaction occurred, recalculate tokens from the same view ``_fetch_response`` uses
            if compacted:
                converted_messages = self._messages_for_token_count_after_history_mutation(
                    system_instructions=system_instructions,
                    input=input,
                )
                estimated_input_tokens, _ = count_tokens_with_tiktoken(converted_messages)
                max_tok = self._get_model_max_tokens(str(self.model))
                if max_tok > 0:
                    os.environ["CAI_CONTEXT_USAGE"] = str(
                        min(1.0, max(0.0, estimated_input_tokens / max_tok))
                    )

            # Pre-check price limit using estimated input tokens and a conservative estimate for output
            # This prevents starting a request that would immediately exceed the price limit
            if hasattr(COST_TRACKER, "check_price_limit"):
                # Use a conservative estimate for output tokens (roughly equal to input)
                estimated_cost = calculate_model_cost(
                    str(self.model), estimated_input_tokens, estimated_input_tokens
                )  # Conservative estimate
                try:
                    COST_TRACKER.check_price_limit(estimated_cost)
                except Exception:
                    # Stop active timer and start idle timer before re-raising the exception
                    stop_active_timer()
                    start_idle_timer()
                    raise

            try:
                max_empty_failures = _empty_completion_max_failures()
                empty_streak = 0
                response = None
                # ``model_wait_hints`` wraps the whole retry loop so the body
                # stays published across attempts (no flicker between iterations).
                # The ``try/finally`` clears any retry/pacing overlay even when
                # ``_fetch_response`` raises a typed error that propagates out.
                _on_pace = make_pace_overlay_callback()
                async with model_wait_hints():
                    try:
                        while True:
                            # Proactive client-side pacing for the alias gateway
                            # (TPM/RPM per API key). The projection adds a small
                            # completion-token buffer because the gateway counts
                            # input+output against TPM but we only know input
                            # before the call; ``Reservation.update_actual``
                            # reconciles to the real total once the response
                            # arrives. The limiter's 85% safety margin absorbs
                            # any residual drift between our tiktoken estimate
                            # and the gateway's authoritative accounting (the
                            # gateway does not expose ``x-ratelimit-*`` headers,
                            # confirmed by direct probe).
                            if self._is_alias_model:
                                _projection = estimated_input_tokens + COMPLETION_BUDGET_TOKENS
                                async with get_gateway_rate_limiter().alias_gateway_slot(
                                    _projection,
                                    on_pace=_on_pace,
                                ) as _reservation:
                                    response = await self._fetch_response(
                                        system_instructions,
                                        input,
                                        model_settings,
                                        tools,
                                        output_schema,
                                        handoffs,
                                        span_generation,
                                        tracing,
                                        stream=False,
                                    )
                                    # Reconcile pre-flight estimate with the
                                    # gateway's real ``prompt + completion``
                                    # so the deque tracks ground truth for
                                    # subsequent pacing decisions.
                                    try:
                                        _usage = getattr(response, "usage", None)
                                        if _usage is not None:
                                            _real = (
                                                int(getattr(_usage, "prompt_tokens", 0) or 0)
                                                + int(getattr(_usage, "completion_tokens", 0) or 0)
                                            )
                                            _reservation.update_actual(_real)
                                    except Exception:
                                        pass
                            else:
                                response = await self._fetch_response(
                                    system_instructions,
                                    input,
                                    model_settings,
                                    tools,
                                    output_schema,
                                    handoffs,
                                    span_generation,
                                    tracing,
                                    stream=False,
                                )
                            first_choice = _get_first_choice(response)
                            if not first_choice:
                                raise AgentsException("LLM returned response with no choices")
                            if _is_effectively_empty_assistant_message(first_choice.message):
                                empty_streak += 1
                                _empty_usage = getattr(response, "usage", None)
                                _empty_msg = first_choice.message
                                self.logger.warning(
                                    "Empty assistant completion (%s/%s); "
                                    "pt=%s ct=%s reasoning_len=%s tools=%s; repeating.",
                                    empty_streak,
                                    max_empty_failures,
                                    (
                                        getattr(_empty_usage, "prompt_tokens", None)
                                        if _empty_usage
                                        else None
                                    ),
                                    (
                                        getattr(_empty_usage, "completion_tokens", None)
                                        if _empty_usage
                                        else None
                                    ),
                                    len(
                                        str(
                                            getattr(_empty_msg, "reasoning_content", None)
                                            or ""
                                        )
                                    ),
                                    len(getattr(_empty_msg, "tool_calls", None) or []),
                                )
                                if empty_streak >= max_empty_failures:
                                    stop_active_timer()
                                    start_idle_timer()
                                    raise LLMEmptyAssistantError(
                                        "Consecutive empty assistant completions from the provider.",
                                        {"attempts": max_empty_failures},
                                    )
                                input, system_instructions, estimated_input_tokens = (
                                    await self._recover_after_empty_completion(
                                        empty_streak=empty_streak,
                                        estimated_input_tokens=estimated_input_tokens,
                                        input=input,
                                        system_instructions=system_instructions,
                                    )
                                )
                                set_model_wait_retry_overlay(
                                    "Provider returned an empty response; "
                                    f"retrying ({empty_streak}/{max_empty_failures})…"
                                )
                                continue
                            break
                    finally:
                        set_model_wait_retry_overlay(None)
            except KeyboardInterrupt:
                # Handle KeyboardInterrupt during API call.
                # ``alias_gateway_slot`` already released any in-flight
                # reservation before KbInt propagated to this handler.
                if hasattr(self, "_pending_tool_calls"):
                    self._pending_tool_calls.clear()

                # Let the interrupt propagate up to end the current operation
                stop_active_timer()
                start_idle_timer()

                raise

            except (litellm.exceptions.Timeout, LLMTimeout) as e:
                # High-level timeout recovery with exponential backoff
                self.logger.warning(f"Timeout error: {e}")
                stop_active_timer()
                start_idle_timer()

                if not hasattr(self, "_high_level_retry_count"):
                    self._high_level_retry_count = 0
                self._high_level_retry_count += 1

                if self._high_level_retry_count > 3:
                    self._high_level_retry_count = 0
                    raise LLMTimeout(f"Timed out after 3 attempts [{self.model}]") from e

                # Exponential backoff — NO "continue" injected into history
                await self._retry_with_backoff(self._high_level_retry_count - 1, "Timeout")

                # Clean retry: re-send the SAME input, don't pollute history
                result = await self.get_response(
                    system_instructions, input, model_settings,
                    tools, output_schema, handoffs, tracing,
                )
                self._high_level_retry_count = 0
                return result

            except (litellm.exceptions.RateLimitError, LLMRateLimited) as e:
                # High-level rate-limit recovery with exponential backoff
                self.logger.warning(f"Rate limit (high-level): {e}")
                stop_active_timer()
                start_idle_timer()

                if not hasattr(self, "_high_level_retry_count"):
                    self._high_level_retry_count = 0
                self._high_level_retry_count += 1

                if self._high_level_retry_count > 3:
                    self._high_level_retry_count = 0
                    raise LLMRateLimited(
                        f"Rate limit after 3 attempts [{self.model}]",
                        retry_after=getattr(e, "retry_after", None),
                    ) from e

                # Exponential backoff — NO "continue" injected into history
                await self._retry_with_backoff(self._high_level_retry_count - 1, "Rate limit")

                # Clean retry: re-send the SAME input
                result = await self.get_response(
                    system_instructions, input, model_settings,
                    tools, output_schema, handoffs, tracing,
                )
                self._high_level_retry_count = 0
                return result

            except (
                litellm.exceptions.BadGatewayError,
                litellm.exceptions.ServiceUnavailableError,
                litellm.exceptions.InternalServerError,
            ) as e:
                # Transient server errors (502, 503, 500): retry with backoff
                self.logger.warning(f"Server error (high-level recovery): {str(e)[:200]}")

                stop_active_timer()
                start_idle_timer()

                if not hasattr(self, "_high_level_retry_count"):
                    self._high_level_retry_count = 0
                self._high_level_retry_count += 1

                if self._high_level_retry_count > 3:
                    self._high_level_retry_count = 0
                    if verbose_http_retries():
                        print(f"\n❌ Server error after 3 recovery attempts [{self.model}]")
                    raise

                wait_secs = 10 * self._high_level_retry_count  # 10s, 20s, 30s
                self.logger.warning(
                    f"Server error recovery attempt {self._high_level_retry_count}/3 "
                    f"({type(e).__name__}), waiting {wait_secs}s"
                )
                if verbose_http_retries():
                    from rich.console import Console
                    console = Console()
                    console.print(
                        f"\n[yellow]⚠️  Server error (attempt {self._high_level_retry_count}/3): "
                        f"{type(e).__name__}[/yellow]"
                    )
                    console.print(f"[yellow]Waiting {wait_secs}s before retrying...[/yellow]")

                await sleep_with_retry_backoff_hint(wait_secs)

                if verbose_http_retries():
                    from rich.console import Console
                    Console().print("[green]Retrying request...[/green]\n")

                stop_idle_timer()
                start_active_timer()

                result = await self.get_response(
                    system_instructions,
                    input,
                    model_settings,
                    tools,
                    output_schema,
                    handoffs,
                    tracing,
                )
                self._high_level_retry_count = 0
                return result

            if _debug.DONT_LOG_MODEL_DATA:
                logger.debug("Received model response")
            else:
                import json

                _first = _get_first_choice(response)
                if _first:
                    if _is_effectively_empty_assistant_message(_first.message):
                        logger.debug(
                            "LLM resp: assistant message is empty (no full JSON dump — "
                            "see CAI_DEBUG=2 if you need the raw object)."
                        )
                    else:
                        logger.debug(
                            f"LLM resp:\n{json.dumps(_first.message.model_dump(), indent=2)}\n"
                        )

            # Ensure we have reasonable token counts
            if response.usage:
                input_tokens = response.usage.prompt_tokens
                output_tokens = response.usage.completion_tokens
                total_tokens = response.usage.total_tokens

                # Use estimated tokens if API returns zeroes or implausible values
                if input_tokens == 0 or input_tokens < (len(str(input)) // 10):  # Sanity check
                    input_tokens = estimated_input_tokens
                    total_tokens = input_tokens + output_tokens

                # # Debug information
                # print(f"\nDEBUG CONSISTENT TOKEN COUNTS - API tokens: input={input_tokens}, output={output_tokens}, total={total_tokens}")
                # print(f"Estimated tokens were: input={estimated_input_tokens}")
            else:
                # If no usage info, use our estimates
                input_tokens = estimated_input_tokens
                output_tokens = 0
                total_tokens = input_tokens
                # print(f"\nDEBUG CONSISTENT TOKEN COUNTS - No API tokens, using estimates: input={input_tokens}, output={output_tokens}")

            # Update token totals for CLI display
            self.total_input_tokens += input_tokens
            self.total_output_tokens += output_tokens
            # Update per-interaction tokens (reset each call)
            self.interaction_input_tokens = input_tokens
            self.interaction_output_tokens = output_tokens
            # Extract and update cache tokens
            # Support both Anthropic format (cache_read_input_tokens) and OpenAI format (prompt_tokens_details.cached_tokens)
            if response.usage:
                # Try Anthropic format first
                cache_read = getattr(response.usage, 'cache_read_input_tokens', 0) or 0
                # Fallback to OpenAI format (prompt_tokens_details.cached_tokens)
                if not cache_read:
                    prompt_details = getattr(response.usage, 'prompt_tokens_details', None)
                    if prompt_details:
                        cache_read = getattr(prompt_details, 'cached_tokens', 0) or 0
                self.cache_read_tokens = cache_read
                # cache_creation_tokens is Anthropic-only (OpenAI doesn't charge for cache writes)
                self.cache_creation_tokens = getattr(response.usage, 'cache_creation_input_tokens', 0) or 0

                # Also update COST_TRACKER so it's available for tool panels
                try:
                    import cai.util
                    cai.util.COST_TRACKER.interaction_input_tokens = self.interaction_input_tokens
                    cai.util.COST_TRACKER.interaction_output_tokens = self.interaction_output_tokens
                    cai.util.COST_TRACKER.interaction_reasoning_tokens = self.interaction_reasoning_tokens
                    cai.util.COST_TRACKER.cache_read_tokens = self.cache_read_tokens
                    cai.util.COST_TRACKER.cache_creation_tokens = self.cache_creation_tokens
                except Exception:
                    pass
            else:
                self.cache_read_tokens = 0
                self.cache_creation_tokens = 0
            reasoning_tokens = 0
            if (
                response.usage
                and hasattr(response.usage, "completion_tokens_details")
                and response.usage.completion_tokens_details
                and hasattr(response.usage.completion_tokens_details, "reasoning_tokens")
            ):
                # Guard against None or unexpected types for reasoning_tokens
                try:
                    reasoning_tokens = response.usage.completion_tokens_details.reasoning_tokens
                    if reasoning_tokens is None:
                        reasoning_tokens = 0
                    else:
                        # coerce numeric-like values to int
                        reasoning_tokens = int(reasoning_tokens)
                except Exception:
                    reasoning_tokens = 0

                self.total_reasoning_tokens += reasoning_tokens
            # Update per-interaction reasoning tokens
            self.interaction_reasoning_tokens = reasoning_tokens

            # Process costs for non-streaming mode
            model_name = str(self.model)
            interaction_cost = calculate_model_cost(model_name, input_tokens, output_tokens)

            # Process the costs through COST_TRACKER only once
            if interaction_cost > 0.0:
                # Check price limit before processing
                if hasattr(COST_TRACKER, "check_price_limit"):
                    COST_TRACKER.check_price_limit(interaction_cost)

                # Process interaction cost
                COST_TRACKER.process_interaction_cost(
                    model_name,
                    input_tokens,
                    output_tokens,
                    reasoning_tokens,
                    interaction_cost,
                    agent_name=self.agent_name,
                    agent_id=self.agent_id
                )

                # Process total cost
                total_cost = COST_TRACKER.process_total_cost(
                    model_name,
                    self.total_input_tokens,
                    self.total_output_tokens,
                    self.total_reasoning_tokens,
                    None,
                    agent_name=self.agent_name,
                    agent_id=self.agent_id
                )

                # Track usage globally
                GLOBAL_USAGE_TRACKER.track_usage(
                    model_name=model_name,
                    input_tokens=input_tokens,
                    output_tokens=output_tokens,
                    cost=interaction_cost,
                    agent_name=self.agent_name
                )
            else:
                # For free models
                total_cost = COST_TRACKER.session_total_cost

                # Still track token usage even for free models
                GLOBAL_USAGE_TRACKER.track_usage(
                    model_name=model_name,
                    input_tokens=input_tokens,
                    output_tokens=output_tokens,
                    cost=0.0,
                    agent_name=self.agent_name
                )

            # Check if this message contains tool calls
            tool_output = None
            should_display_message = True

            _first_choice = _get_first_choice(response)
            if (
                _first_choice
                and hasattr(_first_choice.message, "tool_calls")
                and _first_choice.message.tool_calls
            ):
                # For each tool call in the message, get corresponding output if available
                for tool_call in _first_choice.message.tool_calls:
                    call_id = tool_call.id

                    # Check if this tool call has already been displayed
                    if (
                        hasattr(_Converter, "tool_outputs")
                        and call_id in self._converter.tool_outputs
                    ):
                        tool_output_content = self._converter.tool_outputs[call_id]

                        # Check if this is a command sent to an existing async session
                        is_async_session_input = False
                        has_auto_output = False
                        is_regular_command = False
                        try:
                            import json

                            # Handle empty arguments before trying to parse JSON
                            tool_args = tool_call.function.arguments
                            if tool_args is None or (isinstance(tool_args, str) and tool_args.strip() == ""):
                                tool_args = "{}"

                            args = _safe_json_loads(tool_args, "tool_call arguments")
                            # Check if this is a regular command (not a session command)
                            if (
                                isinstance(args, dict)
                                and args.get("command")
                                and not args.get("session_id")
                                and not args.get("async_mode")
                            ):
                                is_regular_command = True
                            # Only consider it an async session input if it has session_id AND it's not creating a new session
                            elif (
                                isinstance(args, dict)
                                and args.get("session_id")
                                and not args.get("async_mode")  # Not creating a new session
                                and not args.get("creating_session")
                            ):  # Not marked as session creation
                                is_async_session_input = True
                                # Check if this has auto_output flag
                                has_auto_output = args.get("auto_output", False)
                        except (json.JSONDecodeError, AttributeError, TypeError):
                            pass

                        # For regular commands that were already shown via streaming, suppress the agent message
                        if (
                            is_regular_command
                            and tool_call.function.name == "generic_linux_command"
                        ):
                            # Check if this was executed very recently (likely shown via streaming)
                            if (
                                hasattr(_Converter, "recent_tool_calls")
                                and call_id in self._converter.recent_tool_calls
                            ):
                                tool_call_info = self._converter.recent_tool_calls[call_id]
                                if "start_time" in tool_call_info:
                                    import time

                                    time_since_execution = (
                                        time.time() - tool_call_info["start_time"]
                                    )
                                    # If executed within last 2 seconds, it was likely shown via streaming
                                    if time_since_execution < 2.0:
                                        should_display_message = False
                                        tool_output = None
                        elif is_async_session_input:
                            should_display_message = True
                            tool_output = None
                        # For async session inputs without auto_output, always show the agent message
                        elif is_async_session_input and not has_auto_output:
                            should_display_message = True
                            tool_output = None
                        # For session creation messages, also show them
                        elif (
                            "Started async session" in tool_output_content
                            or "session" in tool_output_content.lower()
                            and "async" in tool_output_content.lower()
                        ):
                            should_display_message = True
                            tool_output = None
                        else:
                            # For other tool calls, check if we should suppress based on timing
                            # Only suppress if this tool was JUST executed (within last 2 seconds)
                            if (
                                hasattr(_Converter, "recent_tool_calls")
                                and call_id in self._converter.recent_tool_calls
                            ):
                                tool_call_info = self._converter.recent_tool_calls[call_id]
                                if "start_time" in tool_call_info:
                                    import time

                                    time_since_execution = (
                                        time.time() - tool_call_info["start_time"]
                                    )
                                    # Only suppress if this was executed very recently
                                    if time_since_execution < 2.0:
                                        should_display_message = False
                                    else:
                                        # For older tool calls, show the message
                                        should_display_message = True
                        break

            # Additional check: Always show messages that have text content
            # This ensures agent explanations are not suppressed
            _fc = _get_first_choice(response)
            if (
                _fc
                and hasattr(_fc.message, "content")
                and _fc.message.content
                and str(_fc.message.content).strip()
            ):
                # If the message has actual text content, always show it
                should_display_message = True

            # Display the agent message (this will show the command for async sessions)
            if should_display_message:
                # Ensure we're in non-streaming mode for proper markdown parsing
                previous_stream_setting = os.environ.get("CAI_STREAM", "false")
                os.environ["CAI_STREAM"] = "false"  # Force non-streaming mode for markdown parsing

                # Extract cache metrics for display
                # Support both Anthropic format and OpenAI format (prompt_tokens_details.cached_tokens)
                cache_create = getattr(response.usage, 'cache_creation_input_tokens', None) if response.usage else None
                cache_read = getattr(response.usage, 'cache_read_input_tokens', None) if response.usage else None
                # Fallback to OpenAI format if Anthropic format not available
                if not cache_read and response.usage:
                    prompt_details = getattr(response.usage, 'prompt_tokens_details', None)
                    if prompt_details:
                        cache_read = getattr(prompt_details, 'cached_tokens', None)

                # Print the agent message for CLI display
                cli_print_agent_messages(
                    agent_name=getattr(self, "agent_name", "Agent"),
                    message=_get_first_choice(response).message if _get_first_choice(response) else None,
                    counter=getattr(self, "interaction_counter", 0),
                    model=str(self.model),
                    debug=False,
                    interaction_input_tokens=input_tokens,
                    interaction_output_tokens=output_tokens,
                    interaction_reasoning_tokens=reasoning_tokens,
                    total_input_tokens=getattr(self, "total_input_tokens", 0),
                    total_output_tokens=getattr(self, "total_output_tokens", 0),
                    total_reasoning_tokens=getattr(self, "total_reasoning_tokens", 0),
                    interaction_cost=interaction_cost,
                    total_cost=total_cost,
                    tool_output=tool_output,  # Pass tool_output only when needed
                    suppress_empty=True,  # Keep suppress_empty=True as requested
                    cache_creation_tokens=cache_create,
                    cache_read_tokens=cache_read,
                )

                # Restore previous streaming setting
                os.environ["CAI_STREAM"] = previous_stream_setting

            # --- DEFERRED: Tool calls are no longer added immediately ---
            # Tool calls will be added atomically with their responses
            # to prevent incomplete message history on interruption
            _first_for_msg = _get_first_choice(response)
            if not _first_for_msg:
                raise AgentsException("LLM returned response with no choices")
            assistant_msg = _first_for_msg.message
            if hasattr(assistant_msg, "tool_calls") and assistant_msg.tool_calls:
                # Store pending tool calls but don't add to history yet
                if not hasattr(self, "_pending_tool_calls"):
                    self._pending_tool_calls = {}


                # Fix Google Gemini OpenAI compatibility issues.
                # When using the OpenAI-compatible API to call tools with Google Gemini
                # tool_call.id is returned as an empty string.
                if "openai/gemini" in get_config().model:
                    for tool_call in assistant_msg.tool_calls:
                        if tool_call.id is None or tool_call.id == "":
                            tool_call.id = uuid.uuid4().hex[:16]

                for tool_call in assistant_msg.tool_calls:
                    # Handle empty arguments before storing
                    tool_args = tool_call.function.arguments
                    if tool_args is None or (isinstance(tool_args, str) and tool_args.strip() == ""):
                        tool_args = "{}"

                    # Compose a message for the tool call
                    tool_call_msg = {
                        "role": "assistant",
                        "content": None,
                        "tool_calls": [
                            {
                                "id": tool_call.id,
                                "type": tool_call.type,
                                "function": {
                                    "name": tool_call.function.name,
                                    "arguments": tool_args,
                                },
                            }
                        ],
                    }

                    # Store for later atomic addition with response
                    self._pending_tool_calls[tool_call.id] = tool_call_msg

                    # Save the tool call details for later matching with output
                    # This is important for non-streaming mode to track tool calls properly
                    if not hasattr(self._converter, "recent_tool_calls"):
                        self._converter.recent_tool_calls = {}

                    # Store the tool call by ID for later reference
                    import time

                    current_time = time.time()

                    # Periodic cleanup of old tool calls (older than 5 minutes)
                    # This prevents unbounded growth and memory issues
                    if len(self._converter.recent_tool_calls) > 50:
                        stale_threshold = current_time - 300  # 5 minutes
                        stale_keys = [
                            k for k, v in self._converter.recent_tool_calls.items()
                            if v.get("start_time", 0) < stale_threshold
                        ]
                        for k in stale_keys:
                            del self._converter.recent_tool_calls[k]

                    self._converter.recent_tool_calls[tool_call.id] = {
                        "name": tool_call.function.name,
                        "arguments": tool_call.function.arguments,
                        "start_time": current_time,
                        "execution_info": {"start_time": current_time},
                    }

                # Log the assistant tool call message
                tool_calls_list = []
                for tool_call in assistant_msg.tool_calls:
                    tool_calls_list.append(
                        {
                            "id": tool_call.id,
                            "type": tool_call.type,
                            "function": {
                                "name": tool_call.function.name,
                                "arguments": tool_call.function.arguments,
                            },
                        }
                    )
                self.logger.log_assistant_message(None, tool_calls_list)
            # If the assistant message is just text, add it as well
            else:
                text_out = None
                if hasattr(assistant_msg, "content") and assistant_msg.content:
                    text_out = assistant_msg.content
                else:
                    reasoning_text = _assistant_reasoning_text(assistant_msg)
                    if reasoning_text:
                        text_out = reasoning_text
                if text_out:
                    asst_msg = {"role": "assistant", "content": text_out}
                    self.add_to_message_history(asst_msg)
                    self.logger.log_assistant_message(text_out)

            # En no-streaming, también necesitamos añadir cualquier tool output al message_history
            # Esto se hace procesando los items de output del ModelResponse
            items = self._converter.message_to_output_items(assistant_msg)

            # Además, necesitamos añadir los tool outputs que se hayan generado
            # durante la ejecución de las herramientas
            if hasattr(_Converter, "tool_outputs"):
                for call_id, output_content in self._converter.tool_outputs.items():
                    # Verificar si ya existe un mensaje tool con este call_id en self.message_history
                    tool_msg_exists = any(
                        msg.get("role") == "tool" and msg.get("tool_call_id") == call_id
                        for msg in self.message_history
                    )

                    if not tool_msg_exists:
                        # Añadir el mensaje tool al message_history
                        tool_msg = {
                            "role": "tool",
                            "tool_call_id": call_id,
                            "content": output_content,
                        }
                        self.add_to_message_history(tool_msg)

            # Log the complete response for the session
            self.logger.rec_training_data(
                {
                    "model": str(self.model),
                    "messages": converted_messages,
                    "stream": False,
                    "tools": [t.params_json_schema for t in tools] if tools else [],
                    "tool_choice": model_settings.tool_choice,
                },
                response,
                self.total_cost,
                self.agent_name,
            )

            # Extract cache metrics from response if available
            # Support both Anthropic format and OpenAI format (prompt_tokens_details.cached_tokens)
            cache_creation = None
            cache_read = None
            if response.usage:
                cache_creation = getattr(response.usage, 'cache_creation_input_tokens', None)
                cache_read = getattr(response.usage, 'cache_read_input_tokens', None)
                # Fallback to OpenAI format if Anthropic format not available
                if not cache_read:
                    prompt_details = getattr(response.usage, 'prompt_tokens_details', None)
                    if prompt_details:
                        cache_read = getattr(prompt_details, 'cached_tokens', None)

            usage = (
                Usage(
                    requests=1,
                    input_tokens=input_tokens,
                    output_tokens=output_tokens,
                    total_tokens=input_tokens + output_tokens,
                    cache_creation_input_tokens=cache_creation,
                    cache_read_input_tokens=cache_read,
                )
                if response.usage or input_tokens > 0
                else Usage()
            )
            _trace_choice = _get_first_choice(response)
            if tracing.include_data() and _trace_choice:
                span_generation.span_data.output = [_trace_choice.message.model_dump()]
            span_generation.span_data.usage = {
                "input_tokens": usage.input_tokens,
                "output_tokens": usage.output_tokens,
            }

            if not _trace_choice:
                raise AgentsException("LLM returned response with no choices")
            items = self._converter.message_to_output_items(_trace_choice.message)

            # For non-streaming responses, make sure we also log token usage with compatible field names
            # This ensures both streaming and non-streaming use consistent naming
            if not hasattr(response, "usage"):
                response.usage = {}
            if hasattr(response.usage, "prompt_tokens") and not hasattr(
                response.usage, "input_tokens"
            ):
                response.usage.input_tokens = response.usage.prompt_tokens
            if hasattr(response.usage, "completion_tokens") and not hasattr(
                response.usage, "output_tokens"
            ):
                response.usage.output_tokens = response.usage.completion_tokens

            # Ensure cost is properly initialized
            if not hasattr(response, "cost"):
                response.cost = None

            return ModelResponse(
                output=items,
                usage=usage,
                referenceable_id=None,
            )

        # Stop active timer and start idle timer when response is complete
        stop_active_timer()
        start_idle_timer()

    async def stream_response(
        self,
        system_instructions: str | None,
        input: str | list[TResponseInputItem],
        model_settings: ModelSettings,
        tools: list[Tool],
        output_schema: AgentOutputSchema | None,
        handoffs: list[Handoff],
        tracing: ModelTracing,
        *,
        _empty_completion_streak: int = 0,
    ) -> AsyncIterator[TResponseStreamEvent]:
        """
        Yields a partial message as it is generated, as well as the usage information.
        """
        # Close any open streaming panels from the previous cycle
        # This ensures panels don't stay open when the model starts a new inference
        try:
            from cai.util import close_all_streaming_panels
            close_all_streaming_panels()
        except ImportError:
            pass

        # Initialize streaming contexts as None
        streaming_context = None
        thinking_context = None
        stream_interrupted = False
        stream_wait_hints: ModelStreamWaitHints | None = None

        try:
            # IMPORTANT: Pre-process input to ensure it's in the correct format
            # for streaming. This helps prevent errors during stream handling.
            if not isinstance(input, str):
                # Convert input items to messages and verify structure
                try:
                    input_items = list(input)  # Make sure it's a list
                    # Pre-verify the input messages to avoid errors during streaming
                    from cai.util import fix_message_list

                    # Apply fix_message_list to the input items that are dictionaries
                    dict_items = [item for item in input_items if isinstance(item, dict)]
                    if dict_items:
                        fixed_dict_items = fix_message_list(dict_items)

                        # Replace the original dict items with fixed ones while preserving non-dict items
                        new_input = []
                        dict_index = 0
                        for item in input_items:
                            if isinstance(item, dict):
                                if dict_index < len(fixed_dict_items):
                                    new_input.append(fixed_dict_items[dict_index])
                                    dict_index += 1
                            else:
                                new_input.append(item)

                        # Update input with the fixed version
                        input = new_input
                except Exception as e:
                    # Silently continue with original input if pre-processing failed
                    # This is not critical and shouldn't show warnings
                    pass

            # Increment the interaction counter for CLI display
            self.interaction_counter += 1
            self._intermediate_logs()

            # Stop idle timer and start active timer to track LLM processing time
            stop_idle_timer()
            start_active_timer()

            # --- Check if streaming should be shown in rich panel ---
            # Sub-agents invoked as tools by the orchestration agent must not
            # render Rich streaming panels — only the orchestrator's final
            # synthesis is shown to the user. See ``_worker_silence``.
            should_show_rich_stream = (
                get_config().stream
                and not self.disable_rich_streaming
                and not worker_display_silenced()
            )

            # Lazy-init Rich Live: build streaming_context on first text delta (not before HTTP),
            # so startup work (terminal sizing, Live allocation) stays off the pre-TTFT path.

            with generation_span(
                model=str(self.model),
                model_config=dataclasses.asdict(model_settings)
                | {"base_url": str(self._get_client().base_url)},
                disabled=tracing.is_disabled(),
            ) as span_generation:
                # Prepare messages for consistent token counting
                # IMPORTANT: Include existing message history for context (matching get_response pattern)
                converted_messages = self._shallow_copy_history_messages()

                # Then convert and add the new input
                new_messages = self._converter.items_to_messages(input, model_instance=self)
                converted_messages.extend(new_messages)

                if system_instructions:
                    # Check if we already have a system message
                    has_system = any(msg.get("role") == "system" for msg in converted_messages)
                    if not has_system:
                        converted_messages.insert(
                            0,
                            {
                                "content": system_instructions,
                                "role": "system",
                            },
                        )

                #    # --- Add to message_history: user, system prompts ---
                #     if system_instructions:
                #         sys_msg = {
                #             "role": "system",
                #             "content": system_instructions
                #         }
                #         self.add_to_message_history(sys_msg)

                if isinstance(input, str):
                    user_msg = {"role": "user", "content": input}
                    self.add_to_message_history(user_msg)
                    # Log the user message
                    self.logger.log_user_message(input)
                elif isinstance(input, list):
                    for item in input:
                        if isinstance(item, dict):
                            if item.get("role") == "user":
                                user_msg = {"role": "user", "content": item.get("content", "")}
                                self.add_to_message_history(user_msg)
                                # Log the user message
                                if item.get("content"):
                                    self.logger.log_user_message(item.get("content"))

                # IMPORTANT: Ensure the message list has valid tool call/result pairs
                # This needs to happen before the API call AND before applying cache_control
                try:
                    from cai.util import fix_message_list

                    converted_messages = fix_message_list(converted_messages)
                except Exception:
                    pass

                # Request-path message normalization/cache-control is applied in _fetch_response().
                # Keep startup estimation lightweight to avoid duplicate per-turn preprocessing work.

                # Get token count estimate before API call for consistent counting
                estimated_input_tokens, _ = count_tokens_with_tiktoken(converted_messages)

                # Check if auto-compaction is needed
                input, system_instructions, compacted = await self._auto_compact_if_needed(estimated_input_tokens, input, system_instructions)

                # If compaction occurred, recalculate tokens from the same view ``_fetch_response`` uses
                if compacted:
                    converted_messages = self._messages_for_token_count_after_history_mutation(
                        system_instructions=system_instructions,
                        input=input,
                    )
                    estimated_input_tokens, _ = count_tokens_with_tiktoken(converted_messages)
                    max_tok = self._get_model_max_tokens(str(self.model))
                    if max_tok > 0:
                        os.environ["CAI_CONTEXT_USAGE"] = str(
                            min(1.0, max(0.0, estimated_input_tokens / max_tok))
                        )

                # Pre-check price limit using estimated input tokens and a conservative estimate for output
                # This prevents starting a stream that would immediately exceed the price limit
                if hasattr(COST_TRACKER, "check_price_limit"):
                    # Use a conservative estimate for output tokens (roughly equal to input)
                    estimated_cost = calculate_model_cost(
                        str(self.model), estimated_input_tokens, estimated_input_tokens
                    )  # Conservative estimate
                    try:
                        COST_TRACKER.check_price_limit(estimated_cost)
                    except Exception:
                        # Ensure streaming context is cleaned up in case of errors
                        if streaming_context:
                            try:
                                finish_agent_streaming(streaming_context, None)
                            except Exception:
                                pass
                        # Stop active timer and start idle timer before re-raising the exception
                        stop_active_timer()
                        start_idle_timer()
                        raise

                stream_wait_hints = ModelStreamWaitHints()
                await stream_wait_hints.start()
                # Proactive client-side pacing for the alias gateway. The
                # stream wait hint is already active and reads
                # ``_retry_overlay_message``, so the overlay surfaces
                # automatically. ``alias_gateway_slot`` auto-releases on
                # pre-gateway errors (connect failures, KbInt) so a burst
                # doesn't pin the budget for 60s.
                _stream_on_pace = make_pace_overlay_callback()
                try:
                    if self._is_alias_model:
                        # Streaming pacing: same projection (+completion buffer)
                        # as get_response. ``Reservation.update_actual`` is NOT
                        # called here — final ``usage`` arrives at end-of-stream
                        # outside the slot. The limiter's 85% safety margin
                        # absorbs that residual estimation gap.
                        _stream_projection = estimated_input_tokens + COMPLETION_BUDGET_TOKENS
                        async with get_gateway_rate_limiter().alias_gateway_slot(
                            _stream_projection,
                            on_pace=_stream_on_pace,
                        ):
                            response, stream = await self._fetch_response(
                                system_instructions,
                                input,
                                model_settings,
                                tools,
                                output_schema,
                                handoffs,
                                span_generation,
                                tracing,
                                stream=True,
                            )
                    else:
                        response, stream = await self._fetch_response(
                            system_instructions,
                            input,
                            model_settings,
                            tools,
                            output_schema,
                            handoffs,
                            span_generation,
                            tracing,
                            stream=True,
                        )
                    # Clear any pacing overlay so the default stream body
                    # ("Esperando…") shows during the HTTP response read.
                    set_model_wait_retry_overlay(None)
                except KeyboardInterrupt:
                    await stream_wait_hints.stop()
                    set_model_wait_retry_overlay(None)
                    stop_active_timer()
                    start_idle_timer()
                    raise
                except (litellm.exceptions.Timeout, LLMTimeout) as e:
                    await stream_wait_hints.stop()
                    # Streaming timeout with exponential backoff — clean retry
                    self.logger.warning(f"Timeout in stream_response: {e}")
                    stop_active_timer()
                    start_idle_timer()

                    if not hasattr(self, "_high_level_retry_count"):
                        self._high_level_retry_count = 0
                    self._high_level_retry_count += 1

                    if self._high_level_retry_count > 3:
                        self._high_level_retry_count = 0
                        raise LLMTimeout(f"Timed out after 3 attempts [{self.model}]") from e

                    await self._retry_with_backoff(self._high_level_retry_count - 1, "Timeout")

                    # Clean retry: same input, no "continue" in history
                    async for event in self.stream_response(
                        system_instructions, input, model_settings,
                        tools, output_schema, handoffs, tracing,
                    ):
                        yield event
                    self._high_level_retry_count = 0
                    return

                except (litellm.exceptions.RateLimitError, LLMRateLimited) as e:
                    await stream_wait_hints.stop()
                    # Streaming rate-limit with exponential backoff — clean retry
                    self.logger.warning(f"Rate limit in stream_response: {e}")
                    stop_active_timer()
                    start_idle_timer()

                    if not hasattr(self, "_high_level_retry_count"):
                        self._high_level_retry_count = 0
                    self._high_level_retry_count += 1

                    if self._high_level_retry_count > 3:
                        self._high_level_retry_count = 0
                        raise LLMRateLimited(
                            f"Rate limit after 3 attempts [{self.model}]",
                            retry_after=getattr(e, "retry_after", None),
                        ) from e

                    await self._retry_with_backoff(self._high_level_retry_count - 1, "Rate limit")

                    # Clean retry: same input, no "continue" in history
                    async for event in self.stream_response(
                        system_instructions, input, model_settings,
                        tools, output_schema, handoffs, tracing,
                    ):
                        yield event
                    self._high_level_retry_count = 0
                    return

                except BaseException:
                    await stream_wait_hints.stop()
                    raise

                usage: CompletionUsage | None = None
                state = _StreamingState()

                def next_sequence_number() -> int:
                    sequence_number = state.sequence_number
                    state.sequence_number += 1
                    return sequence_number

                # Manual token counting (when API doesn't provide it)
                output_text = ""
                estimated_output_tokens = 0
                streaming_reasoning_text = ""

                # Initialize a streaming text accumulator for rich display
                streaming_text_buffer = ""
                # For tool call streaming, accumulate tool_calls to add to message_history at the end
                streamed_tool_calls = []

                # Initialize Claude thinking display if applicable
                if should_show_rich_stream:  # Only show thinking in rich streaming mode
                    thinking_context = start_claude_thinking_if_applicable(
                        str(self.model), self.agent_name, self.interaction_counter
                    )

                # Ollama specific: accumulate full content to check for function calls at the end
                # Some Ollama models output the function call as JSON in the text content
                ollama_full_content = ""
                is_ollama = False

                model_str = str(self.model).lower()
                is_ollama = (
                    self.is_ollama
                    or "ollama" in model_str
                    or ":" in model_str
                    or "qwen" in model_str
                )

                # Add visual separation before agent output
                if streaming_context and should_show_rich_stream:
                    # If we're using rich context, we'll add separation through that
                    pass
                else:
                    # Removed clear visual separator to avoid blank lines during streaming
                    pass

                try:
                    async for chunk in stream:
                        await stream_wait_hints.stop()

                        # Check if we've been interrupted
                        if stream_interrupted:
                            break

                        if not state.started:
                            state.started = True
                            yield ResponseCreatedEvent(
                                response=response,
                                sequence_number=next_sequence_number(),
                                type="response.created",
                            )

                        # The usage is only available in the last chunk
                        if hasattr(chunk, "usage"):
                            usage = chunk.usage
                        # For Ollama/LiteLLM streams that don't have usage attribute
                        else:
                            usage = None

                        # Handle different stream chunk formats
                        if hasattr(chunk, "choices") and chunk.choices:
                            choices = chunk.choices
                        elif hasattr(chunk, "delta") and chunk.delta:
                            # Some providers might return delta directly
                            choices = [{"delta": chunk.delta}]
                        elif isinstance(chunk, dict) and "choices" in chunk:
                            choices = chunk["choices"]
                        # Special handling for Qwen/Ollama chunks
                        elif isinstance(chunk, dict) and (
                            "content" in chunk or "function_call" in chunk
                        ):
                            # Qwen direct delta format - convert to standard
                            choices = [{"delta": chunk}]
                        else:
                            # Skip chunks that don't contain choice data
                            continue

                        if not choices or len(choices) == 0:
                            continue

                        # Get the delta content
                        delta = None
                        if hasattr(choices[0], "delta"):
                            delta = choices[0].delta
                        elif isinstance(choices[0], dict) and "delta" in choices[0]:
                            delta = choices[0]["delta"]

                        if not delta:
                            continue

                        # Handle Claude reasoning content first (before regular content)
                        reasoning_content = None

                        # Check for Claude reasoning in different possible formats
                        if (
                            hasattr(delta, "reasoning_content")
                            and delta.reasoning_content is not None
                        ):
                            reasoning_content = delta.reasoning_content
                        elif (
                            isinstance(delta, dict)
                            and "reasoning_content" in delta
                            and delta["reasoning_content"] is not None
                        ):
                            reasoning_content = delta["reasoning_content"]

                        # Also check for thinking_blocks structure (Claude 4 format)
                        thinking_blocks = None
                        if hasattr(delta, "thinking_blocks") and delta.thinking_blocks is not None:
                            thinking_blocks = delta.thinking_blocks
                        elif (
                            isinstance(delta, dict)
                            and "thinking_blocks" in delta
                            and delta["thinking_blocks"] is not None
                        ):
                            thinking_blocks = delta["thinking_blocks"]

                        # Extract reasoning content from thinking blocks if available
                        if thinking_blocks and not reasoning_content:
                            for block in thinking_blocks:
                                if isinstance(block, dict) and block.get("type") == "thinking":
                                    reasoning_content = block.get("thinking", "")
                                    break
                                elif (
                                    isinstance(block, dict)
                                    and block.get("type") == "text"
                                    and "thinking" in str(block)
                                ):
                                    # Sometimes thinking content comes as text blocks
                                    reasoning_content = block.get("text", "")
                                    break

                        # Check for direct thinking field (some Claude models)
                        if not reasoning_content:
                            if hasattr(delta, "thinking") and delta.thinking is not None:
                                reasoning_content = delta.thinking
                            elif (
                                isinstance(delta, dict)
                                and "thinking" in delta
                                and delta["thinking"] is not None
                            ):
                                reasoning_content = delta["thinking"]

                        # Update thinking display if we have reasoning content
                        if reasoning_content:
                            if isinstance(reasoning_content, str):
                                streaming_reasoning_text += reasoning_content
                            if thinking_context:
                                # Streaming mode: Update the rich thinking display
                                from cai.util import update_claude_thinking_content

                                update_claude_thinking_content(thinking_context, reasoning_content)
                            else:
                                # Non-streaming mode: Use simple text output
                                from cai.util import (
                                    detect_claude_thinking_in_stream,
                                    print_claude_reasoning_simple,
                                )

                                # Check if model supports reasoning (Claude or DeepSeek)
                                model_str_lower = str(self.model).lower()
                                if (
                                    detect_claude_thinking_in_stream(str(self.model))
                                    or "deepseek" in model_str_lower
                                ):
                                    print_claude_reasoning_simple(
                                        reasoning_content, self.agent_name, str(self.model)
                                    )

                        # Handle text
                        content = None
                        if hasattr(delta, "content") and delta.content is not None:
                            content = delta.content
                        elif (
                            isinstance(delta, dict)
                            and "content" in delta
                            and delta["content"] is not None
                        ):
                            content = delta["content"]

                        if content:
                            # IMPORTANT: If we have content and thinking_context is active,
                            # it means thinking is complete and normal content is starting
                            # Close the thinking display automatically
                            if thinking_context:
                                from cai.util import finish_claude_thinking_display

                                finish_claude_thinking_display(thinking_context)
                                thinking_context = None  # Clear the context

                            # For Ollama, we need to accumulate the full content to check for function calls
                            if is_ollama:
                                ollama_full_content += content

                            # Add to the streaming text buffer
                            streaming_text_buffer += content

                            # Update streaming display if enabled - ALWAYS respect CAI_STREAM setting
                            # Both thinking and regular content should stream if streaming is enabled
                            if (
                                should_show_rich_stream
                                and streaming_context is None
                            ):
                                try:
                                    streaming_context = create_agent_streaming_context(
                                        agent_name=self.agent_name,
                                        counter=self.interaction_counter,
                                        model=str(self.model),
                                    )
                                except Exception:
                                    streaming_context = None

                            if streaming_context:
                                # Calculate cost for current interaction
                                current_cost = calculate_model_cost(
                                    str(self.model), estimated_input_tokens, estimated_output_tokens
                                )

                                # Check price limit only for paid models
                                if (
                                    current_cost > 0
                                    and hasattr(COST_TRACKER, "check_price_limit")
                                    and estimated_output_tokens % 50 == 0
                                ):
                                    try:
                                        COST_TRACKER.check_price_limit(current_cost)
                                    except Exception:
                                        # Ensure streaming context is cleaned up
                                        if streaming_context:
                                            try:
                                                finish_agent_streaming(streaming_context, None)
                                            except Exception:
                                                pass
                                        # Stop timers and re-raise the exception
                                        stop_active_timer()
                                        start_idle_timer()
                                        raise

                                # Update session total cost for real-time display
                                # This is a temporary estimate during streaming that will be properly updated at the end
                                estimated_session_total = getattr(
                                    COST_TRACKER, "session_total_cost", 0.0
                                )

                                # For free models, don't add to the total cost
                                display_total_cost = estimated_session_total
                                if current_cost > 0:
                                    display_total_cost += current_cost

                                # Create token stats with both current interaction cost and updated total cost
                                token_stats = {
                                    "input_tokens": estimated_input_tokens,
                                    "output_tokens": estimated_output_tokens,
                                    "cost": current_cost,
                                    "total_cost": display_total_cost,
                                }

                                update_agent_streaming_content(
                                    streaming_context, content, token_stats
                                )

                            # More accurate token counting for text content
                            output_text += content
                            token_count, _ = count_tokens_with_tiktoken(output_text)
                            estimated_output_tokens = token_count

                            # Periodically check price limit during streaming
                            # This allows early termination if price limit is reached mid-stream
                            if (
                                estimated_output_tokens > 0 and estimated_output_tokens % 50 == 0
                            ):  # Check every ~50 tokens
                                # Calculate current estimated cost
                                current_estimated_cost = calculate_model_cost(
                                    str(self.model), estimated_input_tokens, estimated_output_tokens
                                )

                                # Check price limit only for paid models
                                if current_estimated_cost > 0 and hasattr(
                                    COST_TRACKER, "check_price_limit"
                                ):
                                    try:
                                        COST_TRACKER.check_price_limit(current_estimated_cost)
                                    except Exception:
                                        # Ensure streaming context is cleaned up
                                        if streaming_context:
                                            try:
                                                finish_agent_streaming(streaming_context, None)
                                            except Exception:
                                                pass
                                        # Stop timers and re-raise the exception
                                        stop_active_timer()
                                        start_idle_timer()
                                        raise

                                # Update the COST_TRACKER with the running cost for accurate display
                                if hasattr(COST_TRACKER, "interaction_cost"):
                                    COST_TRACKER.interaction_cost = current_estimated_cost

                                # Also update streaming context if available for live display
                                if streaming_context:
                                    # For free models, don't add to the session total
                                    if current_estimated_cost == 0:
                                        session_total = getattr(
                                            COST_TRACKER, "session_total_cost", 0.0
                                        )
                                    else:
                                        session_total = (
                                            getattr(COST_TRACKER, "session_total_cost", 0.0)
                                            + current_estimated_cost
                                        )

                                    updated_token_stats = {
                                        "input_tokens": estimated_input_tokens,
                                        "output_tokens": estimated_output_tokens,
                                        "cost": current_estimated_cost,
                                        "total_cost": session_total,
                                    }
                                    update_agent_streaming_content(
                                        streaming_context, "", updated_token_stats
                                    )

                            if not state.text_content_index_and_output:
                                # Initialize a content tracker for streaming text
                                state.text_content_index_and_output = (
                                    0 if not state.refusal_content_index_and_output else 1,
                                    ResponseOutputText(
                                        text="",
                                        type="output_text",
                                        annotations=[],
                                    ),
                                )
                                # Start a new assistant message stream
                                assistant_item = ResponseOutputMessage(
                                    id=FAKE_RESPONSES_ID,
                                    content=[],
                                    role="assistant",
                                    type="message",
                                    status="in_progress",
                                )
                                # Notify consumers of the start of a new output message + first content part
                                yield ResponseOutputItemAddedEvent(
                                    item=assistant_item,
                                    output_index=0,
                                    sequence_number=next_sequence_number(),
                                    type="response.output_item.added",
                                )
                                yield ResponseContentPartAddedEvent(
                                    content_index=state.text_content_index_and_output[0],
                                    item_id=FAKE_RESPONSES_ID,
                                    output_index=0,
                                    part=ResponseOutputText(
                                        text="",
                                        type="output_text",
                                        annotations=[],
                                    ),
                                    sequence_number=next_sequence_number(),
                                    type="response.content_part.added",
                                )
                            # Emit the delta for this segment of content
                            yield ResponseTextDeltaEvent(
                                content_index=state.text_content_index_and_output[0],
                                delta=content,
                                item_id=FAKE_RESPONSES_ID,
                                logprobs=[],
                                sequence_number=next_sequence_number(),
                                output_index=0,
                                type="response.output_text.delta",
                            )
                            # Accumulate the text into the response part
                            state.text_content_index_and_output[1].text += content

                        # Handle refusals (model declines to answer)
                        refusal_content = None
                        if hasattr(delta, "refusal") and delta.refusal:
                            refusal_content = delta.refusal
                        elif isinstance(delta, dict) and "refusal" in delta and delta["refusal"]:
                            refusal_content = delta["refusal"]

                        if refusal_content:
                            if not state.refusal_content_index_and_output:
                                # Initialize a content tracker for streaming refusal text
                                state.refusal_content_index_and_output = (
                                    0 if not state.text_content_index_and_output else 1,
                                    ResponseOutputRefusal(refusal="", type="refusal"),
                                )
                                # Start a new assistant message if one doesn't exist yet (in-progress)
                                assistant_item = ResponseOutputMessage(
                                    id=FAKE_RESPONSES_ID,
                                    content=[],
                                    role="assistant",
                                    type="message",
                                    status="in_progress",
                                )
                                # Notify downstream that assistant message + first content part are starting
                                yield ResponseOutputItemAddedEvent(
                                    item=assistant_item,
                                    output_index=0,
                                    sequence_number=next_sequence_number(),
                                    type="response.output_item.added",
                                )
                                yield ResponseContentPartAddedEvent(
                                    content_index=state.refusal_content_index_and_output[0],
                                    item_id=FAKE_RESPONSES_ID,
                                    output_index=0,
                                    part=ResponseOutputText(
                                        text="",
                                        type="output_text",
                                        annotations=[],
                                    ),
                                    sequence_number=next_sequence_number(),
                                    type="response.content_part.added",
                                )
                            # Emit the delta for this segment of refusal
                            yield ResponseRefusalDeltaEvent(
                                content_index=state.refusal_content_index_and_output[0],
                                delta=refusal_content,
                                item_id=FAKE_RESPONSES_ID,
                                sequence_number=next_sequence_number(),
                                output_index=0,
                                type="response.refusal.delta",
                            )
                            # Accumulate the refusal string in the output part
                            state.refusal_content_index_and_output[1].refusal += refusal_content

                        # Handle tool calls
                        # Because we don't know the name of the function until the end of the stream, we'll
                        # save everything and yield events at the end
                        tool_calls = self._detect_and_format_function_calls(delta)

                        if tool_calls:
                            for tc_delta in tool_calls:
                                tc_index = (
                                    tc_delta.index
                                    if hasattr(tc_delta, "index")
                                    else tc_delta.get("index", 0)
                                )
                                if tc_index not in state.function_calls:
                                    state.function_calls[tc_index] = ResponseFunctionToolCall(
                                        id=FAKE_RESPONSES_ID,
                                        arguments="",
                                        name="",
                                        type="function_call",
                                        call_id="",
                                    )

                                tc_function = None
                                if hasattr(tc_delta, "function"):
                                    tc_function = tc_delta.function
                                elif isinstance(tc_delta, dict) and "function" in tc_delta:
                                    tc_function = tc_delta["function"]

                                if tc_function:
                                    # Handle both object and dict formats
                                    args = ""
                                    if hasattr(tc_function, "arguments"):
                                        args = tc_function.arguments or ""
                                    elif (
                                        isinstance(tc_function, dict) and "arguments" in tc_function
                                    ):
                                        args = tc_function.get("arguments", "") or ""

                                    name = ""
                                    if hasattr(tc_function, "name"):
                                        name = tc_function.name or ""
                                    elif isinstance(tc_function, dict) and "name" in tc_function:
                                        name = tc_function.get("name", "") or ""

                                    state.function_calls[tc_index].arguments += args
                                    state.function_calls[tc_index].name += name

                                # Handle call_id in both formats
                                call_id = ""
                                if hasattr(tc_delta, "id"):
                                    call_id = tc_delta.id or ""
                                elif isinstance(tc_delta, dict) and "id" in tc_delta:
                                    call_id = tc_delta.get("id", "") or ""
                                else:
                                    # For Qwen models, generate a predictable ID if none is provided
                                    if state.function_calls[tc_index].name:
                                        # Generate a stable ID from the function name and arguments
                                        call_id = f"call_{hashlib.md5(state.function_calls[tc_index].name.encode()).hexdigest()[:8]}"

                                state.function_calls[tc_index].call_id += call_id

                                # --- Accumulate tool call for message_history ---
                                # Only add if not already present (avoid duplicates in streaming)
                                # Handle empty arguments before storing
                                tool_args = state.function_calls[tc_index].arguments
                                if tool_args is None or (isinstance(tool_args, str) and tool_args.strip() == ""):
                                    tool_args = "{}"

                                tool_call_msg = {
                                    "role": "assistant",
                                    "content": None,
                                    "tool_calls": [
                                        {
                                            "id": state.function_calls[tc_index].call_id,
                                            "type": "function",
                                            "function": {
                                                "name": state.function_calls[tc_index].name,
                                                "arguments": tool_args,
                                            },
                                        }
                                    ],
                                }
                                # Only add if not already in streamed_tool_calls
                                if tool_call_msg not in streamed_tool_calls:
                                    streamed_tool_calls.append(tool_call_msg)
                                    # Don't add to message history here - wait for tool output
                                    # to add both tool call and response atomically

                                    # NEW: Display tool call immediately when detected in streaming mode
                                    # But only if it has complete arguments and name
                                    if (
                                        state.function_calls[tc_index].name
                                        and state.function_calls[tc_index].arguments
                                        and state.function_calls[tc_index].call_id
                                    ):
                                        # First, finish any existing streaming context if it exists
                                        if streaming_context:
                                            try:
                                                finish_agent_streaming(streaming_context, None)
                                                streaming_context = None
                                            except Exception:
                                                pass

                                        # Create a message-like object for displaying the function call
                                        tool_msg = type(
                                            "ToolCallStreamDisplay",
                                            (),
                                            {
                                                "content": None,
                                                "tool_calls": [
                                                    type(
                                                        "ToolCallDetail",
                                                        (),
                                                        {
                                                            "function": type(
                                                                "FunctionDetail",
                                                                (),
                                                                {
                                                                    "name": state.function_calls[
                                                                        tc_index
                                                                    ].name,
                                                                    "arguments": state.function_calls[
                                                                        tc_index
                                                                    ].arguments,
                                                                },
                                                            ),
                                                            "id": state.function_calls[
                                                                tc_index
                                                            ].call_id,
                                                            "type": "function",
                                                        },
                                                    )
                                                ],
                                            },
                                        )

                                        # Display the tool call during streaming
                                        cli_print_agent_messages(
                                            agent_name=getattr(self, "agent_name", "Agent"),
                                            message=tool_msg,
                                            counter=getattr(self, "interaction_counter", 0),
                                            model=str(self.model),
                                            debug=False,
                                            interaction_input_tokens=estimated_input_tokens,
                                            interaction_output_tokens=estimated_output_tokens,
                                            interaction_reasoning_tokens=0,
                                            total_input_tokens=getattr(
                                                self, "total_input_tokens", 0
                                            )
                                            + estimated_input_tokens,
                                            total_output_tokens=getattr(
                                                self, "total_output_tokens", 0
                                            )
                                            + estimated_output_tokens,
                                            total_reasoning_tokens=getattr(
                                                self, "total_reasoning_tokens", 0
                                            ),
                                            interaction_cost=None,
                                            total_cost=None,
                                            tool_output=None,
                                            suppress_empty=True,
                                        )
                                        # Set flag to suppress final output to avoid duplication
                                        self.suppress_final_output = True

                except KeyboardInterrupt:
                    # Handle interruption during streaming
                    stream_interrupted = True
                    print("\n[Streaming interrupted by user]", file=sys.stderr)

                    # Let the exception propagate after cleanup
                    raise

                except Exception as e:
                    # Handle other exceptions during streaming
                    logger.error(f"Error during streaming: {e}")
                    if "token" in str(e).lower() or "limit" in str(e).lower():
                        print("\n📏 Token limit exceeded - Response truncated")
                    raise

                # Special handling for Ollama - check if accumulated text contains a valid function call
                if is_ollama and ollama_full_content and len(state.function_calls) == 0:
                    # Look for JSON object that might be a function call
                    try:
                        # Try to extract a JSON object from the content
                        json_start = ollama_full_content.find("{")
                        json_end = ollama_full_content.rfind("}") + 1

                        if json_start >= 0 and json_end > json_start:
                            json_str = ollama_full_content[json_start:json_end]
                            # Try to parse the JSON
                            parsed = _safe_json_loads(json_str, "Ollama function call")
                            if not parsed:
                                raise ValueError("Failed to parse Ollama function call JSON")

                            # Check if it looks like a function call
                            if "name" in parsed and "arguments" in parsed:
                                logger.debug(
                                    f"Found valid function call in Ollama output: {json_str}"
                                )

                                # Create a tool call ID
                                tool_call_id = f"call_{hashlib.md5((parsed['name'] + str(time.time())).encode()).hexdigest()[:8]}"

                                # Ensure arguments is a valid JSON string
                                arguments_str = ""
                                if isinstance(parsed["arguments"], dict):
                                    # Remove 'ctf' field if it exists
                                    if "ctf" in parsed["arguments"]:
                                        del parsed["arguments"]["ctf"]
                                    arguments_str = json.dumps(parsed["arguments"])
                                elif isinstance(parsed["arguments"], str):
                                    # If it's already a string, check if it's valid JSON
                                    # Try parsing to validate and remove 'ctf' if present
                                    args_dict = _safe_json_loads(parsed["arguments"], "Ollama tool arguments")
                                    if args_dict:
                                        if isinstance(args_dict, dict) and "ctf" in args_dict:
                                            del args_dict["ctf"]
                                        arguments_str = json.dumps(args_dict)
                                    else:
                                        # If not valid JSON, encode it as a JSON string
                                        arguments_str = json.dumps(parsed["arguments"])
                                else:
                                    # For any other type, convert to string and then JSON
                                    arguments_str = json.dumps(str(parsed["arguments"]))
                                # Add it to our function_calls state
                                state.function_calls[0] = ResponseFunctionToolCall(
                                    id=FAKE_RESPONSES_ID,
                                    arguments=arguments_str,
                                    name=parsed["name"],
                                    type="function_call",
                                    call_id=tool_call_id[:40],
                                )

                                # Display the tool call in CLI
                                try:
                                    # First, finish any existing streaming context if it exists
                                    if streaming_context:
                                        try:
                                            finish_agent_streaming(streaming_context, None)
                                            streaming_context = None
                                        except Exception:
                                            pass

                                    # Create a message-like object to display the function call
                                    tool_msg = type(
                                        "ToolCallWrapper",
                                        (),
                                        {
                                            "content": None,
                                            "tool_calls": [
                                                type(
                                                    "ToolCallDetail",
                                                    (),
                                                    {
                                                        "function": type(
                                                            "FunctionDetail",
                                                            (),
                                                            {
                                                                "name": parsed["name"],
                                                                "arguments": arguments_str,
                                                            },
                                                        ),
                                                        "id": tool_call_id[:40],
                                                        "type": "function",
                                                    },
                                                )
                                            ],
                                        },
                                    )

                                    # Print the tool call using the CLI utility
                                    cli_print_agent_messages(
                                        agent_name=getattr(self, "agent_name", "Agent"),
                                        message=tool_msg,
                                        counter=getattr(self, "interaction_counter", 0),
                                        model=str(self.model),
                                        debug=False,
                                        interaction_input_tokens=estimated_input_tokens,
                                        interaction_output_tokens=estimated_output_tokens,
                                        interaction_reasoning_tokens=0,
                                        total_input_tokens=getattr(
                                            self, "total_input_tokens", 0
                                        )
                                        + estimated_input_tokens,
                                        total_output_tokens=getattr(
                                            self, "total_output_tokens", 0
                                        )
                                        + estimated_output_tokens,
                                        total_reasoning_tokens=getattr(
                                            self, "total_reasoning_tokens", 0
                                        ),
                                        interaction_cost=None,
                                        total_cost=None,
                                        tool_output=None,
                                        suppress_empty=True,
                                    )

                                    # Set flag to suppress final output to avoid duplication
                                    self.suppress_final_output = True
                                except Exception as e:
                                    # Silently log the error - don't disrupt the flow
                                    logger.debug(f"Display error (non-critical): {e}")

                                # Add to message history
                                tool_call_msg = {
                                    "role": "assistant",
                                    "content": None,
                                    "tool_calls": [
                                        {
                                            "id": tool_call_id,
                                            "type": "function",
                                            "function": {
                                                "name": parsed["name"],
                                                "arguments": arguments_str,
                                            },
                                        }
                                    ],
                                }

                                streamed_tool_calls.append(tool_call_msg)
                                # Don't add to message history here - wait for tool output
                                # to add both tool call and response atomically

                                logger.debug(
                                    f"Added function call: {parsed['name']} with args: {arguments_str}"
                                )
                    except Exception:
                        pass

                if _is_effectively_empty_stream_accumulation(
                    state,
                    streamed_tool_calls,
                    output_text,
                    streaming_reasoning_text,
                ):
                    empty_streak = _empty_completion_streak + 1
                    max_empty_failures = _empty_completion_max_failures()
                    self.logger.warning(
                        "Empty streamed assistant completion (%s/%s); "
                        "pt_est=%s reasoning_len=%s tools=%s; repeating.",
                        empty_streak,
                        max_empty_failures,
                        estimated_input_tokens,
                        len(streaming_reasoning_text),
                        len(streamed_tool_calls) + len(state.function_calls),
                    )
                    if empty_streak >= max_empty_failures:
                        stop_active_timer()
                        start_idle_timer()
                        raise LLMEmptyAssistantError(
                            "Consecutive empty assistant completions from the provider.",
                            {"attempts": max_empty_failures},
                        )
                    if streaming_context:
                        try:
                            finish_agent_streaming(streaming_context, None)
                        except Exception:
                            pass
                        streaming_context = None
                    if thinking_context:
                        try:
                            from cai.util import finish_claude_thinking_display

                            finish_claude_thinking_display(thinking_context)
                        except Exception:
                            pass
                        thinking_context = None
                    await stream_wait_hints.stop()
                    set_model_wait_retry_overlay(None)
                    input, system_instructions, estimated_input_tokens = (
                        await self._recover_after_empty_completion(
                            empty_streak=empty_streak,
                            estimated_input_tokens=estimated_input_tokens,
                            input=input,
                            system_instructions=system_instructions,
                        )
                    )
                    set_model_wait_retry_overlay(
                        "Provider returned an empty response; "
                        f"retrying ({empty_streak}/{max_empty_failures})…"
                    )
                    async for event in self.stream_response(
                        system_instructions,
                        input,
                        model_settings,
                        tools,
                        output_schema,
                        handoffs,
                        tracing,
                        _empty_completion_streak=empty_streak,
                    ):
                        yield event
                    return

                function_call_starting_index = 0
                if state.text_content_index_and_output:
                    function_call_starting_index += 1
                    # Send end event for this content part
                    yield ResponseContentPartDoneEvent(
                        content_index=state.text_content_index_and_output[0],
                        item_id=FAKE_RESPONSES_ID,
                        output_index=0,
                        part=state.text_content_index_and_output[1],
                        sequence_number=next_sequence_number(),
                        type="response.content_part.done",
                    )

                if state.refusal_content_index_and_output:
                    function_call_starting_index += 1
                    # Send end event for this content part
                    yield ResponseContentPartDoneEvent(
                        content_index=state.refusal_content_index_and_output[0],
                        item_id=FAKE_RESPONSES_ID,
                        output_index=0,
                        part=state.refusal_content_index_and_output[1],
                        sequence_number=next_sequence_number(),
                        type="response.content_part.done",
                    )

                # Actually send events for the function calls
                for function_call in state.function_calls.values():
                    # First, a ResponseOutputItemAdded for the function call
                    yield ResponseOutputItemAddedEvent(
                        item=ResponseFunctionToolCall(
                            id=FAKE_RESPONSES_ID,
                            call_id=function_call.call_id[:40],
                            arguments=function_call.arguments,
                            name=function_call.name,
                            type="function_call",
                        ),
                        output_index=function_call_starting_index,
                        sequence_number=next_sequence_number(),
                        type="response.output_item.added",
                    )
                    # Then, yield the args
                    yield ResponseFunctionCallArgumentsDeltaEvent(
                        delta=function_call.arguments,
                        item_id=FAKE_RESPONSES_ID,
                        output_index=function_call_starting_index,
                        sequence_number=next_sequence_number(),
                        type="response.function_call_arguments.delta",
                    )
                    # Finally, the ResponseOutputItemDone
                    yield ResponseOutputItemDoneEvent(
                        item=ResponseFunctionToolCall(
                            id=FAKE_RESPONSES_ID,
                            call_id=function_call.call_id[:40],
                            arguments=function_call.arguments,
                            name=function_call.name,
                            type="function_call",
                        ),
                        output_index=function_call_starting_index,
                        sequence_number=next_sequence_number(),
                        type="response.output_item.done",
                    )

                # Finally, send the Response completed event
                outputs: list[ResponseOutputItem] = []
                if state.text_content_index_and_output or state.refusal_content_index_and_output:
                    assistant_msg = ResponseOutputMessage(
                        id=FAKE_RESPONSES_ID,
                        content=[],
                        role="assistant",
                        type="message",
                        status="completed",
                    )
                    if state.text_content_index_and_output:
                        assistant_msg.content.append(state.text_content_index_and_output[1])
                    if state.refusal_content_index_and_output:
                        assistant_msg.content.append(state.refusal_content_index_and_output[1])
                    outputs.append(assistant_msg)

                    # send a ResponseOutputItemDone for the assistant message
                    yield ResponseOutputItemDoneEvent(
                        item=assistant_msg,
                        output_index=0,
                        sequence_number=next_sequence_number(),
                        type="response.output_item.done",
                    )

                for function_call in state.function_calls.values():
                    outputs.append(function_call)

                final_response = response.model_copy()
                final_response.output = outputs

                # Get final token counts using consistent method
                input_tokens = estimated_input_tokens
                output_tokens = estimated_output_tokens

                # Use API token counts if available and reasonable
                if usage and hasattr(usage, "prompt_tokens") and usage.prompt_tokens > 0:
                    input_tokens = usage.prompt_tokens
                if usage and hasattr(usage, "completion_tokens") and usage.completion_tokens > 0:
                    output_tokens = usage.completion_tokens

                # Extract cache metrics from the usage object (if available from direct HTTP path)
                # Support both Anthropic format and OpenAI format (prompt_tokens_details.cached_tokens)
                cache_creation = getattr(usage, 'cache_creation_input_tokens', None) if usage else None
                cache_read = getattr(usage, 'cache_read_input_tokens', None) if usage else None
                # Fallback to OpenAI format if Anthropic format not available
                if not cache_read and usage:
                    prompt_details = getattr(usage, 'prompt_tokens_details', None)
                    if prompt_details:
                        cache_read = getattr(prompt_details, 'cached_tokens', None)

                # Create a proper usage object with our token counts
                final_response.usage = CustomResponseUsage(
                    input_tokens=input_tokens,
                    output_tokens=output_tokens,
                    total_tokens=input_tokens + output_tokens,
                    output_tokens_details=OutputTokensDetails(
                        reasoning_tokens=usage.completion_tokens_details.reasoning_tokens
                        if usage
                        and hasattr(usage, "completion_tokens_details")
                        and usage.completion_tokens_details
                        and hasattr(usage.completion_tokens_details, "reasoning_tokens")
                        and usage.completion_tokens_details.reasoning_tokens
                        else 0
                    ),
                    input_tokens_details={
                        "prompt_tokens": input_tokens,
                        "cached_tokens": usage.prompt_tokens_details.cached_tokens
                        if usage
                        and hasattr(usage, "prompt_tokens_details")
                        and usage.prompt_tokens_details
                        and hasattr(usage.prompt_tokens_details, "cached_tokens")
                        and usage.prompt_tokens_details.cached_tokens
                        else 0,
                    },
                    cache_creation_input_tokens=cache_creation,
                    cache_read_input_tokens=cache_read,
                )

                yield ResponseCompletedEvent(
                    response=final_response,
                    sequence_number=next_sequence_number(),
                    type="response.completed",
                )

                # Update token totals for CLI display
                if final_response.usage:
                    # Always update the total counters with the best available counts
                    self.total_input_tokens += final_response.usage.input_tokens
                    self.total_output_tokens += final_response.usage.output_tokens
                    if final_response.usage.output_tokens_details and hasattr(
                        final_response.usage.output_tokens_details, "reasoning_tokens"
                    ):
                        self.total_reasoning_tokens += (
                            final_response.usage.output_tokens_details.reasoning_tokens
                        )

                # Prepare final statistics for display
                interaction_input = final_response.usage.input_tokens if final_response.usage else 0
                interaction_output = (
                    final_response.usage.output_tokens if final_response.usage else 0
                )
                total_input = getattr(self, "total_input_tokens", 0)
                total_output = getattr(self, "total_output_tokens", 0)

                # Calculate costs for this model
                model_name = str(self.model)
                interaction_cost = calculate_model_cost(
                    model_name, interaction_input, interaction_output
                )
                # Get the previous total cost and add this interaction's cost
                # Don't recalculate cost for all tokens - that causes double-counting
                previous_total = getattr(COST_TRACKER, "session_total_cost", 0.0)
                total_cost = previous_total + interaction_cost

                # If interaction cost is zero, this is a free model
                if interaction_cost == 0:
                    # For free models, keep existing total and ensure cost tracking system knows it's free
                    total_cost = getattr(COST_TRACKER, "session_total_cost", 0.0)
                    if hasattr(COST_TRACKER, "reset_cost_for_local_model"):
                        COST_TRACKER.reset_cost_for_local_model(model_name)

                # Explicit conversion to float with fallback to ensure they're never None or 0
                interaction_cost = float(interaction_cost if interaction_cost is not None else 0.0)
                total_cost = float(total_cost if total_cost is not None else 0.0)

                # Process costs through COST_TRACKER only once per interaction
                if interaction_cost > 0.0:
                    # Check price limit before processing the new cost
                    if hasattr(COST_TRACKER, "check_price_limit"):
                        try:
                            COST_TRACKER.check_price_limit(interaction_cost)
                        except Exception:
                            # Ensure streaming context is cleaned up
                            if streaming_context:
                                try:
                                    finish_agent_streaming(streaming_context, None)
                                except Exception:
                                    pass
                            # Stop timers and re-raise the exception
                            stop_active_timer()
                            start_idle_timer()
                            raise

                    # Process the interaction cost (updates internal tracking)
                    COST_TRACKER.process_interaction_cost(
                        model_name,
                        interaction_input,
                        interaction_output,
                        final_response.usage.output_tokens_details.reasoning_tokens
                        if final_response.usage
                        and final_response.usage.output_tokens_details
                        and hasattr(final_response.usage.output_tokens_details, "reasoning_tokens")
                        else 0,
                        interaction_cost,
                        agent_name=self.agent_name,
                        agent_id=self.agent_id
                    )

                    # Process the total cost (updates session total correctly)
                    total_cost = COST_TRACKER.process_total_cost(
                        model_name,
                        total_input,
                        total_output,
                        getattr(self, "total_reasoning_tokens", 0),
                        None,  # Let it calculate from tokens
                        agent_name=self.agent_name,
                        agent_id=self.agent_id
                    )

                    # Track usage globally
                    GLOBAL_USAGE_TRACKER.track_usage(
                        model_name=model_name,
                        input_tokens=interaction_input,
                        output_tokens=interaction_output,
                        cost=interaction_cost,
                        agent_name=self.agent_name
                    )
                else:
                    # For free models, still track token usage
                    GLOBAL_USAGE_TRACKER.track_usage(
                        model_name=model_name,
                        input_tokens=interaction_input,
                        output_tokens=interaction_output,
                        cost=0.0,
                        agent_name=self.agent_name
                    )

                # Store the total cost for future recording
                self.total_cost = total_cost
                # Update per-interaction tokens for tool panels
                self.interaction_input_tokens = int(interaction_input)
                self.interaction_output_tokens = int(interaction_output)
                self.interaction_reasoning_tokens = int(
                    final_response.usage.output_tokens_details.reasoning_tokens
                    if final_response.usage
                    and final_response.usage.output_tokens_details
                    and hasattr(final_response.usage.output_tokens_details, "reasoning_tokens")
                    else 0
                )
                # Update cache tokens for tool panels
                self.cache_read_tokens = int(cache_read) if cache_read else 0
                self.cache_creation_tokens = int(cache_creation) if cache_creation else 0

                # Also update COST_TRACKER so it's available for tool panels
                try:
                    import cai.util
                    cai.util.COST_TRACKER.interaction_input_tokens = self.interaction_input_tokens
                    cai.util.COST_TRACKER.interaction_output_tokens = self.interaction_output_tokens
                    cai.util.COST_TRACKER.interaction_reasoning_tokens = self.interaction_reasoning_tokens
                    cai.util.COST_TRACKER.cache_read_tokens = self.cache_read_tokens
                    cai.util.COST_TRACKER.cache_creation_tokens = self.cache_creation_tokens
                except Exception:
                    pass

                # Create final stats with explicit type conversion for all values
                final_stats = {
                    "interaction_input_tokens": int(interaction_input),
                    "interaction_output_tokens": int(interaction_output),
                    "interaction_reasoning_tokens": int(
                        final_response.usage.output_tokens_details.reasoning_tokens
                        if final_response.usage
                        and final_response.usage.output_tokens_details
                        and hasattr(final_response.usage.output_tokens_details, "reasoning_tokens")
                        else 0
                    ),
                    "total_input_tokens": int(total_input),
                    "total_output_tokens": int(total_output),
                    "total_reasoning_tokens": int(getattr(self, "total_reasoning_tokens", 0)),
                    "interaction_cost": float(interaction_cost),
                    "total_cost": float(total_cost),
                    "cache_read_tokens": int(cache_read) if cache_read else 0,
                    "cache_creation_tokens": int(cache_creation) if cache_creation else 0,
                }

                # At the end of streaming, finish the streaming context if we were using it
                if streaming_context:
                    # Create a direct copy of the costs to ensure they remain as floats
                    direct_stats = final_stats.copy()
                    direct_stats["interaction_cost"] = float(interaction_cost)
                    direct_stats["total_cost"] = float(total_cost)
                    # Use the direct copy with guaranteed float costs
                    finish_agent_streaming(streaming_context, direct_stats)
                    streaming_context = None

                    # Removed extra newline after streaming completes to avoid blank lines
                    pass

                # Finish Claude thinking display if it was active
                if thinking_context:
                    from cai.util import finish_claude_thinking_display

                    finish_claude_thinking_display(thinking_context)

                    # Note: Content is now displayed during streaming, no need to show it again here

                if tracing.include_data():
                    span_generation.span_data.output = [final_response.model_dump()]

                span_generation.span_data.usage = {
                    "input_tokens": input_tokens,
                    "output_tokens": output_tokens,
                }

                # --- DEFERRED: Tool calls are no longer added immediately ---
                # Store pending tool calls but don't add to history yet
                if not hasattr(self, "_pending_tool_calls"):
                    self._pending_tool_calls = {}

                for tool_call_msg in streamed_tool_calls:
                    # Extract tool call ID from the message
                    if tool_call_msg.get("tool_calls"):
                        for tc in tool_call_msg["tool_calls"]:
                            self._pending_tool_calls[tc["id"]] = tool_call_msg

                # Log the assistant tool call message if any tool calls were collected
                if streamed_tool_calls:
                    tool_calls_list = []
                    for tool_call_msg in streamed_tool_calls:
                        for tool_call in tool_call_msg.get("tool_calls", []):
                            tool_calls_list.append(tool_call)
                    self.logger.log_assistant_message(None, tool_calls_list)

                # Always log text content if it exists, regardless of suppress_final_output
                # The suppress_final_output flag is only for preventing duplicate tool call display
                if (
                    state.text_content_index_and_output
                    and state.text_content_index_and_output[1].text
                ):
                    asst_msg = {
                        "role": "assistant",
                        "content": state.text_content_index_and_output[1].text,
                    }
                    self.add_to_message_history(asst_msg)
                    # Log the assistant message
                    self.logger.log_assistant_message(state.text_content_index_and_output[1].text)

                # Reset the suppress flag for future requests
                self.suppress_final_output = False

                # Log the complete response
                self.logger.rec_training_data(
                    {
                        "model": str(self.model),
                        "messages": converted_messages,
                        "stream": True,
                        "tools": [t.params_json_schema for t in tools] if tools else [],
                        "tool_choice": model_settings.tool_choice,
                    },
                    final_response,
                    self.total_cost,
                    self.agent_name,
                )

                # Stop active timer and start idle timer when streaming is complete
                stop_active_timer()
                start_idle_timer()

        except KeyboardInterrupt:
            # Handle keyboard interruption specifically
            stream_interrupted = True

            # Ensure message history consistency by adding synthetic tool results
            # for any tool calls that were added but don't have corresponding results
            try:
                # Find all tool calls in recent assistant messages
                orphaned_tool_calls = []
                for msg in reversed(self.message_history[-10:]):  # Check recent messages
                    if msg.get("role") == "assistant" and msg.get("tool_calls"):
                        for tool_call in msg["tool_calls"]:
                            call_id = tool_call.get("id")
                            if call_id:
                                # Check if this tool call has a corresponding tool result
                                has_result = any(
                                    m.get("role") == "tool" and m.get("tool_call_id") == call_id
                                    for m in self.message_history
                                )
                                if not has_result:
                                    orphaned_tool_calls.append((call_id, tool_call))

                # Add synthetic tool results for orphaned tool calls
                for call_id, tool_call in orphaned_tool_calls:
                    tool_response_msg = {
                        "role": "tool",
                        "tool_call_id": call_id,
                        "content": "Tool execution interrupted"
                    }
                    self.add_to_message_history(tool_response_msg)

            except Exception as cleanup_error:
                # Don't let cleanup errors mask the original KeyboardInterrupt
                logger.debug(f"Error during interrupt cleanup: {cleanup_error}")

            # Make sure to clean up and re-raise
            raise

        except Exception as e:
            # Handle other exceptions
            logger.error(f"Error in stream_response: {e}")
            raise

        finally:
            # Always clean up resources
            # This block executes whether the try block succeeds, fails, or is interrupted

            if stream_wait_hints is not None:
                try:
                    await stream_wait_hints.stop()
                except Exception:
                    pass

            # Clean up streaming context
            if streaming_context:
                try:
                    # Check if we need to force stop the streaming panel
                    if streaming_context.get("is_started", False) and streaming_context.get("live"):
                        streaming_context["live"].stop()

                    # Remove from active streaming contexts
                    if hasattr(create_agent_streaming_context, "_active_streaming"):
                        for key, value in list(
                            create_agent_streaming_context._active_streaming.items()
                        ):
                            if value is streaming_context:
                                del create_agent_streaming_context._active_streaming[key]
                                break
                except Exception as cleanup_error:
                    logger.debug(f"Error cleaning up streaming context: {cleanup_error}")

            # Clean up thinking context
            if thinking_context:
                try:
                    # Force finish the thinking display
                    from cai.util import finish_claude_thinking_display

                    finish_claude_thinking_display(thinking_context)
                except Exception as cleanup_error:
                    logger.debug(f"Error cleaning up thinking context: {cleanup_error}")

            # Clean up any live streaming panels
            if hasattr(cli_print_tool_output, "_streaming_sessions"):
                # Find any sessions related to this stream
                for call_id in list(cli_print_tool_output._streaming_sessions.keys()):
                    if call_id in _LIVE_STREAMING_PANELS:
                        try:
                            live = _LIVE_STREAMING_PANELS[call_id]
                            live.stop()
                            del _LIVE_STREAMING_PANELS[call_id]
                        except Exception:
                            pass

            # Stop active timer and start idle timer
            try:
                stop_active_timer()
                start_idle_timer()
            except Exception:
                pass

            # Stream cleanup completed

    @overload
    async def _fetch_response(
        self,
        system_instructions: str | None,
        input: str | list[TResponseInputItem],
        model_settings: ModelSettings,
        tools: list[Tool],
        output_schema: AgentOutputSchema | None,
        handoffs: list[Handoff],
        span: Span[GenerationSpanData],
        tracing: ModelTracing,
        stream: Literal[True],
    ) -> tuple[Response, AsyncStream[ChatCompletionChunk]]: ...

    @overload
    async def _fetch_response(
        self,
        system_instructions: str | None,
        input: str | list[TResponseInputItem],
        model_settings: ModelSettings,
        tools: list[Tool],
        output_schema: AgentOutputSchema | None,
        handoffs: list[Handoff],
        span: Span[GenerationSpanData],
        tracing: ModelTracing,
        stream: Literal[False],
    ) -> ChatCompletion: ...

    async def _fetch_response(
        self,
        system_instructions: str | None,
        input: str | list[TResponseInputItem],
        model_settings: ModelSettings,
        tools: list[Tool],
        output_schema: AgentOutputSchema | None,
        handoffs: list[Handoff],
        span: Span[GenerationSpanData],
        tracing: ModelTracing,
        stream: bool = False,
    ) -> ChatCompletion | tuple[Response, AsyncStream[ChatCompletionChunk]]:
        # Debug: Print when entering _fetch_response
        if os.getenv("CAI_SHOW_CACHE", "").lower() in ("true", "1", "yes"):
            print(f"[CACHE-DEBUG] _fetch_response called, stream={stream}, model={self.model}")

        # start by re-fetching self.is_ollama
        self.is_ollama = os.getenv("OLLAMA") is not None and os.getenv("OLLAMA").lower() == "true"

        # IMPORTANT: Include existing message history for context
        converted_messages = self._shallow_copy_history_messages()

        # IMPORTANT: We maintain our own message_history which already contains all messages.
        # The SDK also passes 'input' with conversation items, but these duplicate what we have.
        # To avoid duplication: if we have message_history, DON'T add anything from input.
        # The caller (get_response/get_streamed_response) already adds messages to our history.
        if not self.message_history:
            # First turn: no history yet, so we need to use input
            new_messages = self._converter.items_to_messages(input, model_instance=self)
            converted_messages.extend(new_messages)

        if system_instructions:
            # Check if we already have a system message
            has_system = any(msg.get("role") == "system" for msg in converted_messages)
            if not has_system:
                # Inject shared session context if available
                from cai.sdk.agents.simple_agent_manager import AGENT_MANAGER
                shared_context = AGENT_MANAGER.get_shared_context_injection()

                final_system_instructions = system_instructions + shared_context

                converted_messages.insert(
                    0,
                    {
                        "content": final_system_instructions,
                        "role": "system",
                    },
                )
            else:
                # System message already exists, append shared context to it
                from cai.sdk.agents.simple_agent_manager import AGENT_MANAGER
                shared_context = AGENT_MANAGER.get_shared_context_injection()

                if shared_context:
                    for msg in converted_messages:
                        if msg.get("role") == "system":
                            msg["content"] = str(msg.get("content", "")) + shared_context
                            break

        # IMPORTANT: Always sanitize the message list to prevent tool call errors
        # This is critical to fix common errors with tool/assistant sequences
        # Must happen BEFORE applying cache_control
        try:
            from cai.util import fix_message_list

            prev_length = len(converted_messages)
            converted_messages = fix_message_list(converted_messages)
            new_length = len(converted_messages)

            # Log if the message list was changed significantly
            if new_length != prev_length:
                logger.debug(f"Message list was fixed: {prev_length} -> {new_length} messages")
        except Exception:
            pass

        # Add support for prompt caching for claude (not automatically applied)
        # Gemini supports it too
        # https://www.anthropic.com/news/token-saving-updates
        # Maximize cache efficiency by using up to 4 cache_control blocks
        # IMPORTANT: Apply cache_control AFTER fix_message_list() to ensure it's preserved
        # Note: Use "claude" in string to support both direct and openrouter/anthropic/claude models
        model_str = str(self.model).lower()
        if ("claude" in model_str or "gemini" in model_str) and len(
            converted_messages
        ) > 0:
            # Debug: Show messages BEFORE normalization
            if os.getenv("CAI_SHOW_CACHE", "").lower() in ("true", "1", "yes"):
                print(f"[CACHE-DEBUG] BEFORE normalization: {len(converted_messages)} messages")
                # Compute pre-normalization hashes to see if messages change during normalization
                for i, msg in enumerate(converted_messages[:5]):  # Show first 5 only
                    role = msg.get("role", "?")
                    content = msg.get("content")
                    content_type = type(content).__name__
                    has_cc = False
                    if isinstance(content, list):
                        for block in content:
                            if isinstance(block, dict) and "cache_control" in block:
                                has_cc = True
                                break
                    elif isinstance(content, dict) and "cache_control" in content:
                        has_cc = True
                    cc_note = " (has cache_control)" if has_cc else ""
                    pre_hash = hashlib.md5(json.dumps(msg, sort_keys=True, default=str).encode()).hexdigest()[:8]
                    print(f"  [PRE-{i}] {role}: content={content_type}{cc_note} hash={pre_hash}")
            # STEP 1: Normalize messages for consistent structure
            # This is CRITICAL for cache matching - the content structure must be identical
            # between turns for Anthropic to recognize the prefix and read from cache.
            #
            # IMPORTANT: Normalize ALL messages to block format for cache_control support
            # The proxy converts to Anthropic format anyway, so block format works for all roles.
            # - system/user/assistant/tool: All use block format [{"type": "text", "text": "..."}]
            # - assistant with tool_calls: Content can be None, tool_calls is separate
            for msg in converted_messages:
                role = msg.get("role")
                content = msg.get("content")

                # Skip messages without content (e.g., assistant with only tool_calls)
                if content is None:
                    continue

                # Normalize ALL messages to block format (including tool messages)
                # This allows us to add cache_control to any message
                if isinstance(content, str):
                    msg["content"] = [{"type": "text", "text": content}]
                elif isinstance(content, list):
                    normalized = []
                    for block in content:
                        if isinstance(block, str):
                            normalized.append({"type": "text", "text": block})
                        elif isinstance(block, dict):
                            # Remove any existing cache_control - we'll add fresh ones
                            block_copy = {k: v for k, v in block.items() if k != "cache_control"}
                            normalized.append(block_copy)
                        else:
                            normalized.append(block)
                    msg["content"] = normalized

                # Remove message-level cache_control
                if "cache_control" in msg:
                    del msg["cache_control"]

            # STEP 2: Determine cache breakpoints
            # Anthropic's recommended caching strategy for multi-turn conversations:
            # 1. ALWAYS mark the LAST message with cache_control - this caches the ENTIRE
            #    conversation prefix (system + all messages including tool_use/tool_results)
            # 2. Optionally mark system message for when cache expires and needs rebuilding
            #
            # From Anthropic docs: "During each turn, we mark the final block of the final
            # message with cache_control so the conversation can be incrementally cached."
            #
            # IMPORTANT: Not all messages can have cache_control in OpenAI format:
            # - tool messages have string content (no block format)
            # - assistant with only tool_calls has None content
            # For these, we find the nearest cacheable message before them.

            def can_have_cache_control(msg):
                """Check if a message can have cache_control applied."""
                role = msg.get("role")
                content = msg.get("content")
                # Tool messages now use block format, so they CAN have cache_control
                # (They were normalized to block format above)
                # Assistant with only tool_calls - no content to add cache_control
                if content is None and msg.get("tool_calls"):
                    return False
                # Must have list content (normalized block format)
                if isinstance(content, list) and content:
                    return True
                return False

            cache_indices = []

            # 1. Find and mark system message (for cache rebuild after expiry)
            for i, msg in enumerate(converted_messages):
                if msg.get("role") == "system":
                    cache_indices.append(i)
                    break

            # 2. Find the last CACHEABLE message for incremental caching
            # Start from the end and go back until we find a message that can have cache_control
            last_cacheable_idx = None
            for i in range(len(converted_messages) - 1, -1, -1):
                if can_have_cache_control(converted_messages[i]):
                    last_cacheable_idx = i
                    break

            if last_cacheable_idx is not None and last_cacheable_idx not in cache_indices:
                cache_indices.append(last_cacheable_idx)

            # STEP 3: Apply cache_control ONLY to breakpoint messages
            for idx in cache_indices:
                msg = converted_messages[idx]
                content = msg.get("content")
                # For list content (normalized block format), add to last block
                if isinstance(content, list) and content:
                    last_block = content[-1]
                    if isinstance(last_block, dict):
                        last_block["cache_control"] = {"type": "ephemeral"}

            # Debug: Show cache_control was applied
            if os.getenv("CAI_SHOW_CACHE", "").lower() in ("true", "1", "yes"):
                global _PREVIOUS_TURN_MSG_HASHES
                print(f"[CACHE-DEBUG] Applied cache_control to indices: {cache_indices}, total messages: {len(converted_messages)}")

                # Collect hashes for this turn
                current_turn_hashes = []

                # Show message structure with hashes for cache debugging
                # Hash helps identify which messages change between turns
                for i, msg in enumerate(converted_messages):
                    role = msg.get("role", "?")
                    content = msg.get("content")
                    has_tc = "tool_calls" in msg
                    tool_id = msg.get("tool_call_id", "")[:8] if msg.get("tool_call_id") else ""

                    # Compute hash of message content (excluding cache_control for comparison)
                    msg_for_hash = msg.copy()
                    if isinstance(msg_for_hash.get("content"), list):
                        # Remove cache_control from blocks for hashing
                        clean_content = []
                        for block in msg_for_hash["content"]:
                            if isinstance(block, dict):
                                clean_block = {k: v for k, v in block.items() if k != "cache_control"}
                                clean_content.append(clean_block)
                            else:
                                clean_content.append(block)
                        msg_for_hash["content"] = clean_content
                    msg_hash = hashlib.md5(json.dumps(msg_for_hash, sort_keys=True, default=str).encode()).hexdigest()[:8]
                    current_turn_hashes.append(msg_hash)

                    # Check if this message matches the same position in previous turn
                    match_marker = ""
                    if i < len(_PREVIOUS_TURN_MSG_HASHES):
                        if _PREVIOUS_TURN_MSG_HASHES[i] == msg_hash:
                            match_marker = " ✓MATCH"
                        else:
                            match_marker = f" ✗CHANGED (was {_PREVIOUS_TURN_MSG_HASHES[i]})"

                    if isinstance(content, list) and content:
                        last_block = content[-1]
                        has_cc = isinstance(last_block, dict) and "cache_control" in last_block
                        cc_marker = " ✓CC" if has_cc else ""
                        # Show first text content preview
                        text_preview = ""
                        for block in content:
                            if isinstance(block, dict) and block.get("type") == "text":
                                text = block.get("text", "")[:40].replace('\n', ' ')
                                text_preview = f" '{text}...'" if len(block.get("text", "")) > 40 else f" '{text}'"
                                break
                        print(f"  [{i}] {role} [hash:{msg_hash}]: list({len(content)} blocks){cc_marker}{match_marker}{text_preview}")
                    elif isinstance(content, str):
                        # String content should not happen after normalization - warn if it does
                        content_preview = content[:30].replace('\n', ' ') + "..." if len(content) > 30 else content.replace('\n', ' ')
                        print(f"  [{i}] {role} [hash:{msg_hash}]: string({len(content)} chars) - SHOULD BE LIST!{match_marker} '{content_preview}'")
                    elif content is None:
                        tc_info = ""
                        if has_tc:
                            tc_list = msg.get("tool_calls", [])
                            tc_ids = [tc.get("id", "?")[:12] for tc in tc_list]
                            tc_info = f", tool_calls={len(tc_list)} ids={tc_ids}"
                        print(f"  [{i}] {role} [hash:{msg_hash}]: None{tc_info}{match_marker}")

                # Summary of matches (before storing new hashes)
                if len(_PREVIOUS_TURN_MSG_HASHES) > 0:
                    common_len = min(len(current_turn_hashes), len(_PREVIOUS_TURN_MSG_HASHES))
                    matches = sum(1 for i in range(common_len) if current_turn_hashes[i] == _PREVIOUS_TURN_MSG_HASHES[i])
                    print(f"[CACHE-DEBUG] PREFIX MATCH: {matches}/{common_len} messages match previous turn (cache needs prefix match)")
                    if matches < common_len:
                        # Find first mismatch for detailed debugging
                        for i in range(common_len):
                            if current_turn_hashes[i] != _PREVIOUS_TURN_MSG_HASHES[i]:
                                print(f"[CACHE-DEBUG] FIRST MISMATCH at index {i}: messages diverge here, cache breaks")
                                # Print full JSON of mismatched message for debugging
                                msg = converted_messages[i]
                                msg_json = json.dumps(msg, indent=2, default=str)[:500]
                                print(f"[CACHE-DEBUG] Current message[{i}] JSON (truncated):\n{msg_json}")
                                break

                # Store current turn's hashes for next comparison
                _PREVIOUS_TURN_MSG_HASHES = current_turn_hashes
                print(f"[CACHE-DEBUG] Stored {len(current_turn_hashes)} message hashes for next turn comparison")

        if tracing.include_data():
            span.span_data.input = converted_messages

        parallel_tool_calls = (
            True if model_settings.parallel_tool_calls and tools and len(tools) > 0 else NOT_GIVEN
        )
        tool_choice = self._converter.convert_tool_choice(model_settings.tool_choice)
        response_format = self._converter.convert_response_format(output_schema)
        converted_tools = [ToolConverter.to_openai(tool) for tool in tools] if tools else []

        for handoff in handoffs:
            converted_tools.append(ToolConverter.convert_handoff_tool(handoff))

        if _debug.DONT_LOG_MODEL_DATA:
            logger.debug("Calling LLM")
        else:
            logger.debug(
                f"{json.dumps(converted_messages, indent=2)}\n"
                f"Tools:\n{json.dumps(converted_tools, indent=2)}\n"
                f"Stream: {stream}\n"
                f"Tool choice: {tool_choice}\n"
                f"Response format: {response_format}\n"
                f"Using OLLAMA: {self.is_ollama}\n"
            )

        # Use NOT_GIVEN for store if not explicitly set to avoid compatibility issues
        store = self._non_null_or_not_given(model_settings.store)

        # Check if we should use the agent's model instead of self.model
        # This prioritizes the model from Agent when available
        agent_model = None
        if hasattr(model_settings, "agent_model") and model_settings.agent_model:
            agent_model = model_settings.agent_model
            logger.debug(f"Using agent model: {agent_model} instead of {self.model}")

        # Prepare kwargs for the API call
        kwargs = {
            "model": agent_model if agent_model else self.model,
            "messages": converted_messages,
            "tools": converted_tools or NOT_GIVEN,
            "temperature": self._non_null_or_not_given(model_settings.temperature),
            "top_p": self._non_null_or_not_given(model_settings.top_p),
            "frequency_penalty": self._non_null_or_not_given(model_settings.frequency_penalty),
            "presence_penalty": self._non_null_or_not_given(model_settings.presence_penalty),
            "max_tokens": self._non_null_or_not_given(model_settings.max_tokens),
            "tool_choice": tool_choice,
            "response_format": response_format,
            "parallel_tool_calls": parallel_tool_calls,
            "stream": stream,
            "stream_options": {"include_usage": True} if stream else NOT_GIVEN,
            "store": store,
            "extra_headers": _HEADERS,
        }

        # Determine provider based on model string
        model_str = str(kwargs["model"]).lower()

        # Gateway base: CSI_CUSTOM_ENDPOINT / ALIAS_API_URL if model qualifies; else OPENAI_API_BASE (see llm_api_base).
        _model_for_base = str(kwargs.get("model") or os.getenv("CAI_MODEL") or "")
        _alias_gateway_base = resolve_llm_openai_compatible_base(_model_for_base).rstrip("/")
        if model_str == "alias2-mini":
            kwargs["api_base"] = _alias_gateway_base
            kwargs["custom_llm_provider"] = "openai"
            kwargs["api_key"] = (get_config().alias_api_key or "sk-alias-1234567890").strip()
        elif "alias" in model_str and "alias1.5" not in model_str:  # NOTE: exclude alias1.5
            kwargs["api_base"] = _alias_gateway_base
            kwargs["custom_llm_provider"] = "openai"
            kwargs["api_key"] = (get_config().alias_api_key or "sk-alias-1234567890").strip()
        elif "/" in model_str:
            # Handle provider/model format
            provider = model_str.split("/")[0]

            # Apply provider-specific configurations
            if provider == "ollama_cloud":
                # Ollama Cloud configuration
                ollama_api_key = os.getenv("OLLAMA_API_KEY")
                ollama_api_base = os.getenv("OLLAMA_API_BASE", "https://ollama.com")

                if ollama_api_key:
                    kwargs["api_key"] = ollama_api_key
                if ollama_api_base:
                    kwargs["api_base"] = ollama_api_base

                # Drop params not supported by Ollama
                litellm.drop_params = True
                kwargs.pop("parallel_tool_calls", None)
                kwargs.pop("store", None)
                if not converted_tools:
                    kwargs.pop("tool_choice", None)
            elif provider == "deepseek":
                litellm.drop_params = True
                kwargs.pop("parallel_tool_calls", None)
                kwargs.pop("store", None)  # DeepSeek doesn't support store parameter
                # Remove tool_choice if no tools are specified
                if not converted_tools:
                    kwargs.pop("tool_choice", None)

                # Add reasoning support for DeepSeek
                # DeepSeek supports reasoning_effort parameter
                if hasattr(model_settings, "reasoning_effort") and model_settings.reasoning_effort:
                    kwargs["reasoning_effort"] = model_settings.reasoning_effort
                else:
                    # Default to "high" reasoning effort if model supports it
                    kwargs["reasoning_effort"] = "high"
            elif provider == "claude" or "claude" in model_str:
                litellm.drop_params = True
                kwargs.pop("store", None)
                kwargs.pop(
                    "parallel_tool_calls", None
                )  # Claude doesn't support parallel tool calls
                # Remove tool_choice if no tools are specified
                if not converted_tools:
                    kwargs.pop("tool_choice", None)

                # Add extended reasoning support for Claude models
                # Supports Claude 3.7, Claude 4, and any model with "thinking" in the name
                has_reasoning_capability = (
                    "thinking" in model_str
                    or
                    # Claude 4 models support reasoning
                    "-4-" in model_str
                    or "sonnet-4" in model_str
                    or "haiku-4" in model_str
                    or "opus-4" in model_str
                    or "3.7" in model_str
                )

                if has_reasoning_capability:
                    # Clean the model name by removing "thinking" before sending to API
                    clean_model = kwargs["model"]
                    if isinstance(clean_model, str) and "thinking" in clean_model.lower():
                        # Remove "thinking" and clean up any extra spaces/separators
                        clean_model = re.sub(
                            r"[_-]?thinking[_-]?", "", clean_model, flags=re.IGNORECASE
                        )
                        clean_model = re.sub(
                            r"[-_]{2,}", "-", clean_model
                        )  # Clean up multiple separators
                        clean_model = clean_model.strip(
                            "-_"
                        )  # Clean up leading/trailing separators
                        kwargs["model"] = clean_model

                    # Check if message history is compatible with reasoning
                    messages = kwargs.get("messages", [])
                    is_compatible = _check_reasoning_compatibility(messages)

                    if is_compatible:
                        kwargs["reasoning_effort"] = (
                            "high"  # Use reasoning_effort instead of thinking
                        )
            elif provider == "gemini":
                kwargs.pop("parallel_tool_calls", None)
                # Add any specific gemini settings if needed
        else:
            # Handle models without provider prefix
            if "claude" in model_str or "anthropic" in model_str:
                litellm.drop_params = True
                # Remove parameters that Anthropic doesn't support
                kwargs.pop("store", None)
                kwargs.pop("parallel_tool_calls", None)
                # Remove tool_choice if no tools are specified
                if not converted_tools:
                    kwargs.pop("tool_choice", None)

                # Add extended reasoning support for Claude models
                # Supports Claude 3.7, Claude 4, and any model with "thinking" in the name
                has_reasoning_capability = "thinking" in model_str

                if has_reasoning_capability:
                    # Clean the model name by removing "thinking" before sending to API
                    clean_model = kwargs["model"]
                    if isinstance(clean_model, str) and "thinking" in clean_model.lower():
                        # Remove "thinking" and clean up any extra spaces/separators
                        clean_model = re.sub(
                            r"[_-]?thinking[_-]?", "", clean_model, flags=re.IGNORECASE
                        )
                        clean_model = re.sub(
                            r"[-_]{2,}", "-", clean_model
                        )  # Clean up multiple separators
                        clean_model = clean_model.strip(
                            "-_"
                        )  # Clean up leading/trailing separators
                        kwargs["model"] = clean_model

                    # Check if message history is compatible with reasoning
                    messages = kwargs.get("messages", [])
                    is_compatible = _check_reasoning_compatibility(messages)

                    if is_compatible:
                        kwargs["reasoning_effort"] = (
                            "high"  # Use reasoning_effort instead of thinking
                        )
            elif "gemini" in model_str:
                kwargs.pop("parallel_tool_calls", None)
            elif "qwen" in model_str or ":" in model_str:
                # Handle Ollama-served models with custom formats (e.g., alias1)
                # These typically need the Ollama provider
                litellm.drop_params = True
                kwargs.pop("parallel_tool_calls", None)
                kwargs.pop("store", None)  # Ollama doesn't support store parameter
                # These models may not support certain parameters
                if not converted_tools:
                    kwargs.pop("tool_choice", None)
                # Don't add custom_llm_provider here to avoid duplication with Ollama provider
                if self.is_ollama:
                    # Clean kwargs for ollama to avoid parameter conflicts
                    for param in ["custom_llm_provider"]:
                        kwargs.pop(param, None)
            elif any(x in model_str for x in ["o1", "o3", "o4"]):
                # Handle OpenAI reasoning models (o1, o3, o4)
                kwargs.pop("parallel_tool_calls", None)
                # Add reasoning effort if provided
                if hasattr(model_settings, "reasoning_effort"):
                    kwargs["reasoning_effort"] = model_settings.reasoning_effort

        # Filter out NotGiven values to avoid JSON serialization issues
        filtered_kwargs = {}
        for key, value in kwargs.items():
            if value is not NOT_GIVEN:
                filtered_kwargs[key] = value
        kwargs = filtered_kwargs

        # Abliteration steering: CAI_UNRESTRICTED=true → steering ON + thinking OFF.
        # `steering_enabled` is an alias-backend-only field. Injecting it into
        # vanilla Anthropic/OpenAI/OpenRouter requests causes upstream 400s
        # ("extra_body: Extra inputs are not permitted") which LiteLLM
        # misclassifies as InternalServerError → silent retry-loop hang.
        # Gate steering payload on Alias-backed models only. CAI_UNRESTRICTED must not
        # inject extra_body for Azure/OpenAI/Anthropic — upstream rejects it and LiteLLM
        # may retry (see comment above).
        _unrestricted = os.getenv("CAI_UNRESTRICTED", "false").strip().lower() in ("true", "1", "yes")
        if self._is_alias_model:
            kwargs.setdefault("extra_body", {})
            kwargs["extra_body"]["steering_enabled"] = _unrestricted
            if _unrestricted:
                kwargs["extra_body"]["chat_template_kwargs"] = {"enable_thinking": False}

        global _UNRESTRICTED_EXTRA_BODY_LOGGED
        if _unrestricted and not _UNRESTRICTED_EXTRA_BODY_LOGGED:
            if os.getenv("CAI_UNRESTRICTED_LOG", "").strip().lower() in (
                "1",
                "true",
                "yes",
                "on",
            ):
                _UNRESTRICTED_EXTRA_BODY_LOGGED = True
                print(
                    "[CAI] Unrestricted: client will send extra_body="
                    f"{kwargs.get('extra_body')!r} (steering_enabled / chat_template_kwargs). "
                    "If the model behaves like the normal route, LiteLLM or the backend may be ignoring these fields.",
                    file=sys.stderr,
                )

        # Add retry logic for rate limits
        max_retries = 3
        retry_count = 0

        # Use httpx directly when a custom base is configured and messages carry cache_control
        # (LiteLLM strips cache_control from messages when using the OpenAI client).
        _model_for_openai_base = str(kwargs.get("model") or os.getenv("CAI_MODEL") or "")
        openai_api_base = (
            kwargs.get("api_base") or resolve_llm_openai_compatible_base(_model_for_openai_base)
        ).rstrip("/")

        def has_cache_control(messages):
            """Check if any message has cache_control (at message level or in content blocks)"""
            for msg in messages:
                # Check message-level cache_control
                if msg.get("cache_control"):
                    return True
                # Check content blocks for cache_control
                content = msg.get("content")
                if isinstance(content, list):
                    for block in content:
                        if isinstance(block, dict) and block.get("cache_control"):
                            return True
            return False

        use_direct_request = explicit_custom_llm_api_base_configured(
            _model_for_openai_base
        ) and has_cache_control(kwargs.get("messages", []))

        # Debug: Show whether direct HTTP path is being used
        if os.getenv("CAI_SHOW_CACHE", "").lower() in ("true", "1", "yes"):
            has_cc = has_cache_control(kwargs.get("messages", []))
            print(
                f"[CACHE-DEBUG] api_base={openai_api_base}, has_cache_control={has_cc}, "
                f"use_direct={use_direct_request}, stream={stream}"
            )

        if use_direct_request:
            logger.debug(f"[CACHE] Using direct HTTP path to preserve cache_control (stream={stream})")
            try:
                import httpx

                # Build the request body preserving all fields including cache_control
                request_body = {
                    "model": kwargs.get("model", self.model),
                    "messages": kwargs.get("messages", []),
                    "max_tokens": kwargs.get("max_tokens"),
                    "temperature": kwargs.get("temperature"),
                    "top_p": kwargs.get("top_p"),
                    "stream": stream,  # Match the requested stream mode
                }

                # Add tools if present
                if kwargs.get("tools") and kwargs["tools"] is not NOT_GIVEN:
                    request_body["tools"] = kwargs["tools"]

                # Add tool_choice if present
                if kwargs.get("tool_choice") and kwargs["tool_choice"] is not NOT_GIVEN:
                    request_body["tool_choice"] = kwargs["tool_choice"]

                # Propagate extra_body fields (steering_enabled, chat_template_kwargs, etc.)
                for eb_key, eb_val in kwargs.get("extra_body", {}).items():
                    request_body[eb_key] = eb_val

                # Remove None values
                request_body = {k: v for k, v in request_body.items() if v is not None}

                api_url = f"{openai_api_base.rstrip('/')}/chat/completions"
                headers = {
                    "Content-Type": "application/json",
                    "Authorization": f"Bearer {get_config().openai_api_key or 'sk-placeholder'}",
                }

                if stream:
                    # Streaming mode: Return an async generator for SSE chunks
                    # We need to create an httpx client that stays open during streaming
                    client = httpx.AsyncClient(timeout=120.0)

                    async def stream_response():
                        """Async generator that yields SSE chunks from the proxy"""
                        try:
                            async with client.stream("POST", api_url, json=request_body, headers=headers) as resp:
                                resp.raise_for_status()
                                buffer = ""
                                async for chunk in resp.aiter_text():
                                    buffer += chunk
                                    while "\n" in buffer:
                                        line, buffer = buffer.split("\n", 1)
                                        line = line.strip()
                                        if line.startswith("data: "):
                                            data_str = line[6:]
                                            if data_str == "[DONE]":
                                                return
                                            try:
                                                data = json.loads(data_str)
                                                # Yield the parsed chunk as a ModelResponse-like object
                                                from litellm import ModelResponse
                                                from litellm.types.utils import StreamingChoices, Delta

                                                delta_data = data.get("choices", [{}])[0].get("delta", {})
                                                delta = Delta(
                                                    role=delta_data.get("role"),
                                                    content=delta_data.get("content"),
                                                    tool_calls=delta_data.get("tool_calls")
                                                )

                                                choices = [StreamingChoices(
                                                    index=0,
                                                    delta=delta,
                                                    finish_reason=data.get("choices", [{}])[0].get("finish_reason")
                                                )]

                                                # Extract usage if present (usually in final chunk)
                                                usage_data = data.get("usage")
                                                usage = None
                                                if usage_data:
                                                    # Extract cache metrics - support both Anthropic and OpenAI formats
                                                    cache_read = usage_data.get("cache_read_input_tokens")
                                                    cache_creation = usage_data.get("cache_creation_input_tokens")
                                                    # Fallback to OpenAI format (prompt_tokens_details.cached_tokens)
                                                    if not cache_read:
                                                        prompt_details = usage_data.get("prompt_tokens_details", {})
                                                        if prompt_details:
                                                            cache_read = prompt_details.get("cached_tokens")

                                                    # Debug: Log cache metrics from streaming
                                                    if os.getenv("CAI_SHOW_CACHE", "").lower() in ("true", "1", "yes"):
                                                        print(f"[CACHE-DEBUG] Direct HTTP streaming usage: CR={cache_read}, CW={cache_creation}")

                                                    from litellm.types.utils import Usage
                                                    usage = Usage(
                                                        prompt_tokens=usage_data.get("prompt_tokens", 0),
                                                        completion_tokens=usage_data.get("completion_tokens", 0),
                                                        total_tokens=usage_data.get("total_tokens", 0),
                                                        cache_creation_input_tokens=cache_creation,
                                                        cache_read_input_tokens=cache_read,
                                                    )

                                                chunk_response = ModelResponse(
                                                    id=data.get("id", "chatcmpl-proxy"),
                                                    created=data.get("created", int(time.time())),
                                                    model=data.get("model", str(self.model)),
                                                    choices=choices,
                                                    usage=usage,
                                                    object="chat.completion.chunk"
                                                )
                                                yield chunk_response
                                            except json.JSONDecodeError:
                                                continue
                        finally:
                            await client.aclose()

                    # Return Response object and stream generator (similar to LiteLLM streaming)
                    response_obj = Response(
                        id=FAKE_RESPONSES_ID,
                        created_at=time.time(),
                        model=self.model,
                        object="response",
                        output=[],
                        tool_choice="auto"
                        if tool_choice is None or tool_choice == NOT_GIVEN
                        else cast(Literal["auto", "required", "none"], tool_choice),
                        top_p=model_settings.top_p,
                        temperature=model_settings.temperature,
                        tools=[],
                        parallel_tool_calls=parallel_tool_calls or False,
                    )
                    return response_obj, stream_response()

                else:
                    # Non-streaming mode
                    async with httpx.AsyncClient(timeout=120.0) as client:
                        response = await client.post(api_url, json=request_body, headers=headers)
                        response.raise_for_status()
                        data = response.json()

                        usage_data = data.get("usage", {})

                        # Extract cache metrics - support both Anthropic and OpenAI formats
                        cache_read = usage_data.get("cache_read_input_tokens")
                        cache_creation = usage_data.get("cache_creation_input_tokens")
                        # Fallback to OpenAI format (prompt_tokens_details.cached_tokens)
                        if not cache_read:
                            prompt_details = usage_data.get("prompt_tokens_details", {})
                            if prompt_details:
                                cache_read = prompt_details.get("cached_tokens")

                        # Debug: Log what the proxy returned
                        if os.getenv("CAI_SHOW_CACHE", "").lower() in ("true", "1", "yes"):
                            print(f"[CACHE-DEBUG] Direct HTTP non-streaming usage from proxy: CR={cache_read}, CW={cache_creation}, full_usage={usage_data}")

                        # Use litellm's ModelResponse which handles the structure automatically
                        from litellm import ModelResponse
                        result = ModelResponse(**data)

                        # Ensure cache metrics are preserved in usage
                        if result.usage:
                            result.usage.cache_creation_input_tokens = cache_creation
                            result.usage.cache_read_input_tokens = cache_read

                        return result

            except Exception as e:
                # Fall back to LiteLLM if direct request fails
                if os.getenv("CAI_SHOW_CACHE", "").lower() in ("true", "1", "yes"):
                    print(f"[CACHE-DEBUG] Direct HTTP path FAILED, falling back to LiteLLM: {e}")
                logger.debug(f"Direct request failed, falling back to LiteLLM: {e}")

        # Check if this is Ollama Cloud (ollama_cloud/ prefix)
        # Ollama Cloud is OpenAI-compatible, so we bypass LiteLLM to avoid parsing issues
        is_ollama_cloud = "ollama_cloud/" in model_str

        if is_ollama_cloud:
            # Use AsyncOpenAI client directly for Ollama Cloud
            # Ollama Cloud is fully OpenAI-compatible at /v1/chat/completions
            try:
                # Configure the client with Ollama Cloud settings
                ollama_api_key = os.getenv("OLLAMA_API_KEY") or os.getenv("OPENAI_API_KEY")
                ollama_base_url = os.getenv("OLLAMA_API_BASE", "https://ollama.com")

                # Ensure the URL has /v1 for OpenAI compatibility
                if not ollama_base_url.endswith("/v1"):
                    ollama_base_url = f"{ollama_base_url}/v1"

                # Create a temporary client configured for Ollama Cloud
                ollama_client = AsyncOpenAI(
                    api_key=ollama_api_key,
                    base_url=ollama_base_url
                )

                # Remove the ollama_cloud/ prefix from the model name
                clean_model = kwargs["model"].replace("ollama_cloud/", "")
                kwargs["model"] = clean_model

                # Remove LiteLLM-specific parameters
                kwargs.pop("extra_headers", None)
                kwargs.pop("api_key", None)
                kwargs.pop("api_base", None)
                kwargs.pop("custom_llm_provider", None)

                # Call Ollama Cloud using OpenAI-compatible API
                if stream:
                    return await ollama_client.chat.completions.create(**kwargs)
                else:
                    return await ollama_client.chat.completions.create(**kwargs)

            except Exception as e:
                # If Ollama Cloud fails, raise with helpful message
                raise Exception(
                    f"Error connecting to Ollama Cloud: {str(e)}\n"
                    f"Verify OLLAMA_API_KEY and OLLAMA_API_BASE are configured correctly."
                ) from e

        while retry_count < max_retries:
            try:
                cfg = get_config()
                if (self._is_alias_model or cfg.force_httpx) and not self.is_ollama:
                    # [N] Direct httpx — bypass LiteLLM for alias/forced models
                    return await self._direct_httpx_completion(
                        kwargs, model_settings, tool_choice, stream, parallel_tool_calls
                    )
                elif self.is_ollama:
                    return await self._fetch_response_litellm_ollama(
                        kwargs, model_settings, tool_choice, stream, parallel_tool_calls
                    )
                else:
                    return await self._fetch_response_litellm_openai(
                        kwargs, model_settings, tool_choice, stream, parallel_tool_calls
                    )
            except (litellm.exceptions.RateLimitError, LLMRateLimited) as e:
                retry_count += 1
                if retry_count >= max_retries:
                    self.logger.error(f"Rate limit exceeded after {max_retries} retries")
                    if verbose_http_retries():
                        print(f"\n❌ Rate limit exceeded after {max_retries} retries")
                    raise

                self.logger.warning(
                    f"Rate limit retry {retry_count}/{max_retries}: {str(e)[:200]}"
                )
                if verbose_http_retries():
                    print(f"\n⏳ Rate limit reached - Too many requests (attempt {retry_count}/{max_retries})")
                # Extract retry delay: check LLMRateLimited.retry_after, VertexAI
                # JSON details, common header patterns, or fall back to exp backoff
                retry_delay: float | None = None

                # Check if LLMRateLimited carries an explicit retry_after
                if isinstance(e, LLMRateLimited) and getattr(e, "retry_after", None):
                    retry_delay = float(e.retry_after)
                else:
                    try:
                        # VertexAI format: parse RetryInfo from JSON details
                        error_msg = str(e.args[0]) if e.args else str(e)
                        json_str = error_msg.split("VertexAIException - ")[-1]
                        error_details = json.loads(json_str)
                        retry_info = next(
                            (d for d in error_details.get("error", {}).get("details", [])
                             if d.get("@type") == "type.googleapis.com/google.rpc.RetryInfo"),
                            None,
                        )
                        if retry_info and "retryDelay" in retry_info:
                            retry_delay = float(retry_info["retryDelay"].rstrip("s"))
                    except Exception:
                        # Try common retry-after patterns in error message
                        error_str = str(e)
                        for pattern in [
                            r'retry[_-]?after[:\s]+(\d+)',
                            r'wait\s+(\d+)\s+seconds?',
                            r'retry\s+in\s+(\d+)\s+seconds?',
                        ]:
                            m = re.search(pattern, error_str, re.IGNORECASE)
                            if m:
                                retry_delay = float(m.group(1))
                                break

                # Exponential backoff with jitter if no explicit delay
                if retry_delay is None:
                    retry_delay = self._backoff_delay(retry_count - 1)

                if verbose_http_retries():
                    print(f"💤 Waiting {retry_delay:.0f}s before retry... (Rate limit protection)")
                await sleep_with_retry_backoff_hint(retry_delay)
                continue

            except litellm.exceptions.ServiceUnavailableError as e:
                # Handle 503 "queue is full" errors from the LiteLLM proxy server
                retry_count += 1
                if retry_count >= max_retries:
                    self.logger.error(f"Service unavailable after {max_retries} retries")
                    if verbose_http_retries():
                        print(f"\n❌ Service unavailable after {max_retries} retries")
                    raise

                error_msg = str(e)
                self.logger.warning(
                    f"Service unavailable retry {retry_count}/{max_retries}: {error_msg[:200]}"
                )
                if verbose_http_retries():
                    if "queue is full" in error_msg.lower():
                        print(f"\n⏳ Server queue is full (attempt {retry_count}/{max_retries})")
                    else:
                        print(f"\n⏳ Service unavailable: {error_msg[:100]} (attempt {retry_count}/{max_retries})")

                # Exponential backoff with jitter for 503 errors
                retry_delay = self._backoff_delay(retry_count - 1, base=10.0, cap=120.0)

                if verbose_http_retries():
                    print(f"💤 Waiting {retry_delay}s before retry... (Server overload protection)")
                await sleep_with_retry_backoff_hint(retry_delay)
                continue  # Retry the request

            except litellm.exceptions.BadGatewayError as e:
                # Handle 502 Bad Gateway errors (e.g. nginx proxy to litellm)
                retry_count += 1
                if retry_count >= max_retries:
                    self.logger.error(
                        f"Bad Gateway (502) after {max_retries} retries [{self.model}]"
                    )
                    if verbose_http_retries():
                        print(f"\n❌ Bad Gateway (502) after {max_retries} retries [{self.model}]")
                    raise

                error_msg = str(e)[:150]
                self.logger.warning(
                    f"Bad Gateway retry {retry_count}/{max_retries}: {error_msg}"
                )
                if verbose_http_retries():
                    print(f"\n⏳ Bad Gateway (502): {error_msg} (attempt {retry_count}/{max_retries})")

                import random
                base_delay = 10
                retry_delay = min(120, base_delay * (2 ** (retry_count - 1))) + random.randint(0, 5)

                if verbose_http_retries():
                    print(f"💤 Waiting {retry_delay}s before retry... (Backend unavailable)")
                await sleep_with_retry_backoff_hint(retry_delay)
                continue

            except litellm.exceptions.APIConnectionError as e:
                self.logger.warning(f"API connection error [{self.model}]: {e}")
                if verbose_http_retries():
                    print(f"\n🌐 Connection Error [{self.model}]: {str(e)}")
                    print("💡 Check your internet connection or API endpoint")
                raise

            except (litellm.exceptions.Timeout, LLMTimeout) as e:
                self.logger.warning(f"Request timed out [{self.model}]: {e}")
                if verbose_http_retries():
                    print(f"\n⏱️  Request timed out [{self.model}]: {str(e)}")
                    print("💡 The model took too long to respond. Try:")
                    print("  • Using a faster model")
                    print("  • Reducing the prompt size")
                    print("  • Checking your internet connection")
                raise

            except litellm.exceptions.BadRequestError as e:
                error_msg = str(e)

                # Handle Claude reasoning/thinking compatibility errors
                if (
                    "Expected `thinking` or `redacted_thinking`, but found `text`" in error_msg
                    or "When `thinking` is enabled, a final `assistant` message must start with a thinking block"
                    in error_msg
                ):
                    # Retry without reasoning_effort
                    retry_kwargs = kwargs.copy()
                    retry_kwargs.pop("reasoning_effort", None)

                    try:
                        if stream:
                            response = Response(
                                id=FAKE_RESPONSES_ID,
                                created_at=time.time(),
                                model=self.model,
                                object="response",
                                output=[],
                                tool_choice="auto"
                                if tool_choice is None or tool_choice == NOT_GIVEN
                                else cast(Literal["auto", "required", "none"], tool_choice),
                                top_p=model_settings.top_p,
                                temperature=model_settings.temperature,
                                tools=[],
                                parallel_tool_calls=parallel_tool_calls or False,
                            )
                            stream_obj = await litellm.acompletion(**retry_kwargs)
                            return response, stream_obj
                        else:
                            ret = await litellm.acompletion(**retry_kwargs)
                            return ret
                    except Exception:
                        # If retry also fails, raise the original error
                        raise e

                # print(color("BadRequestError encountered: " + str(e), fg="yellow"))
                if "LLM Provider NOT provided" in str(e):
                    model_str = str(self.model).lower()
                    provider = None
                    is_qwen = "qwen" in model_str or ":" in model_str

                    # Special handling for Qwen models
                    if is_qwen:
                        try:
                            # Use the specialized Qwen approach first
                            return await self._fetch_response_litellm_ollama(
                                kwargs, model_settings, tool_choice, stream, parallel_tool_calls
                            )
                        except Exception as qwen_e:
                            print(qwen_e)
                            # If that fails, try our direct OpenAI approach
                            qwen_params = kwargs.copy()
                            qwen_params["api_base"] = get_ollama_api_base()
                            qwen_params["custom_llm_provider"] = "openai"  # Use openai provider

                            # Make sure tools are passed
                            if "tools" in kwargs and kwargs["tools"]:
                                qwen_params["tools"] = kwargs["tools"]
                            if "tool_choice" in kwargs and kwargs["tool_choice"] is not NOT_GIVEN:
                                qwen_params["tool_choice"] = kwargs["tool_choice"]

                            try:
                                if stream:
                                    # Streaming case
                                    response = Response(
                                        id=FAKE_RESPONSES_ID,
                                        created_at=time.time(),
                                        model=self.model,
                                        object="response",
                                        output=[],
                                        tool_choice="auto"
                                        if tool_choice is None or tool_choice == NOT_GIVEN
                                        else cast(Literal["auto", "required", "none"], tool_choice),
                                        top_p=model_settings.top_p,
                                        temperature=model_settings.temperature,
                                        tools=[],
                                        parallel_tool_calls=parallel_tool_calls or False,
                                    )
                                    stream_obj = await litellm.acompletion(**qwen_params)
                                    return response, stream_obj
                                else:
                                    # Non-streaming case
                                    ret = await litellm.acompletion(**qwen_params)
                                    return ret
                            except Exception as direct_e:
                                # All approaches failed, log and raise the original error
                                print(
                                    f"All Qwen approaches failed. Original error: {str(e)}, Direct error: {str(direct_e)}"
                                )
                                raise e

                    # Try to detect provider from model string
                    if "/" in model_str:
                        provider = model_str.split("/")[0]

                    if provider:
                        # Add provider-specific settings based on detected provider
                        provider_kwargs = kwargs.copy()
                        if provider == "deepseek":
                            provider_kwargs["custom_llm_provider"] = "deepseek"
                            provider_kwargs.pop(
                                "store", None
                            )  # DeepSeek doesn't support store parameter
                            provider_kwargs.pop(
                                "parallel_tool_calls", None
                            )  # DeepSeek doesn't support parallel tool calls

                            # Add reasoning support for DeepSeek
                            if (
                                hasattr(model_settings, "reasoning_effort")
                                and model_settings.reasoning_effort
                            ):
                                provider_kwargs["reasoning_effort"] = model_settings.reasoning_effort
                            else:
                                # Default to "high" reasoning effort
                                provider_kwargs["reasoning_effort"] = "high"
                        elif provider == "claude" or "claude" in model_str:
                            provider_kwargs["custom_llm_provider"] = "anthropic"
                            provider_kwargs.pop("store", None)  # Claude doesn't support store parameter
                            provider_kwargs.pop(
                                "parallel_tool_calls", None
                            )  # Claude doesn't support parallel tool calls

                            # Add extended reasoning support for Claude models
                            if "thinking" in model_str:
                                # Clean the model name by removing "thinking" before sending to API
                                clean_model = provider_kwargs["model"]
                                if isinstance(clean_model, str) and "thinking" in clean_model.lower():
                                    # Remove "thinking" and clean up any extra spaces/separators
                                    clean_model = re.sub(
                                        r"[_-]?thinking[_-]?", "", clean_model, flags=re.IGNORECASE
                                    )
                                    clean_model = re.sub(
                                        r"[-_]{2,}", "-", clean_model
                                    )  # Clean up multiple separators
                                    clean_model = clean_model.strip(
                                        "-_"
                                    )  # Clean up leading/trailing separators
                                    provider_kwargs["model"] = clean_model

                                # Check if message history is compatible with reasoning
                                messages = provider_kwargs.get("messages", [])
                                is_compatible = _check_reasoning_compatibility(messages)

                                if is_compatible:
                                    provider_kwargs["reasoning_effort"] = (
                                        "high"  # Use reasoning_effort instead of thinking
                                    )
                        elif provider == "gemini":
                            provider_kwargs["custom_llm_provider"] = "gemini"
                            provider_kwargs.pop("store", None)  # Gemini doesn't support store parameter
                            provider_kwargs.pop(
                                "parallel_tool_calls", None
                            )  # Gemini doesn't support parallel tool calls
                        else:
                            # For unknown providers, try ollama as fallback
                            return await self._fetch_response_litellm_ollama(
                                kwargs, model_settings, tool_choice, stream, parallel_tool_calls
                            )

                # Check for message sequence errors
                if (
                    "An assistant message with 'tool_calls'" in str(e)
                    or "`tool_use` blocks must be followed by a user message with `tool_result`"
                    in str(e)  # noqa: E501 # pylint: disable=C0301
                    or "`tool_use` ids were found without `tool_result` blocks immediately after"
                    in str(e)  # noqa: E501 # pylint: disable=C0301
                    or "An assistant message with 'tool_calls' must be followed by tool messages"
                    in str(e)
                    or "messages with role 'tool' must be a response to a preceeding message with 'tool_calls'"
                    in str(e)
                ):
                    print("⚠️  Message sequence error - Tool calls and results are out of order")

                    # Use the pretty message history printer instead of the simple loop
                    try:
                        from cai.util import print_message_history

                        print("\n📋 Current message sequence:")
                        print_message_history(kwargs["messages"], title="Message History")
                    except ImportError:
                        # Fall back to simple printing if the function isn't available
                        print("\n📋 Current message sequence:")
                        for i, msg in enumerate(kwargs["messages"]):
                            role = msg.get("role", "unknown")
                            content_type = (
                                "text"
                                if isinstance(msg.get("content"), str)
                                else "list"
                                if isinstance(msg.get("content"), list)
                                else "None"
                                if msg.get("content") is None
                                else type(msg.get("content")).__name__
                            )
                            tool_calls = "with tool_calls" if msg.get("tool_calls") else ""
                            tool_call_id = (
                                f", tool_call_id: {msg.get('tool_call_id')}"
                                if msg.get("tool_call_id")
                                else ""
                            )

                            print(
                                f"  [{i}] {role}{tool_call_id} (content: {content_type}) {tool_calls}"
                            )

                    # NOTE: EDGE CASE: Report Agent CTRL C error
                    #
                    # This fix CTRL-C error when message list is incomplete
                    # When a tool is not finished but the LLM generates a tool call
                    try:
                        from cai.util import fix_message_list

                        print("🔧 Auto-fixing message sequence...")
                        fixed_messages = fix_message_list(kwargs["messages"])

                        # Show the fixed messages if they're different
                        if fixed_messages != kwargs["messages"]:
                            try:
                                from cai.util import print_message_history

                                print_message_history(fixed_messages, title="Fixed Message Sequence")
                            except ImportError:
                                print("✅ Message sequence fixed successfully")

                        kwargs["messages"] = fixed_messages
                    except Exception:
                        pass

                    return await self._fetch_response_litellm_openai(
                        kwargs, model_settings, tool_choice, stream, parallel_tool_calls
                    )

                # this captures an error related to the fact
                # that the messages list contains an empty
                # content position
                if "expected a string, got null" in str(e):
                    print("⚠️  Empty content detected - Filling with placeholder")
                    # Fix for null content in messages
                    kwargs["messages"] = [
                        msg if msg.get("content") is not None else {**msg, "content": ""}
                        for msg in kwargs["messages"]
                    ]
                    return await self._fetch_response_litellm_openai(
                        kwargs, model_settings, tool_choice, stream, parallel_tool_calls
                    )

                # Handle Anthropic error for empty text content blocks
                if "text content blocks must be non-empty" in str(
                    e
                ) or "cache_control cannot be set for empty text blocks" in str(e):  # noqa
                    # Print the error message only once
                    print("⚠️  Empty text blocks detected - Adding placeholder content") if not self.empty_content_error_shown else None
                    self.empty_content_error_shown = True

                    # Fix for empty content in messages for Anthropic models
                    kwargs["messages"] = [
                        msg
                        if msg.get("content") not in [None, ""]
                        else {**msg, "content": "Empty content block"}
                        for msg in kwargs["messages"]
                    ]
                    return await self._fetch_response_litellm_openai(
                        kwargs, model_settings, tool_choice, stream, parallel_tool_calls
                    )
                # Check for Python formatting errors - NOT context errors
                if "Cannot specify ',' with 's'" in str(e):
                    print("\n❌ Python formatting error - Not a context error")
                    print("⚠️  There's a bug in the code trying to format strings as numbers")
                    print(f"Error: {str(e)}")
                    raise
                # Check for context length errors in BadRequestError
                if (
                    "context_length_exceeded" in str(e) 
                    or "prompt is too long" in str(e).lower()
                    or "maximum context length" in str(e).lower()
                    or "max_tokens" in str(e) and "exceeded" in str(e).lower()
                    or "too many tokens" in str(e).lower()
                    or "token limit" in str(e).lower()
                ):
                    print("\n📦 Context window exceeded - Message history too long")

                    # Try to extract token info from different error formats
                    # NOTE: re is imported at module level — do NOT re-import here
                    # as it causes UnboundLocalError for earlier uses in this scope
                    error_str = str(e)

                    # Pattern 1: "X tokens > Y maximum" (Anthropic)
                    match1 = re.search(r'(\d+)\s*tokens?\s*>\s*(\d+)\s*maximum', error_str)
                    # Pattern 2: "requested X tokens...maximum context length is Y" (OpenAI)
                    match2 = re.search(r'requested\s+(\d+)\s+tokens.*maximum.*?(\d+)', error_str)
                    # Pattern 3: "This model's maximum context length is X tokens, however you requested Y"
                    match3 = re.search(r'maximum context length is\s+(\d+).*requested\s+(\d+)', error_str)

                    if match1:
                        used_tokens = int(match1.group(1))
                        max_tokens = int(match1.group(2))
                        print(f"🎯 Actual: {used_tokens:,} / {max_tokens:,} tokens")
                    elif match2:
                        used_tokens = int(match2.group(1))
                        max_tokens = int(match2.group(2))
                        print(f"🎯 Requested: {used_tokens:,} tokens (max: {max_tokens:,})")
                    elif match3:
                        max_tokens = int(match3.group(1))
                        used_tokens = int(match3.group(2))
                        print(f"🎯 Requested: {used_tokens:,} tokens (max: {max_tokens:,})")
                    elif 'estimated_input_tokens' in locals():
                        print(f"📊 Estimated tokens: ~{estimated_input_tokens:,}")
                        # Get model's max tokens
                        model_max = self._get_model_max_tokens(str(self.model))
                        print(f"🎯 Model limit: {model_max:,} tokens")

                    # Best-effort recovery: pin the provider limit (when available) and compact+retry once.
                    # Keeps a hard 80% safety cap when estimates are high; avoids stuck jobs when
                    # pricing heuristics overestimate the true context window (e.g. alias models).
                    try:
                        inferred_max = locals().get("max_tokens", None)
                        inferred_used = locals().get("used_tokens", None)
                        if isinstance(inferred_max, int) and inferred_max > 0:
                            os.environ["CAI_MODEL_MAX_INPUT_TOKENS"] = str(inferred_max)
                        if not getattr(self, "_context_compact_retry", False):
                            self._context_compact_retry = True
                            # Force compaction attempt (estimated_tokens must exceed threshold).
                            force_est = int(inferred_used or inferred_max or estimated_input_tokens or 0)
                            force_est = max(force_est, 1)
                            _in = input
                            _sys = system_instructions
                            _in, _sys, compacted = await self._auto_compact_if_needed(
                                force_est,
                                _in,
                                _sys,
                            )
                            if compacted:
                                rebuilt = self._messages_for_token_count_after_history_mutation(
                                    system_instructions=_sys,
                                    input=_in,
                                )
                                kwargs["messages"] = rebuilt
                                return await self._fetch_response_litellm_openai(
                                    kwargs, model_settings, tool_choice, stream, parallel_tool_calls
                                )
                    except Exception:
                        pass

                    print("\n💡 Quick fixes:")
                    print("  • /flush - Clear conversation history")
                    print("  • /compact - Manually compact context")
                    print("  • /model <larger-model> - Switch to model with more context")

                    raise
            else:
                raise e

    # ------------------------------------------------------------------
    # Direct httpx completion — bypasses LiteLLM for alias models [N]
    # ------------------------------------------------------------------
    async def _direct_httpx_completion(
        self,
        kwargs: dict,
        model_settings: ModelSettings,
        tool_choice: ChatCompletionToolChoiceOptionParam | NotGiven,
        stream: bool,
        parallel_tool_calls: bool,
    ):
        """Delegate to chatcompletions.httpx_client.direct_httpx_completion."""
        return await _direct_httpx_completion_impl(
            kwargs=kwargs,
            model_settings=model_settings,
            tool_choice=tool_choice,
            stream=stream,
            parallel_tool_calls=parallel_tool_calls,
            model_name=str(self.model),
            user_agent=_USER_AGENT,
        )

    async def _fetch_response_litellm_openai(
        self,
        kwargs: dict,
        model_settings: ModelSettings,
        tool_choice: ChatCompletionToolChoiceOptionParam | NotGiven,
        stream: bool,
        parallel_tool_calls: bool,
    ) -> ChatCompletion | tuple[Response, AsyncStream[ChatCompletionChunk]]:
        """Delegate to chatcompletions.litellm_adapter.fetch_response_litellm_openai."""
        return await _fetch_litellm_openai_impl(
            kwargs=kwargs,
            model_name=str(self.model),
            model_settings=model_settings,
            tool_choice=tool_choice,
            stream=stream,
            parallel_tool_calls=parallel_tool_calls,
        )

    async def _fetch_response_litellm_ollama(
        self,
        kwargs: dict,
        model_settings: ModelSettings,
        tool_choice: ChatCompletionToolChoiceOptionParam | NotGiven,
        stream: bool,
        parallel_tool_calls: bool,
    ) -> ChatCompletion | tuple[Response, AsyncStream[ChatCompletionChunk]]:
        """Delegate to chatcompletions.litellm_adapter.fetch_response_litellm_ollama."""
        return await _fetch_litellm_ollama_impl(
            kwargs=kwargs,
            model_name=str(self.model),
            model_settings=model_settings,
            tool_choice=tool_choice,
            stream=stream,
            parallel_tool_calls=parallel_tool_calls,
            )

    def _get_model_max_tokens(self, model_name: str) -> int:
        """Delegate to chatcompletions.auto_compactor.get_model_max_tokens."""
        return _get_model_max_tokens_impl(model_name)

    async def _auto_compact_if_needed(self, estimated_tokens: int, input: str | list[TResponseInputItem], system_instructions: str | None) -> tuple[str | list[TResponseInputItem], str | None, bool]:
        """Delegate to chatcompletions.auto_compactor.auto_compact_if_needed."""
        global _compaction_in_progress

        def _set_flag(val: bool):
            global _compaction_in_progress
            _compaction_in_progress = val

        return await _auto_compact_if_needed_impl(
            estimated_tokens=estimated_tokens,
            input=input,
            system_instructions=system_instructions,
            model_name=str(self.model),
            agent_name=self.agent_name,
            message_history=self.message_history,
            converter=self._converter,
            compaction_in_progress_flag=_compaction_in_progress,
            set_compaction_flag=_set_flag,
        )

    def _intermediate_logs(self):
        """Intermediate logging if conditions are met."""
        if (
            self.logger
            and self.interaction_counter > 0
            and self.interaction_counter % self.INTERMEDIATE_LOG_INTERVAL == 0
        ):
            process_intermediate_logs(self.logger.filename, self.logger.session_id)

    def _get_client(self) -> AsyncOpenAI:
        if self._client is None:
            api_key = resolve_llm_openai_compatible_api_key(str(self.model))
            if not api_key:
                _c = get_config()
                raise UserError(
                    "Missing API key for selected model. "
                    "For alias-family models (alias*/cai*/csi*), set ALIAS_API_KEY. "
                    "For OpenAI models, set OPENAI_API_KEY. "
                    f"(CAI_MODEL={_c.model!r})"
                )
            self._client = AsyncOpenAI(api_key=api_key)
        return self._client

    # Helper function to detect and format function calls from various models
    def _detect_and_format_function_calls(self, delta):
        """
        Helper to detect function calls in different formats and normalize them.
        Handles Qwen specifics where function calls may be formatted differently.

        Returns: List of normalized tool calls or None
        """
        # Standard OpenAI-style tool_calls format
        if hasattr(delta, "tool_calls") and delta.tool_calls:
            return delta.tool_calls
        elif isinstance(delta, dict) and "tool_calls" in delta and delta["tool_calls"]:
            return delta["tool_calls"]

        # Qwen/Ollama function_call format
        if isinstance(delta, dict) and "function_call" in delta:
            function_call = delta["function_call"]
            if function_call is None:
                return None
            return [
                {
                    "index": 0,
                    "id": f"call_{time.time_ns()}",  # Generate a unique ID
                    "type": "function",
                    "function": {
                        "name": function_call.get("name", ""),
                        "arguments": function_call.get("arguments", ""),
                    },
                }
            ]

        if isinstance(delta, dict) and "content" in delta:
            content = delta["content"]
            # Try to detect if the content is a JSON string with function call format
            try:
                if isinstance(content, str) and "{" in content and "}" in content:
                    # Try to extract JSON from the content (it might be embedded in text)
                    json_start = content.find("{")
                    json_end = content.rfind("}") + 1
                    if json_start >= 0 and json_end > json_start:
                        json_str = content[json_start:json_end]
                        parsed = _safe_json_loads(json_str, "delta content function call")
                        if parsed and "name" in parsed and "arguments" in parsed:
                            # This looks like a function call in JSON format
                            return [
                                {
                                    "index": 0,
                                    "id": f"call_{time.time_ns()}",  # Generate a unique ID
                                    "type": "function",
                                    "function": {
                                        "name": parsed["name"],
                                        "arguments": json.dumps(parsed["arguments"])
                                        if isinstance(parsed["arguments"], dict)
                                        else parsed["arguments"],
                                    },
                                }
                            ]
            except Exception:
                # If JSON parsing fails, just continue with normal processing
                pass

        # Anthropic-style tool_use format
        if hasattr(delta, "tool_use") and delta.tool_use:
            tool_use = delta.tool_use
            return [
                {
                    "index": 0,
                    "id": tool_use.get("id", f"tool_{time.time_ns()}"),
                    "type": "function",
                    "function": {
                        "name": tool_use.get("name", ""),
                        "arguments": tool_use.get("input", "{}"),
                    },
                }
            ]
        elif isinstance(delta, dict) and "tool_use" in delta and delta["tool_use"]:
            tool_use = delta["tool_use"]
            return [
                {
                    "index": 0,
                    "id": tool_use.get("id", f"tool_{time.time_ns()}"),
                    "type": "function",
                    "function": {
                        "name": tool_use.get("name", ""),
                        "arguments": tool_use.get("input", "{}"),
                    },
                }
            ]

        return None

get_full_display_name

get_full_display_name() -> str

Get the full display name including ID.

Source code in src/cai/sdk/agents/models/openai_chatcompletions.py
553
554
555
def get_full_display_name(self) -> str:
    """Get the full display name including ID."""
    return f"{self._display_name} [{self.agent_id}]"

__del__

__del__()

Clean up when the model instance is destroyed.

Source code in src/cai/sdk/agents/models/openai_chatcompletions.py
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
def __del__(self):
    """Clean up when the model instance is destroyed."""
    try:
        # DEPRECATED: Remove from old registry for backward compatibility
        if hasattr(self, '_display_name') and hasattr(self, 'agent_id'):
            key = (self._display_name, self.agent_id)
            if key in ACTIVE_MODEL_INSTANCES:
                del ACTIVE_MODEL_INSTANCES[key]

        # SimpleAgentManager handles history persistence
        # No need to save to PERSISTENT_MESSAGE_HISTORIES

    except Exception:
        # Ignore any errors during cleanup
        pass

add_to_message_history

add_to_message_history(
    msg, skip_deduplication: bool = False
)

Add a message to this instance's history.

Parameters:

Name Type Description Default
msg

The message dictionary to add

required
skip_deduplication bool

If True, skip all duplicate checking and just append. Use this when loading session history where messages are already in correct order and deduplication would cause reordering issues.

False

Now only adds to the instance's local history, no global registry.

Source code in src/cai/sdk/agents/models/openai_chatcompletions.py
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
def add_to_message_history(self, msg, skip_deduplication: bool = False):
    """Add a message to this instance's history.

    Args:
        msg: The message dictionary to add
        skip_deduplication: If True, skip all duplicate checking and just append.
                          Use this when loading session history where messages
                          are already in correct order and deduplication would
                          cause reordering issues.

    Now only adds to the instance's local history, no global registry.
    """
    # When loading session history, skip all deduplication to preserve order
    if skip_deduplication:
        self.message_history.append(msg)
        manager_history = AGENT_MANAGER.get_message_history(self.agent_name)
        if manager_history is not self.message_history:
            AGENT_MANAGER.add_to_history(self.agent_name, msg)
        if PARALLEL_ISOLATION.is_parallel_mode() and self.agent_id:
            PARALLEL_ISOLATION.update_isolated_history(self.agent_id, msg)
        return

    is_duplicate = False

    if self.message_history:
        if msg.get("role") in ["system", "user"]:
            is_duplicate = any(
                existing.get("role") == msg.get("role")
                and existing.get("content") == msg.get("content")
                for existing in self.message_history
            )
        elif msg.get("role") == "assistant" and msg.get("tool_calls"):
            # For tool calls, check if message with same tool call ID already exists
            # If it does, UPDATE IN PLACE to preserve message order
            tool_call_id = msg["tool_calls"][0].get("id") if msg.get("tool_calls") else None
            if tool_call_id:
                existing_idx = None
                for i, existing in enumerate(self.message_history):
                    if (existing.get("role") == "assistant"
                        and existing.get("tool_calls")
                        and existing["tool_calls"][0].get("id") == tool_call_id):
                        existing_idx = i
                        break

                if existing_idx is not None:
                    # UPDATE IN PLACE to preserve order (don't remove and re-add!)
                    self.message_history[existing_idx] = msg
                    is_duplicate = True  # Mark as duplicate so we don't append again
                else:
                    is_duplicate = False
        elif msg.get("role") == "tool":
            is_duplicate = any(
                existing.get("role") == "tool"
                and existing.get("tool_call_id") == msg.get("tool_call_id")
                for existing in self.message_history
            )

    if not is_duplicate:
        self.message_history.append(msg)
        # Also update SimpleAgentManager ONLY if they're not the same list reference
        # This avoids double-adding when they share the same list
        manager_history = AGENT_MANAGER.get_message_history(self.agent_name)
        if manager_history is not self.message_history:
            AGENT_MANAGER.add_to_history(self.agent_name, msg)
        # Update isolated history if in parallel mode
        if PARALLEL_ISOLATION.is_parallel_mode() and self.agent_id:
            PARALLEL_ISOLATION.update_isolated_history(self.agent_id, msg)

set_agent_name

set_agent_name(name: str) -> None

Set the agent name for CLI display purposes.

Source code in src/cai/sdk/agents/models/openai_chatcompletions.py
641
642
643
def set_agent_name(self, name: str) -> None:
    """Set the agent name for CLI display purposes."""
    self.agent_name = name

stream_response async

stream_response(
    system_instructions: str | None,
    input: str | list[TResponseInputItem],
    model_settings: ModelSettings,
    tools: list[Tool],
    output_schema: AgentOutputSchema | None,
    handoffs: list[Handoff],
    tracing: ModelTracing,
    *,
    _empty_completion_streak: int = 0,
) -> AsyncIterator[TResponseStreamEvent]

Yields a partial message as it is generated, as well as the usage information.

Source code in src/cai/sdk/agents/models/openai_chatcompletions.py
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
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
async def stream_response(
    self,
    system_instructions: str | None,
    input: str | list[TResponseInputItem],
    model_settings: ModelSettings,
    tools: list[Tool],
    output_schema: AgentOutputSchema | None,
    handoffs: list[Handoff],
    tracing: ModelTracing,
    *,
    _empty_completion_streak: int = 0,
) -> AsyncIterator[TResponseStreamEvent]:
    """
    Yields a partial message as it is generated, as well as the usage information.
    """
    # Close any open streaming panels from the previous cycle
    # This ensures panels don't stay open when the model starts a new inference
    try:
        from cai.util import close_all_streaming_panels
        close_all_streaming_panels()
    except ImportError:
        pass

    # Initialize streaming contexts as None
    streaming_context = None
    thinking_context = None
    stream_interrupted = False
    stream_wait_hints: ModelStreamWaitHints | None = None

    try:
        # IMPORTANT: Pre-process input to ensure it's in the correct format
        # for streaming. This helps prevent errors during stream handling.
        if not isinstance(input, str):
            # Convert input items to messages and verify structure
            try:
                input_items = list(input)  # Make sure it's a list
                # Pre-verify the input messages to avoid errors during streaming
                from cai.util import fix_message_list

                # Apply fix_message_list to the input items that are dictionaries
                dict_items = [item for item in input_items if isinstance(item, dict)]
                if dict_items:
                    fixed_dict_items = fix_message_list(dict_items)

                    # Replace the original dict items with fixed ones while preserving non-dict items
                    new_input = []
                    dict_index = 0
                    for item in input_items:
                        if isinstance(item, dict):
                            if dict_index < len(fixed_dict_items):
                                new_input.append(fixed_dict_items[dict_index])
                                dict_index += 1
                        else:
                            new_input.append(item)

                    # Update input with the fixed version
                    input = new_input
            except Exception as e:
                # Silently continue with original input if pre-processing failed
                # This is not critical and shouldn't show warnings
                pass

        # Increment the interaction counter for CLI display
        self.interaction_counter += 1
        self._intermediate_logs()

        # Stop idle timer and start active timer to track LLM processing time
        stop_idle_timer()
        start_active_timer()

        # --- Check if streaming should be shown in rich panel ---
        # Sub-agents invoked as tools by the orchestration agent must not
        # render Rich streaming panels — only the orchestrator's final
        # synthesis is shown to the user. See ``_worker_silence``.
        should_show_rich_stream = (
            get_config().stream
            and not self.disable_rich_streaming
            and not worker_display_silenced()
        )

        # Lazy-init Rich Live: build streaming_context on first text delta (not before HTTP),
        # so startup work (terminal sizing, Live allocation) stays off the pre-TTFT path.

        with generation_span(
            model=str(self.model),
            model_config=dataclasses.asdict(model_settings)
            | {"base_url": str(self._get_client().base_url)},
            disabled=tracing.is_disabled(),
        ) as span_generation:
            # Prepare messages for consistent token counting
            # IMPORTANT: Include existing message history for context (matching get_response pattern)
            converted_messages = self._shallow_copy_history_messages()

            # Then convert and add the new input
            new_messages = self._converter.items_to_messages(input, model_instance=self)
            converted_messages.extend(new_messages)

            if system_instructions:
                # Check if we already have a system message
                has_system = any(msg.get("role") == "system" for msg in converted_messages)
                if not has_system:
                    converted_messages.insert(
                        0,
                        {
                            "content": system_instructions,
                            "role": "system",
                        },
                    )

            #    # --- Add to message_history: user, system prompts ---
            #     if system_instructions:
            #         sys_msg = {
            #             "role": "system",
            #             "content": system_instructions
            #         }
            #         self.add_to_message_history(sys_msg)

            if isinstance(input, str):
                user_msg = {"role": "user", "content": input}
                self.add_to_message_history(user_msg)
                # Log the user message
                self.logger.log_user_message(input)
            elif isinstance(input, list):
                for item in input:
                    if isinstance(item, dict):
                        if item.get("role") == "user":
                            user_msg = {"role": "user", "content": item.get("content", "")}
                            self.add_to_message_history(user_msg)
                            # Log the user message
                            if item.get("content"):
                                self.logger.log_user_message(item.get("content"))

            # IMPORTANT: Ensure the message list has valid tool call/result pairs
            # This needs to happen before the API call AND before applying cache_control
            try:
                from cai.util import fix_message_list

                converted_messages = fix_message_list(converted_messages)
            except Exception:
                pass

            # Request-path message normalization/cache-control is applied in _fetch_response().
            # Keep startup estimation lightweight to avoid duplicate per-turn preprocessing work.

            # Get token count estimate before API call for consistent counting
            estimated_input_tokens, _ = count_tokens_with_tiktoken(converted_messages)

            # Check if auto-compaction is needed
            input, system_instructions, compacted = await self._auto_compact_if_needed(estimated_input_tokens, input, system_instructions)

            # If compaction occurred, recalculate tokens from the same view ``_fetch_response`` uses
            if compacted:
                converted_messages = self._messages_for_token_count_after_history_mutation(
                    system_instructions=system_instructions,
                    input=input,
                )
                estimated_input_tokens, _ = count_tokens_with_tiktoken(converted_messages)
                max_tok = self._get_model_max_tokens(str(self.model))
                if max_tok > 0:
                    os.environ["CAI_CONTEXT_USAGE"] = str(
                        min(1.0, max(0.0, estimated_input_tokens / max_tok))
                    )

            # Pre-check price limit using estimated input tokens and a conservative estimate for output
            # This prevents starting a stream that would immediately exceed the price limit
            if hasattr(COST_TRACKER, "check_price_limit"):
                # Use a conservative estimate for output tokens (roughly equal to input)
                estimated_cost = calculate_model_cost(
                    str(self.model), estimated_input_tokens, estimated_input_tokens
                )  # Conservative estimate
                try:
                    COST_TRACKER.check_price_limit(estimated_cost)
                except Exception:
                    # Ensure streaming context is cleaned up in case of errors
                    if streaming_context:
                        try:
                            finish_agent_streaming(streaming_context, None)
                        except Exception:
                            pass
                    # Stop active timer and start idle timer before re-raising the exception
                    stop_active_timer()
                    start_idle_timer()
                    raise

            stream_wait_hints = ModelStreamWaitHints()
            await stream_wait_hints.start()
            # Proactive client-side pacing for the alias gateway. The
            # stream wait hint is already active and reads
            # ``_retry_overlay_message``, so the overlay surfaces
            # automatically. ``alias_gateway_slot`` auto-releases on
            # pre-gateway errors (connect failures, KbInt) so a burst
            # doesn't pin the budget for 60s.
            _stream_on_pace = make_pace_overlay_callback()
            try:
                if self._is_alias_model:
                    # Streaming pacing: same projection (+completion buffer)
                    # as get_response. ``Reservation.update_actual`` is NOT
                    # called here — final ``usage`` arrives at end-of-stream
                    # outside the slot. The limiter's 85% safety margin
                    # absorbs that residual estimation gap.
                    _stream_projection = estimated_input_tokens + COMPLETION_BUDGET_TOKENS
                    async with get_gateway_rate_limiter().alias_gateway_slot(
                        _stream_projection,
                        on_pace=_stream_on_pace,
                    ):
                        response, stream = await self._fetch_response(
                            system_instructions,
                            input,
                            model_settings,
                            tools,
                            output_schema,
                            handoffs,
                            span_generation,
                            tracing,
                            stream=True,
                        )
                else:
                    response, stream = await self._fetch_response(
                        system_instructions,
                        input,
                        model_settings,
                        tools,
                        output_schema,
                        handoffs,
                        span_generation,
                        tracing,
                        stream=True,
                    )
                # Clear any pacing overlay so the default stream body
                # ("Esperando…") shows during the HTTP response read.
                set_model_wait_retry_overlay(None)
            except KeyboardInterrupt:
                await stream_wait_hints.stop()
                set_model_wait_retry_overlay(None)
                stop_active_timer()
                start_idle_timer()
                raise
            except (litellm.exceptions.Timeout, LLMTimeout) as e:
                await stream_wait_hints.stop()
                # Streaming timeout with exponential backoff — clean retry
                self.logger.warning(f"Timeout in stream_response: {e}")
                stop_active_timer()
                start_idle_timer()

                if not hasattr(self, "_high_level_retry_count"):
                    self._high_level_retry_count = 0
                self._high_level_retry_count += 1

                if self._high_level_retry_count > 3:
                    self._high_level_retry_count = 0
                    raise LLMTimeout(f"Timed out after 3 attempts [{self.model}]") from e

                await self._retry_with_backoff(self._high_level_retry_count - 1, "Timeout")

                # Clean retry: same input, no "continue" in history
                async for event in self.stream_response(
                    system_instructions, input, model_settings,
                    tools, output_schema, handoffs, tracing,
                ):
                    yield event
                self._high_level_retry_count = 0
                return

            except (litellm.exceptions.RateLimitError, LLMRateLimited) as e:
                await stream_wait_hints.stop()
                # Streaming rate-limit with exponential backoff — clean retry
                self.logger.warning(f"Rate limit in stream_response: {e}")
                stop_active_timer()
                start_idle_timer()

                if not hasattr(self, "_high_level_retry_count"):
                    self._high_level_retry_count = 0
                self._high_level_retry_count += 1

                if self._high_level_retry_count > 3:
                    self._high_level_retry_count = 0
                    raise LLMRateLimited(
                        f"Rate limit after 3 attempts [{self.model}]",
                        retry_after=getattr(e, "retry_after", None),
                    ) from e

                await self._retry_with_backoff(self._high_level_retry_count - 1, "Rate limit")

                # Clean retry: same input, no "continue" in history
                async for event in self.stream_response(
                    system_instructions, input, model_settings,
                    tools, output_schema, handoffs, tracing,
                ):
                    yield event
                self._high_level_retry_count = 0
                return

            except BaseException:
                await stream_wait_hints.stop()
                raise

            usage: CompletionUsage | None = None
            state = _StreamingState()

            def next_sequence_number() -> int:
                sequence_number = state.sequence_number
                state.sequence_number += 1
                return sequence_number

            # Manual token counting (when API doesn't provide it)
            output_text = ""
            estimated_output_tokens = 0
            streaming_reasoning_text = ""

            # Initialize a streaming text accumulator for rich display
            streaming_text_buffer = ""
            # For tool call streaming, accumulate tool_calls to add to message_history at the end
            streamed_tool_calls = []

            # Initialize Claude thinking display if applicable
            if should_show_rich_stream:  # Only show thinking in rich streaming mode
                thinking_context = start_claude_thinking_if_applicable(
                    str(self.model), self.agent_name, self.interaction_counter
                )

            # Ollama specific: accumulate full content to check for function calls at the end
            # Some Ollama models output the function call as JSON in the text content
            ollama_full_content = ""
            is_ollama = False

            model_str = str(self.model).lower()
            is_ollama = (
                self.is_ollama
                or "ollama" in model_str
                or ":" in model_str
                or "qwen" in model_str
            )

            # Add visual separation before agent output
            if streaming_context and should_show_rich_stream:
                # If we're using rich context, we'll add separation through that
                pass
            else:
                # Removed clear visual separator to avoid blank lines during streaming
                pass

            try:
                async for chunk in stream:
                    await stream_wait_hints.stop()

                    # Check if we've been interrupted
                    if stream_interrupted:
                        break

                    if not state.started:
                        state.started = True
                        yield ResponseCreatedEvent(
                            response=response,
                            sequence_number=next_sequence_number(),
                            type="response.created",
                        )

                    # The usage is only available in the last chunk
                    if hasattr(chunk, "usage"):
                        usage = chunk.usage
                    # For Ollama/LiteLLM streams that don't have usage attribute
                    else:
                        usage = None

                    # Handle different stream chunk formats
                    if hasattr(chunk, "choices") and chunk.choices:
                        choices = chunk.choices
                    elif hasattr(chunk, "delta") and chunk.delta:
                        # Some providers might return delta directly
                        choices = [{"delta": chunk.delta}]
                    elif isinstance(chunk, dict) and "choices" in chunk:
                        choices = chunk["choices"]
                    # Special handling for Qwen/Ollama chunks
                    elif isinstance(chunk, dict) and (
                        "content" in chunk or "function_call" in chunk
                    ):
                        # Qwen direct delta format - convert to standard
                        choices = [{"delta": chunk}]
                    else:
                        # Skip chunks that don't contain choice data
                        continue

                    if not choices or len(choices) == 0:
                        continue

                    # Get the delta content
                    delta = None
                    if hasattr(choices[0], "delta"):
                        delta = choices[0].delta
                    elif isinstance(choices[0], dict) and "delta" in choices[0]:
                        delta = choices[0]["delta"]

                    if not delta:
                        continue

                    # Handle Claude reasoning content first (before regular content)
                    reasoning_content = None

                    # Check for Claude reasoning in different possible formats
                    if (
                        hasattr(delta, "reasoning_content")
                        and delta.reasoning_content is not None
                    ):
                        reasoning_content = delta.reasoning_content
                    elif (
                        isinstance(delta, dict)
                        and "reasoning_content" in delta
                        and delta["reasoning_content"] is not None
                    ):
                        reasoning_content = delta["reasoning_content"]

                    # Also check for thinking_blocks structure (Claude 4 format)
                    thinking_blocks = None
                    if hasattr(delta, "thinking_blocks") and delta.thinking_blocks is not None:
                        thinking_blocks = delta.thinking_blocks
                    elif (
                        isinstance(delta, dict)
                        and "thinking_blocks" in delta
                        and delta["thinking_blocks"] is not None
                    ):
                        thinking_blocks = delta["thinking_blocks"]

                    # Extract reasoning content from thinking blocks if available
                    if thinking_blocks and not reasoning_content:
                        for block in thinking_blocks:
                            if isinstance(block, dict) and block.get("type") == "thinking":
                                reasoning_content = block.get("thinking", "")
                                break
                            elif (
                                isinstance(block, dict)
                                and block.get("type") == "text"
                                and "thinking" in str(block)
                            ):
                                # Sometimes thinking content comes as text blocks
                                reasoning_content = block.get("text", "")
                                break

                    # Check for direct thinking field (some Claude models)
                    if not reasoning_content:
                        if hasattr(delta, "thinking") and delta.thinking is not None:
                            reasoning_content = delta.thinking
                        elif (
                            isinstance(delta, dict)
                            and "thinking" in delta
                            and delta["thinking"] is not None
                        ):
                            reasoning_content = delta["thinking"]

                    # Update thinking display if we have reasoning content
                    if reasoning_content:
                        if isinstance(reasoning_content, str):
                            streaming_reasoning_text += reasoning_content
                        if thinking_context:
                            # Streaming mode: Update the rich thinking display
                            from cai.util import update_claude_thinking_content

                            update_claude_thinking_content(thinking_context, reasoning_content)
                        else:
                            # Non-streaming mode: Use simple text output
                            from cai.util import (
                                detect_claude_thinking_in_stream,
                                print_claude_reasoning_simple,
                            )

                            # Check if model supports reasoning (Claude or DeepSeek)
                            model_str_lower = str(self.model).lower()
                            if (
                                detect_claude_thinking_in_stream(str(self.model))
                                or "deepseek" in model_str_lower
                            ):
                                print_claude_reasoning_simple(
                                    reasoning_content, self.agent_name, str(self.model)
                                )

                    # Handle text
                    content = None
                    if hasattr(delta, "content") and delta.content is not None:
                        content = delta.content
                    elif (
                        isinstance(delta, dict)
                        and "content" in delta
                        and delta["content"] is not None
                    ):
                        content = delta["content"]

                    if content:
                        # IMPORTANT: If we have content and thinking_context is active,
                        # it means thinking is complete and normal content is starting
                        # Close the thinking display automatically
                        if thinking_context:
                            from cai.util import finish_claude_thinking_display

                            finish_claude_thinking_display(thinking_context)
                            thinking_context = None  # Clear the context

                        # For Ollama, we need to accumulate the full content to check for function calls
                        if is_ollama:
                            ollama_full_content += content

                        # Add to the streaming text buffer
                        streaming_text_buffer += content

                        # Update streaming display if enabled - ALWAYS respect CAI_STREAM setting
                        # Both thinking and regular content should stream if streaming is enabled
                        if (
                            should_show_rich_stream
                            and streaming_context is None
                        ):
                            try:
                                streaming_context = create_agent_streaming_context(
                                    agent_name=self.agent_name,
                                    counter=self.interaction_counter,
                                    model=str(self.model),
                                )
                            except Exception:
                                streaming_context = None

                        if streaming_context:
                            # Calculate cost for current interaction
                            current_cost = calculate_model_cost(
                                str(self.model), estimated_input_tokens, estimated_output_tokens
                            )

                            # Check price limit only for paid models
                            if (
                                current_cost > 0
                                and hasattr(COST_TRACKER, "check_price_limit")
                                and estimated_output_tokens % 50 == 0
                            ):
                                try:
                                    COST_TRACKER.check_price_limit(current_cost)
                                except Exception:
                                    # Ensure streaming context is cleaned up
                                    if streaming_context:
                                        try:
                                            finish_agent_streaming(streaming_context, None)
                                        except Exception:
                                            pass
                                    # Stop timers and re-raise the exception
                                    stop_active_timer()
                                    start_idle_timer()
                                    raise

                            # Update session total cost for real-time display
                            # This is a temporary estimate during streaming that will be properly updated at the end
                            estimated_session_total = getattr(
                                COST_TRACKER, "session_total_cost", 0.0
                            )

                            # For free models, don't add to the total cost
                            display_total_cost = estimated_session_total
                            if current_cost > 0:
                                display_total_cost += current_cost

                            # Create token stats with both current interaction cost and updated total cost
                            token_stats = {
                                "input_tokens": estimated_input_tokens,
                                "output_tokens": estimated_output_tokens,
                                "cost": current_cost,
                                "total_cost": display_total_cost,
                            }

                            update_agent_streaming_content(
                                streaming_context, content, token_stats
                            )

                        # More accurate token counting for text content
                        output_text += content
                        token_count, _ = count_tokens_with_tiktoken(output_text)
                        estimated_output_tokens = token_count

                        # Periodically check price limit during streaming
                        # This allows early termination if price limit is reached mid-stream
                        if (
                            estimated_output_tokens > 0 and estimated_output_tokens % 50 == 0
                        ):  # Check every ~50 tokens
                            # Calculate current estimated cost
                            current_estimated_cost = calculate_model_cost(
                                str(self.model), estimated_input_tokens, estimated_output_tokens
                            )

                            # Check price limit only for paid models
                            if current_estimated_cost > 0 and hasattr(
                                COST_TRACKER, "check_price_limit"
                            ):
                                try:
                                    COST_TRACKER.check_price_limit(current_estimated_cost)
                                except Exception:
                                    # Ensure streaming context is cleaned up
                                    if streaming_context:
                                        try:
                                            finish_agent_streaming(streaming_context, None)
                                        except Exception:
                                            pass
                                    # Stop timers and re-raise the exception
                                    stop_active_timer()
                                    start_idle_timer()
                                    raise

                            # Update the COST_TRACKER with the running cost for accurate display
                            if hasattr(COST_TRACKER, "interaction_cost"):
                                COST_TRACKER.interaction_cost = current_estimated_cost

                            # Also update streaming context if available for live display
                            if streaming_context:
                                # For free models, don't add to the session total
                                if current_estimated_cost == 0:
                                    session_total = getattr(
                                        COST_TRACKER, "session_total_cost", 0.0
                                    )
                                else:
                                    session_total = (
                                        getattr(COST_TRACKER, "session_total_cost", 0.0)
                                        + current_estimated_cost
                                    )

                                updated_token_stats = {
                                    "input_tokens": estimated_input_tokens,
                                    "output_tokens": estimated_output_tokens,
                                    "cost": current_estimated_cost,
                                    "total_cost": session_total,
                                }
                                update_agent_streaming_content(
                                    streaming_context, "", updated_token_stats
                                )

                        if not state.text_content_index_and_output:
                            # Initialize a content tracker for streaming text
                            state.text_content_index_and_output = (
                                0 if not state.refusal_content_index_and_output else 1,
                                ResponseOutputText(
                                    text="",
                                    type="output_text",
                                    annotations=[],
                                ),
                            )
                            # Start a new assistant message stream
                            assistant_item = ResponseOutputMessage(
                                id=FAKE_RESPONSES_ID,
                                content=[],
                                role="assistant",
                                type="message",
                                status="in_progress",
                            )
                            # Notify consumers of the start of a new output message + first content part
                            yield ResponseOutputItemAddedEvent(
                                item=assistant_item,
                                output_index=0,
                                sequence_number=next_sequence_number(),
                                type="response.output_item.added",
                            )
                            yield ResponseContentPartAddedEvent(
                                content_index=state.text_content_index_and_output[0],
                                item_id=FAKE_RESPONSES_ID,
                                output_index=0,
                                part=ResponseOutputText(
                                    text="",
                                    type="output_text",
                                    annotations=[],
                                ),
                                sequence_number=next_sequence_number(),
                                type="response.content_part.added",
                            )
                        # Emit the delta for this segment of content
                        yield ResponseTextDeltaEvent(
                            content_index=state.text_content_index_and_output[0],
                            delta=content,
                            item_id=FAKE_RESPONSES_ID,
                            logprobs=[],
                            sequence_number=next_sequence_number(),
                            output_index=0,
                            type="response.output_text.delta",
                        )
                        # Accumulate the text into the response part
                        state.text_content_index_and_output[1].text += content

                    # Handle refusals (model declines to answer)
                    refusal_content = None
                    if hasattr(delta, "refusal") and delta.refusal:
                        refusal_content = delta.refusal
                    elif isinstance(delta, dict) and "refusal" in delta and delta["refusal"]:
                        refusal_content = delta["refusal"]

                    if refusal_content:
                        if not state.refusal_content_index_and_output:
                            # Initialize a content tracker for streaming refusal text
                            state.refusal_content_index_and_output = (
                                0 if not state.text_content_index_and_output else 1,
                                ResponseOutputRefusal(refusal="", type="refusal"),
                            )
                            # Start a new assistant message if one doesn't exist yet (in-progress)
                            assistant_item = ResponseOutputMessage(
                                id=FAKE_RESPONSES_ID,
                                content=[],
                                role="assistant",
                                type="message",
                                status="in_progress",
                            )
                            # Notify downstream that assistant message + first content part are starting
                            yield ResponseOutputItemAddedEvent(
                                item=assistant_item,
                                output_index=0,
                                sequence_number=next_sequence_number(),
                                type="response.output_item.added",
                            )
                            yield ResponseContentPartAddedEvent(
                                content_index=state.refusal_content_index_and_output[0],
                                item_id=FAKE_RESPONSES_ID,
                                output_index=0,
                                part=ResponseOutputText(
                                    text="",
                                    type="output_text",
                                    annotations=[],
                                ),
                                sequence_number=next_sequence_number(),
                                type="response.content_part.added",
                            )
                        # Emit the delta for this segment of refusal
                        yield ResponseRefusalDeltaEvent(
                            content_index=state.refusal_content_index_and_output[0],
                            delta=refusal_content,
                            item_id=FAKE_RESPONSES_ID,
                            sequence_number=next_sequence_number(),
                            output_index=0,
                            type="response.refusal.delta",
                        )
                        # Accumulate the refusal string in the output part
                        state.refusal_content_index_and_output[1].refusal += refusal_content

                    # Handle tool calls
                    # Because we don't know the name of the function until the end of the stream, we'll
                    # save everything and yield events at the end
                    tool_calls = self._detect_and_format_function_calls(delta)

                    if tool_calls:
                        for tc_delta in tool_calls:
                            tc_index = (
                                tc_delta.index
                                if hasattr(tc_delta, "index")
                                else tc_delta.get("index", 0)
                            )
                            if tc_index not in state.function_calls:
                                state.function_calls[tc_index] = ResponseFunctionToolCall(
                                    id=FAKE_RESPONSES_ID,
                                    arguments="",
                                    name="",
                                    type="function_call",
                                    call_id="",
                                )

                            tc_function = None
                            if hasattr(tc_delta, "function"):
                                tc_function = tc_delta.function
                            elif isinstance(tc_delta, dict) and "function" in tc_delta:
                                tc_function = tc_delta["function"]

                            if tc_function:
                                # Handle both object and dict formats
                                args = ""
                                if hasattr(tc_function, "arguments"):
                                    args = tc_function.arguments or ""
                                elif (
                                    isinstance(tc_function, dict) and "arguments" in tc_function
                                ):
                                    args = tc_function.get("arguments", "") or ""

                                name = ""
                                if hasattr(tc_function, "name"):
                                    name = tc_function.name or ""
                                elif isinstance(tc_function, dict) and "name" in tc_function:
                                    name = tc_function.get("name", "") or ""

                                state.function_calls[tc_index].arguments += args
                                state.function_calls[tc_index].name += name

                            # Handle call_id in both formats
                            call_id = ""
                            if hasattr(tc_delta, "id"):
                                call_id = tc_delta.id or ""
                            elif isinstance(tc_delta, dict) and "id" in tc_delta:
                                call_id = tc_delta.get("id", "") or ""
                            else:
                                # For Qwen models, generate a predictable ID if none is provided
                                if state.function_calls[tc_index].name:
                                    # Generate a stable ID from the function name and arguments
                                    call_id = f"call_{hashlib.md5(state.function_calls[tc_index].name.encode()).hexdigest()[:8]}"

                            state.function_calls[tc_index].call_id += call_id

                            # --- Accumulate tool call for message_history ---
                            # Only add if not already present (avoid duplicates in streaming)
                            # Handle empty arguments before storing
                            tool_args = state.function_calls[tc_index].arguments
                            if tool_args is None or (isinstance(tool_args, str) and tool_args.strip() == ""):
                                tool_args = "{}"

                            tool_call_msg = {
                                "role": "assistant",
                                "content": None,
                                "tool_calls": [
                                    {
                                        "id": state.function_calls[tc_index].call_id,
                                        "type": "function",
                                        "function": {
                                            "name": state.function_calls[tc_index].name,
                                            "arguments": tool_args,
                                        },
                                    }
                                ],
                            }
                            # Only add if not already in streamed_tool_calls
                            if tool_call_msg not in streamed_tool_calls:
                                streamed_tool_calls.append(tool_call_msg)
                                # Don't add to message history here - wait for tool output
                                # to add both tool call and response atomically

                                # NEW: Display tool call immediately when detected in streaming mode
                                # But only if it has complete arguments and name
                                if (
                                    state.function_calls[tc_index].name
                                    and state.function_calls[tc_index].arguments
                                    and state.function_calls[tc_index].call_id
                                ):
                                    # First, finish any existing streaming context if it exists
                                    if streaming_context:
                                        try:
                                            finish_agent_streaming(streaming_context, None)
                                            streaming_context = None
                                        except Exception:
                                            pass

                                    # Create a message-like object for displaying the function call
                                    tool_msg = type(
                                        "ToolCallStreamDisplay",
                                        (),
                                        {
                                            "content": None,
                                            "tool_calls": [
                                                type(
                                                    "ToolCallDetail",
                                                    (),
                                                    {
                                                        "function": type(
                                                            "FunctionDetail",
                                                            (),
                                                            {
                                                                "name": state.function_calls[
                                                                    tc_index
                                                                ].name,
                                                                "arguments": state.function_calls[
                                                                    tc_index
                                                                ].arguments,
                                                            },
                                                        ),
                                                        "id": state.function_calls[
                                                            tc_index
                                                        ].call_id,
                                                        "type": "function",
                                                    },
                                                )
                                            ],
                                        },
                                    )

                                    # Display the tool call during streaming
                                    cli_print_agent_messages(
                                        agent_name=getattr(self, "agent_name", "Agent"),
                                        message=tool_msg,
                                        counter=getattr(self, "interaction_counter", 0),
                                        model=str(self.model),
                                        debug=False,
                                        interaction_input_tokens=estimated_input_tokens,
                                        interaction_output_tokens=estimated_output_tokens,
                                        interaction_reasoning_tokens=0,
                                        total_input_tokens=getattr(
                                            self, "total_input_tokens", 0
                                        )
                                        + estimated_input_tokens,
                                        total_output_tokens=getattr(
                                            self, "total_output_tokens", 0
                                        )
                                        + estimated_output_tokens,
                                        total_reasoning_tokens=getattr(
                                            self, "total_reasoning_tokens", 0
                                        ),
                                        interaction_cost=None,
                                        total_cost=None,
                                        tool_output=None,
                                        suppress_empty=True,
                                    )
                                    # Set flag to suppress final output to avoid duplication
                                    self.suppress_final_output = True

            except KeyboardInterrupt:
                # Handle interruption during streaming
                stream_interrupted = True
                print("\n[Streaming interrupted by user]", file=sys.stderr)

                # Let the exception propagate after cleanup
                raise

            except Exception as e:
                # Handle other exceptions during streaming
                logger.error(f"Error during streaming: {e}")
                if "token" in str(e).lower() or "limit" in str(e).lower():
                    print("\n📏 Token limit exceeded - Response truncated")
                raise

            # Special handling for Ollama - check if accumulated text contains a valid function call
            if is_ollama and ollama_full_content and len(state.function_calls) == 0:
                # Look for JSON object that might be a function call
                try:
                    # Try to extract a JSON object from the content
                    json_start = ollama_full_content.find("{")
                    json_end = ollama_full_content.rfind("}") + 1

                    if json_start >= 0 and json_end > json_start:
                        json_str = ollama_full_content[json_start:json_end]
                        # Try to parse the JSON
                        parsed = _safe_json_loads(json_str, "Ollama function call")
                        if not parsed:
                            raise ValueError("Failed to parse Ollama function call JSON")

                        # Check if it looks like a function call
                        if "name" in parsed and "arguments" in parsed:
                            logger.debug(
                                f"Found valid function call in Ollama output: {json_str}"
                            )

                            # Create a tool call ID
                            tool_call_id = f"call_{hashlib.md5((parsed['name'] + str(time.time())).encode()).hexdigest()[:8]}"

                            # Ensure arguments is a valid JSON string
                            arguments_str = ""
                            if isinstance(parsed["arguments"], dict):
                                # Remove 'ctf' field if it exists
                                if "ctf" in parsed["arguments"]:
                                    del parsed["arguments"]["ctf"]
                                arguments_str = json.dumps(parsed["arguments"])
                            elif isinstance(parsed["arguments"], str):
                                # If it's already a string, check if it's valid JSON
                                # Try parsing to validate and remove 'ctf' if present
                                args_dict = _safe_json_loads(parsed["arguments"], "Ollama tool arguments")
                                if args_dict:
                                    if isinstance(args_dict, dict) and "ctf" in args_dict:
                                        del args_dict["ctf"]
                                    arguments_str = json.dumps(args_dict)
                                else:
                                    # If not valid JSON, encode it as a JSON string
                                    arguments_str = json.dumps(parsed["arguments"])
                            else:
                                # For any other type, convert to string and then JSON
                                arguments_str = json.dumps(str(parsed["arguments"]))
                            # Add it to our function_calls state
                            state.function_calls[0] = ResponseFunctionToolCall(
                                id=FAKE_RESPONSES_ID,
                                arguments=arguments_str,
                                name=parsed["name"],
                                type="function_call",
                                call_id=tool_call_id[:40],
                            )

                            # Display the tool call in CLI
                            try:
                                # First, finish any existing streaming context if it exists
                                if streaming_context:
                                    try:
                                        finish_agent_streaming(streaming_context, None)
                                        streaming_context = None
                                    except Exception:
                                        pass

                                # Create a message-like object to display the function call
                                tool_msg = type(
                                    "ToolCallWrapper",
                                    (),
                                    {
                                        "content": None,
                                        "tool_calls": [
                                            type(
                                                "ToolCallDetail",
                                                (),
                                                {
                                                    "function": type(
                                                        "FunctionDetail",
                                                        (),
                                                        {
                                                            "name": parsed["name"],
                                                            "arguments": arguments_str,
                                                        },
                                                    ),
                                                    "id": tool_call_id[:40],
                                                    "type": "function",
                                                },
                                            )
                                        ],
                                    },
                                )

                                # Print the tool call using the CLI utility
                                cli_print_agent_messages(
                                    agent_name=getattr(self, "agent_name", "Agent"),
                                    message=tool_msg,
                                    counter=getattr(self, "interaction_counter", 0),
                                    model=str(self.model),
                                    debug=False,
                                    interaction_input_tokens=estimated_input_tokens,
                                    interaction_output_tokens=estimated_output_tokens,
                                    interaction_reasoning_tokens=0,
                                    total_input_tokens=getattr(
                                        self, "total_input_tokens", 0
                                    )
                                    + estimated_input_tokens,
                                    total_output_tokens=getattr(
                                        self, "total_output_tokens", 0
                                    )
                                    + estimated_output_tokens,
                                    total_reasoning_tokens=getattr(
                                        self, "total_reasoning_tokens", 0
                                    ),
                                    interaction_cost=None,
                                    total_cost=None,
                                    tool_output=None,
                                    suppress_empty=True,
                                )

                                # Set flag to suppress final output to avoid duplication
                                self.suppress_final_output = True
                            except Exception as e:
                                # Silently log the error - don't disrupt the flow
                                logger.debug(f"Display error (non-critical): {e}")

                            # Add to message history
                            tool_call_msg = {
                                "role": "assistant",
                                "content": None,
                                "tool_calls": [
                                    {
                                        "id": tool_call_id,
                                        "type": "function",
                                        "function": {
                                            "name": parsed["name"],
                                            "arguments": arguments_str,
                                        },
                                    }
                                ],
                            }

                            streamed_tool_calls.append(tool_call_msg)
                            # Don't add to message history here - wait for tool output
                            # to add both tool call and response atomically

                            logger.debug(
                                f"Added function call: {parsed['name']} with args: {arguments_str}"
                            )
                except Exception:
                    pass

            if _is_effectively_empty_stream_accumulation(
                state,
                streamed_tool_calls,
                output_text,
                streaming_reasoning_text,
            ):
                empty_streak = _empty_completion_streak + 1
                max_empty_failures = _empty_completion_max_failures()
                self.logger.warning(
                    "Empty streamed assistant completion (%s/%s); "
                    "pt_est=%s reasoning_len=%s tools=%s; repeating.",
                    empty_streak,
                    max_empty_failures,
                    estimated_input_tokens,
                    len(streaming_reasoning_text),
                    len(streamed_tool_calls) + len(state.function_calls),
                )
                if empty_streak >= max_empty_failures:
                    stop_active_timer()
                    start_idle_timer()
                    raise LLMEmptyAssistantError(
                        "Consecutive empty assistant completions from the provider.",
                        {"attempts": max_empty_failures},
                    )
                if streaming_context:
                    try:
                        finish_agent_streaming(streaming_context, None)
                    except Exception:
                        pass
                    streaming_context = None
                if thinking_context:
                    try:
                        from cai.util import finish_claude_thinking_display

                        finish_claude_thinking_display(thinking_context)
                    except Exception:
                        pass
                    thinking_context = None
                await stream_wait_hints.stop()
                set_model_wait_retry_overlay(None)
                input, system_instructions, estimated_input_tokens = (
                    await self._recover_after_empty_completion(
                        empty_streak=empty_streak,
                        estimated_input_tokens=estimated_input_tokens,
                        input=input,
                        system_instructions=system_instructions,
                    )
                )
                set_model_wait_retry_overlay(
                    "Provider returned an empty response; "
                    f"retrying ({empty_streak}/{max_empty_failures})…"
                )
                async for event in self.stream_response(
                    system_instructions,
                    input,
                    model_settings,
                    tools,
                    output_schema,
                    handoffs,
                    tracing,
                    _empty_completion_streak=empty_streak,
                ):
                    yield event
                return

            function_call_starting_index = 0
            if state.text_content_index_and_output:
                function_call_starting_index += 1
                # Send end event for this content part
                yield ResponseContentPartDoneEvent(
                    content_index=state.text_content_index_and_output[0],
                    item_id=FAKE_RESPONSES_ID,
                    output_index=0,
                    part=state.text_content_index_and_output[1],
                    sequence_number=next_sequence_number(),
                    type="response.content_part.done",
                )

            if state.refusal_content_index_and_output:
                function_call_starting_index += 1
                # Send end event for this content part
                yield ResponseContentPartDoneEvent(
                    content_index=state.refusal_content_index_and_output[0],
                    item_id=FAKE_RESPONSES_ID,
                    output_index=0,
                    part=state.refusal_content_index_and_output[1],
                    sequence_number=next_sequence_number(),
                    type="response.content_part.done",
                )

            # Actually send events for the function calls
            for function_call in state.function_calls.values():
                # First, a ResponseOutputItemAdded for the function call
                yield ResponseOutputItemAddedEvent(
                    item=ResponseFunctionToolCall(
                        id=FAKE_RESPONSES_ID,
                        call_id=function_call.call_id[:40],
                        arguments=function_call.arguments,
                        name=function_call.name,
                        type="function_call",
                    ),
                    output_index=function_call_starting_index,
                    sequence_number=next_sequence_number(),
                    type="response.output_item.added",
                )
                # Then, yield the args
                yield ResponseFunctionCallArgumentsDeltaEvent(
                    delta=function_call.arguments,
                    item_id=FAKE_RESPONSES_ID,
                    output_index=function_call_starting_index,
                    sequence_number=next_sequence_number(),
                    type="response.function_call_arguments.delta",
                )
                # Finally, the ResponseOutputItemDone
                yield ResponseOutputItemDoneEvent(
                    item=ResponseFunctionToolCall(
                        id=FAKE_RESPONSES_ID,
                        call_id=function_call.call_id[:40],
                        arguments=function_call.arguments,
                        name=function_call.name,
                        type="function_call",
                    ),
                    output_index=function_call_starting_index,
                    sequence_number=next_sequence_number(),
                    type="response.output_item.done",
                )

            # Finally, send the Response completed event
            outputs: list[ResponseOutputItem] = []
            if state.text_content_index_and_output or state.refusal_content_index_and_output:
                assistant_msg = ResponseOutputMessage(
                    id=FAKE_RESPONSES_ID,
                    content=[],
                    role="assistant",
                    type="message",
                    status="completed",
                )
                if state.text_content_index_and_output:
                    assistant_msg.content.append(state.text_content_index_and_output[1])
                if state.refusal_content_index_and_output:
                    assistant_msg.content.append(state.refusal_content_index_and_output[1])
                outputs.append(assistant_msg)

                # send a ResponseOutputItemDone for the assistant message
                yield ResponseOutputItemDoneEvent(
                    item=assistant_msg,
                    output_index=0,
                    sequence_number=next_sequence_number(),
                    type="response.output_item.done",
                )

            for function_call in state.function_calls.values():
                outputs.append(function_call)

            final_response = response.model_copy()
            final_response.output = outputs

            # Get final token counts using consistent method
            input_tokens = estimated_input_tokens
            output_tokens = estimated_output_tokens

            # Use API token counts if available and reasonable
            if usage and hasattr(usage, "prompt_tokens") and usage.prompt_tokens > 0:
                input_tokens = usage.prompt_tokens
            if usage and hasattr(usage, "completion_tokens") and usage.completion_tokens > 0:
                output_tokens = usage.completion_tokens

            # Extract cache metrics from the usage object (if available from direct HTTP path)
            # Support both Anthropic format and OpenAI format (prompt_tokens_details.cached_tokens)
            cache_creation = getattr(usage, 'cache_creation_input_tokens', None) if usage else None
            cache_read = getattr(usage, 'cache_read_input_tokens', None) if usage else None
            # Fallback to OpenAI format if Anthropic format not available
            if not cache_read and usage:
                prompt_details = getattr(usage, 'prompt_tokens_details', None)
                if prompt_details:
                    cache_read = getattr(prompt_details, 'cached_tokens', None)

            # Create a proper usage object with our token counts
            final_response.usage = CustomResponseUsage(
                input_tokens=input_tokens,
                output_tokens=output_tokens,
                total_tokens=input_tokens + output_tokens,
                output_tokens_details=OutputTokensDetails(
                    reasoning_tokens=usage.completion_tokens_details.reasoning_tokens
                    if usage
                    and hasattr(usage, "completion_tokens_details")
                    and usage.completion_tokens_details
                    and hasattr(usage.completion_tokens_details, "reasoning_tokens")
                    and usage.completion_tokens_details.reasoning_tokens
                    else 0
                ),
                input_tokens_details={
                    "prompt_tokens": input_tokens,
                    "cached_tokens": usage.prompt_tokens_details.cached_tokens
                    if usage
                    and hasattr(usage, "prompt_tokens_details")
                    and usage.prompt_tokens_details
                    and hasattr(usage.prompt_tokens_details, "cached_tokens")
                    and usage.prompt_tokens_details.cached_tokens
                    else 0,
                },
                cache_creation_input_tokens=cache_creation,
                cache_read_input_tokens=cache_read,
            )

            yield ResponseCompletedEvent(
                response=final_response,
                sequence_number=next_sequence_number(),
                type="response.completed",
            )

            # Update token totals for CLI display
            if final_response.usage:
                # Always update the total counters with the best available counts
                self.total_input_tokens += final_response.usage.input_tokens
                self.total_output_tokens += final_response.usage.output_tokens
                if final_response.usage.output_tokens_details and hasattr(
                    final_response.usage.output_tokens_details, "reasoning_tokens"
                ):
                    self.total_reasoning_tokens += (
                        final_response.usage.output_tokens_details.reasoning_tokens
                    )

            # Prepare final statistics for display
            interaction_input = final_response.usage.input_tokens if final_response.usage else 0
            interaction_output = (
                final_response.usage.output_tokens if final_response.usage else 0
            )
            total_input = getattr(self, "total_input_tokens", 0)
            total_output = getattr(self, "total_output_tokens", 0)

            # Calculate costs for this model
            model_name = str(self.model)
            interaction_cost = calculate_model_cost(
                model_name, interaction_input, interaction_output
            )
            # Get the previous total cost and add this interaction's cost
            # Don't recalculate cost for all tokens - that causes double-counting
            previous_total = getattr(COST_TRACKER, "session_total_cost", 0.0)
            total_cost = previous_total + interaction_cost

            # If interaction cost is zero, this is a free model
            if interaction_cost == 0:
                # For free models, keep existing total and ensure cost tracking system knows it's free
                total_cost = getattr(COST_TRACKER, "session_total_cost", 0.0)
                if hasattr(COST_TRACKER, "reset_cost_for_local_model"):
                    COST_TRACKER.reset_cost_for_local_model(model_name)

            # Explicit conversion to float with fallback to ensure they're never None or 0
            interaction_cost = float(interaction_cost if interaction_cost is not None else 0.0)
            total_cost = float(total_cost if total_cost is not None else 0.0)

            # Process costs through COST_TRACKER only once per interaction
            if interaction_cost > 0.0:
                # Check price limit before processing the new cost
                if hasattr(COST_TRACKER, "check_price_limit"):
                    try:
                        COST_TRACKER.check_price_limit(interaction_cost)
                    except Exception:
                        # Ensure streaming context is cleaned up
                        if streaming_context:
                            try:
                                finish_agent_streaming(streaming_context, None)
                            except Exception:
                                pass
                        # Stop timers and re-raise the exception
                        stop_active_timer()
                        start_idle_timer()
                        raise

                # Process the interaction cost (updates internal tracking)
                COST_TRACKER.process_interaction_cost(
                    model_name,
                    interaction_input,
                    interaction_output,
                    final_response.usage.output_tokens_details.reasoning_tokens
                    if final_response.usage
                    and final_response.usage.output_tokens_details
                    and hasattr(final_response.usage.output_tokens_details, "reasoning_tokens")
                    else 0,
                    interaction_cost,
                    agent_name=self.agent_name,
                    agent_id=self.agent_id
                )

                # Process the total cost (updates session total correctly)
                total_cost = COST_TRACKER.process_total_cost(
                    model_name,
                    total_input,
                    total_output,
                    getattr(self, "total_reasoning_tokens", 0),
                    None,  # Let it calculate from tokens
                    agent_name=self.agent_name,
                    agent_id=self.agent_id
                )

                # Track usage globally
                GLOBAL_USAGE_TRACKER.track_usage(
                    model_name=model_name,
                    input_tokens=interaction_input,
                    output_tokens=interaction_output,
                    cost=interaction_cost,
                    agent_name=self.agent_name
                )
            else:
                # For free models, still track token usage
                GLOBAL_USAGE_TRACKER.track_usage(
                    model_name=model_name,
                    input_tokens=interaction_input,
                    output_tokens=interaction_output,
                    cost=0.0,
                    agent_name=self.agent_name
                )

            # Store the total cost for future recording
            self.total_cost = total_cost
            # Update per-interaction tokens for tool panels
            self.interaction_input_tokens = int(interaction_input)
            self.interaction_output_tokens = int(interaction_output)
            self.interaction_reasoning_tokens = int(
                final_response.usage.output_tokens_details.reasoning_tokens
                if final_response.usage
                and final_response.usage.output_tokens_details
                and hasattr(final_response.usage.output_tokens_details, "reasoning_tokens")
                else 0
            )
            # Update cache tokens for tool panels
            self.cache_read_tokens = int(cache_read) if cache_read else 0
            self.cache_creation_tokens = int(cache_creation) if cache_creation else 0

            # Also update COST_TRACKER so it's available for tool panels
            try:
                import cai.util
                cai.util.COST_TRACKER.interaction_input_tokens = self.interaction_input_tokens
                cai.util.COST_TRACKER.interaction_output_tokens = self.interaction_output_tokens
                cai.util.COST_TRACKER.interaction_reasoning_tokens = self.interaction_reasoning_tokens
                cai.util.COST_TRACKER.cache_read_tokens = self.cache_read_tokens
                cai.util.COST_TRACKER.cache_creation_tokens = self.cache_creation_tokens
            except Exception:
                pass

            # Create final stats with explicit type conversion for all values
            final_stats = {
                "interaction_input_tokens": int(interaction_input),
                "interaction_output_tokens": int(interaction_output),
                "interaction_reasoning_tokens": int(
                    final_response.usage.output_tokens_details.reasoning_tokens
                    if final_response.usage
                    and final_response.usage.output_tokens_details
                    and hasattr(final_response.usage.output_tokens_details, "reasoning_tokens")
                    else 0
                ),
                "total_input_tokens": int(total_input),
                "total_output_tokens": int(total_output),
                "total_reasoning_tokens": int(getattr(self, "total_reasoning_tokens", 0)),
                "interaction_cost": float(interaction_cost),
                "total_cost": float(total_cost),
                "cache_read_tokens": int(cache_read) if cache_read else 0,
                "cache_creation_tokens": int(cache_creation) if cache_creation else 0,
            }

            # At the end of streaming, finish the streaming context if we were using it
            if streaming_context:
                # Create a direct copy of the costs to ensure they remain as floats
                direct_stats = final_stats.copy()
                direct_stats["interaction_cost"] = float(interaction_cost)
                direct_stats["total_cost"] = float(total_cost)
                # Use the direct copy with guaranteed float costs
                finish_agent_streaming(streaming_context, direct_stats)
                streaming_context = None

                # Removed extra newline after streaming completes to avoid blank lines
                pass

            # Finish Claude thinking display if it was active
            if thinking_context:
                from cai.util import finish_claude_thinking_display

                finish_claude_thinking_display(thinking_context)

                # Note: Content is now displayed during streaming, no need to show it again here

            if tracing.include_data():
                span_generation.span_data.output = [final_response.model_dump()]

            span_generation.span_data.usage = {
                "input_tokens": input_tokens,
                "output_tokens": output_tokens,
            }

            # --- DEFERRED: Tool calls are no longer added immediately ---
            # Store pending tool calls but don't add to history yet
            if not hasattr(self, "_pending_tool_calls"):
                self._pending_tool_calls = {}

            for tool_call_msg in streamed_tool_calls:
                # Extract tool call ID from the message
                if tool_call_msg.get("tool_calls"):
                    for tc in tool_call_msg["tool_calls"]:
                        self._pending_tool_calls[tc["id"]] = tool_call_msg

            # Log the assistant tool call message if any tool calls were collected
            if streamed_tool_calls:
                tool_calls_list = []
                for tool_call_msg in streamed_tool_calls:
                    for tool_call in tool_call_msg.get("tool_calls", []):
                        tool_calls_list.append(tool_call)
                self.logger.log_assistant_message(None, tool_calls_list)

            # Always log text content if it exists, regardless of suppress_final_output
            # The suppress_final_output flag is only for preventing duplicate tool call display
            if (
                state.text_content_index_and_output
                and state.text_content_index_and_output[1].text
            ):
                asst_msg = {
                    "role": "assistant",
                    "content": state.text_content_index_and_output[1].text,
                }
                self.add_to_message_history(asst_msg)
                # Log the assistant message
                self.logger.log_assistant_message(state.text_content_index_and_output[1].text)

            # Reset the suppress flag for future requests
            self.suppress_final_output = False

            # Log the complete response
            self.logger.rec_training_data(
                {
                    "model": str(self.model),
                    "messages": converted_messages,
                    "stream": True,
                    "tools": [t.params_json_schema for t in tools] if tools else [],
                    "tool_choice": model_settings.tool_choice,
                },
                final_response,
                self.total_cost,
                self.agent_name,
            )

            # Stop active timer and start idle timer when streaming is complete
            stop_active_timer()
            start_idle_timer()

    except KeyboardInterrupt:
        # Handle keyboard interruption specifically
        stream_interrupted = True

        # Ensure message history consistency by adding synthetic tool results
        # for any tool calls that were added but don't have corresponding results
        try:
            # Find all tool calls in recent assistant messages
            orphaned_tool_calls = []
            for msg in reversed(self.message_history[-10:]):  # Check recent messages
                if msg.get("role") == "assistant" and msg.get("tool_calls"):
                    for tool_call in msg["tool_calls"]:
                        call_id = tool_call.get("id")
                        if call_id:
                            # Check if this tool call has a corresponding tool result
                            has_result = any(
                                m.get("role") == "tool" and m.get("tool_call_id") == call_id
                                for m in self.message_history
                            )
                            if not has_result:
                                orphaned_tool_calls.append((call_id, tool_call))

            # Add synthetic tool results for orphaned tool calls
            for call_id, tool_call in orphaned_tool_calls:
                tool_response_msg = {
                    "role": "tool",
                    "tool_call_id": call_id,
                    "content": "Tool execution interrupted"
                }
                self.add_to_message_history(tool_response_msg)

        except Exception as cleanup_error:
            # Don't let cleanup errors mask the original KeyboardInterrupt
            logger.debug(f"Error during interrupt cleanup: {cleanup_error}")

        # Make sure to clean up and re-raise
        raise

    except Exception as e:
        # Handle other exceptions
        logger.error(f"Error in stream_response: {e}")
        raise

    finally:
        # Always clean up resources
        # This block executes whether the try block succeeds, fails, or is interrupted

        if stream_wait_hints is not None:
            try:
                await stream_wait_hints.stop()
            except Exception:
                pass

        # Clean up streaming context
        if streaming_context:
            try:
                # Check if we need to force stop the streaming panel
                if streaming_context.get("is_started", False) and streaming_context.get("live"):
                    streaming_context["live"].stop()

                # Remove from active streaming contexts
                if hasattr(create_agent_streaming_context, "_active_streaming"):
                    for key, value in list(
                        create_agent_streaming_context._active_streaming.items()
                    ):
                        if value is streaming_context:
                            del create_agent_streaming_context._active_streaming[key]
                            break
            except Exception as cleanup_error:
                logger.debug(f"Error cleaning up streaming context: {cleanup_error}")

        # Clean up thinking context
        if thinking_context:
            try:
                # Force finish the thinking display
                from cai.util import finish_claude_thinking_display

                finish_claude_thinking_display(thinking_context)
            except Exception as cleanup_error:
                logger.debug(f"Error cleaning up thinking context: {cleanup_error}")

        # Clean up any live streaming panels
        if hasattr(cli_print_tool_output, "_streaming_sessions"):
            # Find any sessions related to this stream
            for call_id in list(cli_print_tool_output._streaming_sessions.keys()):
                if call_id in _LIVE_STREAMING_PANELS:
                    try:
                        live = _LIVE_STREAMING_PANELS[call_id]
                        live.stop()
                        del _LIVE_STREAMING_PANELS[call_id]
                    except Exception:
                        pass

        # Stop active timer and start idle timer
        try:
            stop_active_timer()
            start_idle_timer()
        except Exception:
            pass