A prompt is a program
Real apps don't type prompts by hand: they render templates with data, send a list of messages (a system message with the rules, then user and assistant turns), and must fit everything into the model's context window. When a chat grows too long, keep the system prompt and the most recent turns.
Render and trim
function render(template, vars) {
return template.replace(/\{\{(\w+)\}\}/g, (_, name) => {
if (!(name in vars)) throw new Error(`Missing variable: ${name}`);
return String(vars[name]);
});
}
const estimateTokens = (text) => Math.ceil(text.length / 4); // ~4 characters per token in English
function fitToBudget(messages, budget, count = estimateTokens) {
const system = messages.filter((m) => m.role === "system");
let used = system.reduce((n, m) => n + count(m.content), 0);
if (used > budget) throw new Error("System prompt too long");
const kept = new Set(system);
for (const m of [...messages].reverse()) {
if (m.role === "system") continue;
if (used + count(m.content) > budget) break; // newest first; stop at the first that doesn't fit
used += count(m.content);
kept.add(m);
}
return messages.filter((m) => kept.has(m));
}