DEV Community

Trần Hoàng Tú
Trần Hoàng Tú

Posted on

How I Built a Zero-Dependency {{Variable}} AI Prompt Compiler & Multi-Agent Blueprint Workbench

Managing AI prompts across multiple agents, IDEs, and APIs quickly turns messy when instructions are scattered across copy-pasted text files. To solve this without bloating my stack, I built a zero-dependency {{Variable}} AI Prompt Compiler & Multi-Agent Blueprint Workbench using pure browser ES Modules.

You can try it live right here:

Here is how the core compiler architecture works under the hood.


1. Regex Token Extraction & Default Fallbacks

Instead of pulling in heavy templating engines like Handlebars or Mustache, a single regular expression handles variable discovery and fallback values cleanly:

const TOKEN_REGEX = /\{\{\s*([a-zA-Z0-9_.-]+)(?:\|([^}]*))?\s*\}\}/g;

export function extractTokens(template) {
  const tokens = new Map();
  for (const match of template.matchAll(TOKEN_REGEX)) {
    const [, key, fallback = ""] = match;
    if (!tokens.has(key)) tokens.set(key, fallback.trim());
  }
  return tokens;
}

export function compilePrompt(template, values = {}) {
  return template.replace(TOKEN_REGEX, (_, key, fallback = "") => {
    const val = values[key];
    return val !== undefined && val !== "" ? String(val) : fallback.trim();
  });
}
Enter fullscreen mode Exit fullscreen mode

With syntax like {{target_stack|TypeScript + Node 22}} or {{tone|concise}}, every template self-documents its inputs and falls back gracefully when optional parameters are omitted.


2. Live Token & Cost Estimation

Before sending compiled prompts to an LLM, developers need immediate feedback on context budget. By pairing a lightweight heuristic tokenizer (Math.ceil(text.length / 4) adjusted for whitespace and code punctuation) with a static pricing table, the workbench calculates real-time input costs across GPT-4.1, Claude Sonnet 4, and Gemini 2.5 Pro:

const MODEL_RATES_PER_1M = {
  "gpt-4.1": 2.00,
  "claude-sonnet-4": 3.00,
  "gemini-2.5-pro": 1.25,
};

export function estimateCost(compiledText, model = "gpt-4.1") {
  const tokens = Math.ceil(compiledText.trim().length / 4);
  const usd = (tokens / 1_000_000) * (MODEL_RATES_PER_1M[model] ?? 2.0);
  return { tokens, usd: Number(usd.toFixed(6)) };
}
Enter fullscreen mode Exit fullscreen mode

Zero network calls, instant reactivity on every keystroke.


3. Multi-Format Exporters (AGENTS.md, .cursorrules, JSON API)

Because the compiled output is plain structured data, exporting to any coding agent ecosystem takes a pure formatter function:

  • AGENTS.md: Wraps compiled system instructions with project boundaries and verification steps for autonomous coding agents.
  • .cursorrules: Formats role rules, coding conventions, and file-pattern constraints ready to drop into your repository root.
  • JSON API Payload: Emits { model, messages: [{ role: "system", content }] } ready for OpenAI, Anthropic, or OpenRouter fetch() calls.

Try the Workbench & Companion Tools

Test your own templates in the Live Interactive Studio Demo or grab the Full 24-Blueprint Commercial Source Pack to deploy production workflows immediately.

If you also work with financial automation or EU/Vietnam XML compliance, check out our companion XML E-Invoice Audit tool (Live Demo | Source Pack) built on the same zero-dependency ES Module philosophy.

Top comments (0)