Token & Cost Accounting
A model call costs real money, and the bill is measured in tokens, not characters or words. A token is roughly a chunk of a few characters. The exact count comes from the model's tokenizer, but for budgeting and dashboards a good rule of thumb is tokens ≈ characters / 4. That estimate is close enough to forecast spend, set per-request limits, and spot a prompt that is about to blow your budget.
Providers price input (prompt) tokens and output (completion) tokens at different rates, usually quoted as dollars per 1,000,000 tokens. Output is often several times more expensive than input, so the split matters. The cost of one call is:
cost = (prompt_tokens / 1_000_000) * in_rate
+ (completion_tokens / 1_000_000) * out_rateIn a real app you would read the true token count straight off the response. For example, an on-device WebLLM engine hands it back with the reply:
const reply = await engine.chat.completions.create({ messages });
const used = reply.usage.completion_tokens; // exact count from the runtimeThat live call is illustrative only. Here you build the deterministic accounting layer that sits around any model: a token estimator and a cost function over a price table. These are the two functions every cost dashboard is made of.
Build two functions. estimate_tokens(text) returns an integer token estimate using the chars/4 rule (use integer division, so "" -> 0). estimate_cost(prompt_tokens, completion_tokens, in_rate, out_rate) returns the dollar cost using the formula above, where the rates are dollars per 1,000,000 tokens. Keep both pure and side-effect free.
Write estimate_tokens(text) that returns an integer token estimate (about len(text) // 4, so an empty string is 0) and estimate_cost(prompt_tokens, completion_tokens, in_rate, out_rate) that returns the dollar cost, where in_rate and out_rate are prices per 1,000,000 tokens. The longer of two texts must never estimate fewer tokens, and swapping a prompt-heavy call for a completion-heavy one must change the cost when the two rates differ.
This lesson is locked
Lessons open one at a time. Finish the previous lesson to unlock this one.