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

> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.

**Goal:** Let llamachat use OpenAI-compatible cloud providers (together.ai, siliconflow) alongside the local llama.cpp router, with per-model metadata entered through a dialog and an approximate per-conversation cost shown beside the context meter.

**Architecture:** Providers become entries in a `[providers.*]` config table; the local router is one of them, named `local`, and a bare top-level `base_url` synthesizes it so existing configs keep working. Model ids are `provider:model`, except `local` which stays bare. Cloud models have no `presets.ini` entry, so context size, vision and prices come from a `models.ini` the app writes, prefilled from per-provider defaults. Token counts and the producing model move onto the `messages` table so cost survives reopening a conversation.

**Tech Stack:** Python 3.11+ standard library (`tomllib`, `configparser`, `subprocess`, `sqlite3`), `httpx` for HTTP, PySide6 for the dialog. No new dependencies.

**Spec:** `docs/superpowers/specs/2026-08-09-external-providers-design.md`

---

## Conventions for this codebase

Read this before Task 1. It is not optional context.

**Tests are not pytest.** `test_llamachat.py` is a single executable file of plain
functions. Each test ends with `print("ok  <short description>")`. Every test must
be registered by name in the `if __name__ == "__main__":` block at the bottom of the
file, in the order it should run. A test that is written but not registered never
runs, and the suite will still say "all checks passed".

Run the whole suite with:

```bash
./test_llamachat.py
```

There is no way to run a single test from the command line. To verify one test
fails or passes in isolation, run the suite and read that test's line. Adding a
temporary `if __name__` entry for just the new test is acceptable during a
red/green cycle but must be restored before committing.

**Every new source file needs the GPL header.** Copy it verbatim from the top of
`llamachat/config.py`, changing nothing but the docstring on the last line.

**Commits are GPG-signed automatically.** Do not pass `-c commit.gpgsign=false`.
Two git hooks scan for personal data and secrets; treat a rejection as correct.
Use `example.org`, fake keys like `sk-test-not-a-real-key`, and generic paths in
tests and fixtures. Never put a real API key anywhere, including a test.

**Style:** comments explain why, not what. Deliberate simplifications get a
`# ponytail:` comment naming the ceiling and the upgrade path. Match the
surrounding code's density.

---

## File Structure

**New files:**

| File | Responsibility |
| --- | --- |
| `llamachat/providers.py` | Parsing `[providers.*]`, splitting/joining model ids, filtering model lists, resolving API keys (`pass:`/`env:`/literal) with caching. No Qt, no HTTP. |
| `llamachat/models.py` | `models.ini` read/write, three-layer metadata resolution, cost arithmetic and formatting. No Qt, no HTTP. |
| `llamachat/modeldialog.py` | The Qt dialog for entering ctx size, vision and prices. Only file in this feature that imports PySide6. |

**Modified files:**

| File | Change |
| --- | --- |
| `llamachat/config.py` | Parse `[providers.*]`, synthesize `local` from bare `base_url`, add `models_path`. |
| `llamachat/backend.py` | `Client` gains `api_key` and sends `Authorization`. New `MultiClient` fans `models()` out across providers and routes requests by model id. |
| `llamachat/db.py` | Three nullable columns on `messages`, migration, and storing them. |
| `llamachat/ui.py` | Model list from `MultiClient`, metadata via `models.py` instead of `presets` alone, dialog triggers, cost label. |
| `test_llamachat.py` | New tests, each registered in the `__main__` block. |

**Dependency direction:** `providers.py` and `models.py` depend on nothing in the
project. `backend.py` imports `providers`. `ui.py` imports all three plus
`modeldialog`. Nothing imports `ui`.

---

## Task 1: Provider config parsing

**Files:**
- Create: `llamachat/providers.py`
- Modify: `test_llamachat.py`

- [ ] **Step 1: Write the failing test**

Add to `test_llamachat.py`, after `test_config_defaults()`:

```python
def test_provider_parsing():
    """Providers come from [providers.*]; a bare base_url synthesizes local."""
    from llamachat import providers

    # A modern config with two providers.
    parsed = providers.parse(
        {
            "providers": {
                "local": {"base_url": "http://localhost:8181/"},
                "together": {
                    "base_url": "https://api.example.org",
                    "api_key": "env:TEST_KEY_NAME",
                    "filter": ["qwen", "deepseek"],
                    "ctx_size": 32768,
                    "price_in": 0.6,
                    "price_out": 0.9,
                },
            }
        }
    )
    assert set(parsed) == {"local", "together"}
    # Trailing slashes are stripped so URL joining stays predictable.
    assert parsed["local"].base_url == "http://localhost:8181"
    assert parsed["local"].api_key == ""
    assert parsed["together"].filter == ["qwen", "deepseek"]
    assert parsed["together"].ctx_size == 32768
    assert parsed["together"].price_in == 0.6
    assert parsed["together"].price_out == 0.9

    # An old config: bare base_url, no providers table at all.
    legacy = providers.parse({"base_url": "http://localhost:8181"})
    assert set(legacy) == {"local"}
    assert legacy["local"].base_url == "http://localhost:8181"

    # Both present: the explicit entry wins over the bare key.
    both = providers.parse(
        {
            "base_url": "http://ignored.example.org",
            "providers": {"local": {"base_url": "http://explicit.example.org"}},
        }
    )
    assert both["local"].base_url == "http://explicit.example.org"

    # A provider with no base_url is skipped rather than half-configured.
    broken = providers.parse(
        {"providers": {"local": {"base_url": "http://x.example.org"},
                       "bad": {"api_key": "literal"}}}
    )
    assert set(broken) == {"local"}

    # Unset numbers stay None so "unknown" is distinguishable from zero.
    assert parsed["local"].ctx_size is None
    assert parsed["local"].price_in is None

    # A [providers.local] that omits base_url inherits the bare one rather
    # than shadowing the local provider out of existence.
    partial = providers.parse(
        {
            "base_url": "http://localhost:8181",
            "providers": {"local": {"api_key": "env:SOME_VAR"}},
        }
    )
    assert set(partial) == {"local"}
    assert partial["local"].base_url == "http://localhost:8181"
    # The explicit entry's own fields survive the merge.
    assert partial["local"].api_key == "env:SOME_VAR"

    # A filter given as a bare string is one needle, not four.
    stringy = providers.parse(
        {"providers": {"p": {"base_url": "http://x.example.org",
                             "filter": "qwen"}}}
    )
    assert stringy["p"].filter == ["qwen"]
    print("ok  provider config parsing")
```

Register it in the `__main__` block immediately after `test_config_defaults()`:

```python
    test_config_defaults()
    test_provider_parsing()
```

- [ ] **Step 2: Run the suite to verify it fails**

Run: `./test_llamachat.py`
Expected: FAIL with `ModuleNotFoundError: No module named 'llamachat.providers'`

- [ ] **Step 3: Write the implementation**

Create `llamachat/providers.py`. Copy the 14-line GPL header verbatim from the top
of `llamachat/config.py`, then:

```python
"""Provider definitions, model-id namespacing and API key resolution."""

import os
import subprocess
from dataclasses import dataclass, field

# The local llama.cpp router. Its models are shown and stored without a
# prefix, so an existing session pointing at a local model still resolves.
LOCAL = "local"

# A stuck pinentry must not freeze the worker thread forever.
KEY_TIMEOUT = 30


class KeyError_(Exception):
    """An api_key that could not be resolved, phrased for the user."""


@dataclass
class Provider:
    name: str
    base_url: str
    api_key: str = ""
    filter: list[str] = field(default_factory=list)
    # None rather than 0: unset must stay distinguishable from "zero".
    ctx_size: int | None = None
    vision: bool | None = None
    price_in: float | None = None
    price_out: float | None = None

    @property
    def is_local(self) -> bool:
        return self.name == LOCAL


def _number(raw, cast):
    if raw is None or raw == "":
        return None
    try:
        return cast(raw)
    except (TypeError, ValueError):
        return None


def parse(values: dict) -> dict[str, Provider]:
    """Build the provider table from already-loaded config values.

    Takes the raw dict rather than a path so config.py owns file reading and
    this stays testable without touching disk.
    """
    table = dict(values.get("providers") or {})

    # An old config has only a bare base_url. Fill it in as the local
    # provider's URL so nothing needs migrating, but never override an
    # explicit one. The test is the URL rather than the key: a
    # [providers.local] that only sets an api_key is adding detail to the
    # provider the user already has, not replacing it, and treating it as a
    # replacement would silently delete local entirely.
    bare = values.get("base_url")
    if bare and not (table.get(LOCAL) or {}).get("base_url"):
        table[LOCAL] = {**(table.get(LOCAL) or {}), "base_url": bare}

    out: dict[str, Provider] = {}
    for name, entry in table.items():
        entry = entry or {}
        base_url = str(entry.get("base_url") or "").rstrip("/")
        if not base_url:
            # ponytail: a provider with no URL is misconfigured, not a
            # partial one. Skipping beats inventing a default endpoint.
            continue
        # filter = "qwen" is an easy TOML slip for filter = ["qwen"], and
        # iterating the string would turn it into four single-character
        # needles that match nearly every model id.
        needles = entry.get("filter") or []
        if isinstance(needles, str):
            needles = [needles]
        vision = entry.get("vision")
        out[str(name)] = Provider(
            name=str(name),
            base_url=base_url,
            api_key=str(entry.get("api_key") or ""),
            filter=[str(f) for f in needles],
            ctx_size=_number(entry.get("ctx_size"), int),
            vision=None if vision is None else bool(vision),
            price_in=_number(entry.get("price_in"), float),
            price_out=_number(entry.get("price_out"), float),
        )
    return out
```

- [ ] **Step 4: Run the suite to verify it passes**

Run: `./test_llamachat.py`
Expected: PASS, including the line `ok  provider config parsing`

- [ ] **Step 5: Commit**

```bash
git add llamachat/providers.py test_llamachat.py
git commit -m "feat: parse provider definitions from config

Providers come from a [providers.*] table. A bare top-level base_url
synthesizes the local provider so existing configs keep working, and an
explicit [providers.local] wins over it. Unset numbers stay None so
'unknown' never collapses into zero.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>"
```

---

## Task 2: Model id namespacing and filtering

**Files:**
- Modify: `llamachat/providers.py`
- Modify: `test_llamachat.py`

- [ ] **Step 1: Write the failing test**

Add after `test_provider_parsing()`:

```python
def test_model_ids_and_filtering():
    """Ids are provider:model, local stays bare, filters are substrings."""
    from llamachat import providers

    table = providers.parse(
        {
            "providers": {
                "local": {"base_url": "http://localhost:8181"},
                "together": {
                    "base_url": "https://api.example.org",
                    "filter": ["qwen", "deepseek"],
                },
                "unfiltered": {"base_url": "https://api2.example.org"},
            }
        }
    )

    # Local models carry no prefix, in the dropdown and in the database.
    assert providers.qualify("local", "gemma4") == "gemma4"
    assert providers.qualify("together", "Qwen/Qwen2.5") == "together:Qwen/Qwen2.5"

    # Splitting is the inverse, and only for providers that exist.
    assert providers.split("gemma4", table) == ("local", "gemma4")
    assert providers.split("together:Qwen/Qwen2.5", table) == (
        "together",
        "Qwen/Qwen2.5",
    )
    # An unknown prefix is part of the model name, not a provider. This is
    # what keeps a local model whose name contains a colon working.
    assert providers.split("weird:name", table) == ("local", "weird:name")
    # Only the first colon splits.
    assert providers.split("together:a:b", table) == ("together", "a:b")

    # Filtering is case-insensitive substring, any match wins.
    listed = [
        "Qwen/Qwen2.5-72B-Instruct-Turbo",
        "deepseek-ai/DeepSeek-V3",
        "meta-llama/Llama-3.3-70B",
    ]
    kept = providers.apply_filter(table["together"], listed)
    assert kept == [
        "Qwen/Qwen2.5-72B-Instruct-Turbo",
        "deepseek-ai/DeepSeek-V3",
    ]

    # No filter means everything.
    assert providers.apply_filter(table["unfiltered"], listed) == listed
    # The local provider is never filtered even if one is configured.
    table["local"].filter = ["nothing-matches-this"]
    assert providers.apply_filter(table["local"], listed) == listed
    # A filter matching nothing yields nothing, it does not fall back to all.
    table["together"].filter = ["zzz"]
    assert providers.apply_filter(table["together"], listed) == []
    print("ok  model ids and filtering")
```

Register it after `test_provider_parsing()` in the `__main__` block.

- [ ] **Step 2: Run the suite to verify it fails**

Run: `./test_llamachat.py`
Expected: FAIL with `AttributeError: module 'llamachat.providers' has no attribute 'qualify'`

- [ ] **Step 3: Write the implementation**

Append to `llamachat/providers.py`:

```python
def qualify(provider: str, model: str) -> str:
    """The stored, displayed id for one model. Local models stay bare."""
    if provider == LOCAL:
        return model
    return f"{provider}:{model}"


def split(model_id: str, table: dict[str, Provider]) -> tuple[str, str]:
    """Inverse of qualify, resolved against the configured providers.

    A prefix that is not a configured provider is treated as part of the
    model name, which keeps a bare local model containing a colon working.
    """
    prefix, sep, rest = model_id.partition(":")
    if sep and prefix in table and prefix != LOCAL:
        return prefix, rest
    return LOCAL, model_id


def apply_filter(provider: Provider, listed: list[str]) -> list[str]:
    """Keep models matching any of the provider's substrings.

    Case-insensitive, because provider ids capitalise inconsistently:
    "qwen" has to match "Qwen/Qwen2.5-72B-Instruct-Turbo".
    """
    if provider.is_local or not provider.filter:
        return list(listed)
    needles = [f.lower() for f in provider.filter if f]
    if not needles:
        return list(listed)
    return [m for m in listed if any(n in m.lower() for n in needles)]
```

- [ ] **Step 4: Run the suite to verify it passes**

Run: `./test_llamachat.py`
Expected: PASS, including `ok  model ids and filtering`

- [ ] **Step 5: Commit**

```bash
git add llamachat/providers.py test_llamachat.py
git commit -m "feat: namespace model ids by provider and filter model lists

Cloud models are addressed as provider:model; local ones stay bare so
existing sessions keep resolving. An unknown prefix is treated as part of
the model name rather than a provider. Filters are case-insensitive
substrings because provider ids capitalise inconsistently.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>"
```

---

## Task 3: API key resolution

**Files:**
- Modify: `llamachat/providers.py`
- Modify: `test_llamachat.py`

- [ ] **Step 1: Write the failing test**

Add after `test_model_ids_and_filtering()`:

```python
def test_key_resolution():
    """api_key is prefix-dispatched, resolved lazily and cached."""
    from llamachat import providers

    resolver = providers.KeyResolver()

    # No key configured: no Authorization header, and nothing is run.
    empty = providers.Provider(name="local", base_url="http://x.example.org")
    assert resolver.resolve(empty) == ""

    # A literal key is used as-is.
    literal = providers.Provider(
        name="p", base_url="http://x.example.org", api_key="sk-test-not-a-real-key"
    )
    assert resolver.resolve(literal) == "sk-test-not-a-real-key"

    # env: reads the environment.
    os.environ["LLAMACHAT_TEST_KEY"] = "from-env"
    env = providers.Provider(
        name="e", base_url="http://x.example.org",
        api_key="env:LLAMACHAT_TEST_KEY",
    )
    assert resolver.resolve(env) == "from-env"
    del os.environ["LLAMACHAT_TEST_KEY"]

    # A missing env var is an error naming the provider, not a silent "".
    missing = providers.Provider(
        name="gone", base_url="http://x.example.org",
        api_key="env:LLAMACHAT_ABSENT_VAR",
    )
    try:
        resolver.resolve(missing)
        assert False, "a missing env var must raise"
    except providers.KeyError_ as exc:
        assert "gone" in str(exc)

    # pass: shells out. Substitute the runner rather than requiring gpg.
    calls = []

    def fake_run(cmd, timeout):
        calls.append((cmd, timeout))
        return "line-one\nline-two\n"

    passed = providers.Provider(
        name="together", base_url="http://x.example.org",
        api_key="pass:api/together",
    )
    cached = providers.KeyResolver(runner=fake_run)
    assert cached.resolve(passed) == "line-one"      # first line only
    assert calls[0][0] == ["pass", "show", "api/together"]
    assert calls[0][1] == providers.KEY_TIMEOUT

    # Cached: a second resolve must not shell out again.
    assert cached.resolve(passed) == "line-one"
    assert len(calls) == 1

    # A failing pass is reported, naming the provider.
    def boom(cmd, timeout):
        raise OSError("pass: entry not found")

    try:
        providers.KeyResolver(runner=boom).resolve(passed)
        assert False, "a failing pass must raise"
    except providers.KeyError_ as exc:
        assert "together" in str(exc)

    # Empty output is a failure too: an empty key would 401 confusingly.
    try:
        providers.KeyResolver(runner=lambda cmd, timeout: "  \n").resolve(passed)
        assert False, "empty pass output must raise"
    except providers.KeyError_:
        pass
    print("ok  api key resolution")
```

Register it after `test_model_ids_and_filtering()`.

- [ ] **Step 2: Run the suite to verify it fails**

Run: `./test_llamachat.py`
Expected: FAIL with `AttributeError: module 'llamachat.providers' has no attribute 'KeyResolver'`

- [ ] **Step 3: Write the implementation**

Append to `llamachat/providers.py`:

```python
def _run_pass(cmd: list[str], timeout: int) -> str:
    """Run `pass show NAME` and return its stdout."""
    result = subprocess.run(
        cmd, capture_output=True, text=True, timeout=timeout, check=True
    )
    return result.stdout


class KeyResolver:
    """Resolves api_key fields, lazily and once per process.

    Lazy matters: `pass` needs the GPG key, so a session that only touches
    local models must never trigger a pinentry. Resolved values stay in
    memory and are never written anywhere.
    """

    def __init__(self, runner=_run_pass):
        self._runner = runner
        self._cache: dict[str, str] = {}

    def resolve(self, provider: Provider) -> str:
        """The bearer token for this provider, or '' when it needs none."""
        spec = provider.api_key
        if not spec:
            return ""
        if provider.name in self._cache:
            return self._cache[provider.name]

        if spec.startswith("env:"):
            value = os.environ.get(spec[4:], "")
            if not value:
                raise KeyError_(
                    f"{provider.name}: environment variable {spec[4:]} is not set"
                )
        elif spec.startswith("pass:"):
            value = self._from_pass(provider, spec[5:])
        else:
            value = spec

        self._cache[provider.name] = value
        return value

    def _from_pass(self, provider: Provider, entry: str) -> str:
        try:
            out = self._runner(["pass", "show", entry], timeout=KEY_TIMEOUT)
        except subprocess.TimeoutExpired:
            raise KeyError_(
                f"{provider.name}: `pass show {entry}` timed out after "
                f"{KEY_TIMEOUT}s. Is a pinentry waiting for input?"
            )
        except Exception as exc:
            raise KeyError_(f"{provider.name}: `pass show {entry}` failed: {exc}")
        # A password store entry keeps the secret on the first line and
        # metadata below it.
        first = (out or "").strip().splitlines()
        if not first or not first[0].strip():
            raise KeyError_(f"{provider.name}: `pass show {entry}` returned nothing")
        return first[0].strip()
```

- [ ] **Step 4: Run the suite to verify it passes**

Run: `./test_llamachat.py`
Expected: PASS, including `ok  api key resolution`

- [ ] **Step 5: Commit**

```bash
git add llamachat/providers.py test_llamachat.py
git commit -m "feat: resolve provider API keys from pass, env or literal

One prefix-dispatched field. Resolution is lazy so a local-only session
never triggers a pinentry, cached for the process lifetime, and bounded
by a timeout so a stuck pinentry surfaces as an error instead of a frozen
send. Failures name the provider.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>"
```

---

## Task 4: Wire providers into config

**Files:**
- Modify: `llamachat/config.py:24-56` (DEFAULTS), `llamachat/config.py:76-95` (Config), `llamachat/config.py:110-152` (load)
- Modify: `test_llamachat.py`

- [ ] **Step 1: Write the failing test**

Add after `test_key_resolution()`:

```python
def test_config_providers():
    """config.load exposes the provider table and the models.ini path."""
    with tempfile.TemporaryDirectory() as tmp:
        path = Path(tmp) / "config.toml"

        # A legacy config: bare base_url only.
        path.write_text('base_url = "http://localhost:9999"\n')
        cfg = config.load(path)
        assert set(cfg.providers) == {"local"}
        assert cfg.providers["local"].base_url == "http://localhost:9999"
        # base_url stays populated: existing code still reads it.
        assert cfg.base_url == "http://localhost:9999"
        assert cfg.models_path == path.parent / "models.ini"

        # A config with an explicit cloud provider.
        path.write_text(
            'base_url = "http://localhost:9999"\n'
            "\n"
            "[providers.together]\n"
            'base_url = "https://api.example.org"\n'
            'api_key = "pass:api/together"\n'
            'filter = ["qwen"]\n'
            "ctx_size = 32768\n"
            "price_in = 0.6\n"
            "price_out = 0.9\n"
        )
        cfg = config.load(path)
        assert set(cfg.providers) == {"local", "together"}
        assert cfg.providers["together"].api_key == "pass:api/together"
        assert cfg.providers["together"].filter == ["qwen"]
        assert cfg.providers["together"].price_out == 0.9

        # A config with no base_url and no providers still loads, with the
        # built-in default synthesizing local.
        path.write_text("request_timeout = 60\n")
        cfg = config.load(path)
        assert set(cfg.providers) == {"local"}
        assert cfg.providers["local"].base_url == config.DEFAULTS["base_url"]
    print("ok  config provider table")
```

Register it after `test_key_resolution()`.

- [ ] **Step 2: Run the suite to verify it fails**

Run: `./test_llamachat.py`
Expected: FAIL with `AttributeError: 'Config' object has no attribute 'providers'`

- [ ] **Step 3: Write the implementation**

In `llamachat/config.py`, add the import beside the existing ones:

```python
from . import providers as providers_mod
```

Add two fields to the `Config` dataclass, after `max_searches`:

```python
    max_searches: int
    providers: dict
    models_path: Path
```

In `load()`, after the `search_url`/`search_enabled` lines and before the
`return Config(`, add:

```python
    # Providers are built from the raw values so a bare base_url still
    # synthesizes the local entry. DEFAULTS supplies base_url when the file
    # names neither, which keeps a config with no network settings working.
    provider_table = providers_mod.parse(values)
```

Then add the two arguments to the `return Config(...)` call, after
`max_searches=int(values["max_searches"]),`:

```python
        providers=provider_table,
        models_path=path.parent / "models.ini",
```

- [ ] **Step 4: Run the suite to verify it passes**

Run: `./test_llamachat.py`
Expected: PASS, including `ok  config provider table`

- [ ] **Step 5: Commit**

```bash
git add llamachat/config.py test_llamachat.py
git commit -m "feat: expose the provider table from config

base_url stays populated so existing callers are untouched; the provider
table is built alongside it. models.ini sits beside config.toml and
state.ini, following the same pattern as the window layout.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>"
```

---

## Task 5: models.ini storage

**Files:**
- Create: `llamachat/models.py`
- Modify: `test_llamachat.py`

- [ ] **Step 1: Write the failing test**

Add after `test_config_providers()`:

```python
def test_models_store():
    """models.ini round-trips per-model metadata and the cancel record."""
    from llamachat import models

    with tempfile.TemporaryDirectory() as tmp:
        path = Path(tmp) / "models.ini"
        store = models.ModelStore(path)

        # Nothing recorded yet.
        assert store.get("together:Qwen/Qwen2.5") is None
        assert store.was_offered("together:Qwen/Qwen2.5") is False

        store.save(
            "together:Qwen/Qwen2.5",
            models.ModelInfo(
                ctx_size=32768, vision=False, price_in=1.2, price_out=1.2
            ),
        )
        # A cancelled dialog records that it was offered, nothing more.
        store.mark_skipped("together:Llama-Vision-Free")

        # Reread from disk, not from memory: this is the round trip.
        fresh = models.ModelStore(path)
        info = fresh.get("together:Qwen/Qwen2.5")
        assert info.ctx_size == 32768
        assert info.vision is False
        assert info.price_in == 1.2
        assert info.price_out == 1.2
        assert fresh.was_offered("together:Qwen/Qwen2.5") is True

        assert fresh.get("together:Llama-Vision-Free") is None
        assert fresh.was_offered("together:Llama-Vision-Free") is True

        # Partial entries are legal: prices may be left blank.
        fresh.save("together:cheap", models.ModelInfo(ctx_size=8192))
        again = models.ModelStore(path)
        partial = again.get("together:cheap")
        assert partial.ctx_size == 8192
        assert partial.price_in is None
        assert partial.vision is None

        # A model id with a colon must survive being an ini section name.
        again.save("together:org/name:v2", models.ModelInfo(ctx_size=4096))
        assert models.ModelStore(path).get("together:org/name:v2").ctx_size == 4096
    print("ok  models.ini storage")
```

Register it after `test_config_providers()`.

- [ ] **Step 2: Run the suite to verify it fails**

Run: `./test_llamachat.py`
Expected: FAIL with `ModuleNotFoundError: No module named 'llamachat.models'`

- [ ] **Step 3: Write the implementation**

Create `llamachat/models.py` with the GPL header copied from `config.py`, then:

```python
"""Per-model metadata: what presets.ini cannot answer for a cloud model."""

import configparser
from dataclasses import dataclass
from pathlib import Path

# Recorded when the dialog is cancelled, so a model tried once never nags.
SKIPPED = "configured"


@dataclass
class ModelInfo:
    """What a model can tell us. Every field may be unknown."""
    ctx_size: int | None = None
    vision: bool | None = None
    price_in: float | None = None
    price_out: float | None = None

    def is_empty(self) -> bool:
        return all(
            v is None
            for v in (self.ctx_size, self.vision, self.price_in, self.price_out)
        )


def _get(section, key, cast):
    raw = section.get(key, "").strip()
    if not raw:
        return None
    try:
        return cast(raw)
    except ValueError:
        return None


def _get_bool(section, key):
    raw = section.get(key, "").strip().lower()
    if raw in ("true", "yes", "1", "on"):
        return True
    if raw in ("false", "no", "0", "off"):
        return False
    return None


class ModelStore:
    """models.ini, keyed by full model id.

    configparser rather than TOML because this file is written by the app,
    and tomllib is read-only in the standard library.
    """

    def __init__(self, path: Path):
        self.path = path
        # Model ids contain colons and slashes, so no key/value delimiter
        # may be inferred from a section name. Sections are safe as-is.
        self.parser = configparser.ConfigParser(interpolation=None)
        if path.exists():
            try:
                self.parser.read(path, encoding="utf-8")
            except configparser.Error:
                # ponytail: a corrupt file reads as empty; the dialog can
                # rewrite it. Failing to start over metadata is worse.
                self.parser = configparser.ConfigParser(interpolation=None)

    def get(self, model_id: str) -> ModelInfo | None:
        """Stored metadata, or None when there is none worth having."""
        if not self.parser.has_section(model_id):
            return None
        section = self.parser[model_id]
        info = ModelInfo(
            ctx_size=_get(section, "ctx_size", int),
            vision=_get_bool(section, "vision"),
            price_in=_get(section, "price_in", float),
            price_out=_get(section, "price_out", float),
        )
        return None if info.is_empty() else info

    def was_offered(self, model_id: str) -> bool:
        """Whether the dialog has already been shown for this model."""
        return self.parser.has_section(model_id)

    def save(self, model_id: str, info: ModelInfo) -> None:
        section = self._section(model_id)
        for key, value in (
            ("ctx_size", info.ctx_size),
            ("vision", info.vision),
            ("price_in", info.price_in),
            ("price_out", info.price_out),
        ):
            if value is None:
                section.pop(key, None)
            elif isinstance(value, bool):
                section[key] = "true" if value else "false"
            else:
                section[key] = str(value)
        section.pop(SKIPPED, None)
        self._write()

    def mark_skipped(self, model_id: str) -> None:
        """Record a cancelled dialog: offered, declined, do not ask again."""
        section = self._section(model_id)
        if not any(k != SKIPPED for k in section):
            section[SKIPPED] = "false"
        self._write()

    def _section(self, model_id: str):
        if not self.parser.has_section(model_id):
            self.parser.add_section(model_id)
        return self.parser[model_id]

    def _write(self) -> None:
        self.path.parent.mkdir(parents=True, exist_ok=True)
        with open(self.path, "w", encoding="utf-8") as fh:
            self.parser.write(fh)
```

- [ ] **Step 4: Run the suite to verify it passes**

Run: `./test_llamachat.py`
Expected: PASS, including `ok  models.ini storage`

- [ ] **Step 5: Commit**

```bash
git add llamachat/models.py test_llamachat.py
git commit -m "feat: store per-model metadata in models.ini

Cloud models have no presets.ini entry, so context size, vision and
prices are recorded per model in a file the app writes. A cancelled
dialog leaves a marker so a model tried once never asks again, which is
distinct from an absent section meaning never asked.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>"
```

---

## Task 6: Metadata resolution and cost arithmetic

**Files:**
- Modify: `llamachat/models.py`
- Modify: `test_llamachat.py`

- [ ] **Step 1: Write the failing test**

Add after `test_models_store()`:

```python
def test_metadata_and_cost():
    """models.ini beats provider defaults beats unknown; cost sums per model."""
    from llamachat import models, providers

    table = providers.parse(
        {
            "providers": {
                "local": {"base_url": "http://localhost:8181"},
                "together": {
                    "base_url": "https://api.example.org",
                    "api_key": "env:X",
                    "ctx_size": 32768,
                    "price_in": 0.6,
                    "price_out": 0.9,
                },
                "free": {"base_url": "https://api3.example.org"},
            }
        }
    )

    with tempfile.TemporaryDirectory() as tmp:
        store = models.ModelStore(Path(tmp) / "models.ini")
        store.save(
            "together:specific",
            models.ModelInfo(ctx_size=8192, vision=True, price_in=5.0),
        )

        # Layer 1: models.ini wins where it has a value.
        info = models.resolve("together:specific", table, store)
        assert info.ctx_size == 8192
        assert info.vision is True
        assert info.price_in == 5.0
        # Layer 2 fills the gap models.ini left: price_out was never set.
        assert info.price_out == 0.9

        # Layer 2 alone for a model with no models.ini entry.
        other = models.resolve("together:other", table, store)
        assert other.ctx_size == 32768
        assert other.price_in == 0.6
        assert other.vision is None       # layer 3: still unknown

        # Layer 3 throughout for a provider that configured nothing.
        bare = models.resolve("free:anything", table, store)
        assert bare.ctx_size is None
        assert bare.price_in is None

        # Cost: prompt at the input rate, completion at the output rate.
        rows = [
            {"model": "together:other", "prompt_tokens": 1_000_000,
             "completion_tokens": 1_000_000},
            # A pre-migration row: no counts, no model. Contributes zero.
            {"model": None, "prompt_tokens": None, "completion_tokens": None},
        ]
        assert models.conversation_cost(rows, table, store) == 1.5

        # A mixed conversation prices each reply at what produced it.
        mixed = [
            {"model": "together:other", "prompt_tokens": 1_000_000,
             "completion_tokens": 0},
            {"model": "together:specific", "prompt_tokens": 1_000_000,
             "completion_tokens": 0},
        ]
        assert models.conversation_cost(mixed, table, store) == 5.6

        # An unpriced model contributes nothing rather than guessing.
        assert models.conversation_cost(
            [{"model": "free:anything", "prompt_tokens": 1_000_000,
              "completion_tokens": 0}], table, store
        ) == 0.0

        # Whether a model can be priced at all decides ? versus blank.
        assert models.is_priced("together:other", table, store) is True
        assert models.is_priced("free:anything", table, store) is False
        # Local is free, never unpriced.
        assert models.is_billable("gemma4", table) is False
        assert models.is_billable("free:anything", table) is False  # no api_key
        assert models.is_billable("together:other", table) is True

        # Formatting: cents matter, so three decimals below a dollar.
        assert models.format_cost(0.0) == "$0.000"
        assert models.format_cost(1.5) == "$1.50"
        assert models.format_cost(12.345) == "$12.35"
    print("ok  metadata resolution and cost")
```

Register it after `test_models_store()`.

- [ ] **Step 2: Run the suite to verify it fails**

Run: `./test_llamachat.py`
Expected: FAIL with `AttributeError: module 'llamachat.models' has no attribute 'resolve'`

- [ ] **Step 3: Write the implementation**

Append to `llamachat/models.py`. Add the import at the top of the file beside the
existing ones:

```python
from . import providers as providers_mod
```

Then append:

```python
def resolve(model_id: str, table: dict, store: "ModelStore") -> ModelInfo:
    """Merge the three metadata layers, most specific first.

    Per field, not per source: a models.ini entry that sets only ctx_size
    still inherits the provider's prices.
    """
    stored = store.get(model_id) or ModelInfo()
    name, _ = providers_mod.split(model_id, table)
    provider = table.get(name)

    def pick(from_store, from_provider):
        return from_store if from_store is not None else from_provider

    if provider is None:
        return stored
    return ModelInfo(
        ctx_size=pick(stored.ctx_size, provider.ctx_size),
        vision=pick(stored.vision, provider.vision),
        price_in=pick(stored.price_in, provider.price_in),
        price_out=pick(stored.price_out, provider.price_out),
    )


def is_billable(model_id: str, table: dict) -> bool:
    """Whether this model costs money, regardless of prices being known.

    Keyed on the provider having an api_key: that is what distinguishes a
    free local model from a cloud one whose price was never entered.
    """
    name, _ = providers_mod.split(model_id, table)
    provider = table.get(name)
    return bool(provider and not provider.is_local and provider.api_key)


def is_priced(model_id: str, table: dict, store: "ModelStore") -> bool:
    """Whether a cost can be computed for this model."""
    info = resolve(model_id, table, store)
    return info.price_in is not None or info.price_out is not None


def message_cost(row, table: dict, store: "ModelStore") -> float:
    """Cost of one stored assistant row, priced at the model that made it."""
    model_id = row["model"] if row["model"] else ""
    if not model_id:
        return 0.0
    info = resolve(model_id, table, store)
    prompt = row["prompt_tokens"] or 0
    completion = row["completion_tokens"] or 0
    total = 0.0
    if info.price_in is not None:
        total += prompt * info.price_in
    if info.price_out is not None:
        total += completion * info.price_out
    return total / 1_000_000


def conversation_cost(rows, table: dict, store: "ModelStore") -> float:
    """Everything spent in one conversation so far.

    Rows predating the token columns carry NULLs and contribute zero, so an
    old conversation reads as free rather than as a fabricated number.
    """
    return sum(message_cost(row, table, store) for row in rows)


def projected_cost(tokens: int, model_id: str, table: dict, store) -> float:
    """What sending `tokens` of input to this model would cost.

    Only the input rate: the length of the reply is unknowable in advance.
    """
    info = resolve(model_id, table, store)
    if info.price_in is None:
        return 0.0
    return tokens * info.price_in / 1_000_000


def format_cost(amount: float) -> str:
    """Money, with enough precision to see a cheap turn move the number."""
    if amount < 1:
        return f"${amount:.3f}"
    return f"${amount:.2f}"
```

- [ ] **Step 4: Run the suite to verify it passes**

Run: `./test_llamachat.py`
Expected: PASS, including `ok  metadata resolution and cost`

- [ ] **Step 5: Commit**

```bash
git add llamachat/models.py test_llamachat.py
git commit -m "feat: resolve model metadata in layers and compute cost

Resolution merges per field, not per source, so an entry setting only
ctx_size still inherits the provider's prices. Cost prices each reply at
the model that produced it, and rows predating the token columns
contribute zero rather than a fabricated number.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>"
```

---

## Task 7: Store token counts and the producing model

**Files:**
- Modify: `llamachat/db.py:35-43` (SCHEMA), `llamachat/db.py:101-124` (_migrate), `llamachat/db.py:210-231` (update_message)
- Modify: `test_llamachat.py`

- [ ] **Step 1: Write the failing test**

Add after `test_metadata_and_cost()`:

```python
def test_token_column_migration():
    """A pre-token database opens, and new rows record counts and model."""
    import sqlite3

    with tempfile.TemporaryDirectory() as tmp:
        path = Path(tmp) / "old.db"
        conn = sqlite3.connect(path)
        conn.executescript(
            "CREATE TABLE sessions (id INTEGER PRIMARY KEY, mode TEXT,"
            " title TEXT, model TEXT, created_at INTEGER, updated_at INTEGER);"
            "CREATE TABLE messages (id INTEGER PRIMARY KEY, session_id INTEGER,"
            " role TEXT NOT NULL, content TEXT NOT NULL,"
            " created_at INTEGER NOT NULL);"
            "INSERT INTO sessions VALUES (1,'chat','old','m',0,0);"
            "INSERT INTO messages VALUES (1,1,'assistant','older reply',0);"
        )
        conn.commit()
        conn.close()

        history = db.History(path)
        rows = history.messages(1)
        # The pre-migration row survives and reads as unknown, not as zero.
        assert rows[0]["content"] == "older reply"
        assert rows[0]["prompt_tokens"] is None
        assert rows[0]["completion_tokens"] is None
        assert rows[0]["model"] is None

        mid = history.add_message(1, "assistant", "")
        history.update_message(
            mid, "new reply",
            prompt_tokens=1200, completion_tokens=340,
            model="together:Qwen/Qwen2.5",
        )
        fresh = history.messages(1)[1]
        assert fresh["prompt_tokens"] == 1200
        assert fresh["completion_tokens"] == 340
        assert fresh["model"] == "together:Qwen/Qwen2.5"

        # Omitting them leaves stored values alone, as with reasoning.
        history.update_message(mid, "edited")
        kept = history.messages(1)[1]
        assert kept["content"] == "edited"
        assert kept["prompt_tokens"] == 1200
        assert kept["model"] == "together:Qwen/Qwen2.5"
        history.close()
    print("ok  token column migration")
```

Register it after `test_metadata_and_cost()`.

- [ ] **Step 2: Run the suite to verify it fails**

Run: `./test_llamachat.py`
Expected: FAIL with `IndexError: No item with that key` (sqlite3.Row has no
`prompt_tokens` column)

- [ ] **Step 3: Write the implementation**

In `llamachat/db.py`, extend the `messages` table in `SCHEMA` so a fresh database
gets the columns directly. The block becomes:

```sql
CREATE TABLE IF NOT EXISTS messages (
    id                INTEGER PRIMARY KEY,
    session_id        INTEGER NOT NULL REFERENCES sessions(id) ON DELETE CASCADE,
    role              TEXT    NOT NULL,
    content           TEXT    NOT NULL,
    reasoning         TEXT    NOT NULL DEFAULT '',
    searches          TEXT,
    prompt_tokens     INTEGER,
    completion_tokens INTEGER,
    model             TEXT,
    created_at        INTEGER NOT NULL
);
```

In `_migrate()`, extend the `messages` entry of the `added` dict:

```python
            "messages": {
                "reasoning": "TEXT NOT NULL DEFAULT ''",
                # Nullable rather than defaulted: NULL means "no searches",
                # which is exactly what every pre-migration row wants.
                "searches": "TEXT",
                # Nullable for the same reason: an old row has no counts,
                # and zero would read as a reply that cost nothing.
                "prompt_tokens": "INTEGER",
                "completion_tokens": "INTEGER",
                # The model on `sessions` is the current one, which prices a
                # switched conversation wrongly. Record what actually replied.
                "model": "TEXT",
            },
```

Replace `update_message` with:

```python
    def update_message(
        self,
        message_id: int,
        content: str,
        reasoning: str | None = None,
        searches: str | None = None,
        prompt_tokens: int | None = None,
        completion_tokens: int | None = None,
        model: str | None = None,
    ) -> None:
        """Fill in a streamed reply. Omitted fields keep their stored value."""
        columns = ["content = ?"]
        values: list = [content]
        for column, value in (
            ("reasoning", reasoning),
            ("searches", searches),
            ("prompt_tokens", prompt_tokens),
            ("completion_tokens", completion_tokens),
            ("model", model),
        ):
            if value is not None:
                columns.append(f"{column} = ?")
                values.append(value)
        values.append(message_id)
        self.conn.execute(
            f"UPDATE messages SET {', '.join(columns)} WHERE id = ?", values
        )
        self.conn.commit()
```

- [ ] **Step 4: Run the suite to verify it passes**

Run: `./test_llamachat.py`
Expected: PASS, including `ok  token column migration`. `ok  reasoning column
migration` and `ok  search storage` must still pass, proving the rewritten
`update_message` kept its old behaviour.

- [ ] **Step 5: Commit**

```bash
git add llamachat/db.py test_llamachat.py
git commit -m "feat: record token counts and the producing model per message

Cost has to survive reopening a conversation, which means storing what
each reply used. The model goes on the message rather than the session
because sessions records only the current one, and a conversation that
switched models would otherwise be priced entirely at whichever is
selected now. All three columns are nullable so old rows read as unknown.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>"
```

---

## Task 8: Authorization header on the client

**Files:**
- Modify: `llamachat/backend.py:169-176` (Client.__init__), `:178-190` (models), `:192-224` (complete), `:299-337` (_stream_once)
- Modify: `test_llamachat.py`

- [ ] **Step 1: Write the failing test**

Add after `test_token_column_migration()`:

```python
def test_client_auth_header():
    """A client with a key sends Bearer auth; one without sends no header."""
    sent = {}

    class _HttpxResponse:
        """Enough of an httpx response for Client.models().

        The existing _FakeResponse in this file wraps bytes for urlopen and
        has neither .json() nor .raise_for_status(), so it cannot stand in
        for an httpx call.
        """

        status_code = 200

        def __init__(self, payload):
            self._payload = payload

        def raise_for_status(self):
            return None

        def json(self):
            return self._payload

    def _recorder(url, timeout=None, headers=None):
        sent["url"] = url
        sent["headers"] = headers or {}
        return _HttpxResponse({"data": [{"id": "m1"}]})

    import httpx
    original = httpx.get
    try:
        httpx.get = _recorder

        assert backend.Client("http://x.example.org").models() == ["m1"]
        assert "Authorization" not in sent["headers"]

        backend.Client(
            "http://x.example.org", api_key="sk-test-not-a-real-key"
        ).models()
        assert sent["headers"]["Authorization"] == "Bearer sk-test-not-a-real-key"
    finally:
        httpx.get = original
    print("ok  client authorization header")
```

Register it after `test_token_column_migration()` in the `__main__` block.

- [ ] **Step 2: Run the suite to verify it fails**

Run: `./test_llamachat.py`
Expected: FAIL with `TypeError: Client.__init__() got an unexpected keyword argument 'api_key'`

- [ ] **Step 3: Write the implementation**

In `llamachat/backend.py`, replace `Client.__init__` with:

```python
    def __init__(self, base_url: str, timeout: int = 300, api_key: str = ""):
        self.base_url = base_url.rstrip("/")
        self.timeout = timeout
        self.api_key = api_key

    def _headers(self) -> dict:
        """Bearer auth when the provider needs it, nothing when it does not."""
        return {"Authorization": f"Bearer {self.api_key}"} if self.api_key else {}
```

Then add `headers=self._headers()` to each of the three outbound calls:

In `models()`:

```python
            resp = httpx.get(
                f"{self.base_url}/v1/models", timeout=10, headers=self._headers()
            )
```

In `complete()`, add the argument after `json={...}`:

```python
                timeout=httpx.Timeout(self.timeout, connect=10),
                headers=self._headers(),
            )
```

In `_stream_once()`, add it to the `httpx.stream` call:

```python
                timeout=httpx.Timeout(self.timeout, connect=10),
                headers=self._headers(),
            ) as resp:
```

- [ ] **Step 4: Run the suite to verify it passes**

Run: `./test_llamachat.py`
Expected: PASS, including `ok  client authorization header`

- [ ] **Step 5: Commit**

```bash
git add llamachat/backend.py test_llamachat.py
git commit -m "feat: send bearer auth when a provider needs a key

The local router needs none, so the header is omitted entirely rather
than sent empty.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>"
```

---

## Task 9: MultiClient routing

**Files:**
- Modify: `llamachat/backend.py` (append after `Client`)
- Modify: `test_llamachat.py`

- [ ] **Step 1: Write the failing test**

Add after `test_client_auth_header()`:

```python
def test_multi_client():
    """Models fan out across providers; requests route by model id."""
    from llamachat import providers

    table = providers.parse(
        {
            "providers": {
                "local": {"base_url": "http://localhost:8181"},
                "together": {
                    "base_url": "https://api.example.org",
                    "api_key": "env:MULTI_TEST_KEY",
                    "filter": ["qwen"],
                },
                "down": {"base_url": "https://dead.example.org"},
            }
        }
    )
    os.environ["MULTI_TEST_KEY"] = "sk-test-not-a-real-key"

    listings = {
        "http://localhost:8181": ["gemma4", "qwen3.5-9b"],
        "https://api.example.org": [
            "Qwen/Qwen2.5-72B",
            "meta-llama/Llama-3.3-70B",
        ],
    }
    built = []

    class _StubClient:
        def __init__(self, base_url, timeout=300, api_key=""):
            self.base_url = base_url
            self.api_key = api_key
            built.append(self)

        def models(self):
            if self.base_url not in listings:
                raise backend.BackendError(f"cannot reach {self.base_url}")
            return listings[self.base_url]

    multi = backend.MultiClient(
        table, timeout=300, resolver=providers.KeyResolver(),
        client_factory=_StubClient,
    )
    listed, problems = multi.models()

    # Local models stay bare, cloud ones are prefixed, and the filter cut
    # the Llama model out of together's listing.
    assert listed == ["gemma4", "qwen3.5-9b", "together:Qwen/Qwen2.5-72B"]

    # The unreachable provider is reported, and did not break the rest.
    assert any("down" in p for p in problems)

    # Routing: the client for a cloud model carries that provider's key.
    client = multi.client_for("together:Qwen/Qwen2.5-72B")
    assert client.base_url == "https://api.example.org"
    assert client.api_key == "sk-test-not-a-real-key"

    # And a local model gets the local client with no key at all.
    local = multi.client_for("gemma4")
    assert local.base_url == "http://localhost:8181"
    assert local.api_key == ""

    # The bare model name is what goes on the wire, not the prefixed id.
    assert multi.wire_name("together:Qwen/Qwen2.5-72B") == "Qwen/Qwen2.5-72B"
    assert multi.wire_name("gemma4") == "gemma4"

    # A filter that matches nothing is reported by name with counts.
    table["together"].filter = ["zzz"]
    empty = backend.MultiClient(
        table, timeout=300, resolver=providers.KeyResolver(),
        client_factory=_StubClient,
    )
    _, notes = empty.models()
    assert any("together: 0 of 2" in n for n in notes)

    del os.environ["MULTI_TEST_KEY"]
    print("ok  multi-provider client")
```

Register it after `test_client_auth_header()`.

- [ ] **Step 2: Run the suite to verify it fails**

Run: `./test_llamachat.py`
Expected: FAIL with `AttributeError: module 'llamachat.backend' has no attribute 'MultiClient'`

- [ ] **Step 3: Write the implementation**

In `llamachat/backend.py`, add the import beside the existing `from . import search`:

```python
from . import providers as providers_mod
```

Append after the `Client` class:

```python
class MultiClient:
    """One façade over every configured provider.

    Holds a `Client` per provider, built on demand so a key is resolved only
    when that provider is actually used. The UI talks in prefixed model ids
    and never needs to know which endpoint one lives on.
    """

    def __init__(self, table, timeout=300, resolver=None, client_factory=Client):
        self.table = table
        self.timeout = timeout
        self.resolver = resolver or providers_mod.KeyResolver()
        self._factory = client_factory
        self._clients: dict[str, Client] = {}

    def client_for(self, model_id: str) -> Client:
        """The client that serves this model, resolving its key on first use."""
        name, _ = providers_mod.split(model_id, self.table)
        if name not in self._clients:
            provider = self.table[name]
            self._clients[name] = self._factory(
                provider.base_url,
                timeout=self.timeout,
                api_key=self.resolver.resolve(provider),
            )
        return self._clients[name]

    def wire_name(self, model_id: str) -> str:
        """The model name the provider itself expects, without our prefix."""
        _, model = providers_mod.split(model_id, self.table)
        return model

    def models(self) -> tuple[list[str], list[str]]:
        """Every offered model id, plus notes about what went wrong.

        A provider that is unreachable or whose filter matched nothing must
        not stop the others being listed: local models have to stay usable
        when the network is down.
        """
        listed: list[str] = []
        problems: list[str] = []
        for name, provider in self.table.items():
            try:
                available = self._listing_client(provider).models()
            except (BackendError, providers_mod.KeyError_) as exc:
                problems.append(f"{name}: {exc}")
                continue
            kept = providers_mod.apply_filter(provider, available)
            if available and not kept:
                problems.append(
                    f"{name}: 0 of {len(available)} models matched filter"
                )
            listed.extend(providers_mod.qualify(name, m) for m in kept)
        return listed, problems

    def _listing_client(self, provider) -> Client:
        """Listing needs a client too, and needs the key for a private API."""
        return self.client_for(providers_mod.qualify(provider.name, ""))
```

- [ ] **Step 4: Run the suite to verify it passes**

Run: `./test_llamachat.py`
Expected: PASS, including `ok  multi-provider client`

- [ ] **Step 5: Commit**

```bash
git add llamachat/backend.py test_llamachat.py
git commit -m "feat: fan model listing out across providers and route by id

A provider that is unreachable or whose filter matched nothing is
reported rather than fatal: local models must stay usable when the
network is down. Clients are built on demand so a key is resolved only
when that provider is really used.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>"
```

---

## Task 10: The model dialog

**Files:**
- Create: `llamachat/modeldialog.py`
- Modify: `test_llamachat.py`

- [ ] **Step 1: Write the failing test**

The dialog's layout needs a running Qt application, but its field conversion does
not. Test the conversion, which is where the bugs live. Add after
`test_multi_client()`:

```python
def test_model_dialog_values():
    """The dialog's field text converts to ModelInfo, blanks meaning unknown."""
    from llamachat import models, modeldialog

    # Everything filled in.
    info = modeldialog.to_info(
        ctx_text="32768", vision=True, in_text="1.2", out_text="0.9"
    )
    assert info.ctx_size == 32768
    assert info.vision is True
    assert info.price_in == 1.2
    assert info.price_out == 0.9

    # Blank prices are legal and mean unpriced, not free.
    blank = modeldialog.to_info(
        ctx_text="8192", vision=False, in_text="", out_text="  "
    )
    assert blank.ctx_size == 8192
    assert blank.price_in is None
    assert blank.price_out is None

    # Garbage reads as unknown rather than crashing the dialog.
    junk = modeldialog.to_info(
        ctx_text="not a number", vision=False, in_text="free", out_text=""
    )
    assert junk.ctx_size is None
    assert junk.price_in is None

    # Prefill is the inverse: unknown becomes an empty field.
    assert modeldialog.to_fields(models.ModelInfo()) == ("", False, "", "")
    assert modeldialog.to_fields(
        models.ModelInfo(ctx_size=4096, vision=True, price_in=0.5)
    ) == ("4096", True, "0.5", "")
    print("ok  model dialog value conversion")
```

Register it after `test_multi_client()`.

- [ ] **Step 2: Run the suite to verify it fails**

Run: `./test_llamachat.py`
Expected: FAIL with `ModuleNotFoundError: No module named 'llamachat.modeldialog'`

- [ ] **Step 3: Write the implementation**

Create `llamachat/modeldialog.py` with the GPL header copied from `config.py`, then:

```python
"""Dialog for entering what presets.ini cannot answer about a model."""

from PySide6.QtWidgets import (
    QCheckBox,
    QDialog,
    QDialogButtonBox,
    QFormLayout,
    QLabel,
    QLineEdit,
    QVBoxLayout,
)

from .models import ModelInfo


def _number(text: str, cast):
    """Field text to a number, treating blank and garbage alike as unknown."""
    text = (text or "").strip()
    if not text:
        return None
    try:
        return cast(text)
    except ValueError:
        return None


def to_info(ctx_text: str, vision: bool, in_text: str, out_text: str) -> ModelInfo:
    """Build a ModelInfo from the dialog's raw field values."""
    return ModelInfo(
        ctx_size=_number(ctx_text, int),
        vision=bool(vision),
        price_in=_number(in_text, float),
        price_out=_number(out_text, float),
    )


def to_fields(info: ModelInfo) -> tuple[str, bool, str, str]:
    """The inverse, for prefilling. Unknown becomes an empty field."""
    return (
        "" if info.ctx_size is None else str(info.ctx_size),
        bool(info.vision),
        "" if info.price_in is None else str(info.price_in),
        "" if info.price_out is None else str(info.price_out),
    )


class ModelDialog(QDialog):
    """Context size, vision and prices for one model.

    Prefilled from the provider's defaults, so the common case is checking
    the numbers rather than typing them.
    """

    def __init__(self, model_id: str, info: ModelInfo, parent=None):
        super().__init__(parent)
        self.setWindowTitle("Model settings")
        self.model_id = model_id

        ctx, vision, price_in, price_out = to_fields(info)
        self.ctx = QLineEdit(ctx)
        self.ctx.setPlaceholderText("unknown")
        self.vision = QCheckBox("Accepts images")
        self.vision.setChecked(vision)
        self.price_in = QLineEdit(price_in)
        self.price_in.setPlaceholderText("unpriced")
        self.price_out = QLineEdit(price_out)
        self.price_out.setPlaceholderText("unpriced")

        layout = QVBoxLayout(self)
        heading = QLabel(f"<b>{model_id}</b>")
        heading.setTextInteractionFlags(heading.textInteractionFlags())
        layout.addWidget(heading)

        form = QFormLayout()
        form.addRow("Context size (tokens)", self.ctx)
        form.addRow("", self.vision)
        form.addRow("Input price (per 1M tokens)", self.price_in)
        form.addRow("Output price (per 1M tokens)", self.price_out)
        layout.addLayout(form)

        note = QLabel(
            "Leave prices empty if you do not want a cost estimate.\n"
            "Context size drives the meter and the attachment budget."
        )
        note.setWordWrap(True)
        layout.addWidget(note)

        buttons = QDialogButtonBox(
            QDialogButtonBox.Save | QDialogButtonBox.Cancel
        )
        buttons.accepted.connect(self.accept)
        buttons.rejected.connect(self.reject)
        layout.addWidget(buttons)

    def info(self) -> ModelInfo:
        """What the user entered."""
        return to_info(
            self.ctx.text(),
            self.vision.isChecked(),
            self.price_in.text(),
            self.price_out.text(),
        )
```

- [ ] **Step 4: Run the suite to verify it passes**

Run: `./test_llamachat.py`
Expected: PASS, including `ok  model dialog value conversion`

- [ ] **Step 5: Commit**

```bash
git add llamachat/modeldialog.py test_llamachat.py
git commit -m "feat: dialog for per-model context size, vision and prices

Field conversion is separated from the widget so the part with the edge
cases is testable without a running Qt application. Blank and unparseable
both read as unknown, which is what an empty price field has to mean.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>"
```

---

## Task 11: Cost label widget

**Files:**
- Modify: `llamachat/ui.py` (add after `_short`, around line 135)
- Modify: `test_llamachat.py`

- [ ] **Step 1: Write the failing test**

Add after `test_model_dialog_values()`:

```python
def test_cost_label_text():
    """The label distinguishes free, unpriced, and a real figure."""
    from llamachat import ui

    # A local model costs nothing, so the label says nothing.
    assert ui.cost_text(spent=0.0, projected=0.0, billable=False, priced=False) == ""

    # A cloud model whose price was never entered: ? rather than blank, so
    # it cannot be mistaken for free.
    assert ui.cost_text(
        spent=0.0, projected=0.0, billable=True, priced=False
    ) == "?"

    # Spent so far, with nothing composed yet.
    assert ui.cost_text(
        spent=0.043, projected=0.0, billable=True, priced=True
    ) == "$0.043"

    # Spent plus what sending the draft would add, kept visually separate.
    assert ui.cost_text(
        spent=0.043, projected=0.011, billable=True, priced=True
    ) == "$0.043 +$0.011"

    # A fresh conversation on a priced model still shows the projection.
    assert ui.cost_text(
        spent=0.0, projected=0.002, billable=True, priced=True
    ) == "$0.000 +$0.002"
    print("ok  cost label text")
```

Register it after `test_model_dialog_values()`.

- [ ] **Step 2: Run the suite to verify it fails**

Run: `./test_llamachat.py`
Expected: FAIL with `AttributeError: module 'llamachat.ui' has no attribute 'cost_text'`

- [ ] **Step 3: Write the implementation**

In `llamachat/ui.py`, add the import beside the existing project imports:

```python
from . import models as models_mod
```

Add after the `_short` function (around line 135):

```python
def cost_text(spent: float, projected: float, billable: bool, priced: bool) -> str:
    """The cost label beside the context meter.

    Three states, deliberately distinct: a local model shows nothing, a
    cloud model with no price entered shows '?', and a priced one shows
    what it has cost plus what the composed draft would add. Blank and '?'
    must not collapse into each other, or an unpriced cloud model reads as
    free.
    """
    if not billable:
        return ""
    if not priced:
        return "?"
    text = models_mod.format_cost(spent)
    if projected > 0:
        text += f" +{models_mod.format_cost(projected)}"
    return text


class CostLabel(QLabel):
    """A one-line money readout that hides itself when there is nothing to say."""

    def __init__(self):
        super().__init__("")
        self.setToolTip("")

    def set_cost(
        self, spent: float, projected: float, billable: bool, priced: bool
    ) -> None:
        text = cost_text(spent, projected, billable, priced)
        self.setText(text)
        self.setVisible(bool(text))
        if not billable:
            self.setToolTip("")
        elif not priced:
            self.setToolTip(
                "No prices set for this model.\n"
                "Use Model settings to enter them."
            )
        else:
            tip = f"{models_mod.format_cost(spent)} spent in this conversation"
            if projected > 0:
                tip += (
                    f"\n+{models_mod.format_cost(projected)} to send what is "
                    "composed now"
                )
            tip += "\nApproximate: based on the prices you entered."
            self.setToolTip(tip)
```

Confirm `QLabel` is in the `PySide6.QtWidgets` import list at the top of `ui.py`;
it is already imported, so no import change is needed beyond `models_mod`.

- [ ] **Step 4: Run the suite to verify it passes**

Run: `./test_llamachat.py`
Expected: PASS, including `ok  cost label text`

- [ ] **Step 5: Commit**

```bash
git add llamachat/ui.py test_llamachat.py
git commit -m "feat: cost label showing spend and the next send's projection

Three states stay distinct: blank for a free local model, ? for a cloud
model whose prices were never entered, and a figure when they were.
Collapsing the first two would make an unpriced cloud model read as free.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>"
```

---

## Task 12: Wire the window to providers

**Files:**
- Modify: `llamachat/__main__.py:155-165`
- Modify: `llamachat/ui.py:356-406` (`ChatWindow.__init__`), `:669-717` (model handling)

This task is wiring, not new logic, and its behaviour is covered by the tests
already written plus a manual check. No new automated test.

- [ ] **Step 1: Build the MultiClient at startup**

In `llamachat/__main__.py`, replace the client construction (line 159) with:

```python
    client = MultiClient(cfg.providers, cfg.request_timeout)
    presets = config.parse_presets(cfg.presets_path)
    store = models.ModelStore(cfg.models_path)
    window = ChatWindow(cfg, history, client, presets, store)
```

Update the imports at the top of `__main__.py`: replace the `Client` import with
`MultiClient` and add `from llamachat import models`. Check the existing import
lines and keep their style.

- [ ] **Step 2: Accept the store in the window**

In `llamachat/ui.py`, change `ChatWindow.__init__` (line 359) to take the new
argument and keep it:

```python
    def __init__(self, cfg, history, client, presets, store):
        ...
        self.presets = presets
        self.store = store
```

Add it right after the existing `self.presets = presets` line at 364.

- [ ] **Step 3: List models from every provider**

Replace `refresh_models` (line 669) with:

```python
    def refresh_models(self) -> None:
        """Repopulate the picker from every provider, keeping the selection."""
        previous = self.model_box.currentText()
        available, problems = self.client.models()
        if not available:
            self.show_status(
                "; ".join(problems) or "No models available", error=True
            )
            return

        self.model_box.blockSignals(True)
        self.model_box.clear()
        for name in available:
            info = self.model_info(name)
            label = f"{name} 👁" if info.vision else name
            self.model_box.addItem(label, name)
        self.model_box.blockSignals(False)

        target = previous or self.cfg.default_model
        if target:
            index = self.model_box.findData(_strip_marker(target))
            if index < 0:
                index = self.model_box.findText(target)
            if index >= 0:
                self.model_box.setCurrentIndex(index)
        # A provider that failed is worth saying so even when others worked.
        if problems:
            self.show_status("; ".join(problems), error=True)
        else:
            self.hide_status()
```

- [ ] **Step 4: Resolve metadata through the three layers**

Replace `current_preset`, `char_budget` and `vision_models` (lines 699-717) with:

```python
    def model_info(self, model_id: str):
        """Metadata for one model: models.ini, then provider, then presets.

        Local models get their context and vision from presets.ini, which
        the first two layers can still override if the user entered values.
        """
        info = models_mod.resolve(model_id, self.cfg.providers, self.store)
        preset = self.presets.get(model_id)
        if preset is not None:
            if info.ctx_size is None:
                info.ctx_size = preset.ctx_size
            if info.vision is None:
                info.vision = preset.vision
        return info

    def current_info(self):
        return self.model_info(self.current_model())

    def current_preset(self):
        return self.presets.get(self.current_model())

    def char_budget(self) -> int:
        ctx = self.current_info().ctx_size or 4096
        return int(ctx * self.cfg.chars_per_token * self.cfg.attach_ctx_fraction)

    def vision_models(self) -> list[str]:
        names = []
        for i in range(self.model_box.count()):
            name = self.model_box.itemData(i)
            if self.model_info(name).vision:
                names.append(name)
        return names
```

- [ ] **Step 5: Permit attachments on unknown-capability models**

In `_ensure_vision_model` (line 842), replace the opening check:

```python
        info = self.current_info()
        if info.vision:
            return True
        # Unknown is not the same as "no": a cloud model we know nothing
        # about may well accept images, and the API will say so if it does
        # not. Only a model known to lack vision gets stopped here.
        if info.vision is None:
            return True
```

- [ ] **Step 6: Route requests through the right client**

Every call that currently does `self.client.<method>(model, ...)` must become a
call on the routed client with the wire name. In `send()` (line 919) and
`_start_stream()` (line 1040), the model passed to `StreamWorker` must be split.
Change `_start_stream` to resolve both up front:

```python
    def _start_stream(self, model: str, messages: list[dict]) -> None:
        try:
            client = self.client.client_for(model)
        except providers_mod.KeyError_ as exc:
            self.show_status(str(exc), error=True)
            return
        wire = self.client.wire_name(model)
```

and pass `client` and `wire` to `StreamWorker` in place of `self.client` and
`model`. Add the import at the top of `ui.py`:

```python
from . import providers as providers_mod
```

Apply the same treatment in `_start_titling` (line 1133), which builds a
`TitleWorker`: resolve `client` and `wire` the same way, and on `KeyError_` skip
titling silently rather than showing an error, since titling is never the user's
own turn.

- [ ] **Step 7: Verify the whole suite still passes**

Run: `./test_llamachat.py`
Expected: PASS, all checks, ending in `all checks passed`

- [ ] **Step 8: Verify the app still starts against the local router**

Run: `./llamachat.py`
Expected: the window opens, the model dropdown lists the local models exactly as
before with no prefix, and sending a message works. Close it.

This is the check that the wiring is right; the unit tests cannot see it.

- [ ] **Step 9: Commit**

```bash
git add llamachat/ui.py llamachat/__main__.py
git commit -m "feat: list and route models through every configured provider

Metadata now resolves models.ini over provider defaults over presets.ini,
so a local model keeps getting its context and vision from the preset
while remaining overridable. A model whose vision support is unknown no
longer blocks an attachment: unknown is not the same as no, and the API
will reject an image if it really cannot take one.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>"
```

---

## Task 13: Dialog triggers

**Files:**
- Modify: `llamachat/ui.py:407-572` (`_build_ui`), `:719-725` (`_on_model_changed`)

- [ ] **Step 1: Add the on-demand button**

In `_build_ui`, immediately after the `model_box` is added to its layout, add:

```python
        self.model_settings_button = QToolButton()
        self.model_settings_button.setText("⚙")
        self.model_settings_button.setToolTip(
            "Context size, vision and prices for the selected model"
        )
        self.model_settings_button.clicked.connect(self.edit_model_settings)
```

Add it to the same layout the model box lives in, directly after it. Confirm
`QToolButton` is in the `PySide6.QtWidgets` import list at the top of `ui.py` and
add it if it is not.

- [ ] **Step 2: Add the trigger and editor methods**

Add after `vision_models` (around line 717):

```python
    def edit_model_settings(self, model_id: str | None = None) -> bool:
        """Open the dialog for one model. True when values were saved."""
        model_id = model_id or self.current_model()
        if not model_id:
            return False
        current = models_mod.resolve(model_id, self.cfg.providers, self.store)
        dialog = ModelDialog(model_id, current, self)
        if dialog.exec() != QDialog.Accepted:
            self.store.mark_skipped(model_id)
            return False
        self.store.save(model_id, dialog.info())
        self.update_meter()
        return True

    def _maybe_offer_model_settings(self, model_id: str) -> None:
        """Ask once, on first selection of an unconfigured cloud model.

        Local models are exempt: presets.ini already answers context and
        vision for them and they cost nothing. Cancelling records that the
        offer was made, so a model tried once never asks again.
        """
        if not model_id or not models_mod.is_billable(model_id, self.cfg.providers):
            return
        if self.store.was_offered(model_id):
            return
        self.edit_model_settings(model_id)
```

- [ ] **Step 3: Fire it on selection**

In `_on_model_changed` (line 719), add the offer before the existing body:

```python
    def _on_model_changed(self, _text: str) -> None:
        self._maybe_offer_model_settings(self.current_model())
```

Keep everything the method already does after that line.

- [ ] **Step 4: Add the imports**

At the top of `ui.py`, beside the other project imports:

```python
from .modeldialog import ModelDialog
```

Confirm `QDialog` is already imported from `PySide6.QtWidgets`; it is, since
`PromptDialog` subclasses it.

- [ ] **Step 5: Verify the suite still passes**

Run: `./test_llamachat.py`
Expected: PASS, all checks

- [ ] **Step 6: Verify the local path is unchanged**

Run: `./llamachat.py`
Expected: switching between local models opens no dialog. The ⚙ button opens the
dialog for the current local model, and Cancel leaves it unchanged. Close it.

- [ ] **Step 7: Commit**

```bash
git add llamachat/ui.py
git commit -m "feat: offer model settings on first use of a cloud model

Fires on selection rather than on send, so the interruption lands while
the user is already changing settings instead of mid-thought. Local
models never trigger it, and a cancelled dialog is recorded so a model
tried once never asks again.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>"
```

---

## Task 14: Show the cost

**Files:**
- Modify: `llamachat/ui.py:407-572` (`_build_ui`), `:993-1029` (`update_meter`), `:1075-1082` (`_on_usage`), `:1110-1123` (`_on_stream_finished`)

- [ ] **Step 1: Put the label beside the meter**

In `_build_ui`, immediately after the line that adds `self.meter` to its layout,
add:

```python
        self.cost = CostLabel()
```

and add it to the same layout directly after the meter.

- [ ] **Step 2: Track the reply's token counts**

In `_on_usage` (line 1075), record the counts so the finished reply can be stored
with them. Replace the method with:

```python
    @Slot(int, int)
    def _on_usage(self, prompt_tokens: int, total_tokens: int) -> None:
        """Replace the estimate with the counts the server reported."""
        info = self.current_info()
        limit = info.ctx_size or 0
        # What the next turn starts from is everything sent plus the reply.
        self.exact_tokens = total_tokens or prompt_tokens
        self.turn_prompt_tokens = prompt_tokens
        self.turn_completion_tokens = max(total_tokens - prompt_tokens, 0)
        self.meter.set_usage(self.exact_tokens, limit, exact=True)
        self.update_cost()
```

Initialise both counters to 0 in `_teardown_stream` (line 1185) and in
`ChatWindow.__init__` beside the other per-turn state:

```python
        self.turn_prompt_tokens = 0
        self.turn_completion_tokens = 0
```

- [ ] **Step 3: Store them with the finished reply**

In `_on_stream_finished` (line 1110), extend the `update_message` call:

```python
            self.history.update_message(
                self.assistant_message_id,
                self.assistant_buffer,
                self.reasoning_buffer,
                self._searches_json(),
                prompt_tokens=self.turn_prompt_tokens or None,
                completion_tokens=self.turn_completion_tokens or None,
                model=self.current_model(),
            )
```

- [ ] **Step 4: Compute and show the cost**

Add after `update_meter` (around line 1029):

```python
    def update_cost(self) -> None:
        """Refresh the money readout from stored counts plus the draft."""
        if not hasattr(self, "cost"):
            return  # still building the window
        model_id = self.current_model()
        billable = models_mod.is_billable(model_id, self.cfg.providers)
        priced = models_mod.is_priced(model_id, self.cfg.providers, self.store)
        if not billable or not priced:
            self.cost.set_cost(0.0, 0.0, billable, priced)
            return

        rows = (
            self.history.messages(self.session_id)
            if self.session_id is not None
            else []
        )
        spent = models_mod.conversation_cost(rows, self.cfg.providers, self.store)

        # Reopening a conversation resends its whole history, so the
        # projection has to price everything that would go out, not just
        # what was typed. That is what makes an expensive turn visible
        # before it is paid rather than after.
        pending = backend.estimate_tokens(
            self._chat_context(
                backend.build_user_content(
                    self.input.toPlainText(), self.attachments
                )
            ),
            self.cfg.chars_per_token,
        ) if self.session_id is not None or self.input.toPlainText() else 0
        projected = models_mod.projected_cost(
            pending, model_id, self.cfg.providers, self.store
        )
        self.cost.set_cost(spent, projected, billable, priced)
```

- [ ] **Step 5: Refresh it wherever the meter refreshes**

At the end of `update_meter` (line 1029), add:

```python
        self.update_cost()
```

Three more call sites, so switching model or reopening a conversation updates the
figure:

- At the end of `refresh_models`. **Task 12 rewrote this method**, so add the call
  to that rewritten version, at the very end of both the `if problems:` and `else:`
  branches, or on a single line after the whole `if/else`.
- At the end of the method that loads a session from the sidebar (around line
  1343, the one starting `session = self.history.get_session(session_id)`).
- At the end of `new_session` (line 789), so starting a fresh conversation clears
  the previous one's figure.

- [ ] **Step 6: Verify the suite still passes**

Run: `./test_llamachat.py`
Expected: PASS, all checks

- [ ] **Step 7: Verify against the local router**

Run: `./llamachat.py`
Expected: no cost label appears for a local model, the context meter behaves
exactly as before, and sending a message still works. Close it.

- [ ] **Step 8: Commit**

```bash
git add llamachat/ui.py
git commit -m "feat: show conversation cost and the next send's projection

The projection prices the whole request, not just the draft, because
reopening a conversation resends its entire history and that is billed
per turn on a cloud provider. Seeing it before sending is the whole point
of the readout.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>"
```

---

## Task 15: Config template and documentation

**Files:**
- Modify: `llamachat/config.py:155-215` (`write_default`)
- Modify: `README.md`
- Modify: `CHANGELOG.md`

- [ ] **Step 1: Document providers in the generated config**

In `write_default`, append to the written text, before the closing parenthesis:

```python
        '\n'
        '# External providers. The local router is a provider named "local",\n'
        '# synthesized from base_url above when no [providers.local] exists.\n'
        '# Any OpenAI-compatible endpoint works.\n'
        '#\n'
        '# Cloud models appear in the picker as provider:model. Local ones\n'
        '# stay bare, so nothing about the local setup changes.\n'
        '#\n'
        '# api_key accepts three forms:\n'
        '#   "pass:api/together"   read from the password store (preferred)\n'
        '#   "env:TOGETHER_KEY"    read from the environment\n'
        '#   "sk-..."              the key itself, in this file\n'
        '# It is read lazily, on the first request to that provider, so a\n'
        '# local-only session never unlocks the password store.\n'
        '#\n'
        '# filter keeps only models whose id contains one of these strings,\n'
        '# case-insensitively. Providers list hundreds of models; without a\n'
        '# filter the picker is unusable. Omit it to list them all.\n'
        '#\n'
        '# ctx_size, vision, price_in and price_out prefill the per-model\n'
        '# dialog. Prices are US dollars per million tokens. Everything the\n'
        '# dialog saves goes to models.ini beside this file, so none of\n'
        '# these has to be set here.\n'
        '#\n'
        '# [providers.together]\n'
        '# base_url = "https://api.together.xyz"\n'
        '# api_key = "pass:api/together"\n'
        '# filter = ["qwen", "deepseek"]\n'
        '# ctx_size = 32768\n'
        '# price_in = 0.60\n'
        '# price_out = 0.60\n'
```

- [ ] **Step 2: Verify the generated config still parses**

Run:

```bash
python3 -c "
import tempfile, tomllib
from pathlib import Path
import sys; sys.path.insert(0, '.')
from llamachat import config
with tempfile.TemporaryDirectory() as t:
    p = config.write_default(Path(t) / 'config.toml')
    tomllib.loads(p.read_text())
    cfg = config.load(p)
    assert set(cfg.providers) == {'local'}, cfg.providers
    print('generated config parses, providers:', list(cfg.providers))
"
```

Expected: `generated config parses, providers: ['local']`

The commented-out provider block must stay commented, or a fresh install would
try to reach an endpoint the user never configured.

- [ ] **Step 3: Document it in the README**

Add a section after the existing web search documentation, matching its tone and
depth. It must cover: the `[providers.*]` table with a worked together.ai example,
the three `api_key` forms and why `pass:` is preferred, lazy resolution meaning no
pinentry for local-only sessions, filtering and why it is needed, the per-model
dialog and `models.ini`, and the cost readout being an approximation based on
hand-entered prices.

State plainly, as the README already does for search: **a conversation resends its
whole history every turn, so a long conversation on a cloud provider is billed for
all of it on each message.** That is the behaviour most likely to surprise, and the
projection in the cost label exists to make it visible.

Also note that tool calling and reasoning output are known to vary between
providers, so web search on a cloud model may not work as it does locally.

- [ ] **Step 4: Add the changelog entry**

Add an `## [Unreleased]` section at the top of `CHANGELOG.md`, following the
existing format, describing: external OpenAI-compatible providers, `pass`/env/
literal key resolution, model filtering, the per-model settings dialog and
`models.ini`, per-conversation cost with projection, and the three new `messages`
columns. Note the cloud tool-calling caveat under a "Known limitations" line if
the file's format has one; otherwise state it in the entry itself.

- [ ] **Step 5: Verify the version test still passes**

`test_version_matches_changelog` matches `^## \[(\d+\.\d+\.\d+)\]` and asserts the
first hit equals `__version__`. `## [Unreleased]` does not match that pattern, so
it is skipped and `## [0.3.0]` stays the first hit. The entry is safe as long as
the heading is exactly `## [Unreleased]` and no version number is invented for it.

Run: `./test_llamachat.py`
Expected: PASS, including `ok  version matches changelog`

- [ ] **Step 6: Commit**

```bash
git add llamachat/config.py README.md CHANGELOG.md
git commit -m "docs: document external providers, keys and cost

The generated config ships the provider block commented out so a fresh
install never reaches an endpoint nobody configured. The README states
plainly that every turn resends the whole conversation, which is free
locally and billed per message on a cloud provider.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>"
```

---

## Task 16: End-to-end verification against a real provider

This task needs the user: it spends real money and needs a real API key. Do not
attempt it autonomously.

- [ ] **Step 1: Ask the user to configure one provider**

The user adds a `[providers.together]` block (or siliconflow) to their
`config.toml` with a `pass:` key and a filter, then starts llamachat.

- [ ] **Step 2: Verify listing and the dialog**

Expected: local models appear bare, cloud models appear as `provider:model` and
only those matching the filter. Selecting a cloud model for the first time opens
the settings dialog once. Cancelling it does not reopen it on reselection.

- [ ] **Step 3: Verify a cloud reply and the cost**

Send a short message to a cloud model with prices entered. Expected: the reply
streams, the cost label shows a figure, and it grows on the next message.

- [ ] **Step 4: Verify cost survives reopening**

Close and reopen the conversation from the history sidebar. Expected: the cost
label shows the accumulated figure, not zero.

- [ ] **Step 5: Verify the local path is untouched**

Switch back to a local model. Expected: no cost label, the meter behaves as
before, and web search still works exactly as it did in 0.3.0.

- [ ] **Step 6: Try web search on a cloud model and record what happens**

This is the known risk. Expected: unknown. Record the outcome, whether the tool
call is emitted, whether the reply arrives, and whether `reasoning_content` shows
up. If it fails, capture the shape of the response and open it as its own piece of
work rather than fixing it inside this one.

- [ ] **Step 7: Commit any fixes found**

Only if the earlier steps surfaced defects. Each fix gets a test first, following
the pattern of every task above.

---

## Notes for the implementer

**The local path is the one that must not regress.** Every task keeps local
models bare, unfiltered, and free of the dialog. If a change makes a local model
behave differently than it did in 0.3.0, that is a bug in the change, not an
acceptable cost.

**Never put a real API key in a test, a fixture or a commit message.** Two git
hooks scan for exactly this and will reject the commit. Use
`sk-test-not-a-real-key` and `example.org`.

**Cloud tool calling is unverified.** Task 16 step 6 is where that gets found out.
Nothing before it should assume search works on a cloud provider, and nothing
should assume it is broken either.