Prompt Engineering Guide for LLMs

Prompt engineering is the art of crafting inputs to large language models to produce desired outputs. Effective prompting significantly improves output quality, consistency, and reliability.


Zero-Shot Prompting


Zero-shot prompting gives the model a task description without examples. The model relies on its training data to understand and execute the request. Be specific about the output format, tone, and constraints.


Structure zero-shot prompts with clear instructions, context, and expected output format. Use delimiters (""", ---, ```) to separate instructions from input. Specify constraints: "Explain this concept to a 10-year-old" or "Respond in JSON format with keys: summary, details."


Few-Shot Prompting


Few-shot prompting provides examples of desired inputs and outputs. Three to five examples typically work best. Examples demonstrate the pattern, format, and reasoning process you want the model to follow.


Select diverse examples that cover edge cases. Order examples from simple to complex. Include examples of what NOT to do for improved accuracy. Few-shot prompting is particularly effective for classification, extraction, and formatting tasks.


Chain-of-Thought


Chain-of-thought prompting asks the model to show its reasoning step by step. This improves accuracy on complex reasoning tasks. Add "Let's think step by step" or provide a chain-of-thought example.


For math and logic problems, chain-of-thought dramatically improves accuracy from baseline. Tree-of-thought extends this by exploring multiple reasoning paths. Self-consistency runs chain-of-thought multiple times and selects the most common answer.


Structured Output


Request structured output formats explicitly. "Return a JSON array of objects" or "Output as a markdown table." Specify required and optional fields. Provide the JSON schema or TypeScript interface in the prompt.


For critical applications, use function calling or structured output APIs (available with GPT-4 Turbo and Claude 3). These guarantee structured responses matching your schema, eliminating parsing errors.


Iterative Refinement


Treat prompting as an iterative process. Test prompts with diverse inputs. Analyze failures and refine. A/B test prompt variations. Build prompt test suites for regression testing. Prompt versioning tracks changes and their impact on output quality.