DEV Community

Cover image for How We Built AI Comment Replies for Instagram (and What We Won't Automate)
sakthivel
sakthivel

Posted on

How We Built AI Comment Replies for Instagram (and What We Won't Automate)

We built an AI system that reads Instagram comments and replies to them automatically. It handles the "price?" messages, the link requests, the "how do I sign up?" questions — all without a human touching the keyboard. It processes thousands of comments per hour and sends them through Meta's official Graph API.

But the harder engineering problem wasn't building the AI. It was deciding what the AI should never do. The line between "automate this" and "escalate to a human" is where most tools fail — and where we spent the most time.

This post walks through the technical decisions behind building an AI comment reply system, the architecture that makes it work at scale, and the guardrails that keep it from turning your brand into a spam bot.

The Problem We Were Solving

Every Instagram creator and business faces the same bottleneck in 2026. A post gets traction, comments roll in, and the first hour — the window that matters most for engagement and conversion — is spent typing the same responses to the same questions.

The data backs this up. According to CreatorFlow's 2026 benchmarks, automated comment-to-DM responses see 85 to 95 percent open rates and 15 to 22 percent click-through rates. Replies sent within 60 seconds drive 21x higher conversion rates compared to manual replies sent hours later. Speed isn't a nice-to-have. It's the difference between a comment that converts and a comment that dies.

Building an AI that replies to comments isn't just "send a response." Comments are a mixed stream of genuine questions, purchase intent, spam, trolling, off-topic noise, and the occasional crisis signal. An AI that treats all comments the same generates as much damage as value.

We built a system with two layers: an intent classifier that decides whether a comment should be automated, and a response generator that decides what to say.

The Architecture: Three Layers, One Pipeline

The system runs as a webhook-driven pipeline with three processing layers. Here's how it flows from Instagram comment to automated response.

Layer 1: Webhook Ingestion

Instagram fires a webhook event every time a new comment lands on a post or Reel you've connected. The webhook payload includes the comment text, the commenter's user ID, the media ID, and a timestamp.

We run a lightweight server (Node.js with Express) that receives webhooks and pushes comment data into a processing queue. The queue is Redis-backed — fast enough for burst traffic when a post goes viral, durable enough that we don't lose comments during delays.

app.post('/webhook', (req, res) => {
  const { object, entry } = req.body;
  if (object !== 'instagram') return res.sendStatus(404);

  entry.forEach(event => {
    event.changes.forEach(change => {
      if (change.field === 'comments') {
        const { text, id: commentId, username } = change.value;
        redis.lpush('comment_queue', JSON.stringify({
          commentId, text, username,
          mediaId: event.id,
          timestamp: Date.now()
        }));
      }
    });
  });

  res.sendStatus(200);
});
Enter fullscreen mode Exit fullscreen mode

The important detail here is that we return 200 immediately. Instagram expects a fast response. If your webhook takes too long, Meta marks it as failed and retries — duplicate processing. We push to the queue synchronously and let workers handle everything else.

Layer 2: Intent Classification

This is the decision layer. For every comment, the classifier determines one of four outcomes: auto-reply publicly, send a DM, escalate to a human, or ignore.

The classifier runs two passes. The first is a fast keyword match for obvious cases. Comments containing "price," "link," "how much," "sign up," or "DM me" get routed to auto-reply with high confidence. This pass runs in under 50 milliseconds.

The second pass is an LLM-based intent classification for comments that don't match keywords. We send the comment text, the post context, and a classification prompt to the model. The categories are: purchase intent, question, support request, positive sentiment, negative sentiment, spam, off-topic, and crisis signal.

CLASSIFICATION_PROMPT = """
Classify this Instagram comment into exactly one category.

Comment: "{comment}"
Post context: {post_caption}

Categories:
- purchase_intent: wants to buy, asks about price, availability
- question: asks how something works, seeks information
- support_request: reports a problem, needs help
- positive_sentiment: praise, compliment, agreement
- negative_sentiment: complaint, dissatisfaction, criticism
- spam: promotional, scam, bot-like
- off_topic: unrelated to the post content
- crisis_signal: mentions harm, legal threats, PR risk

Return ONLY the category name.
"""
Enter fullscreen mode Exit fullscreen mode

The classification determines the routing. Purchase intent and questions go to the response generator. Positive sentiment gets a like or brief reply. Support requests, negative sentiment, and crisis signals get escalated to a human. Spam gets hidden. Off-topic gets ignored.

Layer 3: Response Generation

For comments that pass classification, we generate a response. This is where the AI lives.

We give the model three inputs: the original comment, the post caption for context, and a brand voice guide. The model generates a short public reply — typically one to two sentences — that acknowledges the commenter and either answers their question or directs them to a DM.

RESPONSE_PROMPT = """
You are replying to an Instagram comment on behalf of a brand.

Brand voice: {brand_voice}
Post caption: {post_caption}
Comment by @{username}: "{comment}"

Write a short, friendly public reply (1-2 sentences max).
If the commenter is asking about pricing or products, include "Check your DMs" in the reply.
Never make up information about the brand's products or policies.
Never promise discounts or special offers.
"""
Enter fullscreen mode Exit fullscreen mode

The response goes back to Instagram through the Graph API's comment reply endpoint. We also track the commenter's user ID for the 24-hour messaging window — if we follow up via DM, we have the window open.

What We Won't Automate (and Why)

This is the section that took the longest to get right. The guardrails we built weren't just technical constraints — they were product decisions about where AI should stop and humans should start.

Crisis Signals

Any comment classified as a crisis signal — mentions of harm, legal threats, or reputational risk — goes straight to a human queue with a Slack notification. We don't generate a response. We don't hide the comment. We flag it and wait. AI is wrong about crisis signals more often than any other category.

Negative Sentiment with Specific Complaints

General complaints can get an automated empathetic response and a DM offer. But specific complaints — "I was charged twice," "my order never arrived" — get escalated. The AI doesn't have access to order systems or billing data.

Complex Questions

If someone asks a question requiring nuanced, multi-part answers — "What's the difference between your Pro and Enterprise plans?" — we route to a human. The AI handles simple questions well. The moment a question requires comparison or policy interpretation, the risk of a wrong answer exceeds the benefit of speed.

Sarcasm and Irony

Instagram comments are full of sarcasm. "Great product, definitely not a scam" is a negative comment that an AI might classify as positive. We built sarcasm detection into the classifier, but it's the least reliable category. When confidence drops below 70 percent, we escalate rather than guess.

Comments from the Account Owner

We filter out comments from the account owner and team members. You don't want your AI replying to your own team's comments. The fix was a simple user ID exclusion list that runs before classification.

Rate Limit Compliance at Scale

Instagram's Graph API enforces a 750-call-per-hour limit for private replies to comments. General API calls are capped at 200 per hour per account in 2026. When a post goes viral and triggers thousands of comments, you can't reply to all of them within the hour.

Our solution is a token bucket queue. Each account gets 750 tokens per hour for comment replies. Each automated response consumes one token. When the bucket empties, responses queue for the next window. We monitor the X-Business-Use-Case-Usage header on every API response and throttle at 80 percent utilization.

For accounts connected to InstantDM (instantdm.com), this compliance layer is handled natively. InstantDM is an official Meta Business Partner that monitors Meta's rate limit headers across all connected accounts in real time. Their Dedicated Safety Queues and Viral Mode automatically pace outbound responses during traffic spikes, so every message still goes out — just spread across the compliance window. Their backend detects rate limit error codes (4, 17, 32, 613, 80001, 80002, 80006) and pauses the queue before Meta flags the account.

Building rate limit compliance from scratch is doable but tedious. InstantDM's approach — native webhooks, automatic pacing, and the 24-hour messaging window handled out of the box — is why most teams use their platform instead of building their own.

The Voice Guide Problem

The hardest part of the entire system wasn't classification or rate limiting. It was getting the AI to sound like the brand.

Every brand has a voice. Some are casual and use emojis. Some are professional and concise. If the AI generates generic responses — "Thanks for your interest! Check your DMs!" — the automation becomes obvious and engagement drops.

We solved this by building a brand voice guide that feeds into every response generation prompt. The guide includes tone descriptions, example responses, banned phrases, and emoji rules. For brands using InstantDM's Claude integration, the AI learns the brand's voice from existing DM conversations.

The practical result: multiple public reply variations per comment, each sounding natural and on-brand, without the repetitive patterns that scream "this is a bot."

What We Learned

The biggest lesson in 2026 was that the guardrails matter more than the AI. A system that replies to every comment quickly but sometimes says the wrong thing to the wrong person creates more problems than it solves. A system that replies to 70 percent of comments correctly and escalates the rest to humans is genuinely useful.

Speed matters. Context matters. But knowing when to shut up matters most.

Top comments (0)