← Blog

Build an Email Reply Generator with the Claude API

2026-11-15 · 5 min read · SubToAPI Team

If you're trying to build a Claude API email reply generator, the core problem isn't getting Claude to write text — it's getting it to write the right text consistently: correct tone, correct length, no hallucinated details, and output that's easy to drop into an email client or helpdesk tool. This guide walks through the actual architecture, the prompt structure that works in production, and a runnable code example.

The short version: you send Claude the incoming email, some context (sender history, your reply style, any knowledge base snippets), and a system prompt that locks down tone and format. Claude returns a draft reply, which you either auto-send, queue for review, or insert into a compose box. Everything below expands on how to do that reliably.

What the generator actually needs to do

An email reply generator has three jobs, and most failed implementations only solve one of them:

  1. Understand the incoming email — intent, questions asked, sentiment, urgency.
  2. Generate an appropriate reply — matching your brand voice, answering every question, not inventing facts.
  3. Produce output in a usable format — plain text or HTML, correct greeting/signature, no meta-commentary like "Here's a draft reply:".

Most "it doesn't work well" complaints trace back to step 3: Claude is perfectly capable of writing a good reply but wraps it in explanations unless you constrain the output explicitly.

Architecture

A production setup looks like this:

  1. Ingestion — pull emails via IMAP, a helpdesk webhook (Zendesk, Front, Gmail API push notifications), or a form submission.
  2. Context assembly — gather the thread history, customer record, and any relevant docs (refund policy, product specs).
  3. Claude call — system prompt with tone/format rules, user message with the email + context.
  4. Post-processing — strip any stray formatting, insert signature, optionally classify confidence to decide auto-send vs. human review.
  5. Delivery — send via your email provider's API or push into the agent's compose UI.

Step 3 is where most of the engineering effort goes, so that's the focus here.

Prompt design for email replies

Put all the invariant rules in the system prompt, and put the variable data (the actual email, customer name, order details) in the user message. This keeps the system prompt reusable across every reply you generate.

You are the customer support assistant for Acme Co.
Write email replies that are:
- Friendly but professional, no exclamation marks
- 3-6 sentences unless the question requires a list
- Always address every question in the original email
- Never invent order numbers, prices, or policies — if information
  is missing, say the team will follow up with specifics
- Sign off as "The Acme Support Team"

Output ONLY the reply body. No subject line, no preamble,
no "Here is a draft" text, no markdown formatting.

Then the user message carries the actual content:

Customer email:
---
Subject: Order hasn't arrived
I ordered the desk lamp 10 days ago and it still hasn't shown up.
Can you tell me what's going on?
---

Customer context:
- Name: Jordan
- Order #48213, placed 10 days ago, status: "shipped, in transit"
- Carrier tracking shows a delay at the regional hub

This separation matters because it lets you swap the customer context per request without rewriting your tone rules, and it makes the prompt easy to A/B test — change the system prompt, re-run the same 50 saved emails, compare output.

A working example

Here's a minimal Node.js implementation using SubToAPI's REST endpoint, which exposes Claude through a standard HTTPS API with an API key instead of requiring you to manage Anthropic SDK auth directly:

const SYSTEM_PROMPT = `You are the customer support assistant for Acme Co.
Write friendly, professional email replies in 3-6 sentences.
Address every question in the original email. Never invent facts.
Output ONLY the reply body, no preamble, no subject line.`;

async function generateReply(emailBody, customerContext) {
  const res = await fetch('https://api.subtoapi.app/v1/messages', {
    method: 'POST',
    headers: {
      'Authorization': `Bearer ${process.env.SUBTOAPI_KEY}`,
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      model: 'claude-sonnet-4-5',
      max_tokens: 400,
      system: SYSTEM_PROMPT,
      messages: [{
        role: 'user',
        content: `Customer email:\n---\n${emailBody}\n---\n\nContext:\n${customerContext}`
      }]
    })
  });

  const data = await res.json();
  return data.content[0].text;
}

This runs the same against the standard /v1/messages shape, so if you're already prototyping against the Anthropic API directly, switching the base URL and key is the only change needed. See /docs/quickstart for setup and /docs/messages for the full request reference.

Handling tone, length, and edge cases

A few things that consistently improve output quality:

Streaming for compose-box UIs

If replies are generated inside an agent dashboard rather than auto-sent, streaming the draft into the compose box as it's generated feels far more responsive than waiting for the full response. SubToAPI supports server-sent event streaming on the same endpoint — see /docs/streaming for the implementation pattern, which is a straightforward swap from a single fetch to reading the event stream.

Why not just call the Anthropic API directly

You can, and for a single prototype that's fine. The reasons teams move to a gateway layer once this goes into production: per-application API keys instead of one shared secret, built-in usage metadata so you can see token spend per inbox or per team, and team seat management if multiple people are building or maintaining reply workflows. SubToAPI wraps Claude access into exactly that — application keys (sub_live_...), streaming, and usage tracking in one dashboard, starting with a free trial at /signup and plans from €9/month at /pricing.

Questions

Can Claude generate replies in HTML for rich-text email clients? Yes — ask for HTML output explicitly in the system prompt (e.g., "output valid HTML with <p> tags, no markdown") and parse accordingly. Plain text is more reliable if your sending pipeline already handles formatting.

How do I stop Claude from hallucinating order details or policies? Only include facts you can verify in the context you send, and instruct Claude explicitly to say information is missing rather than guess. Never let it infer policy details it wasn't given.

Should replies send automatically or go through human review? Start with review-required for all replies, track how often humans edit vs. approve unchanged, then automate only the categories (e.g., shipping status, simple FAQs) where edits are rare.

Turn your Claude access into an HTTPS API

SubToAPI gives you application API keys, streaming, tool use and usage insights on top of your existing Claude access — set up in minutes.

Start free  Read the quickstart →