The refresh budget
By the end of this lesson you will have a refresh loop that prices itself from the same requests it sends, an interval per panel with manual as a real choice, pauses for a hidden tab and a closed market, a refresh when you come back to the tab, and the projected time you hit your limit on screen.
One cockpit, several panels, one allowance. This lesson turns that into arithmetic you can defend.
The formula
calls per refresh = sum over visible panels of (their dataNeeds, priced)
calls per day = calls per refresh x refreshes per day
The union from unit 2 is the first line. The price list from Build Your First Tool turns needs into calls, and the two rows that dominate a cockpit are /real-time at 1 per symbol and /fundamentals at 10 per request.
A worked example, using the measured prices:
| Panel | Needs | Calls |
|---|---|---|
| Watchlist | 12 live quotes | 12 |
| Screener result | 1 screen | 5 |
| Quote detail | 1 live quote (already in the 12) | 0 |
| Chart | 1 EOD series | 1 |
| Per refresh | 18 |
Refreshing every 5 minutes for 6.5 hours is 78 × 18 = 1,404 calls a day.
The screen's 5 assume a plan that includes the screener (All-In-One or EOD+Intraday — All World Extended). On the free plan that panel is refused with 403 and shows nothing, so on a free key the same cockpit is 13 calls a refresh.
Note the zero. The quote panel costs nothing because the watchlist already needed that symbol, and that saving exists only because the data layer unions before fetching. Without unit 1's architecture the same cockpit costs 19, and the gap widens with every panel.
Not all panels need the same rate
A single global interval is the wasteful default. Panels have honest, different needs:
- Live quotes — as fast as the data actually changes.
- A screener result — the market has not re-screened itself in thirty seconds; minutes or on demand.
- A daily chart — once a day, plus on demand.
- A backtest result — never, unless you press run. It is a computation over history, and history did not change.
Per-panel intervals with sensible defaults, and one of those defaults is manual. Most of what looks like it needs refreshing does not.
Four rules that cost nothing
Pause when hidden. document.visibilityState and the Page Visibility API. A cockpit in a background tab should do nothing.
Pause when the market is closed. No new trades, no reason to ask. One watchlist can mix New York, London and crypto, so a panel has no single market: decide it per symbol, by its exchange's session.
Never refresh a backtest automatically. The rule cannot change, and the only thing that can is the adjusted series itself, which is rewritten on every dividend and split — so re-pull on a schedule you choose and re-run then, rather than letting a timer do it behind you.
Refresh on focus, not only on a timer. Coming back to the tab is the moment a person wants current data, and it is one event rather than a poll.
Show the budget, do not hide it
Put it in the interface: calls used today, the daily limit, and the projected total at the current refresh rate.
The projection is what makes it useful. "You have used 340 of 1,000" prompts nothing. "At this rate you will hit your limit at 14:20" turns an abstract number into a decision, and it is one multiplication.
The finance behind it
The account endpoint's own vocabulary trips people up before the arithmetic does: Why does the account endpoint call your daily quota a rate limit?
Try it now
Compute your own cockpit's calls per refresh by hand, then measure it against the meter over ten refreshes. If the measured number is higher, something is fetching outside the data layer — and finding that is worth more than the arithmetic.
The prompt
Mock mode on, plan mode on, and paste:
Plan, then build, src/lib/budget.ts. refreshCost(requests) prices one
refresh with estimateCalls from the same requests the market store will
send, and returns null if any of them is not in the cost table, so the
figure cannot drift from the fetch. Each panel's config gets a refresh
of live, minutes, daily or manual; a backtest is manual and cannot be
set otherwise. The container's loop skips while document.visibilityState
is hidden, leaves out each symbol whose exchange is outside its regular
session (crypto never closes, an unknown exchange counts as open), and
refreshes once on focus. Show calls used today from the usage route,
the daily limit, and the time the limit will be reached at the current
rate, with each interval priced as its own round. Write
tests/budget.test.ts first, for this worked cockpit and for the
projection: a watchlist of 12 live quotes costs 12, one screen request
5, a quote panel on a symbol already among the 12 costs 0, one
end-of-day series 1, so 18 per refresh, and refreshing every five
minutes for 6.5 hours is 78 x 18 = 1,404 calls a day. Mock mode; tell me the per-refresh price of my
current layout before wiring the loop. Run npm test, typecheck, lint and
check:leak, and show me the diff.
A good answer adds a budget module and its test and changes the container and the panels' configs. The price on screen comes from refreshCost, never from a constant. The agent will name your layout's price and then stop and wait for your yes before wiring the loop: answer yes once the price matches the panels you have (your own layout costs less than the worked 18, because the numbers in the test are the example's, not yours).
Check it yourself
- The test pins the worked cockpit's price per refresh, 18, with the quote panel adding nothing. That 18 and the 1,404 a day are the worked example's; your own layout's price is the one the agent named before wiring the loop.
- Switch to another browser tab for a minute with the Network tab recording: no requests while hidden, and one round when you come back.
- The budget line shows used, limit and a projected time, and changing a panel's interval moves the projected time.
- Outside mock mode, and only if your allowance has room, one refresh moves the meter by the price on screen and not a call more.
Compare with ours
// src/lib/budget.ts: priced from the requests the store will send, never from a constant
export function refreshCost(requests: { path: string; params: Record<string, string> }[]) {
let total = 0;
for (const r of requests) {
const cost = estimateCalls(r.path, r.params);
if (cost === null) return null; // unknown is "look it up", never zero
total += cost;
}
return total;
}
export function exhaustsAt(left: number, perRefresh: number, everyMs: number, now: number) {
if (perRefresh === 0) return null;
return new Date(now + Math.floor(left / perRefresh) * everyMs); // "you hit it at 14:20"
}
// Several intervals are several rounds: price each per minute, then project per minute.
export const callsPerMinute = (rounds: { cost: number; everyMs: number }[]) =>
rounds.reduce((sum, r) => sum + (r.cost * 60_000) / r.everyMs, 0);
// in the container
const at = exhaustsAt(limit - used, callsPerMinute(rounds), 60_000, Date.now());
A symbol two rounds share is priced in the faster one only, because the store serves it from cache in the slower. The full state at the end of this unit is on the starter's branch ref/build-a-dashboard/u4.
Checkpoint
npm test && npm run typecheck
git status # the files you expect, and no .env file
git add -A
git commit -m "Refresh budget: priced from the fetch, per-panel intervals, paused hidden and closed"
Then answer this lesson's questions.