|
| 1 | +--- |
| 2 | +title: AI context |
| 3 | +description: Optimize your data model for AI by using descriptions and the meta ai_context property to provide additional context. |
| 4 | +--- |
| 5 | + |
| 6 | +When using [Analytics Chat][ref-analytics-chat] or other AI-powered features, |
| 7 | +the AI agent relies on your data model to understand your data. You can |
| 8 | +optimize your data model to help the AI generate more accurate queries and |
| 9 | +provide better insights. |
| 10 | + |
| 11 | +There are two ways to provide additional context to the AI: |
| 12 | + |
| 13 | +- **Descriptions** — visible to both end users and the AI agent. |
| 14 | +- **AI context via `meta`** — only visible to the AI agent, not exposed in the |
| 15 | + user interface. |
| 16 | + |
| 17 | +## Using descriptions |
| 18 | + |
| 19 | +The [`description`][ref-cube-description] parameter on cubes, views, measures, |
| 20 | +dimensions, and segments provides human-readable context that is displayed in |
| 21 | +the UI and also consumed by the AI agent. |
| 22 | + |
| 23 | +Use descriptions to clarify the meaning of a member for both your team and |
| 24 | +end users: |
| 25 | + |
| 26 | +<CodeGroup> |
| 27 | + |
| 28 | +```yaml title="YAML" |
| 29 | +cubes: |
| 30 | + - name: orders |
| 31 | + sql_table: orders |
| 32 | + description: All orders including pending, shipped, and completed |
| 33 | + |
| 34 | + measures: |
| 35 | + - name: total_revenue |
| 36 | + sql: amount |
| 37 | + type: sum |
| 38 | + description: Total revenue from completed orders only |
| 39 | + filters: |
| 40 | + - sql: "{CUBE}.status = 'completed'" |
| 41 | + |
| 42 | + dimensions: |
| 43 | + - name: status |
| 44 | + sql: status |
| 45 | + type: string |
| 46 | + description: "Current order status: pending, shipped, or completed" |
| 47 | +``` |
| 48 | +
|
| 49 | +```javascript title="JavaScript" |
| 50 | +cube(`orders`, { |
| 51 | + sql_table: `orders`, |
| 52 | + description: `All orders including pending, shipped, and completed`, |
| 53 | + |
| 54 | + measures: { |
| 55 | + total_revenue: { |
| 56 | + sql: `amount`, |
| 57 | + type: `sum`, |
| 58 | + description: `Total revenue from completed orders only`, |
| 59 | + filters: [{ sql: `${CUBE}.status = 'completed'` }] |
| 60 | + } |
| 61 | + }, |
| 62 | + |
| 63 | + dimensions: { |
| 64 | + status: { |
| 65 | + sql: `status`, |
| 66 | + type: `string`, |
| 67 | + description: `Current order status: pending, shipped, or completed` |
| 68 | + } |
| 69 | + } |
| 70 | +}) |
| 71 | +``` |
| 72 | + |
| 73 | +</CodeGroup> |
| 74 | + |
| 75 | +Descriptions are a good starting point because they serve double duty — they |
| 76 | +help end users understand the data and also give the AI agent context for |
| 77 | +query generation. |
| 78 | + |
| 79 | +## Using AI context |
| 80 | + |
| 81 | +If you want to provide context to the AI agent **without exposing it in the |
| 82 | +user interface**, use the `ai_context` key inside the |
| 83 | +[`meta`][ref-cube-meta] parameter. The `meta` parameter accepts custom |
| 84 | +metadata on cubes, views, measures, and dimensions. |
| 85 | + |
| 86 | +<CodeGroup> |
| 87 | + |
| 88 | +```yaml title="YAML" |
| 89 | +cubes: |
| 90 | + - name: orders |
| 91 | + sql_table: orders |
| 92 | + description: All orders |
| 93 | + meta: |
| 94 | + ai_context: > |
| 95 | + This cube contains all e-commerce orders. When users ask about |
| 96 | + revenue, prefer the total_revenue measure which filters for |
| 97 | + completed orders only. The amount column includes tax. |
| 98 | +
|
| 99 | + measures: |
| 100 | + - name: total_revenue |
| 101 | + sql: amount |
| 102 | + type: sum |
| 103 | + filters: |
| 104 | + - sql: "{CUBE}.status = 'completed'" |
| 105 | + meta: |
| 106 | + ai_context: > |
| 107 | + Use this measure for any revenue-related questions. |
| 108 | + This excludes pending and cancelled orders. |
| 109 | +
|
| 110 | + dimensions: |
| 111 | + - name: created_at |
| 112 | + sql: created_at |
| 113 | + type: time |
| 114 | + meta: |
| 115 | + ai_context: > |
| 116 | + This is the order creation timestamp in UTC. |
| 117 | + For fiscal year reporting, use the completed_at |
| 118 | + dimension instead. |
| 119 | +``` |
| 120 | +
|
| 121 | +```javascript title="JavaScript" |
| 122 | +cube(`orders`, { |
| 123 | + sql_table: `orders`, |
| 124 | + description: `All orders`, |
| 125 | + meta: { |
| 126 | + ai_context: `This cube contains all e-commerce orders. When users ask |
| 127 | + about revenue, prefer the total_revenue measure which filters for |
| 128 | + completed orders only. The amount column includes tax.` |
| 129 | + }, |
| 130 | + |
| 131 | + measures: { |
| 132 | + total_revenue: { |
| 133 | + sql: `amount`, |
| 134 | + type: `sum`, |
| 135 | + filters: [{ sql: `${CUBE}.status = 'completed'` }], |
| 136 | + meta: { |
| 137 | + ai_context: `Use this measure for any revenue-related questions. |
| 138 | + This excludes pending and cancelled orders.` |
| 139 | + } |
| 140 | + } |
| 141 | + }, |
| 142 | + |
| 143 | + dimensions: { |
| 144 | + created_at: { |
| 145 | + sql: `created_at`, |
| 146 | + type: `time`, |
| 147 | + meta: { |
| 148 | + ai_context: `This is the order creation timestamp in UTC. |
| 149 | + For fiscal year reporting, use the completed_at |
| 150 | + dimension instead.` |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | +}) |
| 155 | +``` |
| 156 | + |
| 157 | +</CodeGroup> |
| 158 | + |
| 159 | +You can also use `ai_context` on [views][ref-view-meta]: |
| 160 | + |
| 161 | +<CodeGroup> |
| 162 | + |
| 163 | +```yaml title="YAML" |
| 164 | +views: |
| 165 | + - name: revenue_overview |
| 166 | + description: Revenue metrics and breakdowns |
| 167 | + meta: |
| 168 | + ai_context: > |
| 169 | + This is the primary view for revenue analysis. It combines |
| 170 | + order and product data. Use this view when users ask about |
| 171 | + sales, revenue, or product performance. |
| 172 | +
|
| 173 | + cubes: |
| 174 | + - join_path: orders |
| 175 | + includes: |
| 176 | + - total_revenue |
| 177 | + - created_at |
| 178 | + - status |
| 179 | +``` |
| 180 | +
|
| 181 | +```javascript title="JavaScript" |
| 182 | +view(`revenue_overview`, { |
| 183 | + description: `Revenue metrics and breakdowns`, |
| 184 | + meta: { |
| 185 | + ai_context: `This is the primary view for revenue analysis. It combines |
| 186 | + order and product data. Use this view when users ask about |
| 187 | + sales, revenue, or product performance.` |
| 188 | + }, |
| 189 | + |
| 190 | + cubes: [ |
| 191 | + { |
| 192 | + join_path: orders, |
| 193 | + includes: [ |
| 194 | + `total_revenue`, |
| 195 | + `created_at`, |
| 196 | + `status` |
| 197 | + ] |
| 198 | + } |
| 199 | + ] |
| 200 | +}) |
| 201 | +``` |
| 202 | + |
| 203 | +</CodeGroup> |
| 204 | + |
| 205 | +## Descriptions vs. AI context |
| 206 | + |
| 207 | +| | `description` | `meta.ai_context` | |
| 208 | +| --- | --- | --- | |
| 209 | +| Visible in the UI | Yes | No | |
| 210 | +| Used by the AI agent | Yes | Yes | |
| 211 | +| Supported on | Cubes, views, measures, dimensions, segments | Cubes, views, measures, dimensions | |
| 212 | + |
| 213 | +Use `description` when the context is useful to both end users and the AI |
| 214 | +agent. Use `ai_context` when you want to provide additional instructions or |
| 215 | +context that is only relevant to the AI agent — for example, guidance on |
| 216 | +which measures to prefer, nuances about data quality, or business logic that |
| 217 | +would be confusing in a user-facing description. |
| 218 | + |
| 219 | +You can use both together. The AI agent reads both the `description` and |
| 220 | +`ai_context` when generating queries: |
| 221 | + |
| 222 | +<CodeGroup> |
| 223 | + |
| 224 | +```yaml title="YAML" |
| 225 | +cubes: |
| 226 | + - name: orders |
| 227 | + sql_table: orders |
| 228 | + description: All customer orders |
| 229 | + meta: |
| 230 | + ai_context: > |
| 231 | + When users ask about "sales", they usually mean completed |
| 232 | + orders only. Prefer total_revenue over raw amount sums. |
| 233 | +
|
| 234 | + measures: |
| 235 | + - name: total_revenue |
| 236 | + sql: amount |
| 237 | + type: sum |
| 238 | + description: Total revenue from completed orders |
| 239 | + filters: |
| 240 | + - sql: "{CUBE}.status = 'completed'" |
| 241 | + meta: |
| 242 | + ai_context: > |
| 243 | + This is the primary revenue metric. Always use this |
| 244 | + instead of summing the amount dimension directly. |
| 245 | +``` |
| 246 | +
|
| 247 | +```javascript title="JavaScript" |
| 248 | +cube(`orders`, { |
| 249 | + sql_table: `orders`, |
| 250 | + description: `All customer orders`, |
| 251 | + meta: { |
| 252 | + ai_context: `When users ask about "sales", they usually mean completed |
| 253 | + orders only. Prefer total_revenue over raw amount sums.` |
| 254 | + }, |
| 255 | + |
| 256 | + measures: { |
| 257 | + total_revenue: { |
| 258 | + sql: `amount`, |
| 259 | + type: `sum`, |
| 260 | + description: `Total revenue from completed orders`, |
| 261 | + filters: [{ sql: `${CUBE}.status = 'completed'` }], |
| 262 | + meta: { |
| 263 | + ai_context: `This is the primary revenue metric. Always use this |
| 264 | + instead of summing the amount dimension directly.` |
| 265 | + } |
| 266 | + } |
| 267 | + } |
| 268 | +}) |
| 269 | +``` |
| 270 | + |
| 271 | +</CodeGroup> |
| 272 | + |
| 273 | +## Best practices |
| 274 | + |
| 275 | +- **Add descriptions to all public members.** Descriptions help both end users |
| 276 | + and the AI agent understand your data model. |
| 277 | +- **Use AI context for agent-specific guidance.** If you need to tell the AI |
| 278 | + agent which measure to prefer or how to interpret ambiguous terms, use |
| 279 | + `ai_context`. |
| 280 | +- **Be specific.** Vague context like "important metric" is less helpful than |
| 281 | + "use this measure when users ask about monthly recurring revenue." |
| 282 | +- **Document relationships.** Use AI context to explain how cubes relate to each |
| 283 | + other and which views to prefer for common questions. |
| 284 | +- **Keep it up to date.** As your data model evolves, update descriptions and AI |
| 285 | + context to reflect the current state. |
| 286 | + |
| 287 | +[ref-analytics-chat]: /docs/explore-analyze/analytics-chat |
| 288 | +[ref-cube-description]: /reference/data-modeling/cube#description |
| 289 | +[ref-cube-meta]: /reference/data-modeling/cube#meta |
| 290 | +[ref-view-meta]: /reference/data-modeling/view#meta |
0 commit comments