--- title: "Sync account request records with Python" description: "Use a server-side Python script to page through XiuAI request records, retry temporary failures, and total exact costs." image: "https://docs.xiu.ai/og.png" --- > XiuAI documentation index > Full documentation index: https://docs.xiu.ai/en/llms.txt > Check the index for current pages before reading. # Sync account request records with Python This Python example uses the [request records endpoint](/en/open-api/requests/). [Issue a key in the console](/en/open-api/#get-a-key) with `usage:read` before running it. The example requires `requests` and reads the key from `XIU_OPEN_API_KEY`. It fixes the query window, follows cursors, applies bounded retries to rate limits and temporary failures, and calculates costs with `Decimal`. ```bash python -m pip install requests ``` ```python import os import time from decimal import Decimal import requests session = requests.Session() session.headers["Authorization"] = f"Bearer {os.environ['XIU_OPEN_API_KEY']}" base_url = "https://api.xiu.ai" def get_json(path, params): for attempt in range(5): try: response = session.get( base_url + path, params=params, timeout=(5, 15) ) except (requests.Timeout, requests.ConnectionError): if attempt == 4: raise time.sleep(2 ** attempt) continue if response.status_code == 200: return response.json() if response.status_code == 429: delay = response.json()["retryAfterSeconds"] elif 500 <= response.status_code < 600: delay = 2 ** attempt else: response.raise_for_status() raise RuntimeError(f"Unexpected HTTP {response.status_code}") if attempt == 4: response.raise_for_status() time.sleep(delay) def iter_requests(start_time, end_time): params = {"startTime": start_time, "endTime": end_time, "limit": 100} while True: page = get_json("/open/v1/requests", params) yield from page["items"] if page["nextCursor"] is None: return params["cursor"] = page["nextCursor"] end_time = int(time.time()) start_time = end_time - 86400 total = Decimal("0") seen = set() for item in iter_requests(start_time, end_time): if item["id"] in seen: continue seen.add(item["id"]) total += Decimal(item["cost"]) print(f"{total:.6f} USD") ``` Scheduled jobs should persist records and sync checkpoints so a process restart does not lose deduplication information. Resolve parameters, credentials, or scopes before retrying a `400`, `401`, or `403` response. Source: https://docs.xiu.ai/en/open-api/examples/index.mdx