{
  "source_file": "0930",
  "source_platform": "Dev.to",
  "author": "Nishikanta Ray",
  "title_listed": "Production-Grade Prompting with the Anthropic API",
  "keywords": "",
  "domain": "大模型算法",
  "content_type": "深度技术解析",
  "is_paid_flag": "否",
  "url": "https://dev.to/nishikantaray/production-grade-prompting-with-the-anthropic-api-ag2",
  "publish_date": "2026-01-17",
  "corpus_stage": "",
  "notes": "",
  "id": "4bf701d20354",
  "collected_at": "2026-10-02T14:05:00",
  "title": "Production-Grade Prompting with the Anthropic API - DEV Community",
  "body_markdown": "Concepts, Parameters, Streaming, Caching, and a Real Enterprise Example\nPrompting in Anthropic (Claude) is not just about writing good instructions.\nIn production systems, prompts behave more like\nspecifications\nthan conversations.\nThis article explains\nhow prompting actually works in Anthropic\n, how to design\nreliable and compliant prompts\n, and how to use\nJavaScript\nto build real systems—ending with a\nproduction-grade sentiment analysis prompt\n.\n1. How Anthropic Prompting Really Works\nAnthropic uses a\nmessage-based API\n.\nEach request contains the\nentire conversation state\n.\nClaude does\nnot\nremember anything between API calls.\nMental model\nYou\n→\nsend\nfull\nconversation\nClaude\n→\ncontinues\nfrom\nthat\ncontext\nEnter fullscreen mode\nExit fullscreen mode\nEvery request must include:\nWho the assistant is\nWhat rules apply\nWhat the user just said\nAny images or documents\n2. Messages and Roles (Core Concept)\nAnthropic uses three roles:\nRole\nPurpose\nsystem\nHard rules, behavior, constraints\nuser\nInput data or tasks\nassistant\nPrevious model replies (conversation priming)\nExample\nmessages\n:\n[\n{\nrole\n:\n\"\nuser\n\"\n,\ncontent\n:\n\"\nHello! Only speak Spanish.\n\"\n},\n{\nrole\n:\n\"\nassistant\n\"\n,\ncontent\n:\n\"\nHola!\n\"\n},\n{\nrole\n:\n\"\nuser\n\"\n,\ncontent\n:\n\"\nHow are you?\n\"\n}\n]\nEnter fullscreen mode\nExit fullscreen mode\nClaude will answer\nin Spanish\nbecause:\nConversation history establishes the rule\nClaude prioritizes consistency\nProduction rule\nPut non-negotiable rules in\nsystem\n, not in conversation history.\n3. System vs User Instructions (Why It Matters)\nInstruction Type\nReliability\nSystem\nStrong, persistent\nUser\nSoft, overrideable\nAssistant\nPriming only\nProduction-safe example\nsystem\n:\n\"\nYou must respond only in Spanish.\n\"\nEnter fullscreen mode\nExit fullscreen mode\nThis survives:\nLong conversations\nTruncated history\nUser attempts to override\n4. Temperature: Controlling Randomness\nTemperature controls how predictable Claude’s word choices are.\nIt does\nnot\ncontrol intelligence.\nTemperature\nBehavior\nUse case\n0.0–0.2\nDeterministic\nJSON, analytics\n0.3–0.5\nControlled\nSummaries\n0.6–0.8\nCreative\nWriting\n0.9+\nUnstable\nRarely useful\nExample\ntemperature\n:\n0.0\nEnter fullscreen mode\nExit fullscreen mode\nUse this when:\nOutput must be parsed\nCompliance matters\nDecisions depend on correctness\n5. stop_sequences: When the Model Must Stop\nstop_sequences\ntells Claude\nwhen to stop generating\n.\nstop_sequences\n:\n[\n\"\n\\n\nUser:\n\"\n]\nEnter fullscreen mode\nExit fullscreen mode\nClaude stops\nimmediately\nwhen it reaches that string.\nCommon uses\nPrevent role leakage\nEnd JSON cleanly\nStop agent loops\nKey rule\nStop sequences are\nguards\n, not fixes for bad prompts.\n6. Streaming Responses (Real-Time Output)\nStreaming lets Claude send text\nincrementally\n.\nwith\nclient\n.\nmessages\n.\nstream\n(...)\nas\nstream\n:\nfor\ntext\nin\nstream\n.\ntext_stream\n:\nprint\n(\ntext\n)\nEnter fullscreen mode\nExit fullscreen mode\nWhy streaming exists\nFaster perceived response\nBetter UX\nLong outputs\nWhen NOT to stream\nJSON APIs\nTool calls\nStrict schema validation\nProduction pattern\nlet\nbuffer\n=\n\"\"\n;\nfor\n(\nconst\nchunk\nof\nstream\n.\ntext_stream\n)\n{\nbuffer\n+=\nchunk\n;\nprocess\n.\nstdout\n.\nwrite\n(\nchunk\n);\n}\n// buffer now contains the full response\nEnter fullscreen mode\nExit fullscreen mode\n7. Multimodal Prompting (Images + Text)\nAnthropic allows\nimage + text together\n.\n{\nrole\n:\n\"\nuser\n\"\n,\ncontent\n:\n[\n{\ntype\n:\n\"\nimage\n\"\n,\nsource\n:\n{...}\n},\n{\ntype\n:\n\"\ntext\n\"\n,\ntext\n:\n\"\nCount the containers.\n\"\n}\n]\n}\nEnter fullscreen mode\nExit fullscreen mode\nRules\ncontent\nmust be an array\nImage first, instruction second\nImages only in\nuser\nrole\n8. Prompt Caching and\ncache_control\nAnthropic optimizes performance by caching repeated prompt prefixes.\nThis is good for:\nSystem prompts\nTemplates\nPolicies\nBut dangerous for:\nUser data\nTranscripts\nImages\nPII\n9.\ncache_control: { type: \"ephemeral\" }\n\"\ncache_control\n\"\n:\n{\n\"\ntype\n\"\n:\n\"\nephemeral\n\"\n}\nEnter fullscreen mode\nExit fullscreen mode\nThis means:\nUse this content once. Do not cache. Do not reuse. Do not persist.\nWhen to use it\nData\nEphemeral\nUser input\n✅\nCall transcripts\n✅\nImages\n✅\nCustomer feedback\n✅\nWhy it matters\nPrevents data reuse\nImproves compliance posture\nReduces audit risk\n10. JavaScript Example: Safe Prompt with Ephemeral Data\nimport\nAnthropic\nfrom\n\"\n@anthropic-ai/sdk\n\"\n;\nconst\nclient\n=\nnew\nAnthropic\n({\napiKey\n:\nprocess\n.\nenv\n.\nANTHROPIC_API_KEY\n});\nconst\nresponse\n=\nawait\nclient\n.\nmessages\n.\ncreate\n({\nmodel\n:\n\"\nclaude-3-5-sonnet-20240620\n\"\n,\nmax_tokens\n:\n200\n,\ntemperature\n:\n0.0\n,\nsystem\n:\n\"\nYou are a deterministic analysis engine.\n\"\n,\nmessages\n:\n[\n{\nrole\n:\n\"\nuser\n\"\n,\ncontent\n:\n[\n{\ntype\n:\n\"\ntext\n\"\n,\ntext\n:\n\"\nThe service was slow and frustrating.\n\"\n,\ncache_control\n:\n{\ntype\n:\n\"\nephemeral\n\"\n}\n}\n]\n}\n]\n});\nconsole\n.\nlog\n(\nresponse\n.\ncontent\n[\n0\n].\ntext\n);\nEnter fullscreen mode\nExit fullscreen mode\n11. What Makes a Prompt Production-Grade\nA production prompt has:\nClear role definition\nExplicit constraints\nDefined output format\nFail-closed behavior\nLow temperature\nNo exposed reasoning\nSafe handling of sensitive data\nGood prompts are specifications, not conversations.\n12. Production-Grade Sentiment Analysis Prompt\nThis prompt follows\nall best practices discussed above\n.\nPrompt\nAnalyze\nthe\nsentiment\nof\nthe\nfollowing\ncustomer\nmessage\n.\nRules\n:\n-\nClassify\nsentiment\nas\nPOSITIVE\n,\nNEGATIVE\n,\nor\nNEUTRAL\n-\nDo\nnot\ninclude\npersonal\ndata\n-\nDo\nnot\ninfer\nintent\nbeyond\nthe\ntext\n-\nLimit\ntext\nfields\nto\n100\ncharacters\nOutput\n:\nReturn\nONLY\nvalid\nJSON\n.\nNo\nexplanations\n.\nSchema\n:\n{\n\"\nsentiment\n\"\n:\n\"\nPOSITIVE | NEGATIVE | NEUTRAL\n\"\n,\n\"\nconfidence\n\"\n:\n0.0\n-\n1.0\n,\n\"\nsummary\n\"\n:\n\"\nstring\n\"\n}\nIf\nsentiment\ncannot\nbe\ndetermined\n,\nreturn\n:\n{\n\"\nsentiment\n\"\n:\n\"\nNEUTRAL\n\"\n,\n\"\nconfidence\n\"\n:\n0.0\n,\n\"\nsummary\n\"\n:\n\"\nInsufficient data\n\"\n}\nEnter fullscreen mode\nExit fullscreen mode\n13. Sentiment Analysis: Full JavaScript Example\nconst\nresponse\n=\nawait\nclient\n.\nmessages\n.\ncreate\n({\nmodel\n:\n\"\nclaude-3-5-sonnet-20240620\n\"\n,\nmax_tokens\n:\n150\n,\ntemperature\n:\n0.0\n,\nsystem\n:\n\"\nYou are a strict sentiment classification engine.\n\"\n,\nmessages\n:\n[\n{\nrole\n:\n\"\nuser\n\"\n,\ncontent\n:\n[\n{\ntype\n:\n\"\ntext\n\"\n,\ntext\n:\n\"\nSupport was slow and unhelpful.\n\"\n,\ncache_control\n:\n{\ntype\n:\n\"\nephemeral\n\"\n}\n}\n]\n}\n]\n});\nconsole\n.\nlog\n(\nresponse\n.\ncontent\n[\n0\n].\ntext\n);\nEnter fullscreen mode\nExit fullscreen mode\n14. Final Takeaway\nAnthropic prompting works best when you treat prompts like contracts: explicit, constrained, deterministic, and safe.",
  "metadata": {
    "title": "Production-Grade Prompting with the Anthropic API - DEV Community",
    "description": "Concepts, Parameters, Streaming, Caching, and a Real Enterprise Example   Prompting in... Tagged with ai, api, javascript, llm.",
    "keywords": "ai, api, javascript, llm, software, coding, development, engineering, inclusive, community",
    "author": null,
    "og:type": "article",
    "og:url": "https://dev.to/nishikantaray/production-grade-prompting-with-the-anthropic-api-ag2",
    "og:title": "Production-Grade Prompting with the Anthropic API",
    "og:description": "Concepts, Parameters, Streaming, Caching, and a Real Enterprise Example   Prompting in...",
    "og:site_name": "DEV Community",
    "og:image": "https://media2.dev.to/dynamic/image/width=1000,height=500,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn7md1l2w8jyznm82y284.png",
    "twitter:site": "@thepracticaldev",
    "twitter:creator": "@",
    "twitter:title": "Production-Grade Prompting with the Anthropic API",
    "twitter:description": "Concepts, Parameters, Streaming, Caching, and a Real Enterprise Example   Prompting in...",
    "twitter:card": "summary_large_image",
    "twitter:widgets:new-embed-design": "on",
    "twitter:image:src": "https://media2.dev.to/dynamic/image/width=1000,height=500,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn7md1l2w8jyznm82y284.png"
  },
  "paywall": {
    "listed_paid": false,
    "detected": false,
    "marker": "",
    "public_only": false,
    "policy": "付费/会员内容只保留页面公开可见部分，不绕过付费墙"
  },
  "crawl": {
    "success": true,
    "status_code": 200,
    "error": ""
  },
  "code_blocks": [
    {
      "file": "attachments/4bf701d20354/code/body-000.txt",
      "chars": 66,
      "language": "text"
    },
    {
      "file": "attachments/4bf701d20354/code/body-001.txt",
      "chars": 160,
      "language": "text"
    },
    {
      "file": "attachments/4bf701d20354/code/body-002.txt",
      "chars": 44,
      "language": "text"
    },
    {
      "file": "attachments/4bf701d20354/code/body-003.txt",
      "chars": 17,
      "language": "text"
    },
    {
      "file": "attachments/4bf701d20354/code/body-004.txt",
      "chars": 28,
      "language": "text"
    },
    {
      "file": "attachments/4bf701d20354/code/body-005.txt",
      "chars": 94,
      "language": "text"
    },
    {
      "file": "attachments/4bf701d20354/code/body-006.txt",
      "chars": 154,
      "language": "text"
    },
    {
      "file": "attachments/4bf701d20354/code/body-007.txt",
      "chars": 127,
      "language": "text"
    },
    {
      "file": "attachments/4bf701d20354/code/body-008.txt",
      "chars": 41,
      "language": "text"
    },
    {
      "file": "attachments/4bf701d20354/code/body-009.txt",
      "chars": 567,
      "language": "text"
    },
    {
      "file": "attachments/4bf701d20354/code/body-010.txt",
      "chars": 513,
      "language": "text"
    },
    {
      "file": "attachments/4bf701d20354/code/body-011.txt",
      "chars": 450,
      "language": "text"
    }
  ],
  "images": [],
  "audio": [],
  "videos": [],
  "links": [],
  "repair_version": 2,
  "repaired_at": "2026-10-14T10:49:54Z",
  "source_url": "https://dev.to/nishikantaray/production-grade-prompting-with-the-anthropic-api-ag2",
  "papers": [],
  "actual_video_files": [],
  "body_chars": 6401,
  "expected_body_images": 0,
  "discovery_status": "ok",
  "delivery_status": "ok",
  "missing_reason": "",
  "raw_html": "attachments/4bf701d20354/pages/source.html",
  "local_html": "attachments/4bf701d20354/pages/article.html",
  "body_base_path": "attachments/4bf701d20354/pages/",
  "body_markdown_path": "attachments/4bf701d20354/pages/article.md"
}