Pattern

How to run a nightly backfill without overspending

Pace against a published rate limit, understand what actually costs a credit, and pick refresh candidates so a backfill costs roughly what you budgeted.

8 min read Updated 5 October 2026

A backfill is where enrichment budgets go to die. Every answered call is billed, batching changes the round-trip count and nothing about the bill, and the naive job (“refresh everything, retry on failure”) pays for the same record twice.

Pick candidates, do not sweep

The first and largest saving is not calling. Refresh what is stale and read:

select company_id, public_id
from companies
where fetched_at < now() - interval '90 days'
  and last_read_at > now() - interval '30 days'   -- someone actually looks at it
order by last_read_at desc
limit :budget;                                     -- a hard cap, in rows

That limit :budget is the important line. Derive it from money, not from row count:

const MONTHLY_BUDGET_CREDITS = 20_000;
const nightlyBudget = Math.floor(MONTHLY_BUDGET_CREDITS / 30);

Size it in credits rather than in calls, because credits are decimal. A profile or a company lookup is 1 credit, network details add 0.1 so a profile with them is 1.1, a post’s comment or reaction list adds 1 each, and a page of GET /v1/posts/search is 1 credit for up to 50 posts. A job that cannot exceed its budget by construction is a job you can leave running.

Pace, do not burst

Your account has a real, published request-per-second allowance, 10 rps by default, and every response tells you where you stand:

X-RateLimit-Limit: 10
X-RateLimit-Remaining: 3

So unlike vendors where 429 is a mystery, you can pace deliberately. Stay under the limit, watch X-RateLimit-Remaining, and treat a 429 as a bug in your pacing rather than weather.

A small fixed gap plus a concurrency cap is enough:

const CONCURRENCY = 4;
const GAP_MS = 100;

async function paced<T>(items: T[], fn: (item: T) => Promise<void>) {
  const queue = [...items];
  const workers = Array.from({ length: CONCURRENCY }, async () => {
    while (queue.length) {
      const item = queue.shift()!;
      await fn(item);
      await sleep(GAP_MS);
    }
  });
  await Promise.all(workers);
}

Batch the urns you already hold

A re-enrichment pass over people is where POST /v1/people/bulk earns its place. It takes 1 to 50 bare entity_urn values, no slugs and no URLs, which is exactly what a backfill already stored on the first pass. Fifty profiles then arrive in one request instead of fifty, so the batch spends one unit of your 10 rps rather than fifty. Companies have no batch path, so they stay one call each.

const res = await fetch('https://api.triguna.ai/v1/people/bulk', {
  method: 'POST',
  headers: { ApiKey: KEY, 'Content-Type': 'application/json' },
  body: JSON.stringify({ urns: urns.slice(0, 50) }),  // bare entity_urn values only
});

What it does not do is make the night cheaper. Bulk bills 1 credit per profile returned, exactly what fetching them one at a time costs, so keep budgeting per profile and treat the saving as round trips and rate-limit headroom. The pacing pattern is the same either way.

Retries that do not double-spend

The failure counts in a badly built backfill are dominated by retries. The same entity attempted four times looks like four failures and can bill more than once. Two rules:

  1. Retry only 429, 502 and 503. Those are transient, and free on the per-record path. The one exception is a bulk 502, which bills 1 credit for the failed batch.
  2. Never retry 400, 401, 402 or 404. They will not change on their own, and a retried 404 costs a credit every single time.
const RETRYABLE = new Set([429, 502, 503]);

if (!res.ok && !RETRYABLE.has(res.status)) {
  await recordTerminal(entity, res.status, await res.text());
  return; // stop. This will not succeed on attempt two, and a 404 bills again.
}

Log outcomes you can actually read

Recording only success / failed throws away the distinctions you need. Record the status code and keep the four outcomes separate:

OutcomeMeaningYour move
successRecord returned and billednone
not_found (404)Identifier bug, costs a creditFix slug extraction, urgently
rate_limited (429)PacingLower concurrency
terminal (401/402)Credentials or balancePage someone

If a 429 outcome is only recorded after three consecutive retries, say so in the metric name. Otherwise “1.8% rate limited” quietly means “5.4% of calls were throttled”.

Guardrails worth having

  • Leave auto-recharge off while a backfill runs. At zero balance calls return 402, which stops the job; with auto-recharge on it just keeps buying credits.
  • A dry-run mode that counts candidates and prints the projected cost without calling.
  • A kill switch the on-call can flip without a deploy.
  • Read X-Credits-Charged and sum it. It is the only number that is not an estimate. Parse it with Number(), never parseInt: a charge can read 1.1 or 0.1, and parseInt turns both into something cheaper than the truth.
  • Check the balance before and after with GET /v1/credits. It is free and never rate limited, and it is decimal too, so a balance of 4.9 is a real reading rather than a rounding bug.

After the run

Compare fetched_at distribution before and after. If the tail did not move, your candidate query is selecting the same rows every night, usually because a terminal failure is not being recorded, so the same unresolvable entities are retried forever.

Next

Start with one request

Free credits on signup, no card required. Usage-based pricing after that.