Greedy Prompt Assembly
Add elements highest-value-first or remove them lowest-value-first to fit the token budget, choosing direction based on how elements interact.
Once a project has matured past the minimal prompt crafter, a greedy algorithm — possibly combined with limited exploration of alternatives — is the recommended approach to prompt assembly for speed. Two variants, chosen based on how elements interact via dependency:
- Additive greedy — start with an empty prompt and add elements one at a time: each step adds the highest-value element that satisfies all its requirements, doesn't conflict with elements already included, and fits the remaining length budget; once the budget is filled, re-sort the chosen elements by position. This stays effective even with far more candidate elements than could ever fit, since it never has to consider most of them. It works best with few cyclical requirements and few cases of a high-value element depending on a low-value one. Pre-sorting candidates by their requirements and values lets each element be considered only once its dependencies are already satisfied, simplifying the implementation.
- Subtractive greedy — start by including all elements, then gradually remove the least valuable ones, or ones whose dependencies are no longer met, until the prompt fits the budget. This works well with a manageable total number of elements and few incompatibilities; otherwise it becomes cumbersome. It has the mirror-image weakness of the additive approach: a high-value element depending on a low-value one can still produce a suboptimal result unless the algorithm specifically prioritizes retaining high-value dependencies. Elastic snippets are normally easier to handle subtractively (progressively shrink a chosen version) than additively (guess which size to add first).
Both are basic prototypes, sufficient for many applications but expected to be outgrown as an application's specific requirements become clear.
Sources
- Prompt Engineering for LLMs (John Berryman), ch. 6