Content
Idempotent model calls: no repeat paid for, no call lost
Pictures from Gemini and OpenAI had to keep the style of an approved reference. Every call is billed, an answer can take minutes, and a restart must not lose a call.
Under NDA
The company’s name and logo are under NDA. We left out the details that could identify it.
- Repeats
- a repeated ask returns the stored result and calls no model
- Queue
- a call is taken by one worker only
- Restarts
- a call cut off by a restart is taken again
The problem
Every picture had to keep the style of an approved reference. Each call to Gemini or OpenAI is billed and can take minutes: a request that waits for it times out, and a restart in the middle loses the call along with the money spent on it.
What we did
01Gemini and OpenAI draw the same test picture. The approved one becomes the reference: it fixes the style and the model and is attached to every later call.
02Every ask is idempotent. Its instruction, references and model form its key, and an ask whose key matches the last one calls no model and returns the stored result. A double click or a retry after a timeout costs nothing. A call whose input changed before it ran is dropped unasked.
03Every call is a row in one PostgreSQL table, and the request that queues it answers at once. A worker takes the oldest free row under a lock that skips rows already taken, so two workers never take one call. The row is deleted in the transaction that stores the answer. Image and text calls wait in separate queues: a slow picture holds up no text.
04A row taken and left unanswered past its timeout, as after a restart, is taken again ahead of newer ones. Takes are capped, so a call that keeps failing ends with an error and stops spending money.
Result
A repeated ask costs nothing, a restart loses no call, and two workers never take one. No request waits for a model: the page picks up the result when it is ready. The tests replay recorded Gemini and OpenAI answers and never call a paid API.
Technologies
- ASP.NET Core
- PostgreSQL
- Gemini API
- OpenAI API
Check your own system
Three questions on this topic. After each one, a short note on why it matters.
Does a restart leave every unfinished model call in the queue?
Why it matters
A call held only in memory dies with the process: the money is spent and the user gets nothing.
Is every paid model call idempotent?
Why it matters
A double click or a retry after a timeout pays for the same answer again.
Do your tests run without calling a paid API?
Why it matters
Tests that call a model are billed on every run and fail whenever its answer changes.
Looks like this part is in order
If you want someone to look at the rest of the system from the outside, write to us.
There is something to check
Incidents usually start in places like these. Write to us about how this part of your system works, and we will suggest what to check first.
This is worth looking at first
There are several weak spots, and the rest of the system probably has some too. Write to us, and we will work out what to fix first.
If your mail app did not open, here is the address: hello@infiniteloop.tech
Have a similar problem?
Write to us about what is going on. We reply within 2 business days.