Log Sume ratelimit-remaining on every Python urllib response
A urllib handler that logs ratelimit-remaining and ratelimit-reset on each Sume response, warning under 10 percent. It also sees 429 replies.

The Sume docs say not to count requests yourself: read ratelimit-remaining and, on a 429, wait for retry-after. Every response carries ratelimit-limit, ratelimit-remaining and ratelimit-reset. With only the Python standard library, a small BaseHandler is enough to log them for every call.
The handler runs before urllib turns an error status into HTTPError, so a 429 is logged too.
The handler
import logging, urllib.request
log = logging.getLogger("sume.rate")
class RateLogger(urllib.request.BaseHandler):
def http_response(self, request, response):
left = response.headers.get("ratelimit-remaining")
limit = response.headers.get("ratelimit-limit")
if left is not None and limit:
level = logging.WARNING if int(left) < int(limit) * 0.1 else logging.DEBUG
log.log(level, "%s %s remaining %s/%s reset %ss", request.get_method(),
request.selector, left, limit, response.headers.get("ratelimit-reset"))
return response
https_response = http_response
opener = urllib.request.build_opener(RateLogger())
# opener.open(req) now logs the budget on every response; no counting on your side.What it logs
Normal responses log at DEBUG. When the remaining count drops under 10 percent of the limit it logs a WARNING such as GET /v1/jobs/job_1/status remaining 0/120 reset 9s. The header describes the bucket the request spent from, so a submit shows the write bucket and a status poll shows the read bucket.
Notes
ratelimit-limiton the response is the authority for the deployment you call; the plan table is only the shipped default.- Logging does not replace backoff: on
429, honorretry-afterand readerror.details.scope. - Keep the API key out of these lines. The handler logs method, path and numbers only.
Sources
Related posts
More in Developers
- Make an AI voiceover louder: generation_config volume and a peak check
Sume TTS takes generation_config.volume from 0.5 to 2. Raise it, then check the WAV peak in Python so you never ship a clipped voiceover.
- MATLAB webwrite and weboptions: generate an AI video and save the MP4
A MATLAB script posts a Wan 3.0 job to Sume with webwrite, polls the job status with webread and saves the finished clip with websave.
- Music-only Short: Timeline silence mode with a soundtrack bed
Build a Short with no voice: audio mode silence plus a soundtrack. The bed plays alone at the default -16 dB with no amix loss. Fields, limits, tail rule.
- Nearest supported aspect ratio per Sume image model, in Python
A model rejects an aspect ratio it does not list. Read the catalog, pick the closest ratio it accepts, generate, then crop to the exact shape you need.
Written by Sume