Claude Code in business

What Is API Polling? Explained for People Who Build With AI

API polling explained simply: what it is, how often to poll, and the five mistakes AI-written polling loops make, with a simulated day and a copy-paste prompt.

· by OI

If you build things by asking an AI to write the code, sooner or later it will hand you a loop that says “check the API every 60 seconds”. That loop is called polling. This post explains what polling is in plain words, how often you should do it, and the five mistakes I see in polling code that an AI writes on the first try. Each mistake comes with a number from a simulated day, and the real bot we run in production made one of them for weeks before we caught it.

At the end there’s a prompt you can paste into Claude, Cursor or whatever you use, so the first version of the loop is the good one.

Polling, explained without the jargon

Polling in one sentence

Polling means your program asks another program “anything new?” again and again, on a timer.

That’s it. You’ve done it yourself. A kid in the back seat asking “are we there yet?” every five minutes is polling. So is walking to the mailbox every afternoon to see if a letter came. Most days the answer is “no”, and that’s fine. You only need to be there on the day the answer is “yes”.

In code, “asking” means calling an API: a web address another service gives you so programs can talk to it. Your program calls it, gets back a list (new orders, new messages, finished chats), does something with anything new, waits, and asks again.

The other way: a webhook is a doorbell

The alternative has a strange name and a simple idea. A webhook is when the other service calls you the moment something happens. Instead of walking to the mailbox, you put in a doorbell, and the mail carrier rings it.

Polling is checking the mailbox on a timer. A webhook is a doorbell the other service rings.

Doorbells are faster, and nobody wastes a walk. But a doorbell needs a door: a public internet address that is always on, can receive messages, and checks that the ringer is who they claim to be. If your program runs on a laptop, it doesn’t have one. If the doorbell rings while your laptop is closed, many services try a few times and then give up, and some switch your doorbell off entirely after enough failures. Our chat platform disables a webhook after 100 failed deliveries in a row, until a person turns it back on.

That’s why an AI assistant reaches for polling first. Polling only needs your program to make normal outgoing calls, like a browser does. No door, no doorbell, nothing on the internet needs to know your laptop exists. I wrote the longer version of this decision, with the rate-limit maths, in polling vs webhooks for a chat-support API.

Six words you’ll see in the code

When the AI writes a polling loop, these words show up. Here’s what each one means, the way I’d explain it to a ten-year-old who is, frankly, smarter than me about computers.

Word What it means The mailbox version
Interval How long the loop waits between checks Checking the mailbox every afternoon vs every 5 minutes
Rate limit How many times the service lets you ask in a given time The post office says “max 10 visits an hour”
429 The error code a service sends when you asked too often The clerk says “you again? come back later”
Pagination The answer comes in pages, e.g. 100 items at a time The mailbox only shows the top 100 letters; the rest are underneath
Cursor A bookmark: “I’ve already seen everything up to here” A sticky note: “read everything older than Tuesday”
Backoff After an error, wait a bit longer before trying again If the clerk is grumpy, come back in 2 minutes, then 4, then 8

Keep those six in your head and you can read almost any polling code an AI writes. Two more you might see: idempotent means “doing it twice has the same result as doing it once” (a light switch set to ON, not a light switch you flip), and long polling is a variation where you ask “anything new?” and the service holds the line open until there is. You rarely need that one.

How often should you poll?

This is the most-asked polling question, and it has a simple answer: ask how late you’re allowed to be, then poll about that often.

What you’re watching How late is OK? Poll every
A customer waiting for a reply in a chat seconds Polling is probably the wrong tool; use a webhook or a chat SDK
Support chats that closed, to log them (our bot) a few minutes 60 seconds
New orders to put in a spreadsheet 10 minutes 5 minutes
A price you check once a day hours once a day (that’s a scheduled task, not really polling)

Then do one sum: checks per day = 86,400 ÷ your interval in seconds. Every 60 seconds is 1,440 checks a day. Every second is 86,400. Compare that number with the service’s limit, which is on its pricing or API docs page. Our chat platform allows 10 calls a second on the list endpoint; our once-a-minute bot uses under 0.2% of that.

If your interval is shorter than how fast things actually change, you’re not getting fresher data. You’re just asking more often to hear “no”.

A simulated day: six polling loops, one fake API

To show what goes wrong, I wrote a small playground script that runs entirely on my laptop with no network. It has a fake support-chat API that works like the real one we use: it returns closed chats newest-first, 100 per page, and it has a rate limit.

The day is built from our own numbers. 134 chats close, which was our busiest day in September and October. Most of them close in a lunch rush. And the laptop running the loop is asleep from 12:00 to 14:00, right through the rush, because that’s what laptops do. Then I ran six polling loops over the same day: the kinds of loops I’ve seen AI assistants write, plus the fixed one.

Simulated day: what each polling loop handled once, twice, or never

“Once” is good: every chat handled exactly one time. “Dup” means handled more than once. “Missed” means never handled. Here’s each mistake in plain words.

Mistake 1: no memory

The simplest loop the AI writes is: every minute, get the list, do something with everything in it. It works the first time you test it. The problem is the second minute: the list still contains the chats from the first minute, so the loop handles them again. And again.

Over the simulated day, 134 chats were handled 62,646 extra times. If “handle” means a database write, you have 62,646 duplicate rows. If it means sending each chat to an AI model for a summary, it’s worse: at the $0.0013 per call we measured for a small Haiku call, that’s about $81 for one day of re-summarising chats you already summarised. If it means sending an email, someone gets very, very annoyed.

The fix: remember what you’ve already handled (the “done list”), and skip it.

Mistake 2: asking too often

“Let’s make it real-time,” says the AI, and sets the interval to one second. With no memory, the extra handling goes from 62,646 to 3,768,118. But say you fixed that. You still made 79,200 calls.

Most free and cheap API plans have a daily limit. I gave the fake API a plan of 10,000 calls a day, which is a common size. The every-second loop used the whole day’s allowance by 02:46 in the morning. Every call after that came back with a 429 error. The chats didn’t start closing until 9am. Result: 134 of 134 missed. The loop was running all day, very busily, and did nothing useful.

The fix: poll as often as you need, not as often as you can. Do the 86,400 ÷ interval sum before you run it.

Mistake 3: memory that disappears on restart

The AI’s next version keeps a done list, but keeps it in a variable. Variables live in the computer’s memory (RAM), and that memory is wiped when the program stops. Programs stop all the time: you edit the code, the laptop restarts for an update, it crashes.

In the playground, the loop restarted once at 16:00. It forgot everything, got the latest list, and handled the 100 chats on it a second time.

The fix: save the done list and the cursor to a file (or a small database) after every check, and read them back when the program starts. A file survives a restart. A variable doesn’t.

Mistake 4: only reading the first page

This one is sneaky, and our real bot did it until 22 September 2026.

A list API usually returns the newest items one page at a time, 100 per page in our case. A loop that only reads the first page works perfectly on a normal day, because between two checks only a few new chats appear. It works on day 1 of the playground too: 134 of 134, no problems.

Then comes a busy day. In the playground’s day 2, 250 chats closed while the laptop slept. When it woke up, the first page showed the newest 100. The cursor moved to the newest one. The other 150 were on page 2 and 3, older than the cursor, so they’d never be looked at again. 150 of 290 missed, with no error message at all.

Only the first page: on a busy day, everything underneath it is silently skipped

In our real bot, we closed 38 to 134 chats a day. A laptop off for a day was enough to open the same hole. We found it when chats that should have been logged weren’t.

The fix: keep asking for the next page until you reach something older than your cursor. On a normal minute that’s still one call, because the first page already reaches back to the cursor.

Mistake 5: moving the bookmark before the work is done

The playground doesn’t simulate this one, but our bot’s code handles it, and it’s worth knowing. Suppose the loop gets 5 new chats, moves the cursor past all 5, then crashes on chat 3. Chats 3, 4 and 5 are now behind the bookmark. Nothing will ever look at them again.

The order matters: do the work, then move the bookmark. If something fails, move the bookmark only up to just before the earliest failure, so the next check tries again from there. Our bot does exactly that; the comment above the line says “stop in front of the earliest failure”.

The loop that gets all of it right

The last loop in the playground does five boring things: a cursor saved in a file, a done list saved in a file, all pages until it reaches the cursor, a short pause between pages, and waiting longer after each 429. 134 of 134 on day 1, 290 of 290 on day 2, zero duplicates, with about the same number of calls as the broken once-a-minute loops: 1,320 on day 1 and 1,322 on day 2 (fewer than 1,440 because the laptop slept for two hours).

The loop that survived both days, about 30 lines

None of it is clever. That’s the point. Polling goes wrong in boring ways, and the fixes are boring too.

Three things that will happen to your poller anyway

These aren’t in the playground. They happened to our real bot, which polls every 60 seconds from a Mac in an office.

1. The computer goes to sleep. A poller only runs while the computer is awake. On 29 September our bot “stopped logging”, and the bot was fine; the laptop lid had been closed. Now the loop compares the clock with the last time it ran. If the gap is much bigger than 60 seconds, it posts a message saying “polling gap of N minutes, the machine was asleep”, then catches up. If you’re going to poll from a laptop, at least make the gap visible. Better, run it on a machine that doesn’t sleep; we moved ours (the move had its own adventures).

2. Things fall off the list. On 28 September a chat the bot had decided to “look at again in 3 hours” was no longer on the first page of the closed list by the time 3 hours had passed. The bot kept a note of it, but could only find chats that were on the list. The fix: for anything you postponed, fetch it directly by its ID instead of hoping it’s still on the list.

3. The API has a burst limit, even when your average is low. Once a minute is nothing. But after a long sleep, a loop that catches up by fetching 120 things back to back is a burst. We measured it on our platform: 56 of 120 back-to-back calls failed. That’s why the fixed loop pauses between pages and backs off after a 429, and why our bot only works on 3 chats at a time.

There’s also a quiet fourth one: the very first run. If the loop starts with an empty cursor, should it process everything that ever happened? Usually not. Our bot sets the cursor to “now” on its first run and only watches what happens after that. Decide this on purpose, or the AI will decide it for you, and its default is usually “everything since the beginning of time”.

A prompt you can paste

Here’s the prompt I’d give an AI coding assistant to get a good poller on the first try. Change the parts in brackets.

Write a polling loop that checks [API name and endpoint] every [60] seconds
and [what to do with each new item].

Requirements:
1. Keep a cursor (the newest item time already handled) and a list of handled
   item IDs. Save both to a JSON file after every check and load them at start.
2. Skip any item whose ID is already in the handled list.
3. If the API returns pages, keep requesting the next page until a page contains
   an item at or before the cursor, or there are no more pages.
4. Do the work first, then move the cursor. If an item fails, do not move the
   cursor past it; retry it on the next check.
5. On HTTP 429 or 5xx, wait and retry with exponential backoff (2s, 4s, 8s,
   max 5 tries). Pause 0.5s between page requests.
6. On the very first run (no state file), set the cursor to now. Do not process
   old items.
7. If the time since the last check is more than 5x the interval, log a warning
   that the machine was probably asleep.
8. Log one line per check: how many items, how many new, how many failed.

Before writing code, tell me: the API's rate limit, how many calls per day this
interval makes, and whether this API offers webhooks instead.

The last paragraph is the one people skip. It makes the AI do the sum from the “how often” section and check the docs before writing code, which is when it’s cheapest to find out you should be using a webhook.

How to check your poller works (three tests, ten minutes)

You don’t need to read the code to test it. You need to be mean to it.

  1. Restart test. Let it run for a few checks, stop it, start it again. Nothing should be handled twice. If something is, the memory isn’t being saved (mistake 3).
  2. Sleep test. Stop it for longer than usual, make several new items appear (or wait for them), then start it. Everything from the gap should be handled. If only some are, check the pages (mistake 4).
  3. Busy test. Ask the AI to add a “dry run” mode that prints what it would do. Point it at a day with lots of items. Count the lines and compare with what the service’s own screen shows.

If the counts match after all three, you have a better poller than ours had in September.

When to stop polling

Polling is the right first tool when “a minute late” is fine and your program has no public address. Switch to a webhook when:

  • a person is waiting on the result in real time (a chat reply, a payment confirmation);
  • your program already runs on a server with a public address;
  • you’re polling many things and the calls add up close to the rate limit.

Many services give you both, and the sturdy setup uses both: a webhook for speed, and a slow poll every so often to catch anything the doorbell missed. If you’re curious what “a slow poll to catch what the other thing missed” looks like in practice, the polling vs webhooks post has a live check of chats our platform’s list never returns at all.

Quick answers

Is polling bad practice? No. It’s simpler, and simple is good. It’s wasteful when you poll far more often than things change, and wrong when someone is waiting in real time.

Does polling cost money? The calls are usually free but limited. The thing that costs money is what you do with each result. If each new item goes to an AI model, a loop with no memory pays for the same item again every check.

Will polling get me banned? Asking too often gets you 429 errors first. Keep well under the published limit and back off when you see a 429, and it won’t come to that.

Is polling the same as a cron job or a scheduled task? Close cousins. A scheduled task decides when something runs. Polling is what it does each time: ask, compare with the bookmark, handle the new things. Plenty of pollers are just a scheduled task that runs every few minutes; we replaced a few cron jobs that way (Claude Code scheduled tasks).

One line from me

Our bot polled happily for weeks while silently skipping everything below the first page of 100. Ask your AI for the boring loop, then be mean to it for ten minutes.

For the decision behind our once-a-minute bot, read polling vs webhooks for a chat-support API; for what it does with each chat it finds, read turning support chats into a claims log.