<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ram Ji Tripathi</title>
    <description>The latest articles on DEV Community by Ram Ji Tripathi (@ramji_tripathi_095c7f4810).</description>
    <link>https://dev.to/ramji_tripathi_095c7f4810</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2066272%2F32e36d61-8064-47fa-9de4-c8b24132fa4b.png</url>
      <title>DEV Community: Ram Ji Tripathi</title>
      <link>https://dev.to/ramji_tripathi_095c7f4810</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ramji_tripathi_095c7f4810"/>
    <language>en</language>
    <item>
      <title>Designing Human-in-the-Loop AI: Why an AI-Generated Reply Should Be a Draft, Not an Action</title>
      <dc:creator>Ram Ji Tripathi</dc:creator>
      <pubDate>Wed, 07 Oct 2026 15:04:19 +0000</pubDate>
      <link>https://dev.to/ramji_tripathi_095c7f4810/designing-human-in-the-loop-ai-why-an-ai-generated-reply-should-be-a-draft-not-an-action-55a0</link>
      <guid>https://dev.to/ramji_tripathi_095c7f4810/designing-human-in-the-loop-ai-why-an-ai-generated-reply-should-be-a-draft-not-an-action-55a0</guid>
      <description>&lt;h2&gt;
  
  
  1. The Dangerous Shortcut Is Treating Generation as Action
&lt;/h2&gt;

&lt;p&gt;An LLM returning plausible text is a different event from an application committing that text to a customer conversation. The first produces a candidate. The second changes application state under an authenticated user's authority. Connecting the two is an architectural decision, not an inevitable next step after generation.&lt;/p&gt;

&lt;p&gt;It is tempting to connect a successful generation response directly to a message-creation request. The integration is short: get text, submit text, display success. But that connection quietly turns a candidate answer into an application decision. It also makes provider completion the trigger for a user-visible side effect.&lt;/p&gt;

&lt;p&gt;In my AI Support Assistant, generation returns text to a suggestion preview. A person chooses &lt;strong&gt;Use Reply&lt;/strong&gt; to move it into an editable composer, then separately chooses &lt;strong&gt;Send&lt;/strong&gt; to submit the current content through the message API. The model supplies a candidate; the application keeps commitment behind a different operation.&lt;/p&gt;

&lt;p&gt;This is a specific choice for a support-reply workflow. It does not establish that every AI operation needs permanent manual approval. It gives me a concrete place to examine authority: who can propose a change, who controls the working copy, and which request commits it?&lt;/p&gt;

&lt;h2&gt;
  
  
  2. The Boundary in My AI Support Assistant
&lt;/h2&gt;

&lt;p&gt;The relevant screen is a ticket detail page with a conversation, an AI Reply Assistant, and a message composer. React handles the interface; an Express backend generates suggestions and persists messages. That is enough application context for this discussion. The broader system is covered in &lt;a href="https://blog.ramjwork.in/ai-engineering/ai-customer-support-architecture" rel="noopener noreferrer"&gt;How I Architected an AI Customer Support System&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The exact interaction matters. &lt;strong&gt;Generate AI Reply&lt;/strong&gt; requests a suggestion and displays it in a separate preview. It does not automatically fill the composer. &lt;strong&gt;Use Reply&lt;/strong&gt; transfers the displayed text into the composer. &lt;strong&gt;Regenerate&lt;/strong&gt; requests another candidate. &lt;strong&gt;Send&lt;/strong&gt; submits the current composer content.&lt;/p&gt;

&lt;p&gt;A user can leave a suggestion unused, regenerate it, or use and edit it. The main workflow controls are Generate AI Reply, Use Reply, Regenerate, and Send. Use Reply changes client working state; it does not approve a message for immediate sending.&lt;/p&gt;

&lt;p&gt;The backend generation controller reads messages for the authorized ticket in ascending creation order. It formats each as &lt;code&gt;ROLE: content&lt;/code&gt; and passes that conversation to the reply service. Although the request identifies a ticket, this path does not add the ticket title or description to the prompt. An unsent composer draft is also absent: the generation request sends a ticket identifier, not the current input text.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Three Different States: Output, Draft, and Action
&lt;/h2&gt;

&lt;p&gt;The reusable model is &lt;strong&gt;model output → human-owned working draft → authoritative side effect&lt;/strong&gt;. Use Reply crosses the first boundary. A successful message creation crosses the second; clicking Send attempts that operation. The words may initially be identical, but their ownership and consequences change.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Stage&lt;/th&gt;
&lt;th&gt;Representation in this implementation&lt;/th&gt;
&lt;th&gt;Owner / source of truth&lt;/th&gt;
&lt;th&gt;Consequence&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Model output&lt;/td&gt;
&lt;td&gt;Returned suggestion, displayed in the AI panel&lt;/td&gt;
&lt;td&gt;Generation response held by the mutation&lt;/td&gt;
&lt;td&gt;Candidate text; no conversation message created&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Human-owned working draft&lt;/td&gt;
&lt;td&gt;Composer content after Use Reply&lt;/td&gt;
&lt;td&gt;Local React state&lt;/td&gt;
&lt;td&gt;User can change or erase the working copy&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Authoritative side effect&lt;/td&gt;
&lt;td&gt;Message persisted through the message API&lt;/td&gt;
&lt;td&gt;Backend and persisted message record&lt;/td&gt;
&lt;td&gt;Successful creation adds a saved message&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;“Human-owned” describes control of the draft, not a claim that the person authored every word. The user might send the suggestion unchanged. The important distinction is that edits and commitment belong to the application workflow rather than being implicit consequences of model output.&lt;/p&gt;

&lt;p&gt;TanStack Query holds the generation mutation result and the message query cache. That does not make both values equivalent server-domain records. A suggestion response is not a saved message, and a cached message is a client representation of a backend record.&lt;/p&gt;

&lt;p&gt;The composer uses local state initialized from the selected suggestion. This ownership distinction overlaps with &lt;a href="https://blog.ramjwork.in/frontend/react-state-architecture" rel="noopener noreferrer"&gt;React State Architecture&lt;/a&gt;, but the concern here is authority. Choosing local state is useful because unfinished text should remain working input until a separate operation commits it.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Generation and Sending Are Separate Operations
&lt;/h2&gt;

&lt;p&gt;The frontend generation function posts to an AI-specific endpoint. This function excerpt omits imports and the timeout constant, which is set to 60 seconds:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getReplySuggestion&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ticketId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ApiResponse&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ReplySuggestionResult&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s2"&gt;`/ai/tickets/&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;ticketId&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;/reply-suggestion`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;REPLY_SUGGESTION_TIMEOUT_MS&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Source: &lt;code&gt;frontend/src/features/ai/api/ai.api.ts&lt;/code&gt;, &lt;code&gt;getReplySuggestion&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The corresponding hook is a generation mutation. The reply service returns &lt;code&gt;suggestion&lt;/code&gt; and &lt;code&gt;tokensUsed&lt;/code&gt;; it does not call message creation. OpenAI is the primary provider, with an optional configured Gemini fallback for eligible failures. Either provider still supplies candidate text to the same workflow.&lt;/p&gt;

&lt;p&gt;The generation controller's sequence is visible in this simplified excerpt. Parameter extraction, the message query details, and response wrapping are omitted:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;getAuthorizedTicketService&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ticketId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;role&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;role&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="c1"&gt;// Read ticket messages, ordered by createdAt ascending.&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;conversation&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;formatConversation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;aiResult&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;generateReplySuggestion&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;conversation&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;ticketId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;requestId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;string&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Source: &lt;code&gt;backend/src/modules/ai/ai.controller.ts&lt;/code&gt;, &lt;code&gt;generateReplySuggestionController&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Sending uses a different frontend function and mutation, posting content to &lt;code&gt;/tickets/${ticketId}/messages&lt;/code&gt;. Its backend controller derives the sender from the authenticated request and invokes the message service. These endpoint paths are relative to the application's API base.&lt;/p&gt;

&lt;p&gt;Generation calls an external provider, produces operational logs, and performs a ticket-access lookup that can populate a cache. The boundary concerns a specific side effect: this suggestion path does not create or send a customer message. Provider activity and operational writes still happen, but they do not add the candidate to the conversation.&lt;/p&gt;

&lt;p&gt;For a future extension, I would preserve this separation in the service contract as well as the screen. A caller asking for a suggestion should receive candidate data, without needing to know how conversation writes work. A caller committing a message should supply the intended content and pass the application’s authorization checks. Moving the same feature to another client should not accidentally change the meaning of generation into permission to send. This is a design recommendation derived from the current boundary, rather than a claim about additional clients in my application.&lt;/p&gt;

&lt;p&gt;Architecturally, separate operations let completion mean different things. Generation completion means a candidate is available. Message completion means the application has accepted a send request and returned a created record. Neither status should silently stand in for the other.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Human Review Is Part of the Architecture, Not Just the UI
&lt;/h2&gt;

&lt;p&gt;The handoff is explicit in the page component. These are two excerpts from the same component, with layout omitted:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;onUseReply&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{(&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;setComposerDraft&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;setComposerKey&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;current&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}}&lt;/span&gt;

&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;MessageComposer&lt;/span&gt;
  &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;composerKey&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
  &lt;span class="na"&gt;ticketId&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
  &lt;span class="na"&gt;initialContent&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;composerDraft&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
  &lt;span class="na"&gt;onMessageSent&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class="nf"&gt;setMessageRefreshToken&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;current&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="si"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Source: &lt;code&gt;frontend/src/features/tickets/pages/TicketDetailPage.tsx&lt;/code&gt;, &lt;code&gt;TicketDetailPage&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Changing the key remounts the composer, whose local content starts from &lt;code&gt;initialContent&lt;/code&gt;. If a person has already typed a draft, clicking Use Reply replaces that content. The current handoff offers neither a merge nor a confirmation dialog. That is a UX limitation of adopting a suggestion, even though the send boundary remains separate.&lt;/p&gt;

&lt;p&gt;Regenerating alone does not invoke this callback, so a new preview does not automatically replace the composer. The replacement happens when the user chooses Use Reply. That detail matters when explaining what control the person actually has.&lt;/p&gt;

&lt;p&gt;Inside the composer, the textarea's value comes from local &lt;code&gt;content&lt;/code&gt;, and its change handler updates that state. The send handler reads the current value, so user edits become the submitted text. There is no requirement that it continue matching the original suggestion.&lt;/p&gt;

&lt;p&gt;The sequence gives a person two distinct decisions: whether to adopt the candidate as working text, and whether to commit the current draft. They can send it unchanged or click through without carefully reading. The server receives content and an authenticated sender, without proof of deliberation. Human control is explicit; meaningful review is still a behavior the interface cannot prove.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Follow One Reply Through the System
&lt;/h2&gt;

&lt;p&gt;Consider an authorized support agent looking at a ticket assigned to them. They request a reply. The backend checks ticket access, reads persisted conversation messages, and generates candidate text. The frontend displays that result without creating a conversation entry.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Authorized generation request
        ↓
Persisted ticket-message context
        ↓
AI generation → returned candidate
        ↓
Suggestion preview
        ↓
Explicit Use Reply
        ↓
Editable composer (local working draft)
        ↓
Explicit Send (separate message mutation)
        ↓
Message API: authentication + ticket authorization
        ↓
Persisted message (authoritative conversation state)
        ↓
Ticket timestamp update + summary enqueue
        ↓
Successful response → append record to query cache
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the current reply flow, including the service work between persistence and response. It is a workflow diagram, not a claim that every arrow is atomic.&lt;/p&gt;

&lt;p&gt;The composer validates nonblank input and submits the trimmed current content. This simplified excerpt preserves the success and failure behavior:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;createMessageMutation&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mutateAsync&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;trim&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="nf"&gt;setContent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;setFieldError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;onMessageSent&lt;/span&gt;&lt;span class="p"&gt;?.();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;setSubmitError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nf"&gt;getTicketErrorMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Unable to send message. Please try again.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Source: &lt;code&gt;frontend/src/features/messages/components/MessageComposer.tsx&lt;/code&gt;, &lt;code&gt;handleSubmit&lt;/code&gt;; validation precedes this block.&lt;/p&gt;

&lt;p&gt;After a successful response, the message hook appends the returned record to the ticket's query cache:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;onSuccess&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newMessage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;queryClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setQueryData&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Message&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;messageKeys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ticket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ticketId&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;existing&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;existing&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;existing&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;newMessage&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;newMessage&lt;/span&gt;&lt;span class="p"&gt;]),&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Source: &lt;code&gt;frontend/src/features/messages/hooks/useMessages.ts&lt;/code&gt;, &lt;code&gt;useCreateMessage&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The cache receives the returned record after success. There is no optimistic message insertion in this hook. The page also increments a refresh token, which the thread uses for scrolling rather than automatic query refetching. This reconciles the returned message into the local list; it does not establish complete consistency across clients.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Failure Becomes Easier to Contain When Suggestion Is Not Action
&lt;/h2&gt;

&lt;p&gt;If generation fails, the AI panel displays an error and offers Try Again. Its mutation error does not call the composer handoff or the send mutation. The generation error path leaves the existing composer content untouched. The interface also handles a returned suggestion that becomes empty after trimming, offering another attempt.&lt;/p&gt;

&lt;p&gt;If a candidate is never used or sent, it does not become a conversation message through this path. The suggestion panel resets its mutation state when the ticket identifier changes. The inspected components do not implement durable draft saving. An unused suggestion or unsent draft is not a persisted review record in this workflow.&lt;/p&gt;

&lt;p&gt;If sending fails, the composer catches the error and displays a send failure. Its content is cleared only after the awaited mutation succeeds, so the draft remains available on the error path. While sending is pending, the textarea is disabled. These are useful local behaviors, but they do not settle what happened on the server.&lt;/p&gt;

&lt;p&gt;The message service first persists the message, then updates the ticket timestamp, then awaits a queue enqueue for summary work. These steps are not wrapped in a transaction in this function. If the timestamp update or enqueue throws, the request can fail after the message has been saved. The client retains its draft, but that does not mean the conversation is unchanged.&lt;/p&gt;

&lt;p&gt;More generally, a lost response can also leave a client uncertain about a completed write. The practical distinction is between failure to receive confirmation and confirmed absence of a side effect. A client-visible send error does not necessarily prove that message creation did not happen.&lt;/p&gt;

&lt;p&gt;A stronger send design could add idempotency and deliberate recovery for uncertain outcomes. That is proposed work, not an existing guarantee. The important current separation is that retrying generation does not itself create another customer message. Retrying a send belongs to a different failure domain and needs different reasoning.&lt;/p&gt;

&lt;p&gt;Keeping suggestion separate from action contains a generation problem at the candidate stage. Send still has its own persistence and recovery concerns. This separation gives each failure a clearer meaning, while leaving uncertain writes to be handled explicitly in the message workflow.&lt;/p&gt;

&lt;h2&gt;
  
  
  8. Authorization Still Belongs on the Server
&lt;/h2&gt;

&lt;p&gt;The ticket page shows the AI panel to agents and administrators. That is a presentation rule. A browser user can construct requests independently of which controls are visible, so the backend must decide what each request is allowed to do.&lt;/p&gt;

&lt;p&gt;The AI route authenticates the request, applies its rate limiter, and restricts generation to AGENT and ADMIN roles. The controller then checks access to the particular ticket. The ticket authorization policy allows administrators access across tickets, agents access to assigned tickets, and customers access to their own tickets. Customers still cannot generate replies because the AI route has the additional role restriction.&lt;/p&gt;

&lt;p&gt;Message creation has different permissions. Its route authenticates and validates content, while its service checks ticket access before persistence. It does not have the AI route's staff-only role gate. Customers can create messages on their own tickets; agents on assigned tickets; administrators across tickets.&lt;/p&gt;

&lt;p&gt;The essential check is short:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;createMessageService&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;CreateMessageInput&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;getAuthorizedTicketService&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ticketId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;accessContext&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;createMessageRepository&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;ticketId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ticketId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;senderId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;senderId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="c1"&gt;// Ticket timestamp update and summary enqueue follow before return.&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Source: &lt;code&gt;backend/src/modules/messages/message.service.ts&lt;/code&gt;; simplified excerpt, not the complete function.&lt;/p&gt;

&lt;p&gt;The controller takes &lt;code&gt;senderId&lt;/code&gt; from authenticated identity rather than a body-supplied author. The shared ticket service checks access on both its cached-ticket and database lookup paths. These checks establish application authority for the request; they do not validate the factual truth of the message.&lt;/p&gt;

&lt;p&gt;Separating generation permission from message permission also keeps the capabilities understandable. Being allowed to obtain a candidate and being allowed to add a message are distinct decisions, even when the same staff member can do both.&lt;/p&gt;

&lt;h2&gt;
  
  
  9. What This Design Does Not Solve
&lt;/h2&gt;

&lt;p&gt;The reply prompt asks for concise, polite, solution-oriented text and instructs the model not to invent policies, promise refunds, or fabricate technical actions. Those are prompt instructions. The inspected reply path does not independently verify the answer against an authoritative policy source or enforce those instructions with a deterministic validator.&lt;/p&gt;

&lt;p&gt;There is no reply confidence threshold or factual-verification stage in this path. Human editing can correct an answer, but human review does not guarantee correctness or eliminate hallucinations. The implementation also does not establish that every suggestion is reviewed before submission.&lt;/p&gt;

&lt;p&gt;Context can become stale. Generation reads persisted messages at request time; the user may send later, after the conversation has changed. The inspected send request contains content, without a conversation-version precondition binding it to the generation snapshot. An empty message history also means this generation path has no conversation text; identifying the ticket does not automatically supply its description.&lt;/p&gt;

&lt;p&gt;Provenance is a separate concern. AI operation logs include operational context and outcomes, but the inspected reply path does not establish a persisted linkage showing that a sent message originated from a particular AI suggestion. The Message model has no suggestion reference or AI-authorship field. Existing AI interaction storage elsewhere does not establish that linkage for this workflow.&lt;/p&gt;

&lt;p&gt;The human boundary does not establish policy compliance, production safety, complete observability or provenance, or measured reliability improvements. The inspected reply path also has no automated policy engine or escalation step. What this implementation demonstrates is narrower and useful: a person has control over the working text before a separately authorized request attempts to commit it.&lt;/p&gt;

&lt;h2&gt;
  
  
  10. When Would I Allow More Automation?
&lt;/h2&gt;

&lt;p&gt;The following framework is proposed guidance for future designs. It is not implemented in this application. I would evaluate the action being committed, rather than treating every model output as equally risky.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Generic action&lt;/th&gt;
&lt;th&gt;Reversible?&lt;/th&gt;
&lt;th&gt;Potential harm&lt;/th&gt;
&lt;th&gt;Validation available?&lt;/th&gt;
&lt;th&gt;Human approval approach&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Save a private draft&lt;/td&gt;
&lt;td&gt;Usually easy to revise&lt;/td&gt;
&lt;td&gt;Lost or misleading working text&lt;/td&gt;
&lt;td&gt;Schema and storage checks&lt;/td&gt;
&lt;td&gt;May be unnecessary if replacement is controlled&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Suggest an internal category&lt;/td&gt;
&lt;td&gt;Usually reversible&lt;/td&gt;
&lt;td&gt;Misrouting or delayed handling&lt;/td&gt;
&lt;td&gt;Allowed categories and routing rules&lt;/td&gt;
&lt;td&gt;Depends on impact and recovery&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Send a customer reply&lt;/td&gt;
&lt;td&gt;Cannot retract what was read&lt;/td&gt;
&lt;td&gt;Incorrect advice or commitments&lt;/td&gt;
&lt;td&gt;Approved facts and policy checks may help&lt;/td&gt;
&lt;td&gt;Retain review until evidence supports narrower automation&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Issue a refund&lt;/td&gt;
&lt;td&gt;Sometimes difficult to reverse&lt;/td&gt;
&lt;td&gt;Financial loss or inconsistent treatment&lt;/td&gt;
&lt;td&gt;Account, amount, and eligibility rules&lt;/td&gt;
&lt;td&gt;Require explicit authority appropriate to the risk&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Delete customer data&lt;/td&gt;
&lt;td&gt;Often hard to reverse&lt;/td&gt;
&lt;td&gt;Data loss and account harm&lt;/td&gt;
&lt;td&gt;Identity, scope, retention rules&lt;/td&gt;
&lt;td&gt;Strong approval and recovery requirements&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Reversibility is only one dimension. A reversible change applied to thousands of accounts may have a larger blast radius than one irreversible but tightly bounded action. I would examine scope, domain risk, and the cost of a wrong action together.&lt;/p&gt;

&lt;p&gt;Validation should come from something stronger than the model's confident wording. Deterministic checks can constrain amounts, destinations, allowed operations, and required permissions. Trusted domain data can support specific factual checks. Neither approach automatically verifies every sentence, but each can reduce uncertainty about a defined commitment.&lt;/p&gt;

&lt;p&gt;I would also require observability and a recovery plan: which input led to the action, who or what authorized it, how failures are detected, and how operators can intervene. If human review is unavailable, that does not itself justify removing the gate; it changes the service and fallback design.&lt;/p&gt;

&lt;p&gt;Before widening automation, I would define what evidence could overturn the decision to require review. That might include evaluation against representative cases, checks for forbidden commitments, and a demonstrated way to detect and recover from mistakes. I would also define conditions that return the workflow to human handling, such as missing domain information or an action outside the permitted scope. These are future decision criteria; the current application does not implement this automation framework.&lt;/p&gt;

&lt;p&gt;Automation can then expand within a bounded class of actions, with evidence appropriate to that class. A decision to automate private drafts should not silently authorize refunds or customer-facing messages. Permission should follow the side effect, with separate evaluation when its consequence changes.&lt;/p&gt;

&lt;h2&gt;
  
  
  11. A Practical Human-in-the-Loop Design Checklist
&lt;/h2&gt;

&lt;p&gt;For an AI-assisted workflow, I would ask these questions before connecting generation to commitment:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What exactly does the model return, and which operation creates the domain record?&lt;/li&gt;
&lt;li&gt;Where can a person inspect and edit the candidate before commitment?&lt;/li&gt;
&lt;li&gt;Does adopting a suggestion replace existing work, and is that behavior clear?&lt;/li&gt;
&lt;li&gt;Which server checks authorize generation and which authorize the action?&lt;/li&gt;
&lt;li&gt;What context was used, and can it change before commitment?&lt;/li&gt;
&lt;li&gt;What does each error prove, particularly after partial persistence?&lt;/li&gt;
&lt;li&gt;Does the client reconcile a confirmed record or merely assume success?&lt;/li&gt;
&lt;li&gt;What evidence would be needed to automate this particular side effect?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These questions are useful even when the model and prompt change. They expose responsibilities that belong to the application: working-state ownership, permissions, persistence, recovery, and the meaning of a successful response.&lt;/p&gt;

&lt;p&gt;For my current reply workflow, the answer is an editable draft and a separate send request. Strengthening draft replacement, uncertain-send recovery, and provenance would be additional work. The existing boundary gives those improvements a clear place to attach without pretending they are already present.&lt;/p&gt;

&lt;h2&gt;
  
  
  12. Closing
&lt;/h2&gt;

&lt;p&gt;In my reply workflow, Use Reply makes candidate text editable working state. Send separately asks the backend to create the message under the authenticated user's authority. The model's output supplies content; the application defines the commitment boundary.&lt;/p&gt;

&lt;p&gt;When integrating AI, ask who has authority to commit the side effect, then make that authority explicit in the interface and the server operation.&lt;/p&gt;




&lt;p&gt;Originally published on my engineering blog: &lt;a href="https://blog.ramjwork.in/ai-engineering/human-in-the-loop-ai-reply-design" rel="noopener noreferrer"&gt;https://blog.ramjwork.in/ai-engineering/human-in-the-loop-ai-reply-design&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>node</category>
      <category>ai</category>
      <category>architecture</category>
    </item>
    <item>
      <title>React State Architecture: What Belongs in Local State, Context, URL State and TanStack Query?</title>
      <dc:creator>Ram Ji Tripathi</dc:creator>
      <pubDate>Tue, 06 Oct 2026 15:26:19 +0000</pubDate>
      <link>https://dev.to/ramji_tripathi_095c7f4810/react-state-architecture-what-belongs-in-local-state-context-url-state-and-tanstack-query-2b5j</link>
      <guid>https://dev.to/ramji_tripathi_095c7f4810/react-state-architecture-what-belongs-in-local-state-context-url-state-and-tanstack-query-2b5j</guid>
      <description>&lt;h2&gt;
  
  
  1. The State Management Question Is Usually Asked Backwards
&lt;/h2&gt;

&lt;p&gt;A fetched ticket, the currently selected ticket, an authenticated user, an editable reply, and a search value are all “state.” They still need different owners. The server owns the saved ticket. Navigation identifies the selected ticket. The user controls the unfinished reply. A search value may be temporary input or a committed view that a link should preserve.&lt;/p&gt;

&lt;p&gt;A support agent moving from reading a conversation to generating, editing, and sending a reply crosses several of those boundaries on one screen. Choosing a global-state library first skips the harder questions: who can change each value, how long should it survive, and what makes it authoritative?&lt;/p&gt;

&lt;p&gt;My AI Support Assistant provides a concrete example. Its React and TypeScript frontend uses Context for the current session, TanStack Query for ticket and message data, React Router for navigation, and component state for editable forms. Shareable search and filter controls are a proposed addition. The useful architectural lesson is how these responsibilities fit together, including the boundaries I still need to strengthen.&lt;/p&gt;

&lt;p&gt;In &lt;a href="https://blog.ramjwork.in/frontend/react-dashboard-system-design" rel="noopener noreferrer"&gt;Frontend System Design: How I Would Architect a Production Dashboard in React&lt;/a&gt;, I considered the broader dashboard architecture. Here I want to isolate one question: how should state ownership be divided inside it? I start with ownership, lifetime, and source of truth, then choose the mechanism that fits each responsibility.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Start With Ownership, Not Libraries
&lt;/h2&gt;

&lt;p&gt;I start by asking where a value comes from. Is it an authoritative server record, a user's unfinished input, a navigation choice, or a computation over other values? Then I ask what should happen when the component unmounts, the route changes, the browser reloads, or the account changes.&lt;/p&gt;

&lt;p&gt;Scope and lifetime are different. A value can be needed by several sibling components while still belonging only to one screen. Conversely, a small user object can have an application-wide session lifetime. “Several consumers” is a reason to share access; it does not automatically justify permanent global ownership.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Question about the value&lt;/th&gt;
&lt;th&gt;Likely owner&lt;/th&gt;
&lt;th&gt;Architectural reason&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Is it unfinished input used by this screen?&lt;/td&gt;
&lt;td&gt;Local component or nearest shared parent&lt;/td&gt;
&lt;td&gt;Short lifetime and direct editing&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Does it identify the current session?&lt;/td&gt;
&lt;td&gt;Session layer / Context&lt;/td&gt;
&lt;td&gt;Shared identity and bootstrap lifecycle&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Does the server own the saved record?&lt;/td&gt;
&lt;td&gt;TanStack Query cache&lt;/td&gt;
&lt;td&gt;Fetching, freshness, and mutation reconciliation&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Should a link or browser history preserve it?&lt;/td&gt;
&lt;td&gt;Route or URL search parameters&lt;/td&gt;
&lt;td&gt;Navigation is part of its meaning&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Can existing values determine it?&lt;/td&gt;
&lt;td&gt;Computation&lt;/td&gt;
&lt;td&gt;Avoid a second independently writable value&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Is it a coordinated, long-lived client workflow?&lt;/td&gt;
&lt;td&gt;Dedicated store, if justified&lt;/td&gt;
&lt;td&gt;Explicit transitions across consumers&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;This is a decision aid, not a rule that every value has exactly one physical representation. A server response can seed an editable form. What matters is whether the form is an intentional working copy or an accidental competing source of truth.&lt;/p&gt;

&lt;p&gt;Persistence also deserves its own decision. Putting state in Context does not make it survive reloads. Putting a draft in localStorage would add retention requirements. Putting it in the URL would make it visible in links and history. I would choose those behaviors explicitly instead of inheriting them from whichever storage mechanism is convenient.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Current implementation: which owner supplies which state?&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;AuthProvider: user + bootstrap status ──&amp;gt; protected UI + computed role checks
URL: screen + ticket ID ────────────────&amp;gt; navigation + query identifier
Server records ──&amp;gt; TanStack Query ──────&amp;gt; ticket/message views + computed metrics
AI suggestion mutation ──&amp;gt; page handoff ──&amp;gt; editable local composer
Local forms ───────────────────────────&amp;gt; unsaved values + validation errors
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. Local State: Keep Ephemeral UI Close to Where It Changes
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;MessageComposer&lt;/code&gt; owns its &lt;code&gt;content&lt;/code&gt;, validation error, and submission error with &lt;code&gt;useState&lt;/code&gt;. Its pending indicator comes from the message mutation. That distinction is useful: the text is editable UI state, while the request lifecycle already has an owner.&lt;/p&gt;

&lt;p&gt;Excerpt from &lt;code&gt;MessageComposer.tsx&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setContent&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;initialContent&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;fieldError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setFieldError&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;submitError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setSubmitError&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isSubmitting&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;createMessageMutation&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isPending&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The component validates the text, submits trimmed content, and clears the input after the mutation succeeds. A failed submission sets an error without executing that success reset. The textarea is disabled while the request is pending. There is no need to put each keystroke into the message query cache: it is not yet a saved message.&lt;/p&gt;

&lt;p&gt;The ticket creation form follows the same boundary. It owns title, description, field errors, and a submission error locally. After successful creation, it navigates to the returned ticket's detail route. The unsaved form and the saved ticket are related, but they have different lifetimes and authorities.&lt;/p&gt;

&lt;p&gt;Local does not mean “must live in the smallest possible component.” The AI reply handoff uses state in &lt;code&gt;TicketDetailPage&lt;/code&gt; because the suggestion panel and composer are siblings. That parent is the nearest place that coordinates their interaction. Sharing within a screen does not require sharing across the whole application.&lt;/p&gt;

&lt;p&gt;For a future dialog or temporary UI toggle, I would begin with the same ownership test: does another screen need to retain or control it? A form that must survive several routes may deserve a longer-lived owner. Component-local storage is a fit for these current forms, not a universal rule for every form.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Context: Give the Session a Shared Owner
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;AuthProvider&lt;/code&gt; owns a nullable user and authentication bootstrap loading. The user shape contains &lt;code&gt;id&lt;/code&gt;, &lt;code&gt;name&lt;/code&gt;, &lt;code&gt;email&lt;/code&gt;, and a role of &lt;code&gt;CUSTOMER&lt;/code&gt;, &lt;code&gt;AGENT&lt;/code&gt;, or &lt;code&gt;ADMIN&lt;/code&gt;. The Context exposes that user, &lt;code&gt;login&lt;/code&gt;, &lt;code&gt;logout&lt;/code&gt;, and &lt;code&gt;isLoading&lt;/code&gt;. It does not contain ticket lists or message arrays. Context could expose server data in another design, but providing access does not by itself define fetching, freshness, or reconciliation. Here those responsibilities belong to the query layer.&lt;/p&gt;

&lt;p&gt;The token has a separate persistence boundary: the storage service reads and writes &lt;code&gt;access_token&lt;/code&gt; in localStorage. At startup, a stored token triggers &lt;code&gt;fetchCurrentUser&lt;/code&gt;, which calls &lt;code&gt;/auth/me&lt;/code&gt;. A successful response establishes the user in Context; a failure removes the token and clears the user.&lt;/p&gt;

&lt;p&gt;Simplified excerpt from &lt;code&gt;AuthProvider.tsx&lt;/code&gt;; the surrounding token check and loading handling are omitted:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;currentUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetchCurrentUser&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="nf"&gt;setUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentUser&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;storage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;removeToken&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="nf"&gt;setUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Login has two steps across this boundary. The auth service saves the response token, and the login page passes the returned user to Context before navigating to the dashboard. Context's &lt;code&gt;login&lt;/code&gt; function itself sets the user and ends loading. A stored credential and the resolved user are complementary session representations, not two interchangeable user databases.&lt;/p&gt;

&lt;p&gt;There is also a temporary login mutation result containing the response user. I would not describe authentication as having literally one object representation. The important current boundary is that protected routes and dashboard consumers read the session through Context rather than maintaining a second durable user store.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ProtectedRoute&lt;/code&gt; waits for bootstrap, redirects when there is no user, and otherwise renders its nested outlet. Axios reads the token for Bearer authentication. On a 401 it removes the token and redirects to login, except that &lt;code&gt;/auth/me&lt;/code&gt; failures are left to bootstrap handling.&lt;/p&gt;

&lt;p&gt;Role-derived booleans control which UI actions appear. For example, the detail page computes management access from the user's role. That is presentation logic, not the final authorization boundary: the backend status route independently requires an agent or admin role. Context makes identity available; it does not make the browser authoritative over permissions.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. TanStack Query: Server State Is Not Just Global Client State
&lt;/h2&gt;

&lt;p&gt;A shared ticket array is only part of the problem. The frontend also needs to know whether it is loading, whether a request failed, which request parameters produced it, and what a successful mutation makes outdated. Sharing access across components does not transfer authority from the server to the browser. These concerns explain why ticket and message records belong in a query cache here.&lt;/p&gt;

&lt;p&gt;The application's &lt;code&gt;QueryClient&lt;/code&gt; has these defaults:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;queryClient&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;QueryClient&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;defaultOptions&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;queries&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;retry&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;staleTime&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;refetchOnWindowFocus&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This config expresses a freshness policy; it does not promise continuous synchronization. The application uses a 60-second freshness window and disables window-focus refetching. That interval is not a polling schedule or proof that another agent's changes will appear immediately. The ticket and message views expose loading/error states and retry or refresh behavior instead of treating cache presence as sufficient evidence of freshness.&lt;/p&gt;

&lt;p&gt;Ticket keys distinguish list queries, including their parameter object, from detail queries keyed by ticket ID. Message keys distinguish conversations by ticket ID. &lt;code&gt;useTicket&lt;/code&gt; and &lt;code&gt;useMessages&lt;/code&gt; enable their queries only when the identifier is truthy.&lt;/p&gt;

&lt;p&gt;Excerpt from &lt;code&gt;useTickets.ts&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;useTickets&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;TicketListParams&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;useQuery&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;queryKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ticketKeys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="na"&gt;queryFn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;getTickets&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The same parameters define the cache identity and the API request. That alignment matters: two differently filtered requests should not silently share one list entry. It also creates a natural place for future normalized URL parameters to feed the fetching layer.&lt;/p&gt;

&lt;p&gt;Mutation reconciliation is explicit. Ticket creation invalidates list queries. A status update patches status and &lt;code&gt;updatedAt&lt;/code&gt; in an existing detail entry, then invalidates the lists and that detail. Message creation appends the returned message to the matching conversation cache on success.&lt;/p&gt;

&lt;p&gt;The inspected list and thread render query data directly; they do not copy those arrays into component state or Context. This keeps the query result as the frontend's shared representation of the server response. It does not eliminate every consistency question: different cache entries can represent overlapping records, so mutation handling still needs to account for the views affected by a change.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. URL State: If Navigation Should Preserve It, Consider Putting It in the URL
&lt;/h2&gt;

&lt;p&gt;Selected ticket identity is already navigation state. The active router declares &lt;code&gt;/dashboard/tickets/:id&lt;/code&gt;, and the detail page derives its query identifier from that parameter:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useParams&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ticketId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A ticket link therefore carries the selected identity without requiring a separate selected-ticket store. Browser navigation can change that identity through the pathname, and a direct link identifies which record to request, subject to authentication and access. This does not imply that browser history restores an unsent composer draft.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Current implementation.&lt;/strong&gt; The list/dashboard behavior is narrower than a full URL-backed search interface. &lt;code&gt;TicketsPage&lt;/code&gt; calls &lt;code&gt;useTickets()&lt;/code&gt; without parameters and has no filter, search, sort, or pagination controls. The dashboard requests fixed parameters: page 1 and limit 100. The ticket parameter type supports status, priority, search, page, and limit, but that API capability is not an implemented navigation UI.&lt;/p&gt;

&lt;p&gt;There is no &lt;code&gt;useSearchParams&lt;/code&gt; or &lt;code&gt;URLSearchParams&lt;/code&gt; usage in the inspected frontend source. The missing piece is a navigation design for future shareable list controls, rather than migrating existing component-only controls.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How I would evolve it.&lt;/strong&gt; If list filters, search, and pagination become navigation-relevant, I would put their committed values in URL search parameters. A link such as &lt;code&gt;/dashboard/tickets?status=OPEN&amp;amp;page=2&lt;/code&gt; would describe which view to request after a refresh, when opening a shared link, or when moving back and forward through history. It would preserve the selection criteria, not freeze the server results. Parsing should validate enum values and normalize page numbers before producing query parameters. Changing a filter should deliberately decide whether to reset the page.&lt;/p&gt;

&lt;p&gt;Search adds a useful distinction. Text currently being typed can remain local while a committed search becomes URL state. History updates should reflect meaningful navigation rather than creating an entry for every keystroke. Composer content, credentials, and transient errors have no comparable reason to appear in the URL.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. A Real Workflow Shows Why State Changes Ownership
&lt;/h2&gt;

&lt;p&gt;The message workflow makes the boundary concrete. Before submission, the text belongs to the composer. During submission, the mutation owns pending/error request state. After server success, the returned message becomes part of the conversation cache and the thread renders it.&lt;/p&gt;

&lt;p&gt;Excerpt from &lt;code&gt;useCreateMessage&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;onSuccess&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newMessage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;queryClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setQueryData&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Message&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;messageKeys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ticket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ticketId&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;existing&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;existing&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;existing&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;newMessage&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;newMessage&lt;/span&gt;&lt;span class="p"&gt;]),&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a cache update after success, not a speculative optimistic message insertion. The composer waits for &lt;code&gt;mutateAsync&lt;/code&gt; before clearing text and invoking its callback. The server-returned record, rather than the raw input string, is what enters the message cache.&lt;/p&gt;

&lt;p&gt;The callback increments a page-local &lt;code&gt;messageRefreshToken&lt;/code&gt;. Despite its name, &lt;code&gt;MessageThread&lt;/code&gt; uses it in a scroll effect alongside message count; it does not refetch messages because the token changed. That small detail matters when explaining ownership: the cache supplies conversation data, while this counter coordinates a UI effect.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Current workflow: when does a draft become a conversation record?&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Ticket identity + query-backed view
  -&amp;gt; separate suggestion request [mutation pending/error/result]
  -&amp;gt; returned suggestion [candidate text; not sent]
  -&amp;gt; click Use Reply [page-local handoff]
  -&amp;gt; editable composer [local draft; human may edit]
  -&amp;gt; explicit Send [message mutation]
       success -&amp;gt; returned server message -&amp;gt; query cache -&amp;gt; rendered thread
                  clear composer + signal scroll
       failure -&amp;gt; retain input + show submission error
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The same send boundary applies to a reply typed without an AI suggestion.&lt;/p&gt;

&lt;p&gt;The AI workflow starts with a separate mutation. &lt;code&gt;ReplySuggestion&lt;/code&gt; requests generated text, displays mutation loading/error/result state, and derives a trimmed suggestion and a visibility boolean. It resets that mutation when the ticket ID changes. Generating a suggestion does not call the message creation hook.&lt;/p&gt;

&lt;p&gt;Only clicking “Use Reply” invokes the parent callback. The page saves the suggestion as &lt;code&gt;composerDraft&lt;/code&gt; and increments &lt;code&gt;composerKey&lt;/code&gt;, remounting the composer so its local content initializes from that draft. The user can then edit the textarea and explicitly send through the ordinary message workflow.&lt;/p&gt;

&lt;p&gt;This is an intentional working-copy boundary: generated text is a candidate, editable content is the human's draft, and a successful send returns a conversation record. Similar strings across these stages do not have identical authority. I would preserve that separation even if the implementation later changes how siblings exchange the draft.&lt;/p&gt;

&lt;p&gt;The current handoff also has a tradeoff. The page retains a seed value while the composer owns the live edited text. Using another suggestion remounts the composer and replaces its local input. I would consider an explicit replacement confirmation or a clearer draft ownership API if preserving unsent edits becomes a requirement. Neither protection is implemented by the shown handoff.&lt;/p&gt;

&lt;h2&gt;
  
  
  8. What About Redux?
&lt;/h2&gt;

&lt;p&gt;Redux and Redux Toolkit are not used in this frontend: there are no matching source imports/usages or dependencies in its package manifest. The current flows use Context, queries, route identity, component state, and mutation state without a Redux store.&lt;/p&gt;

&lt;p&gt;These ownership requirements do not automatically require Redux. A dedicated client store becomes worth considering when there is a coordination problem that the existing owners cannot express cleanly; this implementation alone cannot settle that decision for another product or future workflow.&lt;/p&gt;

&lt;p&gt;I would consider a dedicated client store for a cross-feature workflow that stays active across routes, long-lived unsaved work spanning several screens, or coordinated UI transitions with explicit state-machine-like rules. For example, a future multi-ticket response workspace might need draft identity and transitions beyond a single composer. That is a hypothetical requirement, not an existing feature.&lt;/p&gt;

&lt;p&gt;Even then, I would define what the store owns before adding it. It might own selected workspace items and unsaved drafts while querying authoritative ticket records separately. Copying every fetched ticket into the new store would introduce synchronization work without proving that the workflow needs it.&lt;/p&gt;

&lt;h2&gt;
  
  
  9. The Failure Modes I Would Watch For
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Current limitations.&lt;/strong&gt; Two session/cache boundaries deserve priority. First, ticket and message keys contain no account or session identifier. Second, logout removes the token and user but does not explicitly clear the query cache. The module-level QueryClient wraps AuthProvider, so the normal logout button does not recreate it.&lt;/p&gt;

&lt;p&gt;Together, those choices leave an account-transition isolation gap. A later login in the same application lifetime can address keys used by the previous session. That is a source-supported isolation risk, not a reproduced data leak. Freshness configuration cannot substitute for session isolation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How I would evolve it.&lt;/strong&gt; I would design logout cleanup and account-scoped query identity together, including cancellation of requests that could complete after the session changes. Clearing existing data is only part of the lifecycle. Which requests may still write, and which identity the next request uses, also need explicit answers. This is proposed work, not current behavior.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Other current boundaries.&lt;/strong&gt; The AI handoff replaces local input when another suggestion is used; draft-preservation behavior would need an explicit design. A separate constraint is the dashboard's derived metrics. They count the returned page-1 list capped at 100 requested tickets. The computation marks possible truncation when the returned count reaches that limit. These are summaries of fetched records, not verified complete account totals. Derived state can be internally correct while its input is incomplete.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;General failure modes.&lt;/strong&gt; Copying query arrays into Context or local state and storing computed permissions independently create values that must stay aligned with their sources. Globalizing transient forms extends their lifetime and coordination surface. Keeping a navigation-relevant selection only in a component prevents the URL from describing it. These are design risks to watch for, not findings that all exist here. An intentional editable snapshot can justify duplication; an unedited mirror usually needs a stronger reason.&lt;/p&gt;

&lt;h2&gt;
  
  
  10. The State Ownership Model I Would Use as the Dashboard Grows
&lt;/h2&gt;

&lt;p&gt;I would retain the existing division and strengthen the boundaries where behavior is incomplete. The target model below distinguishes retained choices from proposed additions.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;State&lt;/th&gt;
&lt;th&gt;Target owner&lt;/th&gt;
&lt;th&gt;Status and reason&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Current user and bootstrap&lt;/td&gt;
&lt;td&gt;Session Context&lt;/td&gt;
&lt;td&gt;Current; shared session lifecycle&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Credential persistence&lt;/td&gt;
&lt;td&gt;Session storage service&lt;/td&gt;
&lt;td&gt;Current localStorage mechanism; separate from user object&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Tickets and messages&lt;/td&gt;
&lt;td&gt;TanStack Query&lt;/td&gt;
&lt;td&gt;Current query owner; proposed account scope and session cleanup&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Selected ticket&lt;/td&gt;
&lt;td&gt;URL route parameter&lt;/td&gt;
&lt;td&gt;Current; identity belongs to navigation&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Shareable filters and page&lt;/td&gt;
&lt;td&gt;URL search parameters&lt;/td&gt;
&lt;td&gt;Proposed; reproducible list views&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Composer text and form errors&lt;/td&gt;
&lt;td&gt;Local component state&lt;/td&gt;
&lt;td&gt;Current; unfinished screen input&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;AI result and editable draft&lt;/td&gt;
&lt;td&gt;Mutation result, then local workflow state&lt;/td&gt;
&lt;td&gt;Current boundary; improve replacement semantics if needed&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Permissions and dashboard summaries&lt;/td&gt;
&lt;td&gt;Computed from session/query inputs&lt;/td&gt;
&lt;td&gt;Current; respect backend authority and input completeness&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Future cross-feature client workflow&lt;/td&gt;
&lt;td&gt;Dedicated store only when justified&lt;/td&gt;
&lt;td&gt;Proposed conditional choice&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Derived values already have useful examples. The detail page computes management permissions from the user role; the dashboard computes metrics and a five-item recent slice from query data with &lt;code&gt;useMemo&lt;/code&gt;. The suggestion panel computes whether usable text is present from its mutation result and status. None needs an independently writable global field merely to make consumption convenient.&lt;/p&gt;

&lt;p&gt;As the dashboard grows, I would review a new feature by walking through its transitions. What exists before the request? What changes on failure? What becomes authoritative on success? What should survive navigation, reload, or account change? Those questions expose ownership gaps earlier than asking where to put a new boolean.&lt;/p&gt;

&lt;h2&gt;
  
  
  11. Closing
&lt;/h2&gt;

&lt;p&gt;My working model is to keep unfinished edits close to the UI, share session identity through a session layer, cache server records with explicit reconciliation, and let navigation own the choices a link should preserve. Compute values whose sources already exist. Add a client store when a concrete workflow needs a longer or broader lifetime.&lt;/p&gt;

&lt;p&gt;For each new value, I would write down its source of truth, its consumers, and the event that ends its lifetime. Then I would decide whether navigation should preserve it and whether it can be computed from existing inputs. If those answers are unclear, choosing a more powerful store will only move the ambiguity. Resolve the ownership first; the tool choice follows.&lt;/p&gt;




&lt;p&gt;Originally published on my engineering blog: &lt;a href="https://blog.ramjwork.in/frontend/react-state-architecture" rel="noopener noreferrer"&gt;https://blog.ramjwork.in/frontend/react-state-architecture&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>typescript</category>
      <category>webdev</category>
      <category>architecture</category>
    </item>
    <item>
      <title>Frontend System Design: How I Would Architect a Production Dashboard in React</title>
      <dc:creator>Ram Ji Tripathi</dc:creator>
      <pubDate>Mon, 05 Oct 2026 14:18:32 +0000</pubDate>
      <link>https://dev.to/ramji_tripathi_095c7f4810/frontend-system-design-how-i-would-architect-a-production-dashboard-in-react-f10</link>
      <guid>https://dev.to/ramji_tripathi_095c7f4810/frontend-system-design-how-i-would-architect-a-production-dashboard-in-react-f10</guid>
      <description>&lt;p&gt;React dashboards rarely become difficult because of JSX. They become difficult when ownership gets unclear: who owns server data, session state, URL state, transient UI, permissions, mutations, and failures?&lt;/p&gt;

&lt;p&gt;I use the React frontend of &lt;a href="https://blog.ramjwork.in/ai-engineering/ai-customer-support-architecture" rel="noopener noreferrer"&gt;my AI Support Assistant&lt;/a&gt; as a reference. It has authentication, a dashboard, tickets, conversations, and AI reply suggestions. The code shows a useful set of boundaries and also where those boundaries need more definition. This article distinguishes what the application implements today from what I would change as it grows. The title describes a design goal, not a claim about production traffic or measured scale.&lt;/p&gt;

&lt;h2&gt;
  
  
  Start with boundaries, not components
&lt;/h2&gt;

&lt;p&gt;Before choosing component sizes, I decide where navigation, identity, data access, and interaction state belong. In the current app, main.tsx mounts shared providers and the router. The router selects pages, AppLayout provides the authenticated shell, feature hooks connect UI to domain queries and mutations, and feature API modules use a shared Axios client.&lt;/p&gt;

&lt;p&gt;For ticket reads, the current flow is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Router -&amp;gt; Page -&amp;gt; Feature hook -&amp;gt; TanStack Query
       -&amp;gt; Feature API -&amp;gt; Shared Axios client -&amp;gt; Backend
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This keeps request construction and authentication headers out of the page. It does not remove every dependency: dashboard metrics build on ticket hooks, and message UI reuses ticket error helpers. Those are manageable now, but I would watch cross-feature imports as the application expands.&lt;/p&gt;

&lt;p&gt;The repository already groups auth, tickets, messages, and AI by feature. Dashboard hooks and components are also grouped, though the dashboard page remains in src/pages. The active router is src/routes/AppRouter.tsx; an src/app/router.tsx file is empty. I would consolidate those entry points before adding more folders.&lt;/p&gt;

&lt;h2&gt;
  
  
  State ownership is the central decision
&lt;/h2&gt;

&lt;p&gt;The current frontend keeps tickets and messages in TanStack Query, identity in auth context, ticket identity in the route, and editable form fields in component state. Each has a different authority and lifetime. Treating them as one undifferentiated global store would make synchronization harder to reason about.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;State&lt;/th&gt;
&lt;th&gt;Owner and persistence&lt;/th&gt;
&lt;th&gt;Synchronization&lt;/th&gt;
&lt;th&gt;Common mistake&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Local UI&lt;/td&gt;
&lt;td&gt;Component or nearest feature coordinator; usually mounted lifetime&lt;/td&gt;
&lt;td&gt;Props and callbacks between collaborators&lt;/td&gt;
&lt;td&gt;Put drafts and field errors in a global store by default&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Server&lt;/td&gt;
&lt;td&gt;TanStack Query; current query-client lifetime&lt;/td&gt;
&lt;td&gt;Query keys, confirmed cache writes, invalidation/refetch&lt;/td&gt;
&lt;td&gt;Copy fetched records into local state and maintain two versions&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Session/auth&lt;/td&gt;
&lt;td&gt;AuthProvider owns user/loading; token currently lives in localStorage&lt;/td&gt;
&lt;td&gt;Bootstrap, login, logout, and future identity transitions&lt;/td&gt;
&lt;td&gt;Turn AuthContext into a catch-all domain store&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;URL&lt;/td&gt;
&lt;td&gt;Router owns route identity; list search params are a future choice&lt;/td&gt;
&lt;td&gt;Parse navigation into canonical query inputs&lt;/td&gt;
&lt;td&gt;Keep shareable filters only in component state&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Shared client&lt;/td&gt;
&lt;td&gt;Add a narrow provider/store only for a real cross-feature need&lt;/td&gt;
&lt;td&gt;Explicit subscribers and actions&lt;/td&gt;
&lt;td&gt;Introduce Redux because the app has multiple pages&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Server data  -&amp;gt; TanStack Query
Session      -&amp;gt; AuthProvider
URL          -&amp;gt; Router / search parameters
Transient UI -&amp;gt; Component / feature coordinator
Shared state -&amp;gt; Only for a demonstrated cross-feature need
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I promote a value only when another feature truly needs it, it must survive navigation, or there is a clear owner beyond its current component. If two neighboring components need a draft, their coordinator may be enough. Persistence also has several meanings: a value may survive a render, a route change, a reload, or a new login. Those are separate requirements, not reasons to put everything into the same store. Dashboard counts are derived from fetched tickets rather than stored as a second editable copy; the trade-off is that the chosen dataset limits the accuracy of those counts.&lt;/p&gt;

&lt;p&gt;The same test helps with filters. A dropdown selection being shared by two components does not automatically make it application state. If the user expects to copy the URL, return with Back, or reload and see the same result, the committed filter belongs in navigation state. If it is simply a temporary draft before pressing Apply, local state is a better fit. I would avoid synchronizing two writable copies of the same filter because one will eventually lag the other.&lt;/p&gt;

&lt;h2&gt;
  
  
  Routing, shell, and access boundaries
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Current implementation:&lt;/strong&gt; /, /login, and /register are public. Dashboard, ticket list, create, and detail routes are nested under ProtectedRoute and AppLayout. The guard waits for auth bootstrap, redirects when there is no user, and otherwise renders its outlet. It checks identity, not role. The create page separately redirects non-customers; ticket detail controls check roles before showing status management or AI replies.&lt;/p&gt;

&lt;p&gt;The shell owns navigation, the user name, logout, and the nested page outlet. Pages own workflows and data. I would keep domain queries out of the shell so every authenticated route does not inherit unrelated fetching.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How I would evolve it:&lt;/strong&gt; make route requirements explicit as route count grows, preserve a requested destination through sign-in, and provide a deliberate unknown-route page. The active router has no wildcard; the test helper does. This is a small example of how test composition can drift from the application.&lt;/p&gt;

&lt;p&gt;Authentication establishes identity; it does not authorize each resource action. The current AuthProvider owns user and loading state. If a stored token exists, it fetches /auth/me; any bootstrap error clears the token and user. The essential current behavior is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;      &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;currentUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetchCurrentUser&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="nf"&gt;setUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentUser&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;storage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;removeToken&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="nf"&gt;setUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;finally&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;setIsLoading&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Login and registration persist the returned token, set the user in context, and navigate to the dashboard. Logout clears the token and user. The broad bootstrap catch treats a temporary network failure like an invalid session. I would distinguish those outcomes and define what happens to in-flight requests during logout or account switching.&lt;/p&gt;

&lt;p&gt;The UI hides or shows actions by role, but that is presentation. The inspected backend routes restrict ticket creation to customers and status changes to agents/admins. Ticket access is scoped by customer ownership or agent assignment, and the service rechecks access for cached tickets. The AI suggestion route also has an agent/admin gate. These checks show the authority boundary; they are not a full security audit. For a larger UI, I would centralize capability predicates to keep repeated presentation rules consistent, while leaving authorization decisions on the server.&lt;/p&gt;

&lt;h2&gt;
  
  
  Server state, cache consistency, and the API boundary
&lt;/h2&gt;

&lt;p&gt;The current TanStack Query defaults are one retry, a one-minute stale time, and no refetch on window focus:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;queryClient&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;QueryClient&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;defaultOptions&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;queries&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;retry&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;staleTime&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;refetchOnWindowFocus&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ticket keys distinguish lists and details; message keys distinguish conversations by ticket id. Detail and message queries require a nonempty id. Feature API modules unwrap response data and return domain values. Stale time is a freshness policy; it does not make remote changes arrive immediately.&lt;/p&gt;

&lt;p&gt;There is a meaningful session boundary to improve: the ticket and message keys do not include account identity, and logout does not clear the shared query cache. A later login in the same app lifetime can encounter cached data under the same keys. This is a code-level isolation risk, not a reported incident. I would scope protected queries to identity or tenant and clear or cancel them on session transitions, then test account switching with populated data.&lt;/p&gt;

&lt;p&gt;Mutations make cache consistency explicit. Ticket creation invalidates ticket lists. Status updates patch the confirmed response into an existing detail cache, then invalidate list and detail queries. Message creation appends the server-confirmed response:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;    &lt;span class="nx"&gt;onSuccess&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newMessage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;queryClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setQueryData&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Message&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="nx"&gt;messageKeys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ticket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ticketId&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;existing&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;existing&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;existing&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;newMessage&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;newMessage&lt;/span&gt;&lt;span class="p"&gt;]),&lt;/span&gt;
      &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is not an optimistic update: the write occurs after success. If the conversation was not cached, the code seeds it with only the new message. The hook does not invalidate ticket detail or lists. Whether those views need refresh depends on what sending changes. As more views depend on a mutation, I would document affected queries and define reconciliation for incomplete caches or concurrent writers. I would add optimistic behavior only when its temporary identity, rollback, deduplication, and reconciliation costs are justified.&lt;/p&gt;

&lt;p&gt;All inspected feature APIs use one Axios instance. It sets the API base URL, JSON headers, a ten-second timeout, and a request interceptor that adds the stored Bearer token. The AI suggestion call overrides the timeout to sixty seconds. The response interceptor removes the token on 401 and redirects to /login, except for /auth/me bootstrap failures:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;401&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;storage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;removeToken&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;requestUrl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isSessionBootstrapRequest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;requestUrl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/auth/me&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;isSessionBootstrapRequest&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;location&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;href&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/login&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That broad redirect also applies to a failed login request, while the login page has its own invalid-credentials message. I would assign session expiry and transport classification to an explicit session boundary, with feature-specific recovery messages. That boundary should not flatten every response into “try again”: invalid credentials, forbidden access, missing resources, offline failures, and server errors ask for different next steps. The API client can normalize transport details, while feature code still decides what recovery makes sense in context.&lt;/p&gt;

&lt;p&gt;Typed response annotations do not validate payloads at runtime; runtime checks are a future option if contract drift warrants them. The localStorage token choice also needs to be evaluated against the deployment threat model rather than treated as proof of comprehensive security.&lt;/p&gt;

&lt;h2&gt;
  
  
  The ticket, message, and AI reply workflow
&lt;/h2&gt;

&lt;p&gt;Ticket detail brings together three feature concerns: the ticket query, a message thread, and an AI suggestion. Server-owned ticket and message data stay in Query; the composer draft and coordination state stay local.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Current implementation:&lt;/strong&gt; generating a suggestion is an explicit mutation with pending, error, empty, and success states. Regenerate calls the mutation again; retry resets its error first. “Use Reply” places suggestion text into the composer. It does not send it. The user can review, edit, and submit separately. The handoff is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;                  &lt;span class="nx"&gt;onUseReply&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{(&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="nf"&gt;setComposerDraft&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                    &lt;span class="nf"&gt;setComposerKey&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;current&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                  &lt;span class="p"&gt;}}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Incrementing the key remounts the composer with the supplied initial text. That also resets its local state, so it replaces any existing edits. I would make draft replacement an explicit interaction contract, including a confirmation when preserving unsent text matters.&lt;/p&gt;

&lt;p&gt;On message submit, the composer validates and trims the draft, disables input while the request is pending, and clears the field only after success. On failure it keeps the text and shows an error. The returned message is appended to the query cache. The detail page’s messageRefreshToken name might imply a refetch, but the thread uses it only as a scroll-effect dependency. The current workflow is request/response plus local cache updates; no subscription or polling mechanism was found for remote conversation changes.&lt;/p&gt;

&lt;p&gt;This example shows why state ownership and feature boundaries meet at the page: the page coordinates a draft handoff, the feature hooks own data operations, Query owns server data, and the shared client carries authentication. That is enough coordination for the current flow, but draft lifetime and cross-view freshness need explicit rules as collaboration grows.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where the design would become fragile
&lt;/h2&gt;

&lt;p&gt;Several limitations are useful signals for future design, not evidence of production failures:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Account-independent keys and retained cache:&lt;/strong&gt; protected records can remain cached across logout. Scope or clear that cache when identity changes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sampled metrics:&lt;/strong&gt; the dashboard requests page one with a limit of 100, derives counts from that array, and warns at 100 that results may be truncated. Those values are not a complete aggregate. I would use a server aggregate contract and a separate bounded recent-ticket query.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No list controls:&lt;/strong&gt; ticket types and API accept filters and pagination, but the screen calls useTickets() without parameters and exposes no pagination controls. I would make shareable filters and pages URL state when users need reload, sharing, or browser history.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Loading and refresh semantics:&lt;/strong&gt; list/detail/thread have loading, error, empty, and retry states. An error branch can replace content even if stale data exists. I would distinguish initial failure from background refresh failure and preserve usable content when appropriate.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Feature coupling:&lt;/strong&gt; message UI reuses ticket error wording, and dashboard derives from ticket hooks. I would keep transport errors shared while leaving domain messages with their owning feature.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A common design smell is duplicating server records in Redux or another global store while TanStack Query already owns them. Another is making AuthContext a general-purpose store for tickets, filters, and drafts. Neither is necessary here. Likewise, React-only permission checks, scattered HTTP clients, or component-only filters that users expect to bookmark are future risks to guard against, not current claims about every part of this app.&lt;/p&gt;

&lt;p&gt;A mutation can succeed while one view remains stale even when another view updates correctly. Invalidating a list does not automatically define what happens to its detail page, dashboard aggregate, or sort order. The reverse mistake is invalidating everything and triggering needless network work. I would start by naming the views affected by the server operation, then choose a narrow cache patch, invalidation, or refetch for each. If a workflow has delayed server-side processing, the response to the initial mutation may not contain the final state; the client needs a refresh policy that matches that lifecycle.&lt;/p&gt;

&lt;p&gt;Loading, error, and empty are not interchangeable placeholders. An empty list means the request succeeded and returned no records. A failed request means the interface does not know whether records exist. A background refresh can fail while previously fetched data is still useful. Treating those cases separately preserves context and makes retry behavior clearer. The current screens cover initial loading, error, empty, and content; retaining stale content through a failed background refresh is a possible evolution, not behavior I am claiming today.&lt;/p&gt;

&lt;h2&gt;
  
  
  Proposed architecture for a larger dashboard
&lt;/h2&gt;

&lt;p&gt;I would keep the existing feature organization and sharpen its boundaries. This is &lt;strong&gt;proposed architecture&lt;/strong&gt;, not the repository’s current tree:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;src/
  app/       providers, router, shell
  features/  auth, tickets, messages, ai, dashboard
  shared/    api transport, UI primitives, config
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;App -&amp;gt; Router -&amp;gt; Shell -&amp;gt; Feature page/coordinator
                          |-&amp;gt; local interaction state
                          |-&amp;gt; feature hook -&amp;gt; Query -&amp;gt; API -&amp;gt; Backend
AuthProvider -&amp;gt; session identity and bootstrap
URL          -&amp;gt; ticket id and proposed list parameters
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The import direction matters more than the folder names. Features should own their queries, keys, API functions, and domain UI. Shared code should not import ticket policy. A page or feature coordinator should make cross-feature orchestration visible. The URL should become the canonical owner of committed search, filters, and pagination when those values should survive reloads or be shared; an uncommitted search draft can remain local.&lt;/p&gt;

&lt;p&gt;For larger systems, I would keep list browsing separate from dashboard aggregates, define cache scope around identity, and write down each mutation’s affected views. Those choices address actual pressure points in the reference app rather than introducing a state library or folder structure for appearance.&lt;/p&gt;

&lt;h2&gt;
  
  
  Performance, testing, and observability
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Currently implemented:&lt;/strong&gt; query caching and freshness defaults, parameterized keys, a bounded first-page dashboard request, component-local form state, and useMemo for dashboard metric calculation and recent-ticket slicing. The memoization is present; no measured speedup is established. Ticket rows and message arrays are mapped normally. Routes are eagerly imported; no route code splitting or list virtualization was found.&lt;/p&gt;

&lt;p&gt;I would address performance in this order: avoid unnecessary requests and payloads; cache with deliberate freshness; bound lists through pagination; place state so edits do not update unrelated consumers; inspect rendering scope; then measure whether virtualization or route splitting is justified. For this app, the dashboard’s fixed 100-item request is a bound, but it is also the reason its derived counts are approximate. The ticket list itself has no controls to request a smaller page. Before tuning React renders, I would resolve the data contract and give users a way to navigate a bounded result set.&lt;/p&gt;

&lt;p&gt;Memoization follows evidence of repeated expensive work. It cannot fix a wrong aggregate or an oversized response. Likewise, code splitting only helps if the delivered route bundle or initial load is a demonstrated cost, and virtualization only helps if a large rendered collection is the bottleneck. Each adds behavior and loading edges that need their own verification.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Current tests:&lt;/strong&gt; the frontend uses Vitest, React Testing Library, and MSW. Test sources cover routes, permission-sensitive UI, form validation, query states, mutations, auth interceptors, AI draft use, and accessibility regressions. Some helpers use real auth while many use mocked identity. I reviewed the test sources and configuration, not a fresh test run or coverage report; configured thresholds are not measured coverage. No browser e2e suite was found in the inspected inventory, and the accessibility tests do not establish complete accessibility.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What I would add:&lt;/strong&gt; exercise auth bootstrap failures and account switching with cached data; verify permission-sensitive actions, loading/error/empty states, query/mutation behavior, routing, and critical forms in browser journeys. I found no frontend telemetry integration in the inspected source. I would add sanitized error reporting, API failure context, performance monitoring, and user-impact signals while excluding tokens and message contents.&lt;/p&gt;

&lt;h2&gt;
  
  
  Decisions I would keep explicit
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Concern&lt;/th&gt;
&lt;th&gt;Current approach&lt;/th&gt;
&lt;th&gt;Larger-system direction&lt;/th&gt;
&lt;th&gt;Reason&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Session/cache&lt;/td&gt;
&lt;td&gt;Context, localStorage token, shared query client&lt;/td&gt;
&lt;td&gt;Identity-scoped cache and explicit transition cleanup&lt;/td&gt;
&lt;td&gt;Define protected data lifetime&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Lists/metrics&lt;/td&gt;
&lt;td&gt;Plain list; first-100 dashboard sample&lt;/td&gt;
&lt;td&gt;URL pagination and aggregate endpoint&lt;/td&gt;
&lt;td&gt;Separate browsing from complete totals&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Mutations&lt;/td&gt;
&lt;td&gt;Invalidation or confirmed append&lt;/td&gt;
&lt;td&gt;Affected-view and reconciliation policy&lt;/td&gt;
&lt;td&gt;Keep related views consistent&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Drafts&lt;/td&gt;
&lt;td&gt;Local fields; suggestion remount&lt;/td&gt;
&lt;td&gt;Explicit replacement and ticket lifecycle&lt;/td&gt;
&lt;td&gt;Preserve user intent&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Performance&lt;/td&gt;
&lt;td&gt;Cache, bounded dashboard request, some memoization&lt;/td&gt;
&lt;td&gt;Measure payload/render scope before splitting&lt;/td&gt;
&lt;td&gt;Spend effort on observed costs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Quality&lt;/td&gt;
&lt;td&gt;jsdom/MSW regression tests&lt;/td&gt;
&lt;td&gt;Session/cache cases and browser journeys&lt;/td&gt;
&lt;td&gt;Exercise boundary failures&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Observability&lt;/td&gt;
&lt;td&gt;No integration found&lt;/td&gt;
&lt;td&gt;Sanitized errors and user-impact metrics&lt;/td&gt;
&lt;td&gt;Diagnose failures users encounter&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;My practical rule is simple: give each value an owner, each mutation a consistency policy, each permission an authoritative server check, and each asynchronous boundary a recovery path. The reference frontend has the core pieces; making cache lifetimes, URL state, and interaction transitions explicit is how I would evolve it as the product grows.&lt;/p&gt;




&lt;p&gt;Originally published on my engineering blog: &lt;a href="https://blog.ramjwork.in/frontend/react-dashboard-system-design" rel="noopener noreferrer"&gt;https://blog.ramjwork.in/frontend/react-dashboard-system-design&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>typescript</category>
      <category>webdev</category>
      <category>architecture</category>
    </item>
    <item>
      <title>Redis + BullMQ for AI Workloads: Retries Are Not Enough — You Also Need Idempotency and Ordering</title>
      <dc:creator>Ram Ji Tripathi</dc:creator>
      <pubDate>Sun, 04 Oct 2026 12:43:57 +0000</pubDate>
      <link>https://dev.to/ramji_tripathi_095c7f4810/redis-bullmq-for-ai-workloads-retries-are-not-enough-you-also-need-idempotency-and-ordering-51hj</link>
      <guid>https://dev.to/ramji_tripathi_095c7f4810/redis-bullmq-for-ai-workloads-retries-are-not-enough-you-also-need-idempotency-and-ordering-51hj</guid>
      <description>&lt;p&gt;Moving AI analysis out of an HTTP request improves the request path. Queueing that work does not make it correct.&lt;/p&gt;

&lt;p&gt;In my AI Support Assistant, a new message triggers BullMQ analysis of the ticket conversation. The request waits for database writes and queue publication, then returns without waiting for the AI results.&lt;/p&gt;

&lt;p&gt;But a retryable job can still run more than once, reload different context, race with another job, repeat provider work or overwrite newer results. The message can also be saved without its analysis job reaching Redis.&lt;/p&gt;

&lt;p&gt;Examining the current implementation makes those boundaries concrete. The scenarios below are correctness risks exposed by the code, not production incidents; the proposed fixes are not implemented features.&lt;/p&gt;

&lt;h2&gt;
  
  
  The System This Came From
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://blog.ramjwork.in/ai-engineering/ai-customer-support-architecture" rel="noopener noreferrer"&gt;The first article&lt;/a&gt; described the broader React, Node.js, PostgreSQL, Redis and OpenAI architecture.&lt;/p&gt;

&lt;p&gt;This article focuses on one workflow: a message is added to a support ticket, and background processing generates a summary, sentiment and priority. Those values are written back to the ticket. The worker also records an &lt;code&gt;AIInteraction&lt;/code&gt; containing the conversation, summary response and summary token usage.&lt;/p&gt;

&lt;p&gt;The queue is named &lt;code&gt;ai-processing&lt;/code&gt;. Its job type is &lt;code&gt;generate-ticket-summary&lt;/code&gt;, although the processor does more than summarize. It analyzes the whole ticket conversation, rather than just the message that triggered publication.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why the AI Work Went to BullMQ
&lt;/h2&gt;

&lt;p&gt;Saving a message and enriching a ticket have different completion requirements. The message is the user-authored record. AI metadata can arrive later.&lt;/p&gt;

&lt;p&gt;The current boundary looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;React client → Node.js API → PostgreSQL: Message + Ticket timestamp
                         → Redis/BullMQ: publish analysis job
                         → HTTP 201 after publication

Redis/BullMQ → separate worker → load ticket messages
                              → summary → sentiment → priority
                              → update Ticket → insert AIInteraction
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Publication makes the job available to the worker. The worker may start before HTTP 201 is sent; the diagram shows two execution paths, not a guarantee that background processing begins after the response.&lt;/p&gt;

&lt;p&gt;The important boundary is what the request awaits: persistence and publication, followed by a return to the caller. Summary, sentiment and priority belong to the worker's lifecycle.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Actual Producer Flow
&lt;/h2&gt;

&lt;p&gt;From &lt;code&gt;backend/src/modules/messages/message.service.ts&lt;/code&gt;, lines 26–58. Authorization precedes this excerpt. The excerpt is otherwise complete; blank lines are condensed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;createMessageRepository&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;ticketId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ticketId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;senderId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;senderId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;updateTicketRepository&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ticketId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;updatedAt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;aiQueue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;GENERATE_TICKET_SUMMARY_JOB&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;ticketId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ticketId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;messageId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;attempts&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;backoff&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;exponential&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;removeOnComplete&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;removeOnFail&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The repository helpers execute &lt;code&gt;prisma.message.create&lt;/code&gt; and &lt;code&gt;prisma.ticket.update&lt;/code&gt;. These are separate writes, followed by publication to Redis. There is no encompassing transaction or outbox in this flow.&lt;/p&gt;

&lt;p&gt;The controller awaits this service before returning HTTP 201. Consequently, a message can already exist even if the service does not reach its successful return. Queue publication is part of request completion; AI processing is not.&lt;/p&gt;

&lt;p&gt;The payload includes &lt;code&gt;messageId&lt;/code&gt;, but the worker's processing logic only consumes &lt;code&gt;ticketId&lt;/code&gt;. The message ID currently provides neither a processing boundary nor an idempotency key.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the Worker Actually Does
&lt;/h2&gt;

&lt;p&gt;The worker dispatches &lt;code&gt;generate-ticket-summary&lt;/code&gt; to &lt;code&gt;processTicketSummaryJob&lt;/code&gt; using only the ticket ID. Its configuration in &lt;code&gt;backend/src/queues/ai.worker.ts&lt;/code&gt;, lines 71–75, is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;connection&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;redis&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;concurrency&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the complete options object from the worker constructor, with blank lines condensed.&lt;/p&gt;

&lt;p&gt;Five jobs can run concurrently within this worker instance, including jobs for the same ticket. This is a local worker limit, not a global or per-ticket limit. See BullMQ’s &lt;a href="https://docs.bullmq.io/guide/workers/concurrency" rel="noopener noreferrer"&gt;worker concurrency guide&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The processor loads messages with &lt;code&gt;where: { ticketId }&lt;/code&gt;, includes sender role and name, and sorts by &lt;code&gt;createdAt: "asc"&lt;/code&gt;. It returns early when there are no messages. Otherwise it formats the conversation and executes this sequence from lines 113–127:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;aiResult&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;generateTicketSummary&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;conversation&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="nx"&gt;aiContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sentimentResult&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;generateSentiment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;conversation&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="nx"&gt;aiContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;priorityResult&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;detectTicketPriority&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;conversation&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;aiContext&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These operations are sequential within one job. Different jobs can still overlap. The service routes generation through an OpenAI-primary orchestrator with optional Gemini fallback for eligible failures, so three logical operations do not necessarily mean exactly three external HTTP requests.&lt;/p&gt;

&lt;p&gt;After sentiment and priority validation, lines 133–155 perform two separate writes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;prisma&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ticket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;where&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ticketId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;aiSummary&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;aiResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;summary&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;sentiment&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;priority&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;createAIInteraction&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="nx"&gt;ticketId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;conversation&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;response&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;aiResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;summary&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;tokensUsed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;aiResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tokensUsed&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Blank lines are condensed. &lt;code&gt;createAIInteraction&lt;/code&gt; is a plain Prisma insert. There is no job identity or conversation version attached to that record, and no conditional version check on the ticket update.&lt;/p&gt;

&lt;p&gt;The worker runs through a separate entry point in &lt;code&gt;backend/src/worker.ts&lt;/code&gt;. That separates its execution lifecycle from HTTP handling; it does not add coordination between jobs for one ticket.&lt;/p&gt;

&lt;h2&gt;
  
  
  Retries Solve Failure Recovery, Not Correctness
&lt;/h2&gt;

&lt;p&gt;The producer configures three total attempts, including the initial execution, with exponential backoff and a five-second base. For ordinary thrown-error retries, that gives nominal delays of five and ten seconds before the remaining attempts; scheduling can add waiting time. See BullMQ's &lt;a href="https://docs.bullmq.io/guide/retrying-failing-jobs" rel="noopener noreferrer"&gt;retry documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Completed jobs are removed. Failed jobs are retained. Those choices affect recovery and diagnosis, but they do not make processing idempotent.&lt;/p&gt;

&lt;p&gt;Retry asks, “Should I try this work again after failure?” Idempotency asks, “If I perform it again, will the resulting state remain correct?” Deduplication asks, “Should equivalent work be admitted or executed again at all?”&lt;/p&gt;

&lt;p&gt;Here, retries restart a processor that calls providers and performs database writes. The retry configuration supplies no rule for deciding whether those effects have already happened.&lt;/p&gt;

&lt;h2&gt;
  
  
  Failure Mode 1: The Database Commits but the Queue Does Not
&lt;/h2&gt;

&lt;p&gt;Consider the producer's actual order:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The message insert succeeds.&lt;/li&gt;
&lt;li&gt;The ticket timestamp update succeeds.&lt;/li&gt;
&lt;li&gt;Publication rejects, or the process exits before publication completes.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The database now contains the conversation change without assured corresponding queue work. Retrying a worker cannot recover a job that was never published.&lt;/p&gt;

&lt;p&gt;A publication error can also leave an ambiguous outcome: Redis might have accepted the job before the caller lost its acknowledgement. Recovery must handle both missing work and repeated publication.&lt;/p&gt;

&lt;p&gt;The earlier boundary also matters: if the ticket update fails after message creation, the message remains persisted and publication is never reached. Wrapping those two database writes in a transaction would address that database-only partial state. It would not atomically commit a Redis job with PostgreSQL data.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;proposed transactional outbox&lt;/strong&gt; changes where publication intent becomes durable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;BEFORE — CURRENT
Message insert → Ticket update → Redis publication
     database writes complete       can fail separately

IMPROVED — PROPOSED
PostgreSQL transaction:
  Message insert + Ticket update + Outbox event
                         ↓ committed publication intent
Outbox dispatcher → Redis/BullMQ → worker
       ↓
mark event published after successful queue publication
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The dispatcher retries unpublished events. If it publishes and crashes before marking the event, it may publish again. An outbox therefore needs stable event identity and duplicate-safe consumption; it does not confer exactly-once execution.&lt;/p&gt;

&lt;p&gt;This adds a table, dispatcher lifecycle, backlog monitoring and retention policy. For optional enrichment in an early application, explicit reconciliation may be an interim choice. If every accepted message must eventually be analyzed, I would prioritize an outbox regardless of traffic volume. Reliability requirements, rather than scale alone, determine when it is needed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Failure Mode 2: Retrying AI Work Is Not Automatically Idempotent
&lt;/h2&gt;

&lt;p&gt;Suppose summary generation succeeds and sentiment generation fails with an error that escapes the provider layer. BullMQ can retry the processor. It starts again by loading messages and generating the summary. There is no checkpoint that reuses the previous summary.&lt;/p&gt;

&lt;p&gt;Now move the failure later. Summary, sentiment and priority succeed; the ticket update commits; the interaction insert fails. The ticket already contains AI metadata, but the job has failed. A retry repeats the AI sequence and attempts both database writes again.&lt;/p&gt;

&lt;p&gt;The resulting values need not match the previous output. More messages may also have arrived before the retry reloads the conversation. The same job payload therefore does not necessarily identify the same input snapshot.&lt;/p&gt;

&lt;p&gt;A duplicate execution that reaches the insert can append another &lt;code&gt;AIInteraction&lt;/code&gt;: its schema uses a generated UUID and has no unique analysis key. Interruption after application writes but before queue completion is recorded creates another window for repeated effects. The relevant effects here are provider requests, ticket metadata writes and interaction records.&lt;/p&gt;

&lt;h3&gt;
  
  
  Deterministic job identity helps at publication
&lt;/h3&gt;

&lt;p&gt;A proposed identity such as &lt;code&gt;ticket-analysis-&amp;lt;messageId&amp;gt;&lt;/code&gt; could suppress repeated publication for the same saved message while that queue job exists. It would not suppress jobs triggered by two different messages, nor deduplicate message creation if a repeated HTTP request creates a new message ID.&lt;/p&gt;

&lt;p&gt;BullMQ's &lt;a href="https://docs.bullmq.io/guide/jobs/job-ids" rel="noopener noreferrer"&gt;job ID documentation&lt;/a&gt; explains that an existing custom ID prevents another job with that ID from being added. Once the job is removed, the ID no longer protects against duplicates. That is directly relevant to this application's &lt;code&gt;removeOnComplete: true&lt;/code&gt; setting.&lt;/p&gt;

&lt;h3&gt;
  
  
  Durable idempotency belongs at the application effect
&lt;/h3&gt;

&lt;p&gt;For this workflow, I would define a logical analysis key from ticket identity, conversation revision and analysis/prompt version. A durable record with a unique constraint could track whether that analysis has already been applied.&lt;/p&gt;

&lt;p&gt;The ticket update, interaction insert and successful analysis marker should commit together, with concurrent duplicate attempts resolved by database constraints and conditional writes. A separate initial “already done?” read is insufficient because two workers can both pass it.&lt;/p&gt;

&lt;p&gt;That would protect database effects. Provider requests made before commit could still repeat. Reusing stored generation results would require checkpointing and invalidation rules; I would start with atomic result persistence and durable identity.&lt;/p&gt;

&lt;h2&gt;
  
  
  Failure Mode 3: Ordering Is a Business Constraint
&lt;/h2&gt;

&lt;p&gt;Two messages arrive quickly on ticket T:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Message A is saved and job A starts. It reads the conversation containing A.&lt;/li&gt;
&lt;li&gt;Message B is saved and job B starts in another available worker slot. It reads A and B.&lt;/li&gt;
&lt;li&gt;Job B finishes first and writes metadata based on A and B.&lt;/li&gt;
&lt;li&gt;Job A finishes later and overwrites that metadata using its older context.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The current ticket update matches only &lt;code&gt;id: ticketId&lt;/code&gt;. Nothing prevents step four. This is a possible stale, last-writer-wins result visible from the architecture.&lt;/p&gt;

&lt;p&gt;Both jobs could also start after B exists, read overlapping or identical context, and perform redundant analysis. Because &lt;code&gt;messageId&lt;/code&gt; is unused, neither job is restricted to the conversation as it stood when its triggering message was saved.&lt;/p&gt;

&lt;p&gt;Sorting messages inside each job does not order jobs or their writes. The timestamp sort also has no explicit tie-breaker for equal timestamps.&lt;/p&gt;

&lt;p&gt;Global throughput and per-ticket correctness are separate constraints. Lowering one worker's concurrency to one would reduce overlap there, but also serialize unrelated tickets and would not establish a cross-process ordering policy.&lt;/p&gt;

&lt;h2&gt;
  
  
  Idempotency, Deduplication and Ordering Are Different
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Concept&lt;/th&gt;
&lt;th&gt;Question it answers&lt;/th&gt;
&lt;th&gt;Example in this system&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Retry&lt;/td&gt;
&lt;td&gt;Should failed work be attempted again?&lt;/td&gt;
&lt;td&gt;Retry a ticket analysis after a transient failure.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Deduplication&lt;/td&gt;
&lt;td&gt;Should equivalent work be admitted again?&lt;/td&gt;
&lt;td&gt;Proposed: suppress publication while the same job ID exists.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Idempotency&lt;/td&gt;
&lt;td&gt;Will repeating the work preserve correct state?&lt;/td&gt;
&lt;td&gt;Proposed: commit one analysis record for a ticket revision.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ordering&lt;/td&gt;
&lt;td&gt;In what sequence may work take effect?&lt;/td&gt;
&lt;td&gt;Proposed: apply ticket analyses in conversation-revision order.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Concurrency&lt;/td&gt;
&lt;td&gt;How much work may run at the same time?&lt;/td&gt;
&lt;td&gt;Allow five active jobs within this worker instance.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Only retries and worker concurrency are configured in the current path. The other examples describe proposed behavior. A version check can reject stale results without executing every job in strict order; serialization still needs a policy for retries and earlier failures.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Better Per-Ticket Processing Model
&lt;/h2&gt;

&lt;p&gt;The following is a &lt;strong&gt;proposed model&lt;/strong&gt;, not current code. For summary, sentiment and priority, the business goal is usually a current conversation view. It may not require publishing every intermediate analysis.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Messages A, B, C → atomically advance ticket conversation revision
                                   ↓
                       record latest requested revision
                                   ↓
                 coalesce pending work for this ticket
                                   ↓
                 one active analysis per ticket, if needed
                                   ↓
                    read consistent snapshot at revision R
                                   ↓
                            generate AI results
                                   ↓
                  atomic apply only if revision is still R
                          ↙                      ↘
                stale: discard              current: commit
                    ↓                    results + analysis record
         ensure latest revision remains scheduled
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Version checks are the first correctness protection I would add.&lt;/strong&gt; A dedicated conversation revision should advance atomically with message persistence. The worker must read the revision and messages as a consistent snapshot, then conditionally apply results in a short database transaction. A separate check followed by an unconditional update leaves a race.&lt;/p&gt;

&lt;p&gt;I would avoid using &lt;code&gt;Ticket.updatedAt&lt;/code&gt; as the conversation revision. Prisma marks it &lt;code&gt;@updatedAt&lt;/code&gt;, so AI writes and unrelated ticket edits also change it. A dedicated revision makes the input contract explicit. The tradeoff is schema and transaction work; discarded stale analyses still consume processing. This protection is useful now because the current concurrency already permits stale writes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Coalescing can reduce unnecessary work.&lt;/strong&gt; Several pending triggers can represent one request to analyze the latest conversation. A short debounce period trades freshness for fewer intermediate analyses. BullMQ offers &lt;a href="https://docs.bullmq.io/guide/jobs/deduplication" rel="noopener noreferrer"&gt;deduplication modes&lt;/a&gt;, but their lifetime semantics must match the application.&lt;/p&gt;

&lt;p&gt;In particular, simply suppressing every new job while one analysis is active can miss updates arriving after that analysis loaded its messages. The design must durably track the latest requested revision and arrange another pass when needed. Checking once at the end without an atomic handoff can still lose a concurrent update. Coalescing is a pragmatic next step when message bursts cause redundant work; it is not a substitute for version checks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Per-ticket serialization can limit overlap.&lt;/strong&gt; A coordinated lease or partitioned processing scheme can allow different tickets to progress concurrently while restricting one ticket's active analysis. An in-memory mutex covers only one process. Distributed leases introduce expiry, renewal and crash-recovery concerns; conditional version checks remain valuable when a lease expires while work is still running.&lt;/p&gt;

&lt;p&gt;If a future job must act on every message in business order, it needs explicit sequence tracking and a policy for earlier failures. Coalescing intermediate analyses would no longer satisfy that requirement.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I Would Change First
&lt;/h2&gt;

&lt;p&gt;These are recommendations for this application, not features already implemented.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Define analysis identity and protect freshness together.&lt;/strong&gt; Introduce a conversation revision, conditional result application and an atomic transaction for ticket metadata, interaction and an analysis marker. Then use deterministic publication identity for the chosen unit of work. This addresses correctness exposed by today's concurrency; it costs a schema change and careful transaction design.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Coalesce pending ticket analysis where the product permits it.&lt;/strong&gt; Treat the job as “refresh this ticket” rather than “produce every intermediate result,” while preserving a durable follow-up for changes during active processing. This costs scheduling complexity and may delay freshness slightly. It becomes more useful when bursts create redundant work. Add coordinated per-ticket serialization if overlap remains significant; version protection comes first.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Close the database-to-queue gap when eventual analysis is required.&lt;/strong&gt; Add an outbox and replay-safe dispatcher. Also review producer connection policy: the shared Redis module sets &lt;code&gt;maxRetriesPerRequest: null&lt;/code&gt;, and the message service has no explicit publication deadline. BullMQ’s &lt;a href="https://docs.bullmq.io/guide/connections" rel="noopener noreferrer"&gt;connection guidance&lt;/a&gt; distinguishes worker recovery from bounded waiting in HTTP producers. The operational cost is real, but the need depends on the reliability contract, not a hypothetical high-traffic milestone.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Build recovery on the visibility already present.&lt;/strong&gt; The worker logs receipt, completion, failures, ticket/job IDs and duration. Before using the attempt fields for alerts, I would verify their meaning at receipt and failure events. Add exhausted-attempt alerts, queue age/backlog visibility, stale-result counters and revision correlation. These are useful now; a larger telemetry platform can wait.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Add a deliberate replay policy.&lt;/strong&gt; Retained failed jobs are a starting point, not an implemented dead-letter workflow. An operator should distinguish transient failures from invalid input, replay after repair, and know whether replay means the original snapshot or latest conversation. Controlled replay tooling and retention add maintenance work. A separate dead-letter queue is optional until volume or operational ownership warrants it.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I would evaluate these changes through duplicate work, result freshness and publication failures. Infrastructure changes should follow a demonstrated constraint.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I Would Not Add Yet
&lt;/h2&gt;

&lt;p&gt;I would not introduce Kafka, Kubernetes or decompose the application into microservices solely because it has a queue.&lt;/p&gt;

&lt;p&gt;The concrete concerns are durable publication intent, safe repeated writes and valid application of conversation-derived results. They can be addressed within PostgreSQL, Redis, BullMQ and the existing API/worker boundary.&lt;/p&gt;

&lt;p&gt;More infrastructure may become appropriate under specific throughput, ownership or deployment constraints. It does not remove the need to define what makes an analysis current or safe to repeat.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why AI Workloads Make This More Visible
&lt;/h2&gt;

&lt;p&gt;In this worker, three sequential generation operations separate the initial conversation read from the final ticket update. While those external requests are in flight, another message can arrive and another job can complete. The queue can function normally while the result becomes obsolete.&lt;/p&gt;

&lt;p&gt;Provider calls can be relatively slow, and successful generation consumes usage billed under the provider’s terms; OpenAI documents &lt;a href="https://developers.openai.com/api/docs/pricing" rel="noopener noreferrer"&gt;token-based API pricing&lt;/a&gt;. Repeating a completed summary because sentiment failed can therefore repeat meaningful work, even when no database row has yet changed. This is a reason to control repetition, not a measured cost or latency claim about this application.&lt;/p&gt;

&lt;p&gt;Generated text can also differ between attempts. Overwriting &lt;code&gt;aiSummary&lt;/code&gt; with a second output is not automatically idempotent just because both writes target the same row. Meanwhile, this processor reloads messages on every attempt, so a retry can change both its input and its output.&lt;/p&gt;

&lt;p&gt;For this workload, the useful contract is: which conversation revision does this analysis describe, and when may it take effect? The queue schedules execution. The application must answer that question.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Takeaway
&lt;/h2&gt;

&lt;p&gt;BullMQ gives this application a clear boundary between message creation and background AI enrichment. Correctness still needs an application-level design: durable publication intent, duplicate-safe effects and protection against stale conversation results.&lt;/p&gt;

&lt;p&gt;Retries make another attempt possible. Idempotency and ordering rules determine whether that attempt is safe to apply.&lt;/p&gt;

&lt;p&gt;Originally part of the engineering work behind my AI Support Assistant architecture.&lt;/p&gt;

&lt;p&gt;Originally published on my engineering blog:&lt;br&gt;
&lt;a href="https://blog.ramjwork.in/ai-engineering/redis-bullmq-ai-background-jobs" rel="noopener noreferrer"&gt;https://blog.ramjwork.in/ai-engineering/redis-bullmq-ai-background-jobs&lt;/a&gt;&lt;/p&gt;

</description>
      <category>node</category>
      <category>redis</category>
      <category>ai</category>
      <category>architecture</category>
    </item>
    <item>
      <title>How I Architected an AI Customer Support System with React, Node.js, PostgreSQL, Redis &amp; OpenAI</title>
      <dc:creator>Ram Ji Tripathi</dc:creator>
      <pubDate>Sun, 04 Oct 2026 05:11:40 +0000</pubDate>
      <link>https://dev.to/ramji_tripathi_095c7f4810/how-i-architected-an-ai-customer-support-system-with-react-nodejs-postgresql-redis-openai-2jcf</link>
      <guid>https://dev.to/ramji_tripathi_095c7f4810/how-i-architected-an-ai-customer-support-system-with-react-nodejs-postgresql-redis-openai-2jcf</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Adding AI wasn’t the difficult part. Deciding where AI belongs in the application architecture was.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Adding an LLM API to an application is relatively straightforward.&lt;/p&gt;

&lt;p&gt;The harder questions begin after the first API call works.&lt;/p&gt;

&lt;p&gt;Should AI run inside the user’s HTTP request? What happens when the provider is slow? Which AI operations should be asynchronous? What belongs in React state versus server state? Should an AI-generated support reply be sent automatically? What happens when a background job retries after partially completing?&lt;/p&gt;

&lt;p&gt;I ran into these questions while building an AI-powered customer support application with React, TypeScript, Node.js, PostgreSQL, Redis, BullMQ and OpenAI.&lt;/p&gt;

&lt;p&gt;The interesting part of the project wasn’t the prompt.&lt;/p&gt;

&lt;p&gt;It was deciding &lt;strong&gt;where AI should—and should not—sit inside the system.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This article walks through that architecture, shows selected code from the actual implementation, and discusses several things I would change before scaling the design further.&lt;/p&gt;




&lt;h2&gt;
  
  
  What the Application Does
&lt;/h2&gt;

&lt;p&gt;The application revolves around support tickets and conversations.&lt;/p&gt;

&lt;p&gt;It has three roles:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Customers&lt;/strong&gt; create tickets, read their own tickets and add messages.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Agents&lt;/strong&gt; work with assigned tickets, update their status and request AI-generated reply suggestions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Admins&lt;/strong&gt; have broader ticket access and assignment capabilities.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;AI assists the workflow in two different ways.&lt;/p&gt;

&lt;p&gt;First, when a new message is added, background processing analyzes the ticket conversation to generate:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a ticket summary,&lt;/li&gt;
&lt;li&gt;sentiment,&lt;/li&gt;
&lt;li&gt;priority.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Second, an agent or admin can explicitly request an AI-generated reply suggestion.&lt;/p&gt;

&lt;p&gt;Those two AI workloads look similar at first—they both call an LLM—but they have very different execution requirements.&lt;/p&gt;

&lt;p&gt;That distinction shaped much of the architecture.&lt;/p&gt;




&lt;h2&gt;
  
  
  High-Level Architecture
&lt;/h2&gt;

&lt;p&gt;At a high level, the system looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                    ┌─────────────────────────┐
                    │       React Client      │
                    │                         │
                    │ Auth / Tickets /        │
                    │ Messages / AI Replies   │
                    └────────────┬────────────┘
                                 │
                              REST API
                                 │
                    ┌────────────▼────────────┐
                    │     Node.js / Express   │
                    │                         │
                    │ Auth → Controller →     │
                    │ Service → Repository    │
                    └───────┬─────────┬───────┘
                            │         │
                            │         │ enqueue
                            │         ▼
                    ┌───────▼───┐  ┌──────────────┐
                    │PostgreSQL │  │ Redis/BullMQ │
                    └───────────┘  └──────┬───────┘
                                         │
                                         ▼
                                  ┌─────────────┐
                                  │  AI Worker  │
                                  │             │
                                  │ Summary     │
                                  │ Sentiment   │
                                  │ Priority    │
                                  └──────┬──────┘
                                         │
                                    LLM provider
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The application is deliberately not split into many network services.&lt;/p&gt;

&lt;p&gt;The API is a &lt;strong&gt;modular monolith&lt;/strong&gt;, while AI background execution runs in a separate worker process.&lt;/p&gt;

&lt;p&gt;That gives me an important boundary without introducing a distributed-service architecture prematurely:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;HTTP request handling and background AI processing have independent execution lifecycles.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Frontend State: Don’t Put Everything in One Store
&lt;/h2&gt;

&lt;p&gt;One frontend decision was to separate state based on &lt;strong&gt;who owns it&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;I use three broad categories:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;authentication/session state → React Context,&lt;/li&gt;
&lt;li&gt;remote server state → TanStack Query,&lt;/li&gt;
&lt;li&gt;transient UI state → local component state.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, tickets are server-owned resources, so their fetching and cache lifecycle belong to TanStack Query:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;useTickets&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;TicketListParams&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;useQuery&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;queryKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ticketKeys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="na"&gt;queryFn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;getTickets&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;useTicket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ticketId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;useQuery&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;queryKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ticketKeys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;detail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ticketId&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="na"&gt;queryFn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;getTicketById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ticketId&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="na"&gt;enabled&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Boolean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ticketId&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;useCreateTicket&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;queryClient&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useQueryClient&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;useMutation&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;mutationFn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="na"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;CreateTicketRequest&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;createTicket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;

    &lt;span class="na"&gt;onSuccess&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;queryClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;invalidateQueries&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;queryKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ticketKeys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lists&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The important part here isn’t the library.&lt;/p&gt;

&lt;p&gt;It’s the ownership model.&lt;/p&gt;

&lt;p&gt;A ticket fetched from the API is not fundamentally client state. The server remains authoritative.&lt;/p&gt;

&lt;p&gt;TanStack Query handles fetching, caching and invalidation, while component-local state handles things such as editable drafts and temporary UI interactions.&lt;/p&gt;

&lt;p&gt;This prevents a common React architecture problem: turning one global store into a mirror of the backend.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Backend Is a Modular Monolith
&lt;/h2&gt;

&lt;p&gt;The backend follows a layered flow that is roughly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Route
  ↓
Authentication / Authorization / Validation
  ↓
Controller
  ↓
Service
  ↓
Repository
  ↓
Prisma
  ↓
PostgreSQL
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This gives features such as tickets and messages clear boundaries without requiring each feature to become an independent service.&lt;/p&gt;

&lt;p&gt;The service layer owns application workflows.&lt;/p&gt;

&lt;p&gt;Repositories isolate most persistence operations.&lt;/p&gt;

&lt;p&gt;Controllers remain focused primarily on translating HTTP requests and responses.&lt;/p&gt;

&lt;p&gt;AI is a partial exception because some worker-side operations interact more directly with Prisma, but the overall HTTP architecture follows this separation.&lt;/p&gt;

&lt;p&gt;For the current scope, I prefer this to creating multiple microservices.&lt;/p&gt;

&lt;p&gt;A service boundary should solve an actual deployment, ownership or scaling problem—not exist simply because the system has multiple features.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why I Moved AI Analysis Out of the Request Path
&lt;/h2&gt;

&lt;p&gt;Consider what happens when a customer adds a message.&lt;/p&gt;

&lt;p&gt;The important user-facing operation is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Persist my message successfully.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Summary, sentiment and priority enrichment are useful, but the user should not have to wait for several LLM operations to complete before the message request can finish.&lt;/p&gt;

&lt;p&gt;The service therefore persists the message and publishes a BullMQ job:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;createMessageService&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;CreateMessageInput&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;getAuthorizedTicketService&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ticketId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;accessContext&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;createMessageRepository&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;ticketId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ticketId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;senderId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;senderId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;updateTicketRepository&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ticketId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;updatedAt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;aiQueue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;GENERATE_TICKET_SUMMARY_JOB&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;ticketId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ticketId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;messageId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;attempts&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;backoff&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;exponential&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="na"&gt;removeOnComplete&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;removeOnFail&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is an important architectural detail here.&lt;/p&gt;

&lt;p&gt;The HTTP request &lt;strong&gt;does wait for queue publication&lt;/strong&gt;, but it does &lt;strong&gt;not wait for the AI analysis itself&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Conceptually:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;POST message
     │
     ▼
Authorize access
     │
     ▼
Persist message
     │
     ▼
Update ticket timestamp
     │
     ▼
Publish BullMQ job
     │
     ├──────────────► HTTP response
     │
     ▼
Background worker
     │
     ▼
AI processing
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That removes LLM execution time from the main request path while still ensuring the API knows whether it successfully handed work to the queue.&lt;/p&gt;

&lt;p&gt;It also allows the worker lifecycle to be operated separately from the HTTP server.&lt;/p&gt;




&lt;h2&gt;
  
  
  What the Background Worker Actually Does
&lt;/h2&gt;

&lt;p&gt;BullMQ uses Redis as the queue infrastructure.&lt;/p&gt;

&lt;p&gt;A separate worker consumes AI-processing jobs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;aiWorker&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Worker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;AI_QUEUE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

  &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;job&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;startedAt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nx"&gt;jobStartTimes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;job&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nx"&gt;startedAt&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ticketId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
      &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;job&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;ticketId&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;string&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;job&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ticketId&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nx"&gt;workerLogger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;event&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ai_job_received&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;jobId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;job&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;jobName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;job&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nx"&gt;ticketId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;attempt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;job&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;attemptsMade&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Job received&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;switch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;job&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="na"&gt;GENERATE_TICKET_SUMMARY_JOB&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;processTicketSummaryJob&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ticketId&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="nx"&gt;job&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ticketId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

      &lt;span class="nl"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nx"&gt;workerLogger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;warn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
          &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;event&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ai_job_unknown&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;jobId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;job&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;jobName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;job&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="nx"&gt;ticketId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="p"&gt;},&lt;/span&gt;
          &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Unknown job&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;

  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;connection&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;redis&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;concurrency&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside the ticket-processing workflow, messages are loaded in conversation order and transformed into the conversation passed to the AI layer.&lt;/p&gt;

&lt;p&gt;The relevant part of the processing pipeline is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;conversation&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;formatConversation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;aiResult&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;generateTicketSummary&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;conversation&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="nx"&gt;aiContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sentimentResult&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;generateSentiment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;conversation&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="nx"&gt;aiContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;priorityResult&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;detectTicketPriority&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;conversation&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;aiContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sentiment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;validateSentiment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;sentimentResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sentiment&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;priority&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;validatePriority&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;priorityResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;priority&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Summary, sentiment and priority are currently generated sequentially inside a job.&lt;/p&gt;

&lt;p&gt;The normalized results are then used to update ticket-level AI fields, and an AI interaction record is created for the summary workflow.&lt;/p&gt;

&lt;p&gt;This design gives the application retryable asynchronous execution without coupling LLM latency directly to message creation.&lt;/p&gt;

&lt;p&gt;But queues introduce their own problems.&lt;/p&gt;

&lt;p&gt;I’ll return to those shortly.&lt;/p&gt;




&lt;h2&gt;
  
  
  Not Every AI Operation Belongs in a Queue
&lt;/h2&gt;

&lt;p&gt;Once background processing existed, it would have been easy to conclude:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;AI is slow, therefore every AI operation should be queued.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I don’t think that is the right abstraction.&lt;/p&gt;

&lt;p&gt;The correct execution model depends on what the user is doing.&lt;/p&gt;

&lt;p&gt;Ticket analysis is enrichment. It can happen asynchronously.&lt;/p&gt;

&lt;p&gt;An agent clicking &lt;strong&gt;Generate Reply&lt;/strong&gt;, however, is explicitly waiting for an answer.&lt;/p&gt;

&lt;p&gt;For that workflow, the backend generates the suggestion synchronously:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;conversation&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;formatConversation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;aiResult&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;generateReplySuggestion&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;conversation&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;ticketId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;requestId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;string&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;res&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ApiResponse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Reply suggestion generated&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;aiResult&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The important distinction is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Background analysis
Message → Queue → Worker → AI → Ticket metadata

Interactive assistance
Agent → Request suggestion → AI → Suggested text → Agent
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The second flow doesn’t automatically create or send a support message.&lt;/p&gt;

&lt;p&gt;That is intentional.&lt;/p&gt;




&lt;h2&gt;
  
  
  Human-in-the-Loop AI
&lt;/h2&gt;

&lt;p&gt;A generated support reply can be wrong, incomplete or inappropriate for the context.&lt;/p&gt;

&lt;p&gt;So the system treats AI output as a &lt;strong&gt;suggestion&lt;/strong&gt;, not an autonomous action.&lt;/p&gt;

&lt;p&gt;On the frontend:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ReplySuggestion&lt;/span&gt;
  &lt;span class="na"&gt;ticketId&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
  &lt;span class="na"&gt;onUseReply&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setComposerDraft&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;setComposerKey&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;current&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;

&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;MessageComposer&lt;/span&gt;
  &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;composerKey&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
  &lt;span class="na"&gt;ticketId&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
  &lt;span class="na"&gt;initialContent&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;composerDraft&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
  &lt;span class="na"&gt;onMessageSent&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class="nf"&gt;setMessageRefreshToken&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;current&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="si"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the user selects &lt;strong&gt;Use Reply&lt;/strong&gt;, the suggestion is moved into the normal message composer.&lt;/p&gt;

&lt;p&gt;The agent can then:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Generate
   ↓
Review
   ↓
Edit
   ↓
Send
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The AI does not own the final action.&lt;/p&gt;

&lt;p&gt;That boundary matters more to me than simply adding another confirmation dialog.&lt;/p&gt;

&lt;p&gt;The application architecture itself separates &lt;strong&gt;generation&lt;/strong&gt; from &lt;strong&gt;execution&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This is a pattern I would reuse in many AI-assisted workflows where the output affects another person or an important business process.&lt;/p&gt;




&lt;h2&gt;
  
  
  AI Provider Logic Belongs Behind an Application Boundary
&lt;/h2&gt;

&lt;p&gt;The application currently uses an OpenAI-backed adapter for operations such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;summarization,&lt;/li&gt;
&lt;li&gt;sentiment analysis,&lt;/li&gt;
&lt;li&gt;priority detection,&lt;/li&gt;
&lt;li&gt;reply suggestions.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There is also provider orchestration that can use Gemini as a fallback for selected classes of provider, quota, rate-limit, network or timeout failures.&lt;/p&gt;

&lt;p&gt;I don’t want ticket or message business logic to know the details of every provider.&lt;/p&gt;

&lt;p&gt;Conceptually:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Business workflow
       │
       ▼
AI operation
       │
       ▼
Provider orchestration
       │
       ├── OpenAI
       │
       └── Gemini fallback
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This boundary becomes useful even if the application never changes providers.&lt;/p&gt;

&lt;p&gt;It gives one place to reason about:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;prompts,&lt;/li&gt;
&lt;li&gt;provider failures,&lt;/li&gt;
&lt;li&gt;normalization,&lt;/li&gt;
&lt;li&gt;fallback rules,&lt;/li&gt;
&lt;li&gt;model configuration.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Provider abstraction does not mean every model is interchangeable.&lt;/p&gt;

&lt;p&gt;Different models behave differently.&lt;/p&gt;

&lt;p&gt;The goal is simply to stop provider-specific concerns from leaking through the rest of the application.&lt;/p&gt;




&lt;h2&gt;
  
  
  Authentication Is Not Authorization
&lt;/h2&gt;

&lt;p&gt;The application uses JWT authentication, but authentication alone does not answer:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Is this user allowed to access this particular ticket?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A valid token identifies the user and role.&lt;/p&gt;

&lt;p&gt;Resource authorization still has to be enforced.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;customers should only access their own tickets,&lt;/li&gt;
&lt;li&gt;agents operate on tickets available to their role and assignment rules,&lt;/li&gt;
&lt;li&gt;admins have broader access.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The frontend also hides or exposes functionality according to role, but that is a UX decision—not a security boundary.&lt;/p&gt;

&lt;p&gt;The backend remains authoritative.&lt;/p&gt;

&lt;p&gt;This distinction is especially important in React applications because hiding a button is easy to mistake for enforcing permission.&lt;/p&gt;

&lt;p&gt;It isn’t.&lt;/p&gt;




&lt;h2&gt;
  
  
  Redis Has More Than One Responsibility
&lt;/h2&gt;

&lt;p&gt;Redis is not used only because BullMQ needs it.&lt;/p&gt;

&lt;p&gt;In this project it also supports:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;short-lived ticket caching,&lt;/li&gt;
&lt;li&gt;distributed rate-limit counters.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That makes Redis shared infrastructure serving different concerns:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Redis
├── BullMQ queue infrastructure
├── ticket cache
└── rate-limit counters
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Those responsibilities should still remain conceptually separate even if they use the same underlying technology.&lt;/p&gt;

&lt;p&gt;A queue is not a cache.&lt;/p&gt;

&lt;p&gt;A cache is not a source of truth.&lt;/p&gt;

&lt;p&gt;The database remains the authoritative store for ticket and conversation data.&lt;/p&gt;




&lt;h2&gt;
  
  
  What the Architecture Gets Right
&lt;/h2&gt;

&lt;p&gt;There are several boundaries in this design that I would keep.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. User-facing writes are separated from AI enrichment
&lt;/h3&gt;

&lt;p&gt;Message creation does not wait for summary, sentiment and priority generation.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Interactive AI and background AI use different execution models
&lt;/h3&gt;

&lt;p&gt;The system chooses between synchronous and asynchronous execution based on the user workflow rather than treating all AI calls identically.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Server state remains server state
&lt;/h3&gt;

&lt;p&gt;TanStack Query manages API-backed ticket state rather than copying everything into a global client store.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. AI suggestions do not automatically become actions
&lt;/h3&gt;

&lt;p&gt;Generated replies remain editable suggestions.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. HTTP and worker lifecycles are separate
&lt;/h3&gt;

&lt;p&gt;The API server and AI worker can fail, restart and evolve independently at the process level.&lt;/p&gt;

&lt;p&gt;But none of those decisions makes the architecture finished.&lt;/p&gt;

&lt;p&gt;Several weaknesses become more important as concurrency and operational requirements increase.&lt;/p&gt;




&lt;h2&gt;
  
  
  What I Would Change Before Scaling
&lt;/h2&gt;

&lt;p&gt;This is the part of the architecture I find most useful to examine.&lt;/p&gt;

&lt;p&gt;Queues solve one category of problem while creating others.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Fix the Database-to-Queue Consistency Gap
&lt;/h3&gt;

&lt;p&gt;Look again at message creation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Write message to PostgreSQL
        ↓
Update ticket
        ↓
Publish BullMQ job
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These are separate operations.&lt;/p&gt;

&lt;p&gt;Imagine:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;PostgreSQL successfully saves the message.&lt;/li&gt;
&lt;li&gt;Redis becomes unavailable before &lt;code&gt;aiQueue.add()&lt;/code&gt; succeeds.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The application now has committed business data without the corresponding background event.&lt;/p&gt;

&lt;p&gt;Retries do not solve this consistency problem.&lt;/p&gt;

&lt;p&gt;A stronger design would introduce a &lt;strong&gt;transactional outbox&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Conceptually:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;             PostgreSQL transaction
        ┌────────────────────────────┐
        │ Save message               │
        │ Save outbox event          │
        └──────────────┬─────────────┘
                       │ commit
                       ▼
                Outbox publisher
                       │
                       ▼
                   BullMQ
                       │
                       ▼
                    Worker
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The message and the intent to perform AI processing would be committed atomically in PostgreSQL.&lt;/p&gt;

&lt;p&gt;A separate publisher could then reliably move pending outbox events into BullMQ.&lt;/p&gt;

&lt;p&gt;That doesn’t make the entire system exactly-once.&lt;/p&gt;

&lt;p&gt;It closes a specific and important consistency gap.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Retries Need Idempotency
&lt;/h3&gt;

&lt;p&gt;The queue currently configures multiple attempts with exponential backoff.&lt;/p&gt;

&lt;p&gt;That improves recoverability from transient failures.&lt;/p&gt;

&lt;p&gt;But:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;retryable does not automatically mean safe to retry.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Suppose a job:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;calls the AI provider,&lt;/li&gt;
&lt;li&gt;updates the ticket,&lt;/li&gt;
&lt;li&gt;writes an AI interaction,&lt;/li&gt;
&lt;li&gt;fails somewhere around those operations,&lt;/li&gt;
&lt;li&gt;retries.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Without explicit idempotency, repeated attempts can repeat side effects.&lt;/p&gt;

&lt;p&gt;A stronger implementation would give each logical processing operation a stable identity and make persisted effects safe to replay.&lt;/p&gt;

&lt;p&gt;Possible approaches include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;deterministic job IDs,&lt;/li&gt;
&lt;li&gt;processing-version records,&lt;/li&gt;
&lt;li&gt;unique database constraints,&lt;/li&gt;
&lt;li&gt;upsert semantics,&lt;/li&gt;
&lt;li&gt;explicit processed-event records.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The exact mechanism depends on the workflow.&lt;/p&gt;

&lt;p&gt;The architectural requirement is simpler:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A retry should not accidentally turn one logical operation into multiple business effects.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Protect Against Same-Ticket Concurrency
&lt;/h3&gt;

&lt;p&gt;The worker has concurrency greater than one.&lt;/p&gt;

&lt;p&gt;That’s useful for processing unrelated tickets.&lt;/p&gt;

&lt;p&gt;But it also means two jobs for the same ticket can overlap.&lt;/p&gt;

&lt;p&gt;Consider:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Ticket conversation V1
        │
        └── Job A starts

New message arrives
        │
Ticket conversation V2
        │
        └── Job B starts

Job B finishes first → stores V2 analysis
Job A finishes later → stores older V1 analysis
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The final ticket can now contain stale AI metadata.&lt;/p&gt;

&lt;p&gt;This is not solved by generic worker concurrency configuration alone.&lt;/p&gt;

&lt;p&gt;Before scaling the workflow, I would introduce some combination of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;per-ticket serialization,&lt;/li&gt;
&lt;li&gt;conversation version numbers,&lt;/li&gt;
&lt;li&gt;compare-before-write logic,&lt;/li&gt;
&lt;li&gt;coalescing pending analysis jobs,&lt;/li&gt;
&lt;li&gt;stale-result rejection.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The important principle is that &lt;strong&gt;global concurrency and entity-level ordering are different problems&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Give the UI an Explicit AI-Result Delivery Mechanism
&lt;/h3&gt;

&lt;p&gt;Background processing creates another question:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;How does the browser learn that AI analysis has finished?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The current architecture does not implement a dedicated real-time result-delivery channel.&lt;/p&gt;

&lt;p&gt;At larger scale or with a richer UI, I would make that explicit.&lt;/p&gt;

&lt;p&gt;Depending on the product requirements:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Simplest
Polling

        ↓

More event-driven
Server-Sent Events

        ↓

Bidirectional realtime needs
WebSockets
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I would not automatically choose WebSockets.&lt;/p&gt;

&lt;p&gt;If the browser only needs server-to-client status updates, SSE may be enough.&lt;/p&gt;

&lt;p&gt;Architecture should follow the interaction requirement, not the popularity of the technology.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Tighten Cache Freshness Rules
&lt;/h3&gt;

&lt;p&gt;Caching ticket data can reduce repeated reads, but mutable support conversations create invalidation challenges.&lt;/p&gt;

&lt;p&gt;Every path that changes data relevant to a cached ticket needs a clear freshness strategy.&lt;/p&gt;

&lt;p&gt;Before relying more heavily on caching, I would explicitly document:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;what is cached,&lt;/li&gt;
&lt;li&gt;the cache key,&lt;/li&gt;
&lt;li&gt;TTL,&lt;/li&gt;
&lt;li&gt;which writes invalidate it,&lt;/li&gt;
&lt;li&gt;whether stale reads are acceptable,&lt;/li&gt;
&lt;li&gt;what happens when invalidation fails.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A short TTL is useful, but TTL alone isn’t a complete consistency model.&lt;/p&gt;

&lt;h3&gt;
  
  
  6. Improve the AI Context and Evaluation Layer
&lt;/h3&gt;

&lt;p&gt;The current AI pipeline is intentionally straightforward.&lt;/p&gt;

&lt;p&gt;There are several improvements I would investigate before treating AI behavior as a mature subsystem:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;explicit structured outputs,&lt;/li&gt;
&lt;li&gt;conversation-size/token management,&lt;/li&gt;
&lt;li&gt;better context construction,&lt;/li&gt;
&lt;li&gt;prompt/model version tracking,&lt;/li&gt;
&lt;li&gt;evaluation datasets,&lt;/li&gt;
&lt;li&gt;regression testing for AI behavior,&lt;/li&gt;
&lt;li&gt;provider health telemetry,&lt;/li&gt;
&lt;li&gt;clearer usage accounting,&lt;/li&gt;
&lt;li&gt;privacy and retention policies for model inputs and outputs.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I would add these based on actual product requirements rather than building an elaborate AI platform prematurely.&lt;/p&gt;

&lt;p&gt;The important shift is from:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Does the model return something?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;to:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Can I measure whether this AI behavior remains useful and reliable as the application changes?&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  What I Would Not Add Yet
&lt;/h2&gt;

&lt;p&gt;Scaling discussions can easily turn into architecture shopping lists.&lt;/p&gt;

&lt;p&gt;I would &lt;strong&gt;not&lt;/strong&gt; automatically add:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Kafka,&lt;/li&gt;
&lt;li&gt;Kubernetes,&lt;/li&gt;
&lt;li&gt;multiple microservices,&lt;/li&gt;
&lt;li&gt;vector databases,&lt;/li&gt;
&lt;li&gt;WebSockets,&lt;/li&gt;
&lt;li&gt;event sourcing,&lt;/li&gt;
&lt;li&gt;multiple caches.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;None of those technologies is inherently an upgrade.&lt;/p&gt;

&lt;p&gt;For this system, I would first strengthen the guarantees around the architecture that already exists:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DB/queue consistency
        ↓
Idempotent processing
        ↓
Per-ticket ordering/versioning
        ↓
Observable job state
        ↓
AI evaluation
        ↓
Scale infrastructure when evidence requires it
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Complexity should be purchased with a requirement.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Most Important Lesson: AI Is a Workload, Not the Architecture
&lt;/h2&gt;

&lt;p&gt;It is tempting to describe this application as an OpenAI integration.&lt;/p&gt;

&lt;p&gt;That misses most of the engineering.&lt;/p&gt;

&lt;p&gt;The LLM is one dependency inside a larger system.&lt;/p&gt;

&lt;p&gt;The architecture still has to answer ordinary software-engineering questions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Who owns state?&lt;/li&gt;
&lt;li&gt;Which operations block the user?&lt;/li&gt;
&lt;li&gt;What should happen asynchronously?&lt;/li&gt;
&lt;li&gt;Where is authorization enforced?&lt;/li&gt;
&lt;li&gt;What happens after partial failure?&lt;/li&gt;
&lt;li&gt;Are retries safe?&lt;/li&gt;
&lt;li&gt;Can concurrent work overwrite newer state?&lt;/li&gt;
&lt;li&gt;How does the UI learn that background work finished?&lt;/li&gt;
&lt;li&gt;Where does human approval belong?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Those questions existed before LLMs.&lt;/p&gt;

&lt;p&gt;AI simply makes several of them more visible because model calls are external, comparatively slow, probabilistic and failure-prone.&lt;/p&gt;




&lt;h2&gt;
  
  
  Final Architecture
&lt;/h2&gt;

&lt;p&gt;The current design can be summarized as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                           React + TypeScript
                                  │
                 ┌────────────────┼────────────────┐
                 │                │                │
            Auth Context     TanStack Query    Local UI State
                 │                │                │
                 └────────────────┼────────────────┘
                                  │
                                  ▼
                           Express REST API
                                  │
                    Auth / AuthZ / Validation
                                  │
                                  ▼
                       Application Services
                         │               │
                         │               │
                         ▼               ▼
                    PostgreSQL      Redis / BullMQ
                                         │
                                         ▼
                                     AI Worker
                                         │
                             ┌───────────┼───────────┐
                             │           │           │
                          Summary    Sentiment    Priority
                             │           │           │
                             └───────────┼───────────┘
                                         │
                                    AI Providers

Interactive reply path:

Agent → API → AI reply suggestion → editable composer → human sends
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is intentionally simpler than a large distributed system.&lt;/p&gt;

&lt;p&gt;And it has known limitations.&lt;/p&gt;

&lt;p&gt;I consider both of those facts healthy.&lt;/p&gt;

&lt;p&gt;Good architecture is not about pretending the current system can handle every future requirement.&lt;/p&gt;

&lt;p&gt;It is about knowing &lt;strong&gt;which guarantees the system currently provides, which ones it doesn’t, and where the next architectural pressure points will appear.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Closing
&lt;/h2&gt;

&lt;p&gt;Building this project changed how I think about adding AI to existing applications.&lt;/p&gt;

&lt;p&gt;I wouldn’t start by asking:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Where can I call the LLM?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I would start with:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;What role should AI have in this workflow?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Then decide:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;synchronous or asynchronous,&lt;/li&gt;
&lt;li&gt;advisory or autonomous,&lt;/li&gt;
&lt;li&gt;user-facing or background,&lt;/li&gt;
&lt;li&gt;retryable or idempotent,&lt;/li&gt;
&lt;li&gt;cached or authoritative,&lt;/li&gt;
&lt;li&gt;human-reviewed or automatically executed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once those boundaries are clear, choosing the API is usually the easier part.&lt;/p&gt;

&lt;p&gt;In the next article in this architecture series, I’ll go deeper into &lt;strong&gt;BullMQ background jobs for AI workloads—especially retries, idempotency and ordering&lt;/strong&gt;, because adding a queue is only the beginning of making background processing reliable.&lt;/p&gt;




&lt;h2&gt;
  
  
  About the author
&lt;/h2&gt;

&lt;p&gt;I’m Ram Ji Tripathi, a senior frontend-focused full-stack engineer working across React, TypeScript, Node.js and AI-enabled applications.&lt;/p&gt;

&lt;p&gt;I write about frontend architecture, backend systems, AI engineering, system design and the engineering decisions behind building real software.&lt;/p&gt;

&lt;p&gt;More engineering notes and projects are available on &lt;a href="https://ramjwork.in" rel="noopener noreferrer"&gt;my portfolio&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;Originally published on my engineering blog:&lt;br&gt;
&lt;a href="https://blog.ramjwork.in/ai-engineering/ai-customer-support-architecture" rel="noopener noreferrer"&gt;https://blog.ramjwork.in/ai-engineering/ai-customer-support-architecture&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>node</category>
      <category>ai</category>
      <category>architecture</category>
    </item>
  </channel>
</rss>
