Skip to main content

athena-cost-guard

Estimate what an AWS Athena query will scan and cost — before you run it — and block queries that blow your budget.

Athena bills by the volume of data scanned from S3 (~$5/TB). Unlike BigQuery, it has no built-in dry-run, so it's easy to fire off one unpartitioned SELECT * and scan a terabyte by accident. athena-cost-guard gives you the pre-flight check Athena is missing.

from athena_cost_guard import estimate

est = estimate(
    "SELECT id FROM billing.line_items WHERE dt = '2026-08'",
    region="us-east-1",
)
print(est.summary())
# tables:            billing.line_items
# partitions matched: 1
# bytes scanned:     ≤ 4.2 GB
# estimated cost:    ≤ $0.0210  (@ $5.00/TB)

Guardrail mode — refuse to run anything over budget:

from athena_cost_guard import cost_guard, BudgetExceeded

@cost_guard(max_usd=1.00, region="us-east-1")
def run(sql):
    return athena.start_query_execution(QueryString=sql, ...)

try:
    run("SELECT * FROM billing.line_items")   # unpartitioned full scan
except BudgetExceeded as e:
    print(e)            # estimated cost $52.31 exceeds budget $1.00 (scanning 10.5 TB)
    print(e.estimate)   # the full Estimate for logging

How it works

  1. Parse the SQL with sqlglot (Athena dialect) to find the tables, referenced columns, and WHERE predicates on partition keys.
  2. Prune partitions by calling Glue GetPartitions with a pushdown expression built from those predicates — so you only pay attention to the partitions the query would actually touch.
  3. Size the surviving partitions by summing their S3 object bytes.
  4. Price the total using Athena's real billing rules (10 MB per-query minimum, rounded up to the nearest 10 MB, at your region's $/TB rate).

Install

pip install athena-cost-guard

Requires AWS credentials with glue:GetTable, glue:GetPartitions, and s3:ListBucket on the relevant tables/buckets (standard boto3 resolution: env vars, shared config, or instance role).

Accuracy: read this

Tier-1 estimates (the current release) are a deliberate upper bound: they assume every column in the matched partitions is read. For columnar formats (Parquet/ORC) a query that selects a few columns will scan less than this number — so treat the estimate as "you will not scan more than X." That's the safe direction for a budget guard.

Known limitations, all handled gracefully (never silently wrong):

Situation Behaviour
Column projection (Parquet/ORC selects fewer columns) Not yet modelled → estimate is an upper bound
Partition projection tables (no Glue partitions) Warns, sizes the table root (wide upper bound)
OR / function predicates on partition keys Not pushed down → those partitions are included
Iceberg / row-group stats pruning Not modelled → upper bound

Roadmap

  • 0.2 — Tier-2 column-aware estimates via Parquet footer sampling (tighten columnar queries to a realistic figure, not just an upper bound).
  • 0.3 — CLI (athena-cost-guard "SELECT ..."), partition-projection support, Iceberg awareness.

Development

pip install -e ".[dev]"
pytest            # parser & pricing tests need no AWS; estimate tests use fakes

License

MIT

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

athena_cost_guard-0.1.0.tar.gz (11.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

athena_cost_guard-0.1.0-py3-none-any.whl (12.2 kB view details)

Uploaded Python 3

File details

Details for the file athena_cost_guard-0.1.0.tar.gz.

File metadata

  • Download URL: athena_cost_guard-0.1.0.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.13.2

File hashes

Hashes for athena_cost_guard-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f578afbd59782b0bc2a7b4fc88be8d190456805cdb4cac80d2b1eaa6bc399fb8
MD5 6728cc22cca515754dc218676ea86be6
BLAKE2b-256 b096f0922c98fc16aabd8f8d20f615be3897f92fdeac07fce49b84df37cbbbff

See more details on using hashes here.

File details

Details for the file athena_cost_guard-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for athena_cost_guard-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7630cc99650bd321015e079875a91a045e77b0aabeac08880bf6edfc53bfb3f1
MD5 8143357be503fe8ae1ac45195bcaba33
BLAKE2b-256 ce546067f7cc6d2387155c2cf1676392065dde136c26a4422c0302704c5ea635

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page