<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/idempotent-background-workers-for-llm-calls-in-rails-danial-asghar-ic5brx4vx" -->

---
title: Idempotent Background Workers for LLM Calls in Rails —...
description: When an LLM call runs inside a Sidekiq background job, at-least-once delivery semantics can cause double-billing and inconsistent classifications on retry. The...
canonical: https://daily.dev/posts/idempotent-background-workers-for-llm-calls-in-rails-danial-asghar-ic5brx4vx
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Idempotent Background Workers for LLM Calls in Rails — Danial Asghar | daily.dev
og:description: When an LLM call runs inside a Sidekiq background job, at-least-once delivery semantics can cause double-billing and inconsistent classifications on retry. The...
og:url: https://daily.dev/posts/idempotent-background-workers-for-llm-calls-in-rails-danial-asghar-ic5brx4vx
og:image: https://api.daily.dev/og/posts/IC5brx4Vx.png
og:image:alt: Idempotent Background Workers for LLM Calls in Rails — Danial Asghar
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Idempotent Background Workers for LLM Calls in Rails — Danial Asghar

**[Ruby Flow](https://daily.dev/sources/rubyflow)** · 14 min read · 0 upvotes · 0 comments

## Summary

When an LLM call runs inside a Sidekiq background job, at-least-once delivery semantics can cause double-billing and inconsistent classifications on retry. The post walks through building idempotent Rails workers: starting with a status-based completion gate keyed on the ticket UUID, then explaining its limitation under concurrent duplicate enqueues, and presenting an atomic database-level claim (UPDATE WHERE status IN pending/failed) as the correct fix. Additional failure modes are covered — crash after model call but before write, stuck processing rows needing a reaper, and the transactional outbox pattern for exactly-once enqueue. Common mistakes (per-attempt keys, find_or_create_by without a unique index, swallowing errors) and tradeoffs (eventual consistency, extra state, response caching cost) are also addressed.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://danialasghar.com/blog/idempotent-ai-workers-rails>

## Similar posts on daily.dev

- [From Delayed Job to Solid Queue: How a 10-Year Rails App Finally Achieved Linear Scaling](https://daily.dev/posts/from-delayed-job-to-solid-queue-how-a-10-year-rails-app-finally-achieved-linear-scaling-f4yyqwfox) · RUBYLAND · 0 upvotes · 0 comments
- [Everything you should know about Background Jobs with ActiveJob](https://daily.dev/posts/everything-you-should-know-about-background-jobs-with-activejob-uxyudjznl) · The Miners · 0 upvotes · 0 comments
- [Fair by design: orchestrating background jobs in Ruby—Martian Chronicles, Evil Martians’ team blog](https://daily.dev/posts/fair-by-design-orchestrating-background-jobs-in-ruby-martian-chronicles-evil-martians-team-blog-piyszlqky) · Martian Chronicles · 0 upvotes · 0 comments
- [The Execution Boundary in Rails](https://daily.dev/posts/the-execution-boundary-in-rails-vyn9zhtyw) · RUBYLAND · 0 upvotes · 0 comments

---

Tags: [#rails](https://daily.dev/tags/rails)

[View this post on daily.dev](https://daily.dev/posts/idempotent-background-workers-for-llm-calls-in-rails-danial-asghar-ic5brx4vx)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Idempotent Background Workers for LLM Calls in Rails — Danial Asghar","url":"https://daily.dev/posts/idempotent-background-workers-for-llm-calls-in-rails-danial-asghar-ic5brx4vx","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/idempotent-background-workers-for-llm-calls-in-rails-danial-asghar-ic5brx4vx"},"datePublished":"2026-07-08T20:20:55.626Z","dateModified":"2026-07-08T20:21:20.534Z","description":"When an LLM call runs inside a Sidekiq background job, at-least-once delivery semantics can cause double-billing and inconsistent classifications on retry. The...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/2fea523e4527baad08bff568a5bb80ab?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/2fea523e4527baad08bff568a5bb80ab?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Ruby Flow","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Ruby Flow","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/a21aaa8bcbbf448193a016895c90a0e1","url":"https://daily.dev/sources/rubyflow"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/idempotent-background-workers-for-llm-calls-in-rails-danial-asghar-ic5brx4vx","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"rails","timeRequired":"PT14M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Ruby Flow","item":"https://daily.dev/sources/rubyflow"},{"@type":"ListItem","position":3,"name":"Idempotent Background Workers for LLM Calls in Rails — Danial Asghar"}]}
```

