Dev.to AI πŸ€– Ai πŸ‘ 0 πŸ“– 4 min read

Calling an LLM on every keystroke made one 233-character message cost 200x more

Live AI features feel great in a demo: the user types, and a suggestion updates under the text box. The simplest way to build one is to call the model in the input's change handler. That handler runs on every key press,

Live AI features feel great in a demo: the user types, and a suggestion updates under the text box. The simplest way to build one is to call the model in the input's change handler. That handler runs on every key press, so one short message becomes hundreds of paid calls. The usual fix, a 300 ms debounce, helps less than you'd expect, because people often pause longer than that in the middle of a sentence.

A few words first

A token is a chunk of text, roughly three quarters of an English word. APIs bill per million tokens: input is what you send, output is what the model writes. A system prompt is the fixed instructions you send along with every call. A debounce is a timer that waits for a pause: each key press restarts it, and your code runs only when the timer finally expires.

The setup

My example is a support form that suggests a subject line while the customer is typing. The system prompt is 90 tokens. The message is three sentences about a broken desk lamp: 46 words, 233 characters, 52 tokens. The suggested subject is 7 tokens. I counted message text with tiktoken, OpenAI's tokenizer library, using the o200k_base encoding.

Prices are gpt-5.6 list prices, $2 per million input tokens and $12 per million output, from the catalog snapshot of the LLM gateway I work on (file dated November 25, 2026). The monthly column assumes 1,000 messages a day for 30 days.

For the debounce rows I needed a typing rhythm, so I simulated one: 2,000 typists with a random gap after each key. The median gap is 0.16 seconds inside a word, 0.28 seconds after a space and 1.2 seconds after the end of a sentence. The median simulated typist needs about 50 seconds for the message. The rhythm is an assumption. The call counts in the first and last rows need no simulation.

When you call the model Calls per message Cost per message Per month
Every key press 233 $0.0736 $2,207
300 ms debounce 42.4 $0.0135 $404
500 ms debounce 10.6 $0.0034 $102
1 second debounce 2.6 $0.0009 $26
Once, when the text is finished 1 $0.0004 $11

Why it adds up so fast

Each call sends the system prompt and the whole message so far. Across all 233 calls that's 27,003 input tokens, for a message that contains 52. About 21,000 of those are the same 90-token system prompt sent 233 times.

The multiplier barely depends on the model. Every key press against a single call is 199.9x on gpt-5.6 and gpt-5.6-mini, and 198.6x on claude-haiku-5.5, claude-sonnet-5.5 and gemini-3.8-flash. (I used the same token counts for all of them. Other vendors' tokenizers will differ a little.) A cheap model makes the waste cheaper: claude-haiku-5.5 costs $105 a month instead of $2,109 on claude-sonnet-5.5. It's still about 200 times what the feature needs.

My simulation also has no typos. In a real form, every backspace is another call.

Why 300 ms isn't enough

A debounce fires whenever a pause is longer than its timer. With my typing rhythm, about 44% of the gaps after a space and 10.5% of the gaps inside words are longer than 300 ms. That adds up to 42 calls for a 46-word message. Calling once per word would be 46 calls and $439 a month, so this debounce is barely better.

A short debounce makes sense for a search box, where each request is cheap and the answer should feel instant. Here the suggestion is for half a sentence, and the next word replaces it. A one-second wait mostly fires at sentence ends, where the text makes sense and the user may actually look at the suggestion.

Cancelling doesn't undo it

A common next step is to cancel the previous request when a new one starts, with AbortController in the browser. Do that, because it stops an old answer from landing on top of a newer one. But cancelling doesn't unsend your input. If every cancelled call is billed for input only, and just the last one for output, the key-press version still costs $1,623 a month, 147 times the single call. How a cancelled call is billed varies by provider, so check your usage page.

The fix

Wait for a real pause, skip text that's too short, and skip text you've already sent:

let timer;
let lastSent = "";

function onType(text) {
  clearTimeout(timer);
  timer = setTimeout(() => {
    const t = text.trim();
    if (t.length < 20 || t === lastSent) return;
    lastSent = t;
    askModel(t);
  }, 1000);
}

Here askModel is your own function that calls your backend. The length check stops a call after "Hi there", and the lastSent check stops one when only a space was added. I haven't priced those two checks: the savings in the table come from the timer alone.

If the suggestion isn't needed until the user finishes, use a button or the moment they leave the field. That's the $11 row.

What this doesn't solve

A one-second wait makes the feature feel slower, and no price table tells you whether your users will mind. For real autocomplete, where the suggestion must appear mid-word, a pause-based trigger is the wrong tool. Look at a small model, a very short prompt, or ordinary non-AI completion.

The debounce numbers depend on my invented rhythm. A slow typist who pauses a second between words will trigger far more than 2.6 calls. Log your own calls per finished message before you pick a timer.

Debouncing in the browser is also not a spending limit. Anyone can call your backend directly, so you still need a per-user cap on the server. And prompt caching, where a provider discounts a repeated prefix, isn't included here. It usually needs a much longer prompt than 90 tokens.

πŸ“° Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.