Level 2: AI layer
Where the tokens go: context, output, retries and caching.
Tokens aren't mostly the answer. They're the context you send on every call, the text the model writes back, and everything resent on a retry. Here's how each part works, with counts I measured on example data.
On this page
A token is the unit a language model reads and writes, usually a word or part of a word. People tend to picture tokens as the length of the answer. In data questions, most of them are elsewhere. This piece walks through the mechanics, with counts I measured myself on made-up schemas. They're token counts, not bills, and they come from one tokenizer.
Where the tokens go
Every call to a model carries four things: the instructions, the context (schema, catalog, tool definitions), the conversation so far, and the question. The model sends back its output. If something fails and the call is repeated, all of it goes again. Those are the places to look.
Diagram · Where the tokens go: the cost mechanics of a governed route
Context is sent every time
A model doesn't remember your schema between calls. Whatever it needs goes into each request. So the size of the context, multiplied by the number of calls, is where tokens pile up. I counted two versions with tiktoken, OpenAI's open-source tokenizer, using its o200k_base encoding:
| What was sent | Tokens |
|---|---|
| Raw DDL for a made-up 40-table warehouse (840 columns) | 10,100 |
| Catalog of 12 metrics and 15 dimensions, plus one tool schema | 550 |
| The four example tables as DDL (part 4) | 68 |
| The example catalog (part 4) | 119 |
Counted with tiktoken (o200k_base) on made-up schemas. Other tokenizers count differently, and providers add their own overhead. Example data, not from a client.
Raw DDL grows with every column, whether a question needs it or not. A catalog grows with the metrics and dimensions you decide to publish. At warehouse scale that's the gap between the first two rows. On the tiny example in part 4, the catalog is actually larger than the DDL, because it carries rules the DDL can't. Size isn't the point there. Meaning is.
Output: SQL or a request
On the output side, the model either writes SQL or fills in a tool request. Here's the SQL I measured for the reference question:
WITH rev AS ( SELECT customer, product, SUM(amount) AS april_revenue FROM revenue WHERE reporting_date >= '2025-04-01' AND reporting_date < '2025-05-01' GROUP BY customer, product ) SELECT customer, product, april_revenue FROM rev WHERE customer = 'C-001' AND product = 'P-01';
I've wrapped two long lines so it fits on a phone. The count below is for the version I measured, before the wrapping. Run on the example, it returns C-001, P-01, 100.00. Correct, this time. The tool request for the same question:
{
"operation": "recognized_revenue",
"customer_id": "C-001",
"product_code": "P-01",
"date_basis": "reporting",
"period_start": "2025-04-01",
"period_end_exclusive": "2025-05-01"
}
It's shown here with line breaks so it fits on a phone. The count below is for the one-line version I measured. It passes the schema from part 6, and the tool returns COMPLETE, 100.00.
| What the model writes | Tokens |
|---|---|
| Free-form SQL for the reference question | 84 |
| Governed tool request for the same question | 49 |
Counted with tiktoken (o200k_base). Both return 100.00 on the example. Example data, not from a client.
The request is a little shorter, but length isn't the main difference. The SQL is a fresh program each time, which someone has to read to trust. The request is a few choices from a fixed list, checked before anything runs.
Retries resend everything
When generated SQL fails, the usual fix is to send the error back and ask again. That second call carries the full context again, plus the failed query and the error message. A third try carries all of that once more. Each retry repeats the whole input, not just the change.
A governed tool fails differently. A rejected request comes back as a short reason, such as “period_start is a required property,” before any query runs. The model fixes one field. And an answer with an incomplete status isn't a failure to retry. It's the answer.
Caching needs a stable prefix
Model providers can reuse the processing for the start of a prompt they've seen recently. The rules are strict. Anthropic's docs say cache hits require 100 percent identical prompt segments, and set a minimum length from 512 to 4,096 tokens depending on the model. OpenAI's docs set the minimum at 1,024 tokens for GPT-5.6 and later, with earlier models varying by request settings.
Two practical points follow. Put the parts that don't change first: instructions, catalog, tool schemas. Put the question last. A timestamp or user name at the top changes the prefix and ends the match. And a compact catalog can be too short to cache at all: the 550-token catalog above is under OpenAI's minimum. That's fine. Caching helps big, stable context. It doesn't make a big context necessary.
Don't guess whether caching worked. The usage data on each response tells you: cached_tokens in OpenAI's input token details, and cache_creation_input_tokens and cache_read_input_tokens in Anthropic's. Log them with the receipts from part 7.
One thing that isn't token caching: dbt Semantic Layer caching (on Enterprise plans) caches query results on the data platform side to speed up queries and reduce compute. It doesn't change what the model reads.
What I check in token use
- Is the context a catalog of published metrics, not the raw schema?
- Do answers come from tool requests, not fresh SQL?
- Do rejected requests return short reasons instead of triggering full retries?
- Is stable context first, and are cache usage fields logged?
Measure your own prompts. Then decide.
Related
- All 8 parts of this seriesSeries overview
- All the diagrams in one placeDiagrams
- Level 1: Model the data (7 guides)Read level 1
- The 4-question checklistChecklist