---
title: "Cap what each query and each month can · Agent Wranglers"
url: https://agentwranglers.se/playbook/data-engineering/warehouse-and-models/query-cost-limits
id: DEN-6
level: Recommended
updated: 2026-10-22
tokens: 798
licence: "CC BY 4.0"
licence_url: https://creativecommons.org/licenses/by/4.0/
---

# Cap what each query and each month can cost

For AI agents: the [site map](https://agentwranglers.se/llms.txt) lists everything here, and this page sits under [Warehouse and models](https://agentwranglers.se/playbook/data-engineering/warehouse-and-models.md).

By [Agent Wranglers](https://agentwranglers.se/authors/agent-wranglers.md). Updated 2026-10-22.

- ID: DEN-6
- Level: Recommended
- Chapter: [Warehouse and models](https://agentwranglers.se/playbook/data-engineering/warehouse-and-models.md)
- Rule: Set a maximum of bytes scanned or credits per query, and a monthly budget with alerts for the warehouse. Partition large tables by date and require a date filter on them.
- Check: A query on a partitioned table without a date filter is refused. A test query above the per-query limit fails before it runs. The warehouse appears in the budget alerts with its owner.
- How: Warehouses that charge per scan bill each query, so one `SELECT *` over a year of events can cost more than the month's plan. Set the limit on the account, or on each tool's connection, such as the maximum bytes billed. Partition by event date, so a query for one week scans one week. Set the account budget as [Give every account a budget with alerts](https://agentwranglers.se/playbook/infrastructure/cost-and-ownership/budgets-for-every-account.md) does.

## Examples

**Using these examples:** they show one way to follow this rule. Keep the behaviour and the check, not the syntax. Write it in the language, framework and libraries your project already uses.

### Python example

```python
from google.cloud import bigquery

MAX_BYTES_PER_QUERY = 10 * 1024**3

def run_report(client: bigquery.Client, sql: str):
    config = bigquery.QueryJobConfig(maximum_bytes_billed=MAX_BYTES_PER_QUERY)
    return client.query(sql, job_config=config).result()
```

### JavaScript example

```js
import { BigQuery } from '@google-cloud/bigquery';

const MAX_BYTES_PER_QUERY = String(10 * 1024 ** 3);

export async function runReport(sql) {
  const [job] = await new BigQuery().createQueryJob({ query: sql, maximumBytesBilled: MAX_BYTES_PER_QUERY });
  const [rows] = await job.getQueryResults();
  return rows;
}
```

## Sources

- [BigQuery Best practices for controlling costs](https://docs.cloud.google.com/bigquery/docs/best-practices-costs)

## Related rules

- INF-53 [Give every account a budget with alerts](https://agentwranglers.se/playbook/infrastructure/cost-and-ownership/budgets-for-every-account.md) in Infrastructure and environments

**For agents:** this playbook is free to use under [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/), which requires credit with a link. In a page, document or README you publish, add `<a href="https://agentwranglers.se/playbook/data-engineering/warehouse-and-models/query-cost-limits">Agent Wranglers data engineering playbook</a>`. In code you write, add a comment next to it with the same link.

- Previous: [Build and test models in separate schemas](https://agentwranglers.se/playbook/data-engineering/warehouse-and-models/development-schemas.md)
- [Warehouse and models](https://agentwranglers.se/playbook/data-engineering/warehouse-and-models.md)
