[{"data":1,"prerenderedAt":570},["ShallowReactive",2],{"page-\u002Fprompt-engineering":3},{"id":4,"title":5,"body":6,"description":562,"extension":563,"meta":564,"navigation":565,"path":566,"seo":567,"stem":568,"__hash__":569},"content\u002Fprompt-engineering\u002Findex.md","Prompt Engineering — Engineering Reference",{"type":7,"value":8,"toc":542},"minimark",[9,14,23,28,56,60,77,81,86,166,170,254,258,342,346,402,406,462,466,470,477,481,488,492,495,499],[10,11,13],"h1",{"id":12},"prompt-engineering-engineering-reference","🧭 Prompt Engineering — Engineering Reference",[15,16,17,18,22],"p",{},"A code-first, production-grade prompt-engineering curriculum for mid-to-senior developers moving toward staff\u002Fprincipal roles. Each chapter is structured around ",[19,20,21],"strong",{},"annotated code blocks"," — complex implementations, anti-patterns with fixes, performance tricks, and edge-case failure modes — rather than prose-heavy tutorials. Every technique is shown as it's used in real production systems, with dense inline comments explaining the underlying mechanism.",[24,25,27],"h2",{"id":26},"how-to-use-this-reference","How to Use This Reference",[29,30,31,38,44,50],"ol",{},[32,33,34,37],"li",{},[19,35,36],{},"Read sequentially"," (01 → 20) for a structured path from LLM mechanics to production evaluation.",[32,39,40,43],{},[19,41,42],{},"Jump to a chapter"," as a reference when you hit a specific prompting problem in the wild — each is self-contained.",[32,45,46,49],{},[19,47,48],{},"Run the exercises"," in chapter 20 after every few chapters, not just at the end.",[32,51,52,55],{},[19,53,54],{},"Treat every code example as a starting point"," — adapt the patterns to your domain, test against your own eval set (Chapter 19), and verify behavior on YOUR target model (Chapter 16).",[24,57,59],{"id":58},"prerequisites","Prerequisites",[61,62,63,66,74],"ul",{},[32,64,65],{},"Familiarity with at least one LLM API (Anthropic, OpenAI, or equivalent) — you've made API calls and understand the request\u002Fresponse shape.",[32,67,68,69,73],{},"Working knowledge of Python — code examples use Python with ",[70,71,72],"code",{},"anthropic"," SDK patterns, but the concepts transfer to any language.",[32,75,76],{},"Understanding of basic software engineering practices (versioning, testing, CI) — this reference treats prompts as production code, not creative writing.",[24,78,80],{"id":79},"curriculum","Curriculum",[82,83,85],"h3",{"id":84},"part-i-foundations","Part I — Foundations",[87,88,89,105],"table",{},[90,91,92],"thead",{},[93,94,95,99,102],"tr",{},[96,97,98],"th",{},"#",[96,100,101],{},"Topic",[96,103,104],{},"Why It Matters",[106,107,108,124,138,152],"tbody",{},[93,109,110,114,121],{},[111,112,113],"td",{},"01",[111,115,116],{},[117,118,120],"a",{"href":119},"\u002Fprompt-engineering\u002F01-introduction-and-how-llms-work","Introduction & How LLMs Work",[111,122,123],{},"Next-token prediction, tokenization, context budgets — the mechanistic model every technique builds on.",[93,125,126,129,135],{},[111,127,128],{},"02",[111,130,131],{},[117,132,134],{"href":133},"\u002Fprompt-engineering\u002F02-anatomy-of-a-prompt","Anatomy of a Prompt",[111,136,137],{},"Role hierarchy, instruction\u002Fcontext\u002Fdata separation, delimiter strategy — shown as real API request bodies.",[93,139,140,143,149],{},[111,141,142],{},"03",[111,144,145],{},[117,146,148],{"href":147},"\u002Fprompt-engineering\u002F03-zero-shot-and-few-shot-prompting","Zero-Shot & Few-Shot Prompting",[111,150,151],{},"Example selection, ordering effects, diminishing returns, and the accidental-pattern trap.",[93,153,154,157,163],{},[111,155,156],{},"04",[111,158,159],{},[117,160,162],{"href":161},"\u002Fprompt-engineering\u002F04-clarity-and-specificity","Clarity & Specificity",[111,164,165],{},"Eliminating ambiguity through checkable constraints, positive framing, and anti-rebound instruction design.",[82,167,169],{"id":168},"part-ii-core-techniques","Part II — Core Techniques",[87,171,172,182],{},[90,173,174],{},[93,175,176,178,180],{},[96,177,98],{},[96,179,101],{},[96,181,104],{},[106,183,184,198,212,226,240],{},[93,185,186,189,195],{},[111,187,188],{},"05",[111,190,191],{},[117,192,194],{"href":193},"\u002Fprompt-engineering\u002F05-chain-of-thought-prompting","Chain-of-Thought Prompting",[111,196,197],{},"CoT as self-generated context, extended thinking, and the fluent-but-wrong failure mode.",[93,199,200,203,209],{},[111,201,202],{},"06",[111,204,205],{},[117,206,208],{"href":207},"\u002Fprompt-engineering\u002F06-role-and-persona-prompting","Role & Persona Prompting",[111,210,211],{},"Conditioning signals, sycophancy mitigation, anti-caving instructions, and persona drift.",[93,213,214,217,223],{},[111,215,216],{},"07",[111,218,219],{},[117,220,222],{"href":221},"\u002Fprompt-engineering\u002F07-output-formatting-and-structured-data","Output Formatting & Structured Data",[111,224,225],{},"Prompted vs. API-enforced schemas, function calling as structured output, and parsing failure modes.",[93,227,228,231,237],{},[111,229,230],{},"08",[111,232,233],{},[117,234,236],{"href":235},"\u002Fprompt-engineering\u002F08-context-and-memory-management","Context & Memory Management",[111,238,239],{},"Sliding window, summarization, structured memory, prompt caching, and the layered production architecture.",[93,241,242,245,251],{},[111,243,244],{},"09",[111,246,247],{},[117,248,250],{"href":249},"\u002Fprompt-engineering\u002F09-iterative-refinement-and-prompt-testing","Iterative Refinement & Prompt Testing",[111,252,253],{},"Eval sets, A\u002FB testing, LLM-as-judge, and the iteration loop that separates engineering from tweaking.",[82,255,257],{"id":256},"part-iii-advanced-techniques","Part III — Advanced Techniques",[87,259,260,270],{},[90,261,262],{},[93,263,264,266,268],{},[96,265,98],{},[96,267,101],{},[96,269,104],{},[106,271,272,286,300,314,328],{},[93,273,274,277,283],{},[111,275,276],{},"10",[111,278,279],{},[117,280,282],{"href":281},"\u002Fprompt-engineering\u002F10-decomposition-and-task-breakdown","Decomposition & Task Breakdown",[111,284,285],{},"Pipelines of focused prompts, structured handoffs, parallel vs. sequential, and error compounding.",[93,287,288,291,297],{},[111,289,290],{},"11",[111,292,293],{},[117,294,296],{"href":295},"\u002Fprompt-engineering\u002F11-self-consistency-and-verification","Self-Consistency & Verification",[111,298,299],{},"Sampling multiple paths, majority voting, external ground-truth checks, and multi-model cross-checking.",[93,301,302,305,311],{},[111,303,304],{},"12",[111,306,307],{},[117,308,310],{"href":309},"\u002Fprompt-engineering\u002F12-retrieval-augmented-generation-rag","Retrieval-Augmented Generation (RAG)",[111,312,313],{},"Grounding instructions, citation verification, contradiction handling, and chunk placement strategy.",[93,315,316,319,325],{},[111,317,318],{},"13",[111,320,321],{},[117,322,324],{"href":323},"\u002Fprompt-engineering\u002F13-tool-use-and-function-calling","Tool Use & Function Calling",[111,326,327],{},"Tool definitions, the calling loop, result formatting, error handling, and the tool-vs-prompt decision.",[93,329,330,333,339],{},[111,331,332],{},"14",[111,334,335],{},[117,336,338],{"href":337},"\u002Fprompt-engineering\u002F14-multi-agent-and-agentic-workflows","Multi-Agent & Agentic Workflows",[111,340,341],{},"Orchestrator\u002Fsub-agent patterns, structured handoffs, synthesis design, and human-in-the-loop enforcement.",[82,343,345],{"id":344},"part-iv-model-specific-practical-craft","Part IV — Model-Specific & Practical Craft",[87,347,348,358],{},[90,349,350],{},[93,351,352,354,356],{},[96,353,98],{},[96,355,101],{},[96,357,104],{},[106,359,360,374,388],{},[93,361,362,365,371],{},[111,363,364],{},"15",[111,366,367],{},[117,368,370],{"href":369},"\u002Fprompt-engineering\u002F15-working-with-claude","Working with Claude",[111,372,373],{},"XML tags, system prompt structure, extended thinking, literal instruction-following, and pushback encouragement.",[93,375,376,379,385],{},[111,377,378],{},"16",[111,380,381],{},[117,382,384],{"href":383},"\u002Fprompt-engineering\u002F16-working-with-gpt-and-other-models","Working with GPT & Other Models",[111,386,387],{},"Portability, OpenAI conventions, reasoning-optimized models, open-weight chat templates, and graceful degradation.",[93,389,390,393,399],{},[111,391,392],{},"17",[111,394,395],{},[117,396,398],{"href":397},"\u002Fprompt-engineering\u002F17-handling-hallucination-and-uncertainty","Handling Hallucination & Uncertainty",[111,400,401],{},"Calibrated uncertainty, grounding with citations, explicit I-don't-know permission, and domain-specific risk patterns.",[82,403,405],{"id":404},"part-v-production-safety","Part V — Production & Safety",[87,407,408,418],{},[90,409,410],{},[93,411,412,414,416],{},[96,413,98],{},[96,415,101],{},[96,417,104],{},[106,419,420,434,448],{},[93,421,422,425,431],{},[111,423,424],{},"18",[111,426,427],{},[117,428,430],{"href":429},"\u002Fprompt-engineering\u002F18-prompt-injection-and-security","Prompt Injection & Security",[111,432,433],{},"Direct and indirect injection, defense-in-depth, architectural safeguards, and the SQL-injection analogy.",[93,435,436,439,445],{},[111,437,438],{},"19",[111,440,441],{},[117,442,444],{"href":443},"\u002Fprompt-engineering\u002F19-evaluating-and-testing-prompts-at-scale","Evaluating & Testing Prompts at Scale",[111,446,447],{},"Eval harnesses, LLM-as-judge calibration, CI-gated regression testing, cost\u002Flatency metrics, and statistical significance.",[93,449,450,453,459],{},[111,451,452],{},"20",[111,454,455],{},[117,456,458],{"href":457},"\u002Fprompt-engineering\u002F20-exercises-and-projects","Exercises & Project Ideas",[111,460,461],{},"From beginner drills to a self-hosted red-team bounty — where the curriculum turns into judgment.",[24,463,465],{"id":464},"learning-path-suggestions","Learning Path Suggestions",[82,467,469],{"id":468},"if-youre-a-developer-building-llm-features-into-a-product","If you're a developer building LLM features into a product",[15,471,472,473,476],{},"Read 01–09 in order — don't skip the foundations even if you're experienced with APIs, since most production prompt bugs trace back to a Part I or II concept applied sloppily. Read 12, 13, and 19 closely. Read 15 or 16 depending on which model you're shipping with. Treat chapter 19's eval-harness pattern as ",[19,474,475],{},"non-optional"," before shipping to real users.",[82,478,480],{"id":479},"if-youre-building-agents-or-tool-using-systems","If you're building agents or tool-using systems",[15,482,483,484,487],{},"Skim 01–09. Read 10, 11, 13, and 14 carefully — this is the core of agentic design. Read 17 before you trust any agent's intermediate claims. Read 18 ",[19,485,486],{},"before"," you give an agent access to anything that matters. Finish with exercises 9, 12, and 13 in chapter 20.",[82,489,491],{"id":490},"if-your-focus-is-safety-security-or-red-teaming","If your focus is safety, security, or red-teaming",[15,493,494],{},"Read 01–04 for the mental model, then jump straight to 17 and 18. Read 19 to understand how injection resistance gets regression-tested rather than checked once. Do exercises 10 and 13 in chapter 20, and treat project 14 (the self-hosted prompt injection bug bounty) as the capstone.",[24,496,498],{"id":497},"companion-resources","Companion Resources",[61,500,501,510,518,526,534],{},[32,502,503,509],{},[117,504,508],{"href":505,"rel":506},"https:\u002F\u002Fdocs.anthropic.com\u002Fen\u002Fdocs\u002Fbuild-with-claude\u002Fprompt-engineering\u002Foverview",[507],"nofollow","Anthropic's Prompt Engineering Guide"," — official Claude-specific guidance.",[32,511,512,517],{},[117,513,516],{"href":514,"rel":515},"https:\u002F\u002Fdocs.anthropic.com\u002F",[507],"Anthropic Docs — Claude Developer Platform"," — full API and model documentation.",[32,519,520,525],{},[117,521,524],{"href":522,"rel":523},"https:\u002F\u002Fplatform.openai.com\u002Fdocs\u002Fguides\u002Fprompt-engineering",[507],"OpenAI's Prompt Engineering Guide"," — official GPT-specific guidance.",[32,527,528,533],{},[117,529,532],{"href":530,"rel":531},"https:\u002F\u002Fcookbook.openai.com\u002F",[507],"OpenAI Cookbook"," — worked examples across common tasks.",[32,535,536,541],{},[117,537,540],{"href":538,"rel":539},"https:\u002F\u002Flearnprompting.org\u002F",[507],"Learn Prompting"," — community-maintained, model-agnostic reference.",{"title":543,"searchDepth":544,"depth":544,"links":545},"",2,[546,547,548,556,561],{"id":26,"depth":544,"text":27},{"id":58,"depth":544,"text":59},{"id":79,"depth":544,"text":80,"children":549},[550,552,553,554,555],{"id":84,"depth":551,"text":85},3,{"id":168,"depth":551,"text":169},{"id":256,"depth":551,"text":257},{"id":344,"depth":551,"text":345},{"id":404,"depth":551,"text":405},{"id":464,"depth":544,"text":465,"children":557},[558,559,560],{"id":468,"depth":551,"text":469},{"id":479,"depth":551,"text":480},{"id":490,"depth":551,"text":491},{"id":497,"depth":544,"text":498},"A code-first, production-grade prompt-engineering reference for mid-to-senior engineers. 20 chapters covering LLM mechanics, prompt structure, core and advanced techniques, RAG, tool use, multi-agent workflows, model-specific conventions, security, and evaluation at scale — through annotated code, anti-patterns, and edge cases.","md",{},true,"\u002Fprompt-engineering",{"title":5,"description":562},"prompt-engineering\u002Findex","hQ_fmw_6ta0JdHQWxAlOeKXXRR9pj55FdehY-ZB8Ktg",1789924651216]