Level 2: AI layer

The mechanics

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.

Updated November 2026Example data, not from a client.
On this page
  1. Where the tokens go
  2. Context is sent every time
  3. Output: SQL or a request
  4. Retries resend everything
  5. Caching needs a stable prefix
  6. What I check

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

Where the tokens go: the cost mechanics of a governed route Two routes for one question, each with one retry assumed for illustration (retry rates were not measured), drawn to scale. Free-form SQL route: each attempt sends 10,100 tokens of schema DDL and gets 84 tokens of SQL back, and the retry sends the schema again. Governed route: each attempt sends 550 tokens of catalog plus tool schema and gets a 49-token request back; a bad request is rejected with a reason, and a retry sends the catalog again. Four mechanisms: context, 550 versus 10,100; output, 49 versus 84; retries re-send the context; caching works for a stable prefix above the provider minimum, 1,024 tokens on several models, and a 550-token catalog may fall below it. Output counts are for the guide 1 question. Counts measured with tiktoken o200k_base on made-up DDL with 840 columns and a catalog of 12 metrics and 15 dimensions. Billed counts vary by provider. System prompt and question not shown. Example data, not from a client. ONE QUESTION, ONE ASSUMED RETRY Input and output tokens, drawn to scale FREE-FORM SQL ROUTE Attempt 1 schema DDL in: 10,100 SQL out: 84 Retry: the schema goes again GOVERNED ROUTE Attempt 1 catalog + tool schema in: 550 · request out: 49 Retry after a rejection: the catalog goes again 550 in · 49 out again WHERE THE TOKENS GO 1 Context catalog instead of schema: 550 vs 10,100 2 Output structured request instead of SQL: 49 vs 84 3 Retries each retry re-sends the context 4 Caching a stable prefix can be cached once it passes the provider minimum (1,024 tokens on several models). 550 may fall below it. Measured with tiktoken o200k_base on made-up DDL (840 columns; 12 metrics, 15 dims). Billed counts vary by provider. System prompt and question not shown. Output counts are for the guide 1 question. One retry per route is assumed for illustration, not measured. Example data, not from a client. Measure yours.
Same question on both routes, with one retry assumed on each for illustration, drawn to scale from counts measured on made-up DDL. It shows the mechanism, not a percentage. Example data, not from a client. Open full size

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:

Context tokens, measured
What was sentTokens
Raw DDL for a made-up 40-table warehouse (840 columns)10,100
Catalog of 12 metrics and 15 dimensions, plus one tool schema550
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.

Output tokens, measured
What the model writesTokens
Free-form SQL for the reference question84
Governed tool request for the same question49

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

  1. Is the context a catalog of published metrics, not the raw schema?
  2. Do answers come from tool requests, not fresh SQL?
  3. Do rejected requests return short reasons instead of triggering full retries?
  4. Is stable context first, and are cache usage fields logged?

Measure your own prompts. Then decide.

About to put AI on a Power BI model?

I review one model, fixed scope, and tell you what to fix first.

See the model review

Privacy

Related