Budget Guardrails

The with budget(tokens: X, time: Y) { ... } block enforces hard token and wall-clock limits on any LLM inference inside it. If the LLM burns more than X tokens or takes more than Y seconds, the Turn VM raises a Thermodynamic Constraint Exceeded trap and severs execution immediately.

This is one of Turn's five core safety pillars — ensuring LLM inference is always bounded by construction.

budget_guardrail.tn
// Turn Language: Budget Guardrails
// Run: turn run impl/examples/budget_guardrail.tn --id budget_demo

struct ResearchSummary {
  key_findings: Str,
  confidence_level: Str,
  sources_consulted: Num
};

let topic = "Advances in neuromorphic computing in 2025";
call("echo", "[Research Agent] Starting analysis with 8000-token budget...");

let prompt = "Summarize 2025 advances in: " + topic + ". Be concise.";

let result = with budget(tokens: 8000, time: 60) {
  infer ResearchSummary { prompt; };
};

persist let latest_research = result;

call("echo", "Confidence: " + result.confidence_level);
call("echo", "Findings: " + result.key_findings);

Run it:

export TURN_INFER_PROVIDER=~/.turn/providers/turn_provider_openai.wasm
turn run impl/examples/budget_guardrail.tn --id budget_demo

The full implementation is in impl/examples/budget_guardrail.tn.