Skip to content

feat: add USDC price cache and per-session spend cap - #42

Open
nirholas wants to merge 1 commit into
aws-samples:mainfrom
nirholas:feat/cost-cache-and-spend-cap
Open

feat: add USDC price cache and per-session spend cap#42
nirholas wants to merge 1 commit into
aws-samples:mainfrom
nirholas:feat/cost-cache-and-spend-cap

Conversation

@nirholas

Copy link
Copy Markdown

cost_estimator.py: cache USDC price for 60 seconds
get_usdc_price() previously hit the CoinGecko API on every
estimate_image_cost() call. The price is now stored in a
process-level dict protected by threading.Lock and only refreshed
when the 60-second TTL expires. Falls back to 1.0 on network error.

tools.py: per-session spend cap
Adds SESSION_SPEND_CAP_USD (env var, default $1.00). Before
authorizing a payment, make_payment() checks that the session's
running total plus the new cost would not exceed the cap. After a
successful image generation the cost is added to
session.total_spent_usd so subsequent requests correctly reflect
cumulative usage.

Issue #, if available:

Description of changes:

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

cost_estimator.py: cache USDC price for 60 seconds
  get_usdc_price() previously hit the CoinGecko API on every
  estimate_image_cost() call. The price is now stored in a
  process-level dict protected by threading.Lock and only refreshed
  when the 60-second TTL expires. Falls back to 1.0 on network error.

tools.py: per-session spend cap
  Adds SESSION_SPEND_CAP_USD (env var, default $1.00). Before
  authorizing a payment, make_payment() checks that the session's
  running total plus the new cost would not exceed the cap. After a
  successful image generation the cost is added to
  session.total_spent_usd so subsequent requests correctly reflect
  cumulative usage.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants