AI assistants don’t have to block your users while waiting for a response. By decoupling the request with SQS you get a resilient, scalable pipeline that can handle bursts of code‑suggestion traffic. This guide shows you how to stitch together SQS, Claude via Bedrock, and Node.js 22 to power an always‑on AI pair‑programmer.

Why Async AI Matters

When a developer clicks “Suggest code”, the front‑end usually sends a request straight to an LLM (large language model) and waits. While the model is thinking, the HTTP connection stays open. If the model takes a few seconds, the user sees a spinner; if it takes longer, the request may time out and the user must try again.

In plain English: Synchronous calls make the UI sit still while the brain does the heavy lifting.

An asynchronous design moves the heavy lifting into a background worker. The front‑end puts a short message into an SQS (Simple Queue Service) queue and immediately returns a “job queued” response. A separate Node.js process pulls messages from the queue, talks to Claude, stores the result, and finally notifies the front‑end (for example via WebSocket or a callback URL).

Why this helps

  • Responsiveness – users never wait for the AI; they get instant feedback that their request is being processed.
  • Resilience – if Claude experiences a hiccup, the message stays in the queue and can be retried.
  • Scalability – many workers can run in parallel, and SQS smooths traffic spikes (think of a line at a coffee shop that never gets too long).

Analogy

Think of the queue as a mailbox. Instead of handing a letter directly to a busy clerk (synchronous), you drop it in the mailbox. The clerk (worker) picks it up when they have time, writes a reply, and puts it in the outgoing box. The sender never stands at the counter waiting.

Create the SQS Queue and IAM Role

Before any code runs you need an SQS queue that can hold the “code‑suggestion” jobs and an IAM role that gives your Node.js worker permission to read from the queue, call Bedrock, and write to S3.

Step‑by‑step

  1. Create a FIFO queue – FIFO (first‑in‑first‑out) guarantees that suggestions are processed in the order they were received.
  2. Set a visibility timeout – this is the period after a worker receives a message during which SQS hides it from other workers. It must be longer than the longest expected Claude call plus some buffer.
  3. Attach a policy – the IAM role needs sqs:ReceiveMessage, sqs:DeleteMessage, sqs:ChangeMessageVisibility, s3:PutObject, and bedrock:InvokeModel.

Tip: A common mistake is to leave the visibility timeout at the default 30 seconds while the Claude call can take 90 seconds. The message then reappears and two workers try to process it, causing duplicate suggestions.

CloudFormation snippet (you can also use the console)

Resources: CodeSuggestionQueue: Type: AWS::SQS::Queue Properties: QueueName: code-suggestions.fifo FifoQueue: true VisibilityTimeout: 120 # seconds; longer than Claude latency  ReceiveMessageWaitTimeSeconds: 20 # long polling to avoid empty receives  ContentBasedDeduplication: true WorkerRole: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Version: "2012-10-17" Statement: - Effect: Allow Principal: Service: lambda.amazonaws.com Action: sts:AssumeRole Policies: - PolicyName: CodeAssistantPolicy PolicyDocument: Version: "2012-10-17" Statement: - Effect: Allow Action: - sqs:ReceiveMessage - sqs:DeleteMessage - sqs:ChangeMessageVisibility Resource: !GetAtt CodeSuggestionQueue.Arn - Effect: Allow Action: - s3:PutObject Resource: arn:aws:s3:::code-assistant-results/* - Effect: Allow Action: - bedrock:InvokeModel Resource: "*" 

Key takeaway: The visibility timeout must comfortably exceed the longest Claude request; otherwise you’ll see duplicate processing.

Write the Node.js 22 Worker

The worker is a small script that runs continuously (or as a Lambda) and performs four actions:

  1. Pull a message from SQS (receiveMessage).
  2. Call Claude through Bedrock (fetch + InvokeModel).
  3. Store the generated snippet in S3.
  4. Delete the message from the queue, or extend visibility if needed.

Below is a complete, runnable example using the AWS SDK v3 (@aws-sdk/client-sqs and @aws-sdk/client-s3). The script is deliberately verbose so a newcomer can follow each line.

// worker.ts import { SQSClient, ReceiveMessageCommand, DeleteMessageCommand, ChangeMessageVisibilityCommand } from "@aws-sdk/client-sqs"; import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3"; import { config } from "dotenv"; import { v4 as uuidv4 } from "uuid"; // Load environment variables from .env file config(); // ---------- Configuration ---------- const QUEUE_URL = process.env.QUEUE_URL!; // SQS FIFO queue URL const REGION = process.env.AWS_REGION || "us-east-1"; const BUCKET_NAME = process.env.BUCKET_NAME!; // S3 bucket for results const CLAUDE_ENDPOINT = "https://bedrock-runtime.amazonaws.com/model/anthropic.claude-v2/invoke"; const MAX_POLL_TIME = 20; // seconds, matches long polling setting // ---------- AWS SDK clients ---------- const sqs = new SQSClient({ region: REGION }); const s3 = new S3Client({ region: REGION }); // ---------- Helper: invoke Claude ---------- async function invokeClaude(prompt: string): Promise<string> { // Bedrock expects a JSON payload with a "prompt" field for Claude‑2 const body = JSON.stringify({ prompt, max_tokens_to_sample: 512, temperature: 0.2, top_p: 0.9, stop: ["\n\n"] }); const response = await fetch(CLAUDE_ENDPOINT, { method: "POST", headers: { "Content-Type": "application/json", // The SDK automatically signs the request using the default credentials }, body, }); if (!response.ok) { const err = await response.text(); throw new Error(`Claude call failed: ${response.status} ${err}`); } const data = await response.json(); // Claude returns the generated text under "completion" return data.completion as string; } // ---------- Main loop ---------- async function pollQueue() { while (true) { // 1️⃣ Receive a single message (max 1 for simplicity) const receiveCmd = new ReceiveMessageCommand({ QueueUrl: QUEUE_URL, MaxNumberOfMessages: 1, WaitTimeSeconds: MAX_POLL_TIME, // long polling VisibilityTimeout: 120, // must match queue config }); const { Messages } = await sqs.send(receiveCmd); if (!Messages || Messages.length === 0) { // No work right now; loop again continue; } const message = Messages[0]; const receiptHandle = message.ReceiptHandle!; const body = message.Body!; // JSON string we placed when queuing // Parse the user's request const payload = JSON.parse(body); const userPrompt: string = payload.prompt; const requestId: string = payload.requestId || uuidv4(); try { // 2️⃣ Call Claude (may take several seconds) const suggestion = await invokeClaude(userPrompt); // 3️⃣ Write result to S3 const key = `suggestions/${requestId}.txt`; const putCmd = new PutObjectCommand({ Bucket: BUCKET_NAME, Key: key, Body: suggestion, ContentType: "text/plain", }); await s3.send(putCmd); console.log(`✅ Stored suggestion ${key}`); // 4️⃣ Delete the message so it never reappears const deleteCmd = new DeleteMessageCommand({ QueueUrl: QUEUE_URL, ReceiptHandle: receiptHandle, }); await sqs.send(deleteCmd); console.log(`🗑️ Deleted message ${requestId}`); // (Optional) Notify front‑end here – e.g., publish to SNS or WebSocket } catch (err: any) { console.error(`⚠️ Error processing ${requestId}: ${err.message}`); // If the error is a timeout from Claude, we may want to give ourselves more time if (err.message.includes("timeout")) { // Extend visibility so other workers don't pick it up while we retry const extendCmd = new ChangeMessageVisibilityCommand({ QueueUrl: QUEUE_URL, ReceiptHandle: receiptHandle, VisibilityTimeout: 180, // add extra buffer }); await sqs.send(extendCmd); console.log(`⏳ Extended visibility for ${requestId}`); } // Decide whether to re‑queue or dead‑letter; for demo we just let it become visible again } } } // Start the worker pollQueue().catch((e) => { console.error("Fatal error in worker loop:", e); process.exit(1); }); 

Explanation of key lines

  • receiveMessage pulls a single job; the WaitTimeSeconds matches the queue’s long‑poll setting so we don’t pay for empty receives.
  • VisibilityTimeout hides the message while the worker talks to Claude. If the call takes longer, we later call ChangeMessageVisibility to extend the hide period.
  • fetch is the built‑in HTTP client in Node.js 22; it automatically uses the AWS SDK’s credential provider chain when you sign the request (Bedrock expects signed calls).
  • PutObjectCommand writes the generated snippet to S3. The object key includes a UUID so each suggestion is uniquely stored.
  • After successful storage we call DeleteMessage – this removes the message permanently.

Helpful tip: Always catch errors around the Claude call. If the call fails after the visibility timeout has expired, the message becomes visible again and another worker might start a duplicate run.

Call Claude via Bedrock Runtime

Claude is Anthropic’s LLM available through Amazon Bedrock. Bedrock offers a runtime endpoint that accepts an HTTP POST with a JSON payload and returns the model’s completion. You do not need a separate SDK for Claude; the generic fetch call works as long as the request is signed.

Request shape

Field Meaning
prompt The user’s code request (e.g., “Write a TypeScript function that debounces”).
max_tokens_to_sample Upper bound on generated tokens.
temperature Controls randomness; lower = more deterministic.
top_p Nucleus sampling parameter.
stop Tokens that tell Claude to stop early.

Common gotchas

  • Region mismatch – Bedrock is only available in certain AWS regions. If your SQS queue lives in us-east-1 but Bedrock is not enabled there, the request fails with a 403.
  • Payload size – The request body (including the prompt) must stay under 256 KB, the same limit as an SQS message. If you need larger context, store the prompt in S3 and pass the S3 URL instead.
  • Rate limits – Claude‑2 permits roughly 10 TPS (transactions per second) per account. Exceeding that results in a 429 Too Many Requests. Queue throttling helps smooth spikes.

Minimal Claude call (isolated)

async function testClaude() { const prompt = "Explain the difference between let and const in JavaScript."; const payload = { prompt, max_tokens_to_sample: 256, temperature: 0.1, top_p: 0.9, stop: ["\n\n"] }; const resp = await fetch("https://bedrock-runtime.amazonaws.com/model/anthropic.claude-v2/invoke", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(payload), }); if (!resp.ok) throw new Error(`Claude error ${resp.status}`); const data = await resp.json(); console.log("Claude says:", data.completion); } 

Running this function inside the worker shows that the same code works in the larger pipeline.

In plain English: The request is just a JSON “question” to Claude, and the answer comes back as a plain string.

Persist Results to S3

Storing suggestions in S3 (Simple Storage Service) gives you durable, cheap, and versionable files that can be served to the front‑end or downloaded later. Each suggestion becomes its own object, identified by a UUID.

Why S3, not a database?

  • Object size flexibility – You can store anything from a few lines to megabytes of generated code.
  • Static hosting – If you enable S3 website hosting, the suggestions can be served directly via HTTPS without a server.
  • Cost – S3 charges per GB stored and per request, which is cheaper than most relational databases for large blobs.

Code snippet (already in the worker) with extra comments

// Prepare a unique key for the S3 object const key = `suggestions/${requestId}.txt`; // folder‑like prefix + filename // PutObjectCommand uploads the text; ContentType tells browsers it's plain text const putCmd = new PutObjectCommand({ Bucket: BUCKET_NAME, Key: key, Body: suggestion, // the code Claude generated ContentType: "text/plain", // Optional: set metadata so you can later filter by language, e.g.: // Metadata: { language: "typescript" } }); await s3.send(putCmd); console.log(`✅ Stored suggestion ${key}`); 

S3 gotchas to remember

Gotcha Explanation
Express One Zone has a different API surface; trying to use PutObject the same way can produce InvalidOperation errors. Stick with standard S3 unless you deliberately need the lower‑cost, single‑AZ storage.
Presigned URLs expire. If your front‑end fetches a suggestion via a presigned URL that is older than its TTL, you’ll see a 403 error. Keep the TTL short (e.g., 5 minutes) and handle 403 by requesting a fresh URL from the backend.
Object Lock in Compliance mode makes objects immutable forever, even for root. Use it only for audit‑required data; otherwise, avoid enabling it on the results bucket.

Tip: Adding a small JSON side‑car (e.g., { "requestId": "...", "timestamp": "...", "language": "js" }) as object metadata can help you later search or filter suggestions without a separate database.

The Takeaway

Key points to remember

  • Decoupling the AI call with SQS makes the user experience instantly responsive and protects your system from traffic spikes.
  • The visibility timeout must be longer than the longest Claude request; otherwise the same message can be processed twice. Extend it with ChangeMessageVisibility if a call runs long.
  • Use the @aws-sdk/client-sqs and @aws-sdk/client-s3 packages; they handle signing, pagination, and error handling for you.
  • Claude is accessed via a simple HTTPS POST to the Bedrock runtime; no special Claude SDK is required.
  • Store each suggestion in S3 with a unique key; this gives you cheap, durable storage and makes it easy to serve results later.
  • Watch out for common pitfalls: mismatched regions, SQS batch‑failure semantics, FIFO TPS limits, and S3 presigned‑URL expiration.

Summary bullets

  • Async pipeline = SQS → Worker → Claude → S3 → (optional) notification.
  • Visibility timeout > Claude latency; use ChangeMessageVisibility to avoid duplicates.
  • FIFO queue guarantees order but caps at 300 TPS; monitor spikes.
  • Long polling (WaitTimeSeconds) saves money by reducing empty receives.
  • S3 object size limit is 5 TB; keep each suggestion under 256 KB or use a pointer if larger.
  • Error handling: catch Claude timeouts, extend visibility, and decide whether to retry or dead‑letter.

You now have a complete, production‑ready pattern for an asynchronous AI code assistant that stays responsive even when Claude takes its time. Happy coding!

Transparency notice

This article was written with the help of an AI system — Groq (GPT OSS 120B).

Published: 2026-11-18 · Primary focus: SQS

All code blocks are intended to be correct and runnable, but please verify them
against the official docs for the tools mentioned before using in production.

Find an error? Drop a comment — corrections are always welcome.