> For the complete documentation index, see [llms.txt](https://docs.espresso.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.espresso.ai/snowflake-optimizer/credit-attribution.md).

# Scheduler Credit Attribution

Espresso attributes Snowflake credits by preserving routing metadata in query text, reconstructing which original warehouse each query came from, then slicing billed cluster uptime across active and idle intervals.

## 1. Query comments carry routing context

Every proxied query gets one `Espresso Metadata` line comment. `old_warehouse` is the warehouse the user selected; `new_warehouse` is where Espresso ran it.

```sql
// Espresso Metadata: {"query_id": "01b4...", "old_warehouse": "ANALYTICS_WH", "new_warehouse": "_ESPRESSO_POOL_XS_1", "routing_enabled": true}
```

* Snowflake stores the comment in `QUERY_HISTORY.QUERY_TEXT`; attribution parses `old_warehouse` from it.
* When query text is censored for export, the metadata comment can still be retained.

## 2. Cluster uptime is split into query and idle slices

Snowflake bills for warehouse uptime, not just query runtime. For an interval where a cluster is up, Espresso builds boundaries from query starts, query ends, resize/routing state, and metering windows. Each slice is either active, with one or more running queries, or idle. A model allocates shared active slices across the running queries and allocates idle slices using surrounding warehouse activity and routing context.

<figure><picture><source srcset="/files/FWgxybCCzwv1xnAN9MVS" media="(prefers-color-scheme: dark)"><img src="/files/5rrwDkT3UeOqrTZiUfs4" alt="Overlapping queries on one running cluster with a trailing idle slice"></picture><figcaption><p>Idle spans are first-class slices; the model attributes them alongside active overlap spans instead of dropping them.</p></figcaption></figure>

The widths and ratios in the diagrams are illustrative; they show the accounting shape, not a literal allocation for a specific workload.

## 3. Slices roll up to attributed metering rows

After slicing, Espresso rolls each slice into the original warehouse namespace from the metadata comment and session/routing state. The output is `espresso.reporting.warehouse_metering_history`, shaped like Snowflake metering history so existing BI queries can read attributed credits.

<figure><picture><source srcset="/files/J6JUuIlGdfSsMuk7mEyl" media="(prefers-color-scheme: dark)"><img src="/files/ezTB7TAlR2lITNTchzu7" alt="Uptime slices rolled up into attributed warehouse rows"></picture><figcaption><p>The model allocates both shared active slices and idle slices into warehouse rows; attributed totals still match the metered total.</p></figcaption></figure>
