Abstract
Large language models are often used to answer queries grounded in large textcorpora (e.g. codebases, legal documents, or chat histories) by placing theentire corpus in the context window and leveraging in-context learning (ICL).Although current models support contexts of 100K-1M tokens, this setup iscostly to serve because the memory consumption of the KV cache scales withinput length. We explore an alternative: training a smaller KV cache offline oneach corpus. At inference time, we load this trained KV cache, which we call aCartridge, and decode a response. Critically, the cost of training a Cartridgecan be amortized across all the queries referencing the same corpus. However,we find that the naive approach of training the Cartridge with next-tokenprediction on the corpus is not competitive with ICL. Instead, we proposeself-study, a training recipe in which we generate synthetic conversationsabout the corpus and train the Cartridge with a context-distillation objective.We find that Cartridges trained with self-study replicate the functionality ofICL, while being significantly cheaper to serve. On challenging long-contextbenchmarks, Cartridges trained with self-study match ICL performance whileusing 38.6x less memory and enabling 26.4x higher throughput. Self-study alsoextends the model's effective context length (e.g. from 128k to 484k tokens onMTOB) and surprisingly, leads to Cartridges that can be composed at inferencetime without retraining.