# Warehouses & BI

Read request logs from BigQuery or Snowflake, write the daily ledger to six warehouse and bucket destinations, and push actuals to planning tools.

AI Spendbook documentation, Your app's data. Web page: https://www.aispendbook.com/docs/warehouses

Warehouses work in both directions: AI Spendbook can read request logs from your warehouse, and write its reconciled daily ledger into one. Nothing here has been run against a live vendor account; each source and destination was built from the vendor's public API documentation and tested with recorded responses.

## Request logs from your warehouse

| Connector | How data arrives | What it does |
| --- | --- | --- |
| [BigQuery (request log query)](https://www.aispendbook.com/integrations/bigquery-query) | Direct read with a service account | Rows returned by your SQL, filtered with @window_start and @window_end. Dry run to check it. |
| [Snowflake (request log query)](https://www.aispendbook.com/integrations/snowflake-query) | Direct read with a programmatic access token | Rows returned by your SQL with two ? placeholders (window start and end). |

Write one SELECT that returns one row per AI request, then map its columns to timestamp, id, model, tokens, customer, feature, cost and status. The rows become request telemetry priced by us, never a bill.

```sql
SELECT request_id, created_at, model, input_tokens, output_tokens, customer_id, feature
FROM `my-proj.app_logs.ai_requests`
WHERE created_at >= @window_start AND created_at < @window_end
```

## The daily ledger in your warehouse

Admins add a destination under Settings, Destinations. Each row is one UTC day, provider, ledger kind (`billed`, `estimated` or `allocated`), basis, customer, feature, category and accounting class, with `cost_micros` and `cost_usd`. To get AI spend for a period, sum the `allocated` rows only; never add rows of different kinds.

| Destination | How it writes |
| --- | --- |
| BigQuery | Load job into a table partitioned by day; each day overwrites its partition. |
| Snowflake | SQL API: delete the day, then insert its rows. |
| Databricks | SQL Statement Execution API: delete the day, then insert its rows. |
| Amazon Redshift | Data API: delete and insert in one transaction per day. |
| Amazon S3 | A CSV file per day under day=YYYY-MM-DD, overwritten each run. |
| Google Cloud Storage | A CSV file per day under day=YYYY-MM-DD, overwritten each run. |

- Once a day the last 7 complete UTC days are rewritten in every enabled warehouse or bucket, so a late bill reaches the warehouse. Days are replaced, never appended.
- “Export last 30 days” runs a manual backfill. A failed run shows its error on the destination and does not stop the others.
- CSV only for S3 and Cloud Storage (no Parquet). On Snowflake and Databricks a run stopped after the delete leaves that day empty until the next run.
- Customer ids are as sent in telemetry; customer mappings are not applied in the ledger.

```sql
SELECT DATE_TRUNC(day, MONTH) AS month, customer_id, SUM(cost_micros) / 1e6 AS ai_spend_usd
FROM `my-proj.finance.ai_spend_ledger`
WHERE ledger_kind = 'allocated'
GROUP BY 1, 2
ORDER BY 1, 3 DESC
```

## BI and planning tools

- Looker, Tableau, Power BI and Metabase read the warehouse table (default name `ai_spend_ledger`). There is no separate connector for them.
- Pigment and Anaplan are destinations: an admin clicks “Send closed actuals” to push closed monthly actuals and the official forecast (the baseline forecast when none is set; the Version column says which), and the plan can be read back to import as phased budgets. Pushes are not scheduled. The forecast sent is the workspace total.
- Adaptive Planning, Cube, Datarails and Causal get a file laid out for their import; Fathom, Jirav, Reach Reporting and Mosaic get one client's closed actuals as a file. These are downloads, not pushes. See [exports and spreadsheet feeds](https://www.aispendbook.com/docs/exports-and-feeds).
