Token Accounting: A Usage Log That Saves Money

“Where do the money and the quotas leak?” — the question asked by everyone who doesn’t keep records. The usage field in every API response contains the exact consumption, but without a log it’s just numbers. We break down how to assemble the accounting in one evening and what it reveals within the first week.

What Is in Usage

Every response carries: input_tokens (input, context included), output_tokens (generation), cached tokens (the share of the input that came from the cache), reasoning tokens (if the model thinks). The sum of these fields is your real consumption. A measurement case from practice: on a 10-minute video, Codex burned 67.7 million tokens (99% cache), Claude wrote 213K, and DeepSeek — 102 million for $0.52 — without the usage fields these numbers are invisible.

A Minimal Log in One Evening

A wrapper over the SDK that writes a row to a CSV for every call: date, task, model, input, output, cached, reasoning, cost. Twenty lines of Python:

import csv, time

def ask_log(client, model, prompt, task, log="usage.csv"):
    t0 = time.time()
    r = client.chat.completions.create(
        model=model,
        messages=[{"role": "user", "content": prompt}],
    )
    u = r.usage
    with open(log, "a", newline="", encoding="utf-8") as f:
        w = csv.writer(f)
        w.writerow([time.strftime("%Y-%m-%d %H:%M"), task, model,
                    u.prompt_tokens, u.completion_tokens, time.time() - t0])
    return r.choices[0].message.content

Once a week — a summary table (a pivot by tasks and models): you see which task eats the quota, which model pays for itself, and where the cache isn’t working. For teams — the same log in a spreadsheet with a “project” column.

What the Log Reveals in the First Week

  • Silent eaters: one “harmless” classification with a giant context consumes more than the entire chat. Usually cured by feeding the context with precision.
  • Invisible reasoning: reasoning tokens aren’t visible in the text of the response but are paid for; disabling thinking on routine work is instant savings.
  • A dead cache: the share of cached tokens near zero on repeated requests — the context head is changing; session discipline.
  • Edits versus generation: the share of repeated requests per task shows the quality of the problem statement — many iterations mean a PRD problem, not a model problem.

Free-Tier Quotas: The Same Log

On free tiers, usage matters twice as much: quotas are measured in both requests and tokens. The log will show which provider of the pie gets eaten first and what to move to another layer. The distribution method is in the material about the limits pipeline; for team accounting — a summary table with internal-token coefficients (as at ModelRouter, where consumption is counted with multipliers).

Six Mistakes That Distort the Accounting

The log only works if you don’t distort it. The list below covers the mistakes that show up in the very first week and make the statistics useless or harmful: decisions get made on crooked numbers, and the problem appears to be where it isn’t. Every item is cured by procedure, not by money: an audit against the list takes one evening and requires no pipeline instrumentation.

  • Mixing test runs with production ones. One experimental request with a giant context skews the whole week: mark test rows with a separate task label so you can exclude them from the summary table.
  • Logging only the output. Generation is usually more expensive per token, but there’s more input: context, rules, files. Without the input, cached, and reasoning columns, half of the consumption stays invisible.
  • Calculating the cost by hand and rarely. Price tables are updated quarterly while the dashboard counter ticks constantly: the log’s divergence from reality accumulates unnoticed, and the final number drifts away. Automatic calculation in the log removes the problem entirely.
  • Not reconciling usage with the dashboard counter. At “gray” gateways the fields diverge from reality — a weekly reconciliation shows which providers can be trusted and which can’t.
  • A log without a “task” column. Without task labels you can’t build a pivot by tasks and models: you’ll see the total consumption but never find the silent quota eater.
  • Accounting without action. Every finding must turn into a project rule, a route change, or a plan change — otherwise the log becomes an observation diary, not a savings tool.

A separate trap is over-detailing: the attempt to log twenty fields for every sneeze adds routine work that you’ll abandon after three days. The columns from the wrapper above are enough to close most questions within the first week; you’ll always have time to extend the log when a concrete question appears. Accounting is a tool, not an end in itself: if no pipeline changes appeared within a week, look for the cause not in the log but in the decision-making process. The habit of keeping the log next to the calendar of plans closes the rest: the subscription charge date and the quota reset date are visible on the same line, so routes get revised based on numbers, not memory. That’s how accounting becomes part of the routine rather than a separate project.

Frequently Asked Questions

Are the usage fields accurate? At major providers — yes; at “gray” gateways they sometimes diverge from reality — periodic reconciliation against the dashboard counter.

Should you log every request? Yes, the log is cheap; the value is in completeness. Separate test runs with a marker so they don’t pollute the statistics.

How do you calculate the cost? A table of prices per 1M input/output tokens per model — updated quarterly; the calculation lives in the log as separate columns. For subscriptions — convert the windows into conditional “tasks” based on a diary.

What to do with the findings? Every log finding is either a project rule (discipline), or a pipeline change (a route), or a plan change. Accounting without action is useless.

Will the log slow down the pipeline? Writing a row to a CSV takes milliseconds on top of a request that takes seconds: the overhead is incomparable to token consumption. Keep the writes synchronous: a lost row is a lost number in the summary.

What if a gateway doesn’t return usage? Log at least the length of the prompt and of the response, and reconcile against the dashboard counter: an approximate estimate is better than a blind spot. Mark such rows to tell exact numbers from estimates.

How often should you open the summary table? Once a week is enough: a pivot by tasks and models over seven days shows both the silent eaters and the dead cache. Daily review is needed only in the first week after the log goes live, until the spending profile settles.

Do all models produce reasoning tokens? No: only reasoning models do, and they’re invisible in the text of the response — they’re billed as a separate line. On routine work, disabling thinking brings instant savings without losing the quality of mechanical answers.

The token log is the instrument panel of AI work: assemble it in one evening, and the very first week will show where the money leaks. The method is as of autumn 2026.

Like this post? Please share to your friends:
11AI - Your Free LLM
Leave a Reply

;-) :| :x :twisted: :smile: :shock: :sad: :roll: :razz: :oops: :o :mrgreen: :lol: :idea: :grin: :evil: :cry: :cool: :arrow: :???: :?: :!: