Building a Spending Baseline
Introduction: From One Cost to a Spending Baseline
Welcome to the final unit of Measuring and Understanding LLM Costs in Python! We have come a long way: we started by reading token categories from a real response, then learned to estimate cost before spending anything, and in Unit 3, we packaged all of that billing knowledge into a reusable cost layer.
That cost layer gives us a reliable cost value on every Completion. There is one problem, though: printing that number to the console makes it vanish the moment the program exits. If we want to prove that a later optimization actually saved money, we need something durable to compare against. So, in this unit, we will build a small measurement pipeline.
Throughout this unit, words like "spend" and "cost" refer to calculated token cost under the configured rates, not an actual billed amount from an invoice.
Choosing JSONL as the Log Format
Before writing any code, let us pick a file format. We will use JSONL (JSON Lines): one complete JSON object per line, with one line per request. Appending a line is cheap, a crash in the middle of a run cannot corrupt the lines already written, and a reader can process the file one line at a time instead of loading everything as a single document.
A single line looks like this:
Notice that each record carries the full token breakdown, not just a dollar amount, so we can revisit any past request in detail. We define the file location exactly once:
Turning a Completion into a Log Record
Now we convert a Completion into a record. Because Completion is a dataclass, we do not have to list its fields by hand.
Two moves matter here:
asdict(result)turns the dataclass into a plain dictionary, and**unpacks those keys into our record. If we later add a field toCompletion, it flows into the log automatically.datetime.now(timezone.utc).isoformat()stamps the record with an unambiguous UTC time, which lets us slice spend by hour, day, or deployment later on.
Timestamp first, then the completion fields: a small readability choice that makes raw lines easier to scan.
