Sikau

LLM engineering · Day 3 · about 35 min

Prompts as code

Templates, chat messages and fitting a conversation into the context window.

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));
}

The rest of this lesson

  • Anatomy of a good promptexplain
  • Prompt injection starts hereexplain
  • A system promptfill
  • Where does it go?match
  • Templates and budgetswrite code
  • Badge quizquiz
  • What you learnedrecap

Continue this lesson free — run real code in your browser

Continue with Google

See all 8 lessons of LLM engineering