Originally published on meridianbuild.dev, my build log for the products I'm building, including citedinstead.
I'm building a small Chrome extension for citedinstead. You open it on any website, click "Check this site", and it asks a handful of AI assistants the questions a buyer would ask: what's the best tool for this, what should I use instead of that. Then it shows who they recommend, and whether the site you're on is in the answer.
It is not on the Chrome Web Store yet. The server side is live, the store listing is not, and the buy button stays hidden until it is. This post is about a decision I made before anyone can pay for it, because it shaped most of the code: who pays for the AI calls.
Every click is an API bill
A check is not one call. It drafts questions from the site's homepage, asks each assistant each question with web search on, follows the source links some of them return, and filters the competitors it found. Every one of those is a paid API call, on my keys, unless the user brings their own.
The model I settled on, on November 29:
- No free trial. You buy a license.
- The license includes one check on my keys.
- After that, you paste your own API keys (OpenAI, Perplexity, Gemini, Anthropic, xAI, Meta) and every check runs on them. Bring your own key, BYOK.
It took two commits on the same day to get there. The first version (commit 96945ce) gave one free check per install, on my keys, before any license. The second (commit 310d482) removed it: no license, no check, not even a cached one. An install id is a random value the extension keeps in its own storage. Uninstall the extension and that storage goes with it. The rule I wanted was one trial per install, not one per reinstall. A random id in the extension's own storage can't tell the two apart, so the trial went.
Sizing the one check I pay for
The included check is where my money goes, so it gets the most rules.
The license price decides its size. Under $29, the included check asks 3 questions to the 3 cheapest assistants. From $29, it asks 5 questions to every assistant I have a key for. "Cheapest" is not a list I typed by hand. It is computed from a per call estimate:
export function includedEngines(available: Keys): Engine[] { return EXT_INCLUDED.candidates.filter((e) => available[e]) .sort((a, b) => estimatePerCall(a) - estimatePerCall(b) || ORDER.indexOf(a) - ORDER.indexOf(b)) .slice(0, EXT_INCLUDED.engines); } At the rates I had on November 29, that picks Perplexity, ChatGPT and Gemini, and the code comment next to it estimates about $0.12 for the whole basic check. Claude is a candidate but comes out fourth, the most expensive per answer of the four. Grok and Meta AI are never candidates for the basic check: they only run on the customer's keys or in the full one.
Estimates are estimates, so I looked at a real one. The one full included check that has run in production so far, 6 assistants times 5 questions, cost $1.06 in API calls. That number is why the full included check only comes with the higher price tiers. Sold at $10, it would cost me more than a tenth of the price in one click.
Claiming it exactly once
"One included check per license" sounds like a boolean. It is a race. A user can click twice, open two tabs, or retry while the first check is still running.
The claim is a single conditional update:
const claimIncluded = async () => (await env.DB.prepare("update ext_licenses set included_at=? where id=? and included_at is null") .bind(now, licenseId).run()).meta.changes === 1; Two requests can arrive together. Only one of them changes a row. The other one gets "your included check is used" and never reaches an AI.
The reverse matters as much. If every assistant fails, the claim is released:
const releaseIncluded = () => env.DB.prepare("update ext_licenses set included_at=null where id=?").bind(licenseId).run(); and the user sees "The assistants did not answer. Your included check was not used, please retry." A customer should not lose the one thing they paid for because a provider had a bad minute.
On top of that, two caps that have nothing to do with a single user: at most 50 included checks a day across all licenses, and at most 200 checks a day for one license. The first one is the one that protects me. If a bug or a leaked license ever burns included checks, the damage stops at 50 a day. I learned that one on another product, where a third party API had no budget cap at all.
One more trap: the first popup I built started a check when the user saved their settings, on whatever tab was open. Paste your license on a random page, and your included check was spent on that page. Since commit a6bd3c1, opening the popup only peeks at a cached result, and nothing that costs money starts without a click on "Check this site".
Never mixing my keys with theirs
Once a request carries customer keys, it runs only on customer keys. Once it runs on mine, it runs only on mine, and only for the assistants of the included tier:
const all = serverKeys(env); // our keys: the included check only, never mixed with customer keys for (const e of tier.engines(all)) keys[e] = all[e]; Mixing would be convenient. "You don't have a Grok key, I'll fill in mine" sounds generous. It also means my bill depends on which keys my customers forgot to paste, and that is not a pricing model.
The cache has the same rule in another shape. Results are cached for 7 days per site, so a second check of the same site is free. But a cached basic result, 3 questions to 3 assistants, must not be served to someone who is paying with their own keys for 5 questions to 6. A small function decides:
export function covers(r: Partial<ExtResult>, engines: Engine[], questions: number): boolean { return (r.questions || []).length >= questions && engines.every((e) => (r.engines?.[e]?.asked || 0) >= questions); } If the cached result does not cover what the request is entitled to, the check runs again.
The customer's keys must never land anywhere
BYOK means people hand me API keys that can spend their money. The rule in the code is short: customer keys live in memory for the duration of the request. Never in the database, never in a log.
"Never in a log" is harder than it sounds, because the logs I write are not the only text that ends up there. When a provider rejects a key, some of them put the key, or a partly masked version of it, in the error message. So every error goes through a scrub function before it is logged: it removes the exact keys of the request, then anything shaped like a provider key.
And because I don't trust a rule I can't test, there is a test that does the dumbest possible check. It makes a check with customer keys, makes every provider fail and echo the keys back in its errors, then:
const logged = JSON.stringify([...err.mock.calls, ...log.mock.calls]); const tables = (sql("select name from sqlite_master where type='table'") as { name: string }[]).map((t) => t.name); const dump = JSON.stringify(tables.map((t) => sql(`select * from "${t}"`))); for (const k of Object.values(CLIENT)) { expect(logged).not.toContain(k); expect(dump).not.toContain(k); } expect(dump).not.toContain(lic); // only the hash Every table, every row, every console call, searched for every key. The last line checks the license key too: the database only ever holds its SHA-256 hash. The license key itself is shown once, on the page you land on after paying.
In the extension, the license and the keys sit in chrome.storage.local, not in localStorage and not in synced storage, so they don't follow the user's Google account to other machines.
What I'd keep from this
- If users bring their own keys, design the part you pay for first. It is small, and it is where the abuse goes.
- Size the part you pay for from a measured cost, not only from an estimate.
- Claim a one time benefit with a conditional update, and give it back when the work fails.
- A global daily cap on your own spend is cheap insurance against your own bugs.
- Never fill gaps in a customer's keys with yours.
- Test the absence of secrets the blunt way: dump everything and grep.
The extension is the small sibling of something I wrote about a while ago, how I got ChatGPT to recommend my product. That post was about getting cited. This one is a tool to check whether you are, and it will be in the store when the store says yes.
