← All chapters
Prompt Engineering
Designing reliable prompts, templates, and reusable prompting patterns.
24 terms
Chain-of-Thought PromptingChain-of-thought prompting instructs the model to produce an explicit reasoning trace — a sequence of intermediate steps — before committing to a final answer. By externalizing reasoning into text, the model can condition its next step on prior logic, which substantially improves accuracy on tasks requiring arithmetic, multi-step planning, or causal reasoning. CoT can be elicited with a simple instruction ('think step by step') or with few-shot examples that model correct reasoning chains.Context Window BudgetingContext window budgeting is the practice of deliberately allocating the finite token capacity of a model's context window across competing content — system instructions, few-shot examples, retrieved documents, conversation history, and expected output. Because exceeding the window truncates content and can silently degrade quality, engineers must estimate token counts at design time and implement dynamic truncation or summarization strategies to keep prompts within budget while preserving the highest-value content.DelimitersDelimiters are syntactic markers — such as XML tags, triple backticks, or dashed lines — used to separate distinct sections within a prompt, such as instructions from context, context from user input, or examples from the task. Clear delimiters reduce the risk that the model conflates injected content with trusted instructions, making them an important defense against prompt injection. They also help the model locate the correct section to attend to when processing complex multi-part prompts.Few-Shot PromptingFew-shot prompting includes a small number of worked input-output examples directly in the prompt to demonstrate the expected behavior before presenting the actual task. The examples act as implicit instructions that steer the model's output format, reasoning style, and domain vocabulary without any weight updates. Selecting diverse, representative examples and ordering them thoughtfully can dramatically improve accuracy on tasks where zero-shot instructions alone are insufficient.Grounding InstructionsGrounding instructions explicitly direct the model to base its answer solely on the provided context and to avoid introducing information from parametric memory. Common formulations include 'Answer only using the documents below' or 'If the answer is not in the provided context, say you do not know.' These instructions reduce hallucination in retrieval-augmented applications and make faithfulness easier to evaluate, though sufficiently strong parametric knowledge can sometimes override them.Guardrail PromptA guardrail prompt is an instruction or system-level policy statement embedded in the prompt that defines boundaries on the model's behavior — prohibiting certain topics, requiring specific disclaimers, enforcing personas, or mandating refusal for defined request types. Unlike a separate content-filtering layer, a guardrail prompt relies on the model's instruction-following ability, making it convenient to deploy but potentially vulnerable to jailbreak attacks that bypass in-prompt constraints.Instruction FollowingInstruction following refers to a model's ability to precisely adhere to explicit directives in the prompt — respecting stated constraints like word limits, format requirements, topic scope, tone, and output structure. Strong instruction following is a core product-quality dimension distinct from factual accuracy: a model can be factually correct yet fail to follow instructions by producing the wrong format or ignoring a stated constraint. IFEval and similar benchmarks measure this capability systematically.Instruction HierarchyInstruction hierarchy is the principle that some instructions outrank others: system and developer instructions should take precedence over instructions embedded in user input or retrieved content. Designing prompts and models to respect this ordering is a key defense against prompt injection, since it lets the system ignore lower-priority text that tries to override its rules.Meta-PromptingMeta-prompting uses a language model to generate, refine, or select prompts for another model — or for a subsequent invocation of itself. Rather than handcrafting every prompt, a meta-prompter can iteratively improve a prompt by asking the model to critique its own output and suggest improvements, automatically explore prompt variants, or generate task-specific instructions from a high-level description. Meta-prompting shifts prompt engineering from manual art toward automated optimization.Negative PromptingNegative prompting explicitly tells the model what not to do, produce, or include, complementing the positive instructions that describe the desired output. Saying 'Do not include citations,' 'Avoid jargon,' or 'Do not speculate beyond the provided context' steers the model away from known failure modes. While less powerful on its own than precise positive instructions, negative prompting is a practical corrective when a model persistently exhibits an unwanted behavior despite general guidance.Output FormattingOutput formatting instructs the model to produce its response in a specific structure — such as JSON, Markdown, a numbered list, or a table — rather than free-form prose. Explicit format instructions in the system or user prompt, combined with structured output API features where available, allow downstream code to parse the response reliably. Poor or ambiguous format instructions are a leading cause of integration failures in LLM-powered pipelines.PromptA prompt is the complete text input passed to a language model to elicit a desired response. It typically includes a task description, relevant context, any examples, and formatting guidance. Because language models have no persistent state between calls, everything the model needs to reason correctly must be present in the prompt. Prompt quality has an outsized effect on output quality and is often the fastest lever for improving a deployed LLM application.Prompt ChainingPrompt chaining decomposes a complex task into a sequence of smaller prompts where the output of each step feeds as input to the next. By breaking a large problem into manageable sub-tasks, chaining can exceed the quality achievable in a single long prompt, allows intermediate validation, and makes the overall pipeline easier to debug and modify. Each chain link can use a different model, apply different formatting, or incorporate additional retrieved context.Prompt CompressionPrompt compression reduces the token count of a prompt while preserving the information needed for accurate responses. Techniques range from simple heuristics like removing boilerplate and redundant examples to neural methods that distill long contexts into shorter summaries or soft token representations. Compression is especially important in long-document QA and multi-turn conversations where the raw context can easily overflow the model's context window or drive up inference costs.Prompt InjectionPrompt injection is an attack in which malicious text in user input or retrieved content attempts to override the model's original instructions and redirect its behavior. Direct injection embeds commands in the user's message; indirect injection hides instructions in documents or web pages the agent retrieves. Defenses include strict delimiters, sandboxed retrieval, instruction hierarchy enforcement, and output validation — though no defense is yet fully robust against all injection variants.Prompt LintingPrompt linting applies automated checks to prompt templates before they ship: verifying required variables are present, flagging contradictory instructions, catching unescaped delimiters, and enforcing length or format conventions. Like code linting, it catches a class of avoidable errors early and keeps a growing prompt library consistent as multiple people edit it.Prompt TemplateA prompt template is a reusable string that contains fixed instructional scaffolding alongside clearly marked placeholders for dynamic content. At request time, the application fills in the placeholders with user input, retrieved context, or other runtime values to produce the final prompt sent to the model. Templates enforce consistency across requests, make prompt logic testable, and separate content from structure so non-engineers can adjust wording without touching application code.Prompt Template VariablesPrompt template variables are named placeholders within a prompt template that are replaced with actual values at request time, such as a user's name, a retrieved document, or a task-specific parameter. They enable a single template to serve many different inputs while keeping instructional logic consistent. Well-named variables also serve as documentation, making it clear to engineers and product teams exactly what dynamic content the prompt incorporates and where.Role PromptingRole prompting assigns the model a specific identity or area of expertise at the start of a prompt — for example, 'You are a senior security engineer' — to steer its response style, knowledge depth, and vocabulary. Assigning a credible role can improve output quality on domain-specific tasks by anchoring the model's generation in the frame of someone with relevant expertise. However, role prompts must be carefully designed to avoid granting implicit permission to violate safety policies.Self-ConsistencySelf-consistency improves reasoning accuracy by sampling multiple independent answers to the same question and selecting the most frequent response. It leverages the observation that correct reasoning paths tend to converge on the same answer while errors are more varied. Self-consistency is most effective for tasks with a single correct answer such as math or factual QA, and works best when chain-of-thought reasoning is used to generate each candidate so the model can explore diverse reasoning paths.Structured Output PromptingStructured output prompting instructs a model to produce output conforming to a specific data schema — typically JSON — either through prompt instructions alone or through API-level schema enforcement that constrains the model's token sampling. Schema-constrained generation guarantees parseable output even when the model's instruction-following is imperfect, eliminating the need for fragile post-processing heuristics. It is essential in agentic pipelines where downstream code must reliably parse and act on model responses.System PromptA system prompt is a privileged message injected at the start of a conversation to establish the model's role, behavioral constraints, output format, and any persistent context. Unlike user turns, the system prompt is controlled by the developer rather than the end user and is often invisible in the interface. It is the primary mechanism for customizing a general-purpose model into a specialized application agent with a specific persona and guardrail policy.XML-Tagged PromptingXML-tagged prompting wraps distinct parts of a prompt — instructions, context, examples, and the user's input — in named tags so the model can tell them apart reliably. Clear delimiters reduce the chance that retrieved text or user input is mistaken for instructions, and they make long prompts easier to template and maintain. Many models follow tagged structure more consistently than loose prose.Zero-Shot PromptingZero-shot prompting asks a language model to perform a task from a plain instruction alone, without any worked examples in the prompt. It relies entirely on knowledge and capabilities acquired during pretraining and instruction tuning. Zero-shot prompts are preferred when examples are hard to curate, context is scarce, or the task is straightforward enough that the model's general competency is sufficient without additional demonstration.