Level 2: AI layer
Tools and guardrails: make wrong requests impossible to express.
A rule written in a prompt can be ignored. A rule built into the tool can't be skipped. Give the model a tool that only accepts valid requests, and have it return a reason instead of a number when it says no.
On this page
Every guardrail in this series so far could be written as a sentence: always use a period, never sum term amounts as revenue, never join the two facts. Put those sentences in a prompt and the model will usually follow them. Usually isn't good enough for a number someone will repeat in a meeting.
A prompt is a request, not a rule
The reliable place for a rule is the tool the model calls. If the tool's input can't express a wrong request, the model can't make one, however the question was phrased. And when the tool says no, it should say why, with no number attached, so there's nothing plausible to repeat.
Diagram · Tools and guardrails: make wrong requests impossible to express
Gate 1: the input schema
Tools for AI models describe their input as JSON Schema. In the Model Context Protocol, for example, every tool carries an inputSchema. That schema is your first gate. Here's the one for the example's revenue tool:
{
"type": "object",
"additionalProperties": false,
"required": [
"operation",
"customer_id",
"product_code",
"date_basis",
"period_start",
"period_end_exclusive"
],
"properties": {
"operation": {
"enum": [
"recognized_revenue"
]
},
"customer_id": {
"type": "string",
"pattern": "^C-[0-9]{3}$"
},
"product_code": {
"type": "string",
"pattern": "^P-[0-9]{2}$"
},
"date_basis": {
"enum": [
"reporting",
"accounting"
]
},
"period_start": {
"type": "string",
"format": "date"
},
"period_end_exclusive": {
"type": "string",
"format": "date"
}
}
}
Every line closes a door. The operation is an enum, so “sum term amounts” isn't a value the model can send. The date basis is required and limited to two values, so the model has to choose. Both period fields are required, so “all time” can't be asked. And additionalProperties: false means a stray field, such as a raw SQL string, is rejected instead of quietly ignored.
One catch: in current JSON Schema, “format” is an annotation unless the validator is set to check it. With the Python validator I used, 2025-02-30 passed as a date until I switched format checking on. Check dates in code as well.
Gate 2: business rules in code
Some rules don't fit in a schema. A period whose start comes after its end is valid JSON. So is a date basis that's allowed for one metric and not another. Those checks go in code that runs before the query, and each one returns a specific reason: “period_start must be before period_end_exclusive,” not “invalid request.”
Gate 3: run as the person asking
The tool should query as the user who asked, so their row-level security applies, and over a read-only connection. Two details catch people out. In Power BI, row-level security applies to workspace Viewers, but not to Admins, Members or Contributors, who can edit the model. Test as a Viewer. And in dbt, Semantic Layer caching (on Enterprise plans) stores cached data separately from the models, and the docs say that when metrics come from the cache, the security context isn't applied to those tables at query time. Don't serve row-restricted metrics from the cache.
Fabric data agents show what built-in versions of this gate look like. The docs say they use the requesting user's credentials, keep read-only connections, keep RLS and column-level security on semantic models, and cap chat responses at 25 rows and 25 columns.
Gates 4 and 5: engine and limits
Once a request passes, the engine writes and runs the query: MetricFlow, a DAX measure, or a parameterized query. Engines validate too. MetricFlow runs explain queries to check that the generated SQL runs and to catch ambiguous join paths. A validation error stops the run and comes back as a reason.
The last gate shapes the answer: a row cap, and the status from part 3. A value only goes out with COMPLETE. Anything else goes out as a status with no number.
What came back
I built a small reference version of this tool on the example data and sent it twelve requests:
| Request | What came back |
|---|---|
| April 2025, reporting date | COMPLETE, 100.00 |
| April 2025, accounting date | COMPLETE, -20.00 |
| July 2025 | NO_RECORDS_IN_PERIOD, no value |
| Unknown customer C-999 | NO_MATCHED_RECORDS, no value |
| A row with no date | INCOMPLETE_UNKNOWN_PERIOD, no value |
| A row with no amount | INCOMPLETE_AMOUNT, no value |
| All time (no period) | Rejected: 'period_start' is a required property |
| Sum term amounts as revenue | Rejected: 'sum_term_amounts' is not one of ['recognized_revenue'] |
| Extra field with raw SQL | Rejected: Additional properties are not allowed ('sql' was unexpected) |
| No date basis | Rejected: 'date_basis' is a required property |
| Reversed period | Rejected: period_start must be before period_end_exclusive |
| Not a real date | Rejected: '2025-02-30' is not a 'date' |
Each request was run through a small reference version of this tool on the example data. Example data, not from a client.
Six requests got an answer status, and only the two complete ones carried a number. Six were rejected before any query ran, each with a reason the model can pass on or act on. None of the rejections depended on the model behaving well. They depended on the tool.
The rejections that matter most are the traps from guide 5 and guide 7. “Revenue for all time” fails because a period is required. “Sum the term amounts as revenue” fails because there's no such operation. Those used to be warnings in a document.
What I check in a governed tool
- Does the input schema use enums from the catalog, required periods and additionalProperties: false?
- Are business rules and dates checked in code, each with a specific reason?
- Does every query run as the asking user, read-only, tested as a Viewer?
- Does a rejection or incomplete answer return a reason and no number?
Don't ask the model to follow the rules. Build them into the tool.
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