Generative AI Input Budgets and Cost
Bound input size without confusing bytes with model tokens.
How it works
An application should have explicit input, output and work budgets. A UTF-8 byte limit can protect a local processing boundary, but it does not calculate exact token usage or service cost. Tokenizers and pricing vary by model and provider. Use actual supported usage records and current verified pricing for a real estimate rather than a universal characters-per-token shortcut.
How to use this lesson
This is an authored prompt exercise with a human-written acceptance guide. It is not a captured provider response or a promise that a model will produce the target. No provider account, API, model weights or live inference was used for the course execution review. If you try it in an external service, use an authorised environment, check its actual access and data settings, and record the product/model and run date.
Bhylu does not run a generative model or send this prompt to an AI service. Copy and download preserve the text only. External tools may require accounts, paid access or compatible inputs; this course does not promise availability or prices.
Prompt exercise
Prepare a request with a 2000-byte local source budget.
If the supplied source exceeds the local budget, return a budget error before model submission.
This is a byte guard, not an exact token or currency estimate.
Acceptance guide — not a model transcript
The local lab accepts a small source and refuses a multibyte string whose UTF-8 size exceeds the budget.
The multibyte case matters because JavaScript string length is not a UTF-8 byte count. A real generation flow also needs an output cap, request timeout and bounded tool iterations. These controls should stop work explicitly rather than ask the model to voluntarily limit every resource.
Tested local code lab
This complete Node.js 22.15.1 standard-library example uses fixed synthetic data and assertions. It does not call a model or external API. Save it as input-budget.mjs in a separate learning folder and run node input-budget.mjs. No npm package, API key, Apache, MySQL or website configuration change is required. This Node lab is not linked to an incompatible browser playground.
Download the complete local lab
import assert from "node:assert/strict";
export function withinBudget(source, maxBytes) {
if (
typeof source !== "string" ||
!Number.isInteger(maxBytes) ||
maxBytes < 1 ||
maxBytes > 4096
)
return false;
return Buffer.byteLength(source, "utf8") <= maxBytes;
}
assert.equal(withinBudget("room 2", 16), true);
assert.equal(withinBudget("é".repeat(10), 16), false);
assert.equal(withinBudget("x".repeat(16), 16), true);
assert.equal(withinBudget("x".repeat(17), 16), false);
for (const max of [0, -1, 1.5, Infinity, 4097])
assert.equal(withinBudget("x", max), false);
console.log("UTF-8 byte budget fixtures passed");
Expected local program output
UTF-8 byte budget fixtures passed
Passing these assertions establishes only the documented local fixture behaviour. The role passed into a policy helper is assumed to come from a trusted authenticated caller; the helper does not authenticate a person. The fixture is not a complete production security boundary or a live model evaluation.