Created
February 28, 2026 07:44
-
-
Save kadnan/6d645a469c3b2389596774dace5aa084 to your computer and use it in GitHub Desktop.
Claide Email Responder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "id": "90abc817-c9c3-4533-b29f-29750d1d214c", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "import os\n", | |
| "import json\n", | |
| "from anthropic import Anthropic\n", | |
| "from dotenv import load_dotenv" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 5, | |
| "id": "191837d3-873f-476b-b694-510f2e0a5bac", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "load_dotenv()\n", | |
| "API_KEY = os.environ.get(\"API_KEY\")\n", | |
| "\n", | |
| "SYSTEM_PROMPT = \"\"\"You are “AI Email Responder”, an assistant that drafts professional email replies using the provided email thread context.\n", | |
| "\n", | |
| "GOAL\n", | |
| "Generate a high-quality reply email that the user can copy-paste and send, based strictly on the inbox context and the user’s intent.\n", | |
| "\n", | |
| "INPUT YOU WILL RECEIVE (JSON OR FIELDS)\n", | |
| "- user_language: \"urdu\" | \"hindi\" | \"english\"\n", | |
| "- tone: one of [\"friendly\", \"professional\", \"firm\", \"apologetic\", \"sales\", \"short\", \"detailed\"] (default: \"professional\")\n", | |
| "- user_intent: what the user wants to achieve in the reply (e.g., \"accept meeting\", \"ask for clarification\", \"decline politely\", \"follow up\", \"negotiate price\")\n", | |
| "- sender_name: (optional) name of the person you are replying to\n", | |
| "- recipient_role: (optional) e.g., client, recruiter, vendor, colleague\n", | |
| "- my_name: (optional) name to sign with\n", | |
| "- email_thread: full thread text OR the latest email + important previous messages\n", | |
| "- constraints: (optional) bullet list like “keep under 120 words”, “don’t promise delivery dates”, “ask 2 questions max”\n", | |
| "\n", | |
| "CORE RULES\n", | |
| "1) Use ONLY the information available in email_thread + user_intent + provided fields. Do not invent facts, prices, timelines, attachments, names, links, or policies.\n", | |
| "2) If key info is missing (dates, amounts, names, deliverables, meeting time, account details), DO NOT guess. Instead, ask concise follow-up questions OR write a reply with placeholders like [date], [amount], [link].\n", | |
| "3) Keep the reply actionable and clear. Prefer short paragraphs, clear ask, and a polite closing.\n", | |
| "4) Match the requested language:\n", | |
| " - \"urdu\": write Roman Urdu by default (unless user explicitly wants Urdu script).\n", | |
| " - \"hindi\": write in Hindi (Devanagari) by default.\n", | |
| " - \"english\": write in English.\n", | |
| "5) Avoid overly robotic phrasing. Avoid emojis unless the thread already uses them.\n", | |
| "6) Do not include any internal analysis, reasoning, or commentary. Output ONLY the drafted email reply content.\n", | |
| "7) Do not include a subject line unless the user explicitly asks for it.\n", | |
| "8) Maintain privacy and security:\n", | |
| " - Never request passwords, OTPs, or sensitive personal data.\n", | |
| " - If the email appears to be phishing or suspicious, warn briefly and suggest verification steps, and draft a cautious reply.\n", | |
| "9) If the email is angry/hostile, keep tone calm and de-escalating unless user explicitly requests “firm”.\n", | |
| "\n", | |
| "OUTPUT FORMAT\n", | |
| "Return the final reply email as plain text with:\n", | |
| "- Greeting line\n", | |
| "- Body\n", | |
| "- Closing line\n", | |
| "- Signature (use my_name if provided, otherwise omit name)\n", | |
| "\"\"\"" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 6, | |
| "id": "a51bebb1-26ce-48c1-bae4-252ba8a86bb7", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "def generate_email_reply(payload: dict):\n", | |
| " api_key = API_KEY\n", | |
| " if not api_key:\n", | |
| " raise RuntimeError(\"Missing ANTHROPIC_API_KEY\")\n", | |
| "\n", | |
| " client = Anthropic(api_key=api_key)\n", | |
| "\n", | |
| " response = client.messages.create(\n", | |
| " model=\"claude-sonnet-4-6\",\n", | |
| " max_tokens=500,\n", | |
| " system=SYSTEM_PROMPT,\n", | |
| " messages=[\n", | |
| " {\n", | |
| " \"role\": \"user\",\n", | |
| " \"content\": json.dumps(payload, ensure_ascii=False)\n", | |
| " }\n", | |
| " ],\n", | |
| " )\n", | |
| "\n", | |
| " # Extract reply text\n", | |
| " reply_text = \"\"\n", | |
| " for block in response.content:\n", | |
| " if block.type == \"text\":\n", | |
| " reply_text += block.text\n", | |
| "\n", | |
| " # Token Usage\n", | |
| " input_tokens = response.usage.input_tokens\n", | |
| " output_tokens = response.usage.output_tokens\n", | |
| " total_tokens = input_tokens + output_tokens\n", | |
| "\n", | |
| " return reply_text.strip(), input_tokens, output_tokens, total_tokens" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 7, | |
| "id": "1c4f2423-b404-4080-9a5d-42d7fe77834f", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "\n", | |
| "--- Generated Reply ---\n", | |
| "\n", | |
| "Hi John,\n", | |
| "\n", | |
| "Thank you for reaching out. I would be happy to connect for a call. Unfortunately, 3 PM tomorrow does not work for me. Could we reschedule to [alternative time] on [date] instead? Please let me know if that works for you, and I will confirm right away.\n", | |
| "\n", | |
| "Looking forward to speaking with you.\n", | |
| "\n", | |
| "Best regards,\n", | |
| "Adnan\n", | |
| "\n", | |
| "--- Token Usage ---\n", | |
| "Input Tokens : 736\n", | |
| "Output Tokens : 85\n", | |
| "Total Tokens : 821\n", | |
| "Estimated Cost: $0.003483\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "INPUT_PRICE_PER_MILLION = 3\n", | |
| "OUTPUT_PRICE_PER_MILLION = 15\n", | |
| "\n", | |
| "payload = {\n", | |
| " \"user_language\": \"english\",\n", | |
| " \"tone\": \"professional\",\n", | |
| " \"user_intent\": \"accept meeting and propose a different time\",\n", | |
| " \"sender_name\": \"John\",\n", | |
| " \"my_name\": \"Adnan\",\n", | |
| " \"email_thread\": \"Hi Adnan,\\nCan we schedule a call tomorrow at 3 PM?\\nThanks,\\nJohn\"\n", | |
| " }\n", | |
| "\n", | |
| "reply, in_tok, out_tok, total_tok = generate_email_reply(payload)\n", | |
| "input_cost = (in_tok / 1_000_000) * INPUT_PRICE_PER_MILLION\n", | |
| "output_cost = (out_tok / 1_000_000) * OUTPUT_PRICE_PER_MILLION\n", | |
| "\n", | |
| "total_cost = input_cost + output_cost\n", | |
| "\n", | |
| "\n", | |
| "print(\"\\n--- Generated Reply ---\\n\")\n", | |
| "print(reply)\n", | |
| "\n", | |
| "print(\"\\n--- Token Usage ---\")\n", | |
| "print(f\"Input Tokens : {in_tok}\")\n", | |
| "print(f\"Output Tokens : {out_tok}\")\n", | |
| "print(f\"Total Tokens : {total_tok}\")\n", | |
| "\n", | |
| "print(f\"Estimated Cost: ${total_cost:.6f}\")" | |
| ] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python 3 (ipykernel)", | |
| "language": "python", | |
| "name": "python3" | |
| }, | |
| "language_info": { | |
| "codemirror_mode": { | |
| "name": "ipython", | |
| "version": 3 | |
| }, | |
| "file_extension": ".py", | |
| "mimetype": "text/x-python", | |
| "name": "python", | |
| "nbconvert_exporter": "python", | |
| "pygments_lexer": "ipython3", | |
| "version": "3.9.4" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 5 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment