Level 2: AI layer

All the diagrams

AI layer: the diagrams

Semantic layers, ontologies and AI that stays on the rails. The diagrams from all 8 parts, drawn on the same made-up example as guides 1-7.

Updated November 2026Example data, not from a client.
On this page
  1. 01 · Meaning before the model
  2. 01b · Signed ontology
  3. 02 · Grain, keys and allowed joins
  4. 02b · Join rules by key
  5. 03 · Metric contracts as code
  6. 04 · The semantic catalog
  7. 05 · Route the question
  8. 06 · Tools and guardrails
  9. 07 · Evals and receipts
  10. 07b · Eval set and receipt
  11. 08 · Where the tokens go

These are the diagrams from the 8 parts of Level 2: AI layer, one or two per part. Every number on them comes from the guide 1 example or from token counts measured on made-up DDL. None of them is a promise about your system: measure your own.

01 · Meaning before the model

Build the ontology from evidence

Diagram 1 · Meaning before the model

Meaning before the model: build the ontology from evidence Five evidence sources (SQL and views, measure formulas, workbook calculations, reports people check, incident notes) are read first. They feed draft concept cards, each tagged FACT: Revenue entry means one recognized revenue occurrence and is not a contract term; Contract term means one recorded contract term and is not revenue. Each card names its evidence. An owner review either rejects a card with no evidence, sending it back to draft, or signs it. Example data, not from a client. 1 · READ THE EVIDENCE FIRST SQL / views Measure formulas Workbook calcs Reports people check Incident notes what you can point to read first 2 · DRAFT CONCEPT CARDS Revenue entry FACT Means one recognized revenue occurrence Is not a contract term Evidence revenue view SQL Contract term FACT Means one recorded contract term Is not revenue Evidence terms table, renewals report owner review Rejected: no evidence goes back to draft Signed by the owner No source, no card. Example data, not from a client.
Read the evidence, draft short concept cards with an “is not” line, and let the owner sign each one. No source, no card. Open full size

01b · Signed ontology

What the owner approves

Diagram 1b · Signed ontology

Signed ontology: what the owner approves The signed ontology for the example: entities Customer and Product; facts Revenue entry (an event) and Contract term (a record); the relationship that each revenue entry is for one customer and one product; the rule that a term amount is never revenue; and a named person who signs each card. Every card links to its source. Example data, not from a client. 3 · SIGNED ONTOLOGY Entities Customer, Product Facts Revenue entry (an event), Contract term (a record) Relationship each revenue entry is for one customer and one product Rule a term amount is never revenue Owner named person signs each card Every card links to its source. Example data, not from a client.
What the owner signs: the entities, the facts, how they relate, the rules, and a named owner for each card. Open full size

02 · Grain, keys and allowed joins

Rules the semantic layer enforces

Diagram 2 · Grain, keys and allowed joins

Grain, keys and allowed joins: rules the semantic layer enforces Wrong: Revenue and Terms, both fact tables with PK id and FK customer and product, joined to each other FK to FK on customer and product, many to many, so April's 100.00 becomes 200.00. Right: the dimension tables Customer (PK customer) and Product (PK product) each relate exactly one to zero or many to both Revenue and Terms. A dashed line between Revenue and Terms is crossed out and labeled refused. Fact tables have dark headers tagged FACT and dimension tables have green headers tagged DIM. A legend shows the line-end marks: exactly one, zero or many, and one or many. Example data, not from a client. WRONG · JOINED BEFORE THE SUM Revenue FACT PK id FK customer FK product Terms FACT PK id FK customer FK product FK to FK on customer + product. Rows multiply: April's 100.00 becomes 200.00. RIGHT · TYPED KEYS, ENFORCED JOINS Customer DIM PK customer Revenue FACT PK id FK customer FK product Terms FACT PK id FK customer FK product Product DIM PK product refused exactly one zero or many one or many
Declare each key as PK or FK and the engine walks only allowed joins, so the fact-to-fact join that doubled April is refused instead of documented. Open full size

02b · Join rules by key

What the engine allows

Diagram 2b · Join rules by key

Join rules by key: what the engine allows Grain: Revenue has one row per revenue entry, keyed by id; Terms has one row per contract term, keyed by id. Join rules applied by dbt MetricFlow, by key type: FK to PK allowed; PK to PK allowed; PK to FK refused as fan-out; FK to FK refused as fan-out. A join that lands on an FK could fan out. Example data, not from a client. GRAIN, ONE LINE PER TABLE Revenue: one row per revenue entry (id) Terms: one row per contract term (id) JOIN RULES BY KEY (DBT METRICFLOW) FK → PK allowed PK → PK allowed PK → FK refused: fan-out FK → FK refused: fan-out A join that lands on an FK could fan out.
One line of grain per table, and the join rules the engine applies. The two allowed joins land on a PK. A join that lands on an FK could fan out, so it's refused. Open full size

03 · Metric contracts as code

One definition, many consumers

Diagram 3 · Metric contracts as code

Metric contracts as code: one definition, many consumers A contract box for Net Revenue by reporting date, version 1.0: the definition is SUM of amount over Revenue, the date rule is reporting_date, a period is required with an exclusive end date, Terms amount is never summed as revenue, three empty-case statuses, and the reference answer April 2025 equals 100.00. Arrows fan out to four consumers: a report on a certified model, a Pulse metric or subscription, Copilot or a data agent, and a notebook or API. Each gets the same value and the same status. Three status chips: COMPLETE with 100.00 for April 2025; NO_RECORDS_IN_PERIOD for July 2025, which has no rows; INCOMPLETE_UNKNOWN_PERIOD, withheld when a row has no date. Net Revenue (reporting date) · contract v1.0 Definition SUM(amount) over Revenue Date rule reporting_date Period required, end date exclusive Never sum Terms amount as revenue Empty cases three statuses, below Reference April 2025 = 100.00 same value + same status Report certified model Pulse / subscription metric definition Copilot / data agent reads the measure Notebook / API semantic layer API ANSWER STATUS TRAVELS WITH THE VALUE COMPLETE April 2025 100.00 NO_RECORDS_IN_PERIOD July 2025 no rows INCOMPLETE_UNKNOWN_PERIOD a row has no date withheld Example data, not from a client.
The contract lives in one governed place. Every consumer gets the same value and the same answer status, so a tool can say “withheld” instead of guessing. Open full size

04 · The semantic catalog

What the AI should see instead of your schema

Diagram 4 · The semantic catalog

The semantic catalog: what the AI should see instead of your schema Top: raw DDL for a made-up warehouse, with column lines such as tbl_017.amount_rate_4 DECIMAL(18,4), 840 columns in 40 tables, measured at 10,100 tokens. A funnel labeled names, descriptions, hidden fields and AI data schema narrows it to a catalog: metrics such as net_revenue on reporting_date and net_revenue_by_booking on accounting_date, 10 more metrics, 15 dimensions and two rules (a period is required; term amount is not revenue). The 12 metrics plus the tool schema measured 550 tokens. A to-scale bar compares 10,100 and 550. Counts measured with tiktoken o200k_base on made-up DDL. Example data, not from a client. WHAT THE AI SEES: RAW DDL tbl_017.amount_rate_4 DECIMAL(18,4) tbl_017.dt1 DATE tbl_017.flag_2 CHAR(1) tbl_018.amt DECIMAL(18,2) tbl_018.cust_ref VARCHAR(20) … 835 more columns, 40 tables 840 columns 10,100 tokens names · descriptions hidden fields · AI data schema WHAT THE AI SEES: CATALOG net_revenue reporting_date net_revenue_by_booking accounting_date … 10 more metrics dims: customer, product, month … (15) rule: a period is required rule: term amount is not revenue 12 metrics + tool schema 550 tokens TO SCALE 10,100 550 Measured with tiktoken (o200k_base) on made-up DDL. Your schema will differ. Example data, not from a client.
Raw DDL grows with every column. A catalog of governed metrics stays short. Token counts measured on made-up DDL; measure your own. Open full size

05 · Route the question

Governed metric first, curated query second, free-form SQL last

Diagram 5 · Route the question

Route the question: governed metric first, curated query second, free-form SQL last A question, April 2025 net revenue for C-001, enters a ladder of four checks. Matches a verified answer: yes goes to Tier 0, a reviewed answer set in advance. Fits a catalog metric plus dimensions plus a time window: yes goes to Tier 1, a semantic query where the engine writes the SQL or DAX. Matches a curated query template: yes goes to Tier 2, reviewed SQL with parameters. Exploration allowed for this user and data: yes goes to Tier 3, free-form SQL that is sandboxed (read-only, row-capped) and not certified. Otherwise Tier 4 declines and logs a modeling gap. Every exit writes a receipt with tier, metric, parameters, row count and status. Tiers 0 to 2 run reviewed logic once the request is set; picking the tier and the metric is still the model's call. THE QUESTION “April 2025 net revenue for C-001?” Matches a verified answer? yes Tier 0 · Verified answer reviewed in advance no Catalog metric + dims + window? yes Tier 1 · Semantic query engine writes SQL or DAX no Matches a curated query template? yes Tier 2 · Curated query reviewed SQL, parameters no Exploration allowed for user + data? yes Tier 3 · Free-form SQL sandboxed, not certified no Tier 4 · Decline log it as a modeling gap Every exit writes a receipt tier · metric · parameters row count · status Tiers 0 to 2 run reviewed logic once the request is set. Picking the tier and the metric is still the model's call.
Each question takes the first tier that can answer it. Tiers 0 to 2 run reviewed logic, so the same request gets the same answer; free-form SQL is read-only, row-capped and labeled exploratory. Open full size

06 · Tools and guardrails

Make wrong requests impossible to express

Diagram 6 · Tools and guardrails

Tools and guardrails: make wrong requests impossible to express A request with metric, dimensions, window and date basis passes five gates. Gate 1, schema check: metric and dimensions from an enum, a required window, no extra fields. Gate 2, business rules: period required, date basis named, term amount never summed. Gate 3, permissions: runs as the asking user with row-level security, read-only. Gate 4: execute via the engine, a MetricFlow query, DAX measure or parameterized SQL, where a validation error stops the run. Gate 5, status and limits: a row cap and a COMPLETE or WITHHELD status. Every gate can reject, which returns a reason and no number. The end is the answer plus a receipt. A tool input schema excerpt shows date_basis as an enum of reporting or accounting and additionalProperties false. THE REQUEST metric · dims · window · date basis 1 · Schema check enum metric and dims, required window, no extra fields pass 2 · Business rules period required, date basis named, term amount never summed pass 3 · Permissions runs as the asking user, row-level security, read-only pass 4 · Execute via engine MetricFlow, DAX or SQL; stops on a validation error pass 5 · Status + limits row cap, COMPLETE or WITHHELD status pass Rejected. Return the reason, no number. Answer + receipt value, status, request, row count Tool input schema (excerpt) "date_basis": { "enum": ["reporting", "accounting"] }, "additionalProperties": false A prompt can't talk its way past a rule in the tool. Example data, not from a client.
Five gates between the request and the answer. Anything that fails a gate gets a reason back, never a number. Open full size

07 · Evals and receipts

Test the AI route like you test the model

Diagram 7 · Evals and receipts

Evals and receipts: test the AI route like you test the model A loop of steps: an eval set of reference questions, traps and paraphrases; ask through the real consumer route, N runs each; compare to the expected value and expected status; a scorecard of correct, refused correctly, wrong and inconsistent. Step 5: on a miss, fix the model, catalog or router, never the test, then ask again through the real route. Run each question more than once. Example data, not from a client. 1 · Eval set reference · traps · paraphrases 2 · Ask through the real route same tool users use, N runs each 3 · Compare to expected value + expected status 4 · Scorecard correct · refused correctly · wrong · inconsistent 5 · On a miss, fix the model, catalog or router. Never the test. Run each question more than once. Example data, not from a client.
Ask the reference questions and the traps through the tool people actually use, more than once. On a miss, fix the model, never the test. Open full size

07b · Eval set and receipt

The template and the record

Diagram 7b · Eval set and receipt

Eval set and receipt: the template and the record An eval set template, marked no runs yet, lists each question with its expected result and blank runs, correct and wrong counts: April 2025 revenue, 100.00 COMPLETE; April by accounting date, -20.00 COMPLETE; revenue for all time, refused for no period; term amounts as revenue, refused as not revenue. A receipt per run records the question, tier used, request, value, status, model version and time. Counts stay blank until you run the set yourself. Example data, not from a client. Eval set template NO RUNS YET QUESTION · EXPECTED April 2025 revenue 100.00 · COMPLETE runs ___ correct ___ wrong ___ April, by accounting date -20.00 · COMPLETE runs ___ correct ___ wrong ___ Revenue for all time refused: no period runs ___ correct ___ wrong ___ Term amounts as revenue refused: not revenue runs ___ correct ___ wrong ___ Receipt, one per run question · tier used · request value · status model version · time Counts stay blank until you run the set yourself. Example data, not from a client.
The eval set is a template: the counts stay blank until you run it. Every run leaves a receipt. Open full size

08 · Where the tokens go

The cost mechanics of a governed route

Diagram 8 · Where the tokens go

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. Open full size

Check the grain first. Then route the question.