All case studies

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.

  • Content
  • AI
  • Backend

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


  1. 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.

  2. 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.

  3. 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.

  4. 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

Have a similar problem?

Write to us about what is going on. We reply within 2 business days.

Write to us


More case studies