Skip to content

Context selection

`densityGreedyContextSelection` keeps a selection of messages or tool results within a hard token budget as candidates arrive and expire. Callers supply token counts and a monotone submodular marginal-gain oracle.

densityGreedyContextSelection keeps a selection of messages or tool results within a hard token budget as candidates arrive and expire. Callers supply token counts and a monotone submodular marginal-gain oracle.

Packing three search hits into 21 words: new-query-terms-first keeps the setup rule and the version caveat in 19 words, where rank-and-truncate drops the caveat

  • knotkit/deque: Queues elements at both ends in O(1) amortized time, over array and Float64Array ring buffers, with an optional fixed capacity that turns the queue into a sliding window
  • knotkit/heap: Provides binary and k-ary min/max heaps plus indexed priority queues, ordered by caller-supplied comparators
  • knotkit/sampling: Draws unbiased subsets, pairs, shuffles, and weighted samples over injected randomness
  • knotkit/prng: Generates seeded pseudorandom numbers and unbiased integers for reproducible simulations and tests
{"v":1,"box":[240.0503,174.8111,932.0252,547.8349],"x":{"s":"linear","lim":[0,2.58],"inv":false,"g":0,"t":"Setup rule and version caveat","tk":[[0,"0"],[1,"1"],[2,"2"]]},"y":{"s":"linear","lim":[-0.65,2.65],"inv":false,"g":0,"tk":[[2,"Rank and truncate"],[1,"New query terms first"],[0,"Gain per word"]]},"ti":"Required passages kept","a":[{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#8c8c84"},{"k":"scatter","c":"#5c5c57","s":"xy","d":[1,2],"p":[508.26,248.29],"r":6.09},{"k":"annotation","c":"#2b2b28"},{"k":"segments","c":"#8c8c84"},{"k":"scatter","c":"#1d5fa6","s":"xy","d":[2,1],"p":[776.46,361.32],"r":6.09},{"k":"annotation","c":"#1d5fa6"},{"k":"segments","c":"#8c8c84"},{"k":"scatter","c":"#5c5c57","s":"xy","d":[1,0],"p":[508.26,474.36],"r":6.09},{"k":"annotation","c":"#2b2b28"},{"k":"annotation","c":"#111111"}],"lg":[]} 0 1 2 Setup rule and version caveat Rank and truncate New query terms first Gain per word 1/2 2/2 1/2 Only one policy keeps both facts in 19 of 21 words Required passages kept {"v":1,"box":[980.0252,174.8111,1672,547.8349],"x":{"s":"linear","lim":[-0.395,33.395],"inv":false,"g":1,"t":"Words past the dashed 21-word budget were left out","tk":[[1,"1"],[21,"21"]]},"y":{"s":"linear","lim":[-0.65,2.65],"inv":false,"g":1,"tk":[]},"ti":"Where the words go","a":[{"k":"scatter","c":"#1d5fa6","s":"xy","d":[1,2,2,2,3,2,4,2,5,2,6,2,7,2,8,2,9,2,10,2,11,2],"p":[1008.59,248.29,1029.07,248.29,1049.55,248.29,1070.03,248.29,1090.51,248.29,1110.99,248.29,1131.47,248.29,1151.94,248.29,1172.42,248.29,1192.9,248.29,1213.38,248.29],"r":5.67},{"k":"scatter","c":"#5b6b7c","s":"xy","d":[12,2,13,2,14,2,15,2,16,2,17,2,18,2,19,2,20,2,21,2,22,2,23,2,24,2,25,2,26,2,27,2,28,2,29,2],"p":[1233.86,248.29,1254.34,248.29,1274.82,248.29,1295.29,248.29,1315.77,248.29,1336.25,248.29,1356.73,248.29,1377.21,248.29,1397.69,248.29,1418.17,248.29,1438.65,248.29,1459.12,248.29,1479.6,248.29,1500.08,248.29,1520.56,248.29,1541.04,248.29,1561.52,248.29,1582,248.29],"r":5.67},{"k":"scatter","c":"#1d5fa6","s":"xy","d":[1,1,2,1,3,1,4,1,5,1,6,1,7,1,8,1,9,1,10,1,11,1],"p":[1008.59,361.32,1029.07,361.32,1049.55,361.32,1070.03,361.32,1090.51,361.32,1110.99,361.32,1131.47,361.32,1151.94,361.32,1172.42,361.32,1192.9,361.32,1213.38,361.32],"r":5.67},{"k":"scatter","c":"#c4501b","s":"xy","d":[12,1,13,1,14,1,15,1,16,1,17,1,18,1,19,1],"p":[1233.86,361.32,1254.34,361.32,1274.82,361.32,1295.29,361.32,1315.77,361.32,1336.25,361.32,1356.73,361.32,1377.21,361.32],"r":5.67},{"k":"scatter","c":"#5b6b7c","s":"xy","d":[22,1,23,1,24,1,25,1,26,1,27,1,28,1,29,1,30,1,31,1],"p":[1438.65,361.32,1459.12,361.32,1479.6,361.32,1500.08,361.32,1520.56,361.32,1541.04,361.32,1561.52,361.32,1582,361.32,1602.47,361.32,1622.95,361.32],"r":5.67},{"k":"scatter","c":"#5b6b7c","s":"xy","d":[1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9,0,10,0,22,0,23,0,24,0,25,0,26,0,27,0,28,0,29,0,30,0,31,0,32,0],"p":[1008.59,474.36,1029.07,474.36,1049.55,474.36,1070.03,474.36,1090.51,474.36,1110.99,474.36,1131.47,474.36,1151.94,474.36,1172.42,474.36,1192.9,474.36,1438.65,474.36,1459.12,474.36,1479.6,474.36,1500.08,474.36,1520.56,474.36,1541.04,474.36,1561.52,474.36,1582,474.36,1602.47,474.36,1622.95,474.36,1643.43,474.36],"r":5.67},{"k":"scatter","c":"#c4501b","s":"xy","d":[11,0,12,0,13,0,14,0,15,0,16,0,17,0,18,0],"p":[1213.38,474.36,1233.86,474.36,1254.34,474.36,1274.82,474.36,1295.29,474.36,1315.77,474.36,1336.25,474.36,1356.73,474.36],"r":5.67},{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#8c8c84"},{"k":"line","c":"#8c8c84"},{"k":"annotation","c":"#1d5fa6"},{"k":"annotation","c":"#5b6b7c"},{"k":"annotation","c":"#c4501b"},{"k":"annotation","c":"#1d5fa6"},{"k":"annotation","c":"#c4501b"},{"k":"annotation","c":"#5b6b7c"},{"k":"annotation","c":"#5b6b7c"},{"k":"annotation","c":"#c4501b"},{"k":"annotation","c":"#1d5fa6"},{"k":"annotation","c":"#5c5c57"}],"lg":[]} 1 21 Words past the dashed 21-word budget were left out setup rule overview version caveat setup rule version caveat overview overview version caveat setup rule One dot per word, source labels included Where the words go
{"v":1,"box":[240.0503,175.1931,680,583.4917],"x":{"s":"linear","lim":[0,2.58],"inv":false,"g":0,"t":"Setup rule and version caveat","tk":[[0,"0"],[1,"1"],[2,"2"]]},"y":{"s":"linear","lim":[-0.65,2.65],"inv":false,"g":0,"tk":[[2,"Rank and truncate"],[1,"New query terms first"],[0,"Gain per word"]]},"ti":"Required passages kept","a":[{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#8c8c84"},{"k":"scatter","c":"#5c5c57","s":"xy","d":[1,2],"p":[410.57,255.62],"r":6.09},{"k":"annotation","c":"#2b2b28"},{"k":"segments","c":"#8c8c84"},{"k":"scatter","c":"#1d5fa6","s":"xy","d":[2,1],"p":[581.1,379.34],"r":6.09},{"k":"annotation","c":"#1d5fa6"},{"k":"segments","c":"#8c8c84"},{"k":"scatter","c":"#5c5c57","s":"xy","d":[1,0],"p":[410.57,503.07],"r":6.09},{"k":"annotation","c":"#2b2b28"},{"k":"annotation","c":"#111111"}],"lg":[]} 0 1 2 Setup rule and version caveat Rank and truncate New query terms first Gain per word 1/2 2/2 1/2 Only one policy keeps both facts in 19 of 21 words Required passages kept {"v":1,"box":[240.0503,779.5362,680,1187.8349],"x":{"s":"linear","lim":[-0.395,33.395],"inv":false,"g":1,"t":"Words past the dashed 21-word budget were left out","tk":[[1,"1"],[21,"21"]]},"y":{"s":"linear","lim":[-0.65,2.65],"inv":false,"g":1,"tk":[]},"ti":"Where the words go","a":[{"k":"scatter","c":"#1d5fa6","s":"xy","d":[1,2,2,2,3,2,4,2,5,2,6,2,7,2,8,2,9,2,10,2,11,2],"p":[258.21,859.96,271.23,859.96,284.25,859.96,297.27,859.96,310.29,859.96,323.31,859.96,336.33,859.96,349.35,859.96,362.37,859.96,375.39,859.96,388.41,859.96],"r":5.67},{"k":"scatter","c":"#5b6b7c","s":"xy","d":[12,2,13,2,14,2,15,2,16,2,17,2,18,2,19,2,20,2,21,2,22,2,23,2,24,2,25,2,26,2,27,2,28,2,29,2],"p":[401.43,859.96,414.45,859.96,427.47,859.96,440.5,859.96,453.52,859.96,466.54,859.96,479.56,859.96,492.58,859.96,505.6,859.96,518.62,859.96,531.64,859.96,544.66,859.96,557.68,859.96,570.7,859.96,583.72,859.96,596.74,859.96,609.76,859.96,622.78,859.96],"r":5.67},{"k":"scatter","c":"#1d5fa6","s":"xy","d":[1,1,2,1,3,1,4,1,5,1,6,1,7,1,8,1,9,1,10,1,11,1],"p":[258.21,983.69,271.23,983.69,284.25,983.69,297.27,983.69,310.29,983.69,323.31,983.69,336.33,983.69,349.35,983.69,362.37,983.69,375.39,983.69,388.41,983.69],"r":5.67},{"k":"scatter","c":"#c4501b","s":"xy","d":[12,1,13,1,14,1,15,1,16,1,17,1,18,1,19,1],"p":[401.43,983.69,414.45,983.69,427.47,983.69,440.5,983.69,453.52,983.69,466.54,983.69,479.56,983.69,492.58,983.69],"r":5.67},{"k":"scatter","c":"#5b6b7c","s":"xy","d":[22,1,23,1,24,1,25,1,26,1,27,1,28,1,29,1,30,1,31,1],"p":[531.64,983.69,544.66,983.69,557.68,983.69,570.7,983.69,583.72,983.69,596.74,983.69,609.76,983.69,622.78,983.69,635.8,983.69,648.82,983.69],"r":5.67},{"k":"scatter","c":"#5b6b7c","s":"xy","d":[1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9,0,10,0,22,0,23,0,24,0,25,0,26,0,27,0,28,0,29,0,30,0,31,0,32,0],"p":[258.21,1107.41,271.23,1107.41,284.25,1107.41,297.27,1107.41,310.29,1107.41,323.31,1107.41,336.33,1107.41,349.35,1107.41,362.37,1107.41,375.39,1107.41,531.64,1107.41,544.66,1107.41,557.68,1107.41,570.7,1107.41,583.72,1107.41,596.74,1107.41,609.76,1107.41,622.78,1107.41,635.8,1107.41,648.82,1107.41,661.84,1107.41],"r":5.67},{"k":"scatter","c":"#c4501b","s":"xy","d":[11,0,12,0,13,0,14,0,15,0,16,0,17,0,18,0],"p":[388.41,1107.41,401.43,1107.41,414.45,1107.41,427.47,1107.41,440.5,1107.41,453.52,1107.41,466.54,1107.41,479.56,1107.41],"r":5.67},{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#8c8c84"},{"k":"line","c":"#8c8c84"},{"k":"annotation","c":"#1d5fa6"},{"k":"annotation","c":"#5b6b7c"},{"k":"annotation","c":"#c4501b"},{"k":"annotation","c":"#1d5fa6"},{"k":"annotation","c":"#c4501b"},{"k":"annotation","c":"#5b6b7c"},{"k":"annotation","c":"#5b6b7c"},{"k":"annotation","c":"#c4501b"},{"k":"annotation","c":"#1d5fa6"},{"k":"annotation","c":"#5c5c57"}],"lg":[]} 1 21 Words past the dashed 21-word budget were left out setup rule overview version caveat setup rule version caveat overview overview version caveat setup rule One dot per word, source labels included Where the words go
{"v":1,"box":[240.0503,174.8111,932.0252,547.8349],"x":{"s":"linear","lim":[0,2.58],"inv":false,"g":0,"t":"Setup rule and version caveat","tk":[[0,"0"],[1,"1"],[2,"2"]]},"y":{"s":"linear","lim":[-0.65,2.65],"inv":false,"g":0,"tk":[[2,"Rank and truncate"],[1,"New query terms first"],[0,"Gain per word"]]},"ti":"Required passages kept","a":[{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#8c8c84"},{"k":"scatter","c":"#1d5fa6","s":"xy","d":[1,2],"p":[508.26,248.29],"r":6.09},{"k":"annotation","c":"#1d5fa6"},{"k":"segments","c":"#8c8c84"},{"k":"scatter","c":"#1d5fa6","s":"xy","d":[1,1],"p":[508.26,361.32],"r":6.09},{"k":"annotation","c":"#1d5fa6"},{"k":"segments","c":"#8c8c84"},{"k":"scatter","c":"#1d5fa6","s":"xy","d":[1,0],"p":[508.26,474.36],"r":6.09},{"k":"annotation","c":"#1d5fa6"},{"k":"annotation","c":"#111111"}],"lg":[]} 0 1 2 Setup rule and version caveat Rank and truncate New query terms first Gain per word 1/2 1/2 1/2 With the top two hits, the version caveat is gone before packing starts Required passages kept {"v":1,"box":[980.0252,174.8111,1672,547.8349],"x":{"s":"linear","lim":[0.0775,22.4225],"inv":false,"g":1,"t":"Words past the dashed 21-word budget were left out","tk":[[1,"1"],[21,"21"]]},"y":{"s":"linear","lim":[-0.65,2.65],"inv":false,"g":1,"tk":[]},"ti":"Where the words go","a":[{"k":"scatter","c":"#1d5fa6","s":"xy","d":[1,2,2,2,3,2,4,2,5,2,6,2,7,2,8,2,9,2,10,2,11,2],"p":[1008.59,248.29,1039.56,248.29,1070.53,248.29,1101.5,248.29,1132.46,248.29,1163.43,248.29,1194.4,248.29,1225.37,248.29,1256.34,248.29,1287.3,248.29,1318.27,248.29],"r":5.67},{"k":"scatter","c":"#5b6b7c","s":"xy","d":[12,2,13,2,14,2,15,2,16,2,17,2,18,2,19,2,20,2,21,2],"p":[1349.24,248.29,1380.21,248.29,1411.17,248.29,1442.14,248.29,1473.11,248.29,1504.08,248.29,1535.05,248.29,1566.01,248.29,1596.98,248.29,1627.95,248.29],"r":5.67},{"k":"scatter","c":"#1d5fa6","s":"xy","d":[1,1,2,1,3,1,4,1,5,1,6,1,7,1,8,1,9,1,10,1,11,1],"p":[1008.59,361.32,1039.56,361.32,1070.53,361.32,1101.5,361.32,1132.46,361.32,1163.43,361.32,1194.4,361.32,1225.37,361.32,1256.34,361.32,1287.3,361.32,1318.27,361.32],"r":5.67},{"k":"scatter","c":"#5b6b7c","s":"xy","d":[12,1,13,1,14,1,15,1,16,1,17,1,18,1,19,1,20,1,21,1],"p":[1349.24,361.32,1380.21,361.32,1411.17,361.32,1442.14,361.32,1473.11,361.32,1504.08,361.32,1535.05,361.32,1566.01,361.32,1596.98,361.32,1627.95,361.32],"r":5.67},{"k":"scatter","c":"#5b6b7c","s":"xy","d":[1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9,0,10,0],"p":[1008.59,474.36,1039.56,474.36,1070.53,474.36,1101.5,474.36,1132.46,474.36,1163.43,474.36,1194.4,474.36,1225.37,474.36,1256.34,474.36,1287.3,474.36],"r":5.67},{"k":"scatter","c":"#1d5fa6","s":"xy","d":[11,0,12,0,13,0,14,0,15,0,16,0,17,0,18,0,19,0,20,0,21,0],"p":[1318.27,474.36,1349.24,474.36,1380.21,474.36,1411.17,474.36,1442.14,474.36,1473.11,474.36,1504.08,474.36,1535.05,474.36,1566.01,474.36,1596.98,474.36,1627.95,474.36],"r":5.67},{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#8c8c84"},{"k":"line","c":"#8c8c84"},{"k":"annotation","c":"#1d5fa6"},{"k":"annotation","c":"#5b6b7c"},{"k":"annotation","c":"#1d5fa6"},{"k":"annotation","c":"#5b6b7c"},{"k":"annotation","c":"#5b6b7c"},{"k":"annotation","c":"#1d5fa6"},{"k":"annotation","c":"#5c5c57"}],"lg":[]} 1 21 Words past the dashed 21-word budget were left out setup rule overview setup rule overview overview setup rule One dot per word, source labels included Where the words go
{"v":1,"box":[240.0503,175.1931,680,583.4917],"x":{"s":"linear","lim":[0,2.58],"inv":false,"g":0,"t":"Setup rule and version caveat","tk":[[0,"0"],[1,"1"],[2,"2"]]},"y":{"s":"linear","lim":[-0.65,2.65],"inv":false,"g":0,"tk":[[2,"Rank and truncate"],[1,"New query terms first"],[0,"Gain per word"]]},"ti":"Required passages kept","a":[{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#8c8c84"},{"k":"scatter","c":"#1d5fa6","s":"xy","d":[1,2],"p":[410.57,255.62],"r":6.09},{"k":"annotation","c":"#1d5fa6"},{"k":"segments","c":"#8c8c84"},{"k":"scatter","c":"#1d5fa6","s":"xy","d":[1,1],"p":[410.57,379.34],"r":6.09},{"k":"annotation","c":"#1d5fa6"},{"k":"segments","c":"#8c8c84"},{"k":"scatter","c":"#1d5fa6","s":"xy","d":[1,0],"p":[410.57,503.07],"r":6.09},{"k":"annotation","c":"#1d5fa6"},{"k":"annotation","c":"#111111"}],"lg":[]} 0 1 2 Setup rule and version caveat Rank and truncate New query terms first Gain per word 1/2 1/2 1/2 With the top two hits, the version caveat is gone before packing starts Required passages kept {"v":1,"box":[240.0503,779.5362,680,1187.8349],"x":{"s":"linear","lim":[0.0775,22.4225],"inv":false,"g":1,"t":"Words past the dashed 21-word budget were left out","tk":[[1,"1"],[21,"21"]]},"y":{"s":"linear","lim":[-0.65,2.65],"inv":false,"g":1,"tk":[]},"ti":"Where the words go","a":[{"k":"scatter","c":"#1d5fa6","s":"xy","d":[1,2,2,2,3,2,4,2,5,2,6,2,7,2,8,2,9,2,10,2,11,2],"p":[258.21,859.96,277.9,859.96,297.59,859.96,317.28,859.96,336.97,859.96,356.66,859.96,376.35,859.96,396.04,859.96,415.73,859.96,435.41,859.96,455.1,859.96],"r":5.67},{"k":"scatter","c":"#5b6b7c","s":"xy","d":[12,2,13,2,14,2,15,2,16,2,17,2,18,2,19,2,20,2,21,2],"p":[474.79,859.96,494.48,859.96,514.17,859.96,533.86,859.96,553.55,859.96,573.24,859.96,592.93,859.96,612.61,859.96,632.3,859.96,651.99,859.96],"r":5.67},{"k":"scatter","c":"#1d5fa6","s":"xy","d":[1,1,2,1,3,1,4,1,5,1,6,1,7,1,8,1,9,1,10,1,11,1],"p":[258.21,983.69,277.9,983.69,297.59,983.69,317.28,983.69,336.97,983.69,356.66,983.69,376.35,983.69,396.04,983.69,415.73,983.69,435.41,983.69,455.1,983.69],"r":5.67},{"k":"scatter","c":"#5b6b7c","s":"xy","d":[12,1,13,1,14,1,15,1,16,1,17,1,18,1,19,1,20,1,21,1],"p":[474.79,983.69,494.48,983.69,514.17,983.69,533.86,983.69,553.55,983.69,573.24,983.69,592.93,983.69,612.61,983.69,632.3,983.69,651.99,983.69],"r":5.67},{"k":"scatter","c":"#5b6b7c","s":"xy","d":[1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9,0,10,0],"p":[258.21,1107.41,277.9,1107.41,297.59,1107.41,317.28,1107.41,336.97,1107.41,356.66,1107.41,376.35,1107.41,396.04,1107.41,415.73,1107.41,435.41,1107.41],"r":5.67},{"k":"scatter","c":"#1d5fa6","s":"xy","d":[11,0,12,0,13,0,14,0,15,0,16,0,17,0,18,0,19,0,20,0,21,0],"p":[455.1,1107.41,474.79,1107.41,494.48,1107.41,514.17,1107.41,533.86,1107.41,553.55,1107.41,573.24,1107.41,592.93,1107.41,612.61,1107.41,632.3,1107.41,651.99,1107.41],"r":5.67},{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#8c8c84"},{"k":"line","c":"#8c8c84"},{"k":"annotation","c":"#1d5fa6"},{"k":"annotation","c":"#5b6b7c"},{"k":"annotation","c":"#1d5fa6"},{"k":"annotation","c":"#5b6b7c"},{"k":"annotation","c":"#5b6b7c"},{"k":"annotation","c":"#1d5fa6"},{"k":"annotation","c":"#5c5c57"}],"lg":[]} 1 21 Words past the dashed 21-word budget were left out setup rule overview setup rule overview overview setup rule One dot per word, source labels included Where the words go
{"v":1,"box":[410.5035,140.4708,1006.1406,394.2349],"x":{"s":"linear","lim":[0,11.61],"inv":false,"g":0,"t":"Gain assigned to the kept evidence","tk":[[0,"0"],[3,"3"],[6,"6"],[9,"9"]]},"y":{"s":"linear","lim":[-0.65,1.65],"inv":false,"g":0,"tk":[[1,"Keep search result · 300 prompt tokens"],[0,"Keep earlier test log · 600 prompt tokens"]]},"ti":"Marginal gain kept","a":[{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#8c8c84"},{"k":"scatter","c":"#5c5c57","s":"xy","d":[9,1],"p":[872.24,212.19],"r":6.09},{"k":"annotation","c":"#2b2b28"},{"k":"segments","c":"#8c8c84"},{"k":"scatter","c":"#1d5fa6","s":"xy","d":[4,0],"p":[615.72,322.52],"r":6.09},{"k":"annotation","c":"#1d5fa6"},{"k":"annotation","c":"#111111"}],"lg":[]} 0 3 6 9 Gain assigned to the kept evidence Keep search result · 300 prompt tokens Keep earlier test log · 600 prompt tokens 9 4 Cache pricing cuts estimated cost from 300 to 60 full-price tokens Marginal gain kept {"v":1,"box":[1076.3628,140.4708,1672,394.2349],"x":{"s":"linear","lim":[0,387],"inv":false,"g":1,"t":"Full-price tokens, with cached tokens at 0.1×","tk":[[0,"0"],[100,"100"],[200,"200"],[300,"300"]]},"y":{"s":"linear","lim":[-0.65,1.65],"inv":false,"g":1,"tk":[[1,""],[0,""]]},"ti":"Estimated price","a":[{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#8c8c84"},{"k":"scatter","c":"#5c5c57","s":"xy","d":[300,1],"p":[1538.1,212.19],"r":6.09},{"k":"annotation","c":"#2b2b28"},{"k":"segments","c":"#8c8c84"},{"k":"scatter","c":"#1d5fa6","s":"xy","d":[60,0],"p":[1168.71,322.52],"r":6.09},{"k":"annotation","c":"#1d5fa6"}],"lg":[]} 0 100 200 300 Full-price tokens, with cached tokens at 0.1× 300 tokens 60 tokens Estimated price
{"v":1,"box":[410.5035,175.1931,680,448.6483],"x":{"s":"linear","lim":[0,11.61],"inv":false,"g":0,"t":"Gain assigned to the kept evidence","tk":[[0,"0"],[3,"3"],[6,"6"],[9,"9"]]},"y":{"s":"linear","lim":[-0.65,1.65],"inv":false,"g":0,"tk":[[1,"Keep search result · 300 prompt tokens"],[0,"Keep earlier test log · 600 prompt tokens"]]},"ti":"Marginal gain kept","a":[{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#8c8c84"},{"k":"scatter","c":"#5c5c57","s":"xy","d":[9,1],"p":[619.42,252.47],"r":6.09},{"k":"annotation","c":"#2b2b28"},{"k":"segments","c":"#8c8c84"},{"k":"scatter","c":"#1d5fa6","s":"xy","d":[4,0],"p":[503.35,371.37],"r":6.09},{"k":"annotation","c":"#1d5fa6"},{"k":"annotation","c":"#111111"}],"lg":[]} 0 3 6 9 Gain assigned to the kept evidence Keep search result · 300 prompt tokens Keep earlier test log · 600 prompt tokens 9 4 Cache pricing cuts estimated cost from 300 to 60 full-price tokens Marginal gain kept {"v":1,"box":[410.5035,607.1796,680,880.6349],"x":{"s":"linear","lim":[0,387],"inv":false,"g":1,"t":"Full-price tokens, with cached tokens at 0.1×","tk":[[0,"0"],[100,"100"],[200,"200"],[300,"300"]]},"y":{"s":"linear","lim":[-0.65,1.65],"inv":false,"g":1,"tk":[[1,"Keep search result · 300 prompt tokens"],[0,"Keep earlier test log · 600 prompt tokens"]]},"ti":"Estimated price","a":[{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#8c8c84"},{"k":"scatter","c":"#5c5c57","s":"xy","d":[300,1],"p":[619.42,684.46],"r":6.09},{"k":"annotation","c":"#2b2b28"},{"k":"segments","c":"#8c8c84"},{"k":"scatter","c":"#1d5fa6","s":"xy","d":[60,0],"p":[452.29,803.35],"r":6.09},{"k":"annotation","c":"#1d5fa6"}],"lg":[]} 0 100 200 300 Full-price tokens, with cached tokens at 0.1× Keep search result · 300 prompt tokens Keep earlier test log · 600 prompt tokens 300 tokens 60 tokens Estimated price
  • 3 search hits, 21 words. Keep the setup rule and the version caveat.
  • Rank and truncate uses 21 words and loses the version caveat.
  • Gain per word uses 18 words and loses the setup rule.
  • New query terms first keeps both in 19 words.
  • With only the top two hits, the version caveat never arrives. At most 1 of 2 facts survive.
  • At 0.1× input price, the cached log replaces the search result in a 600-token prompt.
  • Prompt 300 → 600 tokens. Gain 9 → 4. Estimated cost 300 → 60 full-price tokens.

Hover or focus a chart for exact values. Drawn by demo.ts at build time, from the fixtures the benchmark uses; it reproduces the README figure.

Terminal window
git clone https://github.com/jkomyno/knotkit.git && cd knotkit && bun install
bun run demo:problem:context-selection

problems/context-selection/README.md states the fixture, the seed, and the result table.