The agent first asks what to optimize for: the time period, the metric (execution time, bytes scanned, cost or spillage), and whether to look at one warehouse or user or at all of them. It then queries `QUERY_ATTRIBUTION_HISTORY` for credit and cost analysis and `QUERY_HISTORY` for performance stats on specific queries, running the two separately rather than joining them.
Pattern checks look for queries with high compute credits, a repeated `query_hash` that points to a caching opportunity, scans where partitions scanned equals partitions total so nothing was pruned, and high gigabytes spilled under memory pressure. The answer is a ranked list of queries with key metrics, the common patterns, three to five optimization recommendations and specific queries to investigate next. A time range filter is always applied.