feat: add USDC price cache and per-session spend cap - #42
Conversation
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.
|
Nice — the 60s USDC price cache + SESSION_SPEND_CAP_USD is exactly the right pattern for pay-per-use rails. Two hardening thoughts from running similar x402 seller gateways:
General question: are you tracking spend caps per-session only, or considering a broader per-agent/per-API-key budget across sessions? The per-session cap is a good default, but builders often need a parent-level budget too (e.g. a team allowance that spans many agent sessions). Keen to know if that's on the roadmap — happy to share notes on how we structure multi-level budgets in our gateway. |
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.