AX Notes · Measurement

The first LLM feature in a spell-check app: cost, model evaluation and daily caps for "Why is this wrong?"

Adding an LLM to an app means choosing a model, a billing path and a spending cap. Our spelling app picked its model with a 59-case eval, and its server function stops calling the LLM after 3,000 calls a day.

By Stephen Android development 11 min read Last updated

Adding an LLM to an app 1/4

The first eval run on the night of September 27, 2026 failed on all 59 calls with HTTP 402. It used an AI Studio key that the coding agent had said around noon that day would be billed against our Firebase credit, and we were in the middle of giving our Korean spelling app SugarSpeller its first server function and LLM. After the key was replaced with Vertex AI, gemini-3.8-flash gave a correct reason for all 59 corrections (the right rule for 54), with a p95 latency of 4.3 seconds and a cost of $0.93 per 1,000 calls. In the two weeks after the October 1 release, 3.7% of users who ran a check tapped the button; among users who had opened the correction sheet, it was 41%.

Sources: the design doc in the app repository (approved 2026-09-27), git history, the release decision log and the work log. Usage is from GA4; cost is from the usage records the function writes (checked 2026-11-02, times in UTC+9).

Contents (9)
  1. The 59-case eval: flash-lite at 78%, 3.8-flash at 100%
  2. The AI Studio key never touched the $300 credit
  3. The function refuses LLM calls past 3,000 a day
  4. A 7.9-second cold start pushed the app timeout to 15 seconds
  5. 3.7% of checking users, 41% of sheet openers
  6. Mass-created anonymous accounts hit only the global cap
  7. Adding the same feature to your app
  8. Still open: usage after the tap hint, and a price doubling
  9. FAQ

The 59-case eval: flash-lite at 78%, 3.8-flash at 100%

The feature explains one correction at a time: the user taps a corrected word and gets a one-line reason plus the name of the Korean orthography rule behind it. We picked the model with 59 corrections taken from real checker responses stored as test fixtures in the app: 38 from Naver's spell checker and 21 from Daum's (Naver and Daum are the two big Korean portals, and the app uses their checkers). The pass mark was written into the design doc before the run: at least 90% correct and a p95 of 2.5 seconds or less. Every call went to the Vertex AI global endpoint.

Model (prompt)Reason correctRule also correctp50 / p95Per 1,000
gemini-3.5-flash-lite (v1)43/59 (73%)30/591.06 s / 1.26 s$0.27
gemini-3.5-flash-lite (v2)46/59 (78%)38/591.08 s / 4.6 s$0.42
gemini-3.8-flash (v2)59/59 (100%)54/59 (92%)2.0 s / 4.3 s$0.93

Prompt v2 added an order of checks for spacing errors and a table of reasons for the most common corrections. It moved flash-lite from 73% to 78% and no further. Its wrong answers included explaining a noun fused with hada ("to do") as an ending or particle, garbled syllables, and calling an archaic spelling of a polite ending a past tense. 3.8-flash made no typos; its five misses were all wrong rule citations. It costs more than twice as much as flash-lite v2 per 1,000 calls, and it was the only model over the 90% bar, so it became the default.

Bar chart: cost per 1,000 calls was $0.27 for 3.5-flash-lite with prompt v1 (73% correct), $0.42 with v2 (78%), and $0.93 for 3.8-flash with v2 (100%).
Only 3.8-flash cleared the 90% bar, at $0.93 per 1,000 calls.

The first 3.8-flash run returned 59 empty answers. The model's thinking tokens used up the 160-token output limit before any JSON came out; setting thinkingBudget: 0 fixed it. The 2.5-second target was missed. This model is served only from the global endpoint (the Seoul and Tokyo regions returned 404), and each call took 2.1 to 2.8 seconds. Because that was still inside the app's 8-second timeout and the button shows a progress indicator, the design doc moved the target to a p95 of 5 seconds.

The AI Studio key never touched the $300 credit

The 402 came from billing. The error read Your prepayment credits are depleted, and authentication had succeeded. In this project, AI Studio billing was prepaid only, so it had no link to the 90-day $300 free-trial credit (held in Cloud Billing) that came with moving the Firebase project to the pay-as-you-go Blaze plan.

At 12:01 the agent wrote that with the $300 credit, paid-tier Gemini API usage would be billed to the same project, and ten minutes later it produced a table showing the credit applied to both. The spec switched to keyless Vertex AI at 12:18, then at 12:21 went back to an AI Studio key the operator had already created, on the grounds that it shared the billing account.

The function, the quota code and the cache were all tested against a fake provider during the day, so the first real Gemini call was the eval run around 10 p.m. When the agent reported the 402 at 22:04, its first recommendation was to buy AI Studio prepaid credit (minimum $5). At 22:06 the operator asked why, if the $300 was supposed to cover it. The agent then said its earlier explanation was wrong: AI Studio keys used to bill a project's Cloud billing account after the fact, and this project didn't work that way.

A commit at 22:10 moved Gemini authentication to Vertex AI with ADC (Application Default Credentials, the service account the code already runs as). Vertex bills through Cloud Billing, which per the design doc is where the credit applies, and there is no API key to store or rotate. The deploy 49 minutes later hit one more snag: declaring an unused secret on the function was enough to make the deploy query Secret Manager and fail with 403, so the secret is now declared only in key mode.

Before building an LLM feature, make one real call and check which bill it lands on. A 200 response does not say which balance paid for it. After the move to Vertex, this app computed its cost from token counts the function records times the list price, and the repository has no record of the credit deduction being checked against a billing report.

The function refuses LLM calls past 3,000 a day

explainCorrection is a single callable function on Cloud Functions for Firebase (2nd gen). The app signs in anonymously when its process opens the first screen, and the Firebase SDK attaches the auth token to each call. The function validates input, checks the cache, takes from the quota, calls the LLM, then stores the result. The cache comes before the quota, so an explanation that already exists costs the user nothing.

ControlDefaultHow to change it
Free quota5 a day per anonymous uidFirestore doc config/limits (cached 60 s)
Subscriber ceiling100 a day per uidSame doc
Global cap3,000 LLM attempts a daySame doc; 0 closes it
App Check enforcementOff (measured only); current setting unconfirmedrequireAppCheck in the same doc
Button in the appOnRemote Config explain_enabled = false

The day rolls over at midnight Korea time. The day key is computed by adding nine hours to UTC milliseconds rather than trusting the server's time zone. The global cap counts LLM attempts, not requests: retries and hedged duplicates each count, and only attempts that failed before billing (network errors and the like) are given back. Timeouts stay counted, because the provider may already have charged for them.

A budget alert sends an email and does not stop charges, which is why the design doc calls the 3,000-call cap the real safeguard. At the eval price ($0.93), a full day at the cap is about $2.8; with the longer v3 prompt from September 28 (about $1.1 per 1,000) it is about $3.3 by our calculation. A call cap works as a money cap only because each call is bounded: the original and corrected words are limited to 60 code points each, and the output to 160 tokens. The agent's morning estimate (under 0.1 won per call, under 1,000 won a month) used flash-lite prices; the chosen model costs 3.4 times flash-lite v1 per 1,000 calls.

In the first release stage the function trusts the app's claim that the user subscribes. A forged claim buys 95 extra explanations a day at under 10 won (well under a cent), so receipt checks were deferred. Putting the daily total document in the same transaction as the user's quota made concurrent requests die on lock timeouts, and on September 28 the total moved outside the transaction. As a result, the global cap can overshoot by the number of requests in flight.

Remote Config refreshed only on the launcher screen, and people who used only the mini window (opened from text selection in another app) never pass through it, so switching off explain_enabled would have left their button in place. The September 29 release review caught it, and the mini window now fetches config before it opens the sheet.

A 7.9-second cold start pushed the app timeout to 15 seconds

The smoke test after the production deploy on the night of September 27 measured 7.9 seconds for the first call and 1.6 to 3.0 seconds after that. The function runs with zero minimum instances, and the app's timeout was 8 seconds, so a cold call came within 0.1 seconds of it. So the app timeout went up to 15 seconds. Keeping one instance warm (about $3 to $4 a month) stayed in the function docs as a fallback if cold starts became a problem. LLM calls on October 26 and 27 cost $0.020 in total, so the warm instance would cost far more than the feature itself.

The next day, one repeated eval call that stretched to 44.5 seconds set the server-side deadlines: 6 seconds per attempt and 12 for the whole request. From October 25 the function hedges: if an attempt is silent for 4 seconds, it sends the same request again and takes whichever answers first. Failures went from 16 of 25 in the October 24 function log to 0 of 16 on October 26 and 27.

3.7% of checking users, 41% of sheet openers

The decision rule went into the decision log before release. Fourteen days in, if 8% or more of users who ran a check tapped the button, keep it; under 3%, test a new position and wording. The October 17 review (October 2 to 16, app 1.6.025 or later) counted 56 of 1,527 checking users, 3.7%, with 77 requests. That sits between the two thresholds.

Over the same period, 138 users (9.0%) tapped a corrected word to open the sheet, and 56 of them pressed the button. 41% of people who reached the sheet wanted the reason, so the bottleneck was finding the sheet rather than the button. Version 1.6.031, rolled out in stages from October 18, added a one-time hint that the highlighted words can be tapped.

SugarSpeller's Korean screen: tapping a corrected phrase opens a sheet headed "Here's what we fixed", with the original words struck through, the corrected words in red, a one-line reason and an OK button.
The sheet that opens when you tap a corrected phrase, in the app's Korean UI (a real screen from the app page, taken before the AI button existed). Its heading reads "Here's what we fixed", followed by the original, the correction and the reason. The "Why is this wrong?" button and its answer appear between the corrected line and the OK button.

With about 8 requests a day, the 14-day cache hit rate was 11% against a 40% target. How the shared cache key was built is covered in the blind A/B that let us stop sending context to the LLM.

Mass-created anonymous accounts hit only the global cap

A uid's quota starts over when the app is reinstalled. A script that keeps creating anonymous accounts makes the per-user limit of 5 meaningless; what remains is the global 3,000 and App Check, which defaults to measuring only. If one explanation were expensive, verify the subscription on the server from day one.

The 59 cases came only from the checkers' red, green and blue groups (Naver red 25, green 13; Daum red 14, green 5, blue 2); there was not one purple or violet case, and the September 27 scoring was done by the same agent that wrote the code. No human scored it. A model that lives only on the global endpoint also changes what a privacy policy can say about where data is processed; another of our apps ran into that with the same model, written up in the privacy-policy note on AI processing location.

Adding the same feature to your app

  1. Make one real model call through a path that bills to Cloud Billing (Vertex AI here). On 402 or 403, stop and settle the billing path first. On 200, check the billing report for the credit deduction.
  2. Pull about 50 cases from your app's test data and put accuracy, p95 and cost per 1,000 calls for each candidate in one table. Write the pass mark first, and set the thinking budget of thinking models to 0.
  3. Expose one callable function that requires anonymous auth, with three limits (free per user, subscriber ceiling, global) keyed by day in your users' time zone. Keep the limits in a document you can change without a deploy, with 0 meaning closed.
  4. Cap input length and output tokens.
  5. Give the app a remote switch that hides the button, and check that every entry screen refreshes it.
  6. After the production deploy, time the first call to a cold instance and set the app timeout above it. Before release, write the decision rule: two usage thresholds, cache hit rate, failure rate and daily cost.

The default limits and the thinking switch (MAX_OUTPUT_TOKENS is 160):

// default limits
export const DEFAULT_LIMITS: Limits = { freePerDay: 5, entitledPerDay: 100, globalPerDay: 3000, requireAppCheck: false };

// Gemini generateContent config, in part
maxOutputTokens: MAX_OUTPUT_TOKENS,
thinkingConfig: { thinkingBudget: 0 },

Still open: usage after the tap hint, and a price doubling

The tap hint in 1.6.031 is judged by whether 6% or more of users who saw it request an explanation. The review was due November 1, and as of November 2 the log has no row for it yet. 3.8-flash is scheduled to double in price on January 1, 2027, to $1.50 input and $7.50 output per million tokens; on that day the prices and the worst-day figure need recalculating. We also haven't seen a real bill yet, since the 90-day credit is still running.

FAQ

Can Firebase Blaze credit pay for the Gemini API?

Per the design doc, only Vertex AI is on the credit path in this project; we haven't confirmed it on an invoice yet. The AI Studio key was on prepaid-only billing, so all 59 eval calls on September 27, 2026 were rejected with 402.

How do you cap the cost of an LLM feature?

Count LLM calls inside the function and refuse them past a daily limit. A budget alert only sends an email. This app closes at 3,000 calls a day, and one Firestore document can change that number or set it to 0 without a deploy.

This note is written from values SugarMount measured itself; an AI coding agent was used to draft it.

All AX notes