Prompt Engineering
Getting reliable behavior without touching weights.
What is few-shot prompting and when does it help vs hurt?
Few-shot prompting gives the model example input-output pairs in the prompt itself so it infers the pattern/format without any weight updates. It helps most for formatting-sensitive or unusual tasks; it can hurt by consuming context budget, anchoring the model to the exact style of examples (reducing diversity), or introducing bias if examples aren't representative.
Explain Chain-of-Thought (CoT) prompting and why it improves performance.
CoT asks the model to generate intermediate reasoning steps before the final answer ("let's think step by step"), rather than jumping straight to a conclusion. It helps because generating tokens sequentially gives the model a place to do computation via its own output - effectively more "thinking steps" - improving accuracy on multi-step reasoning, math, and logic tasks.
What's the difference between zero-shot, few-shot, and CoT prompting?
Zero-shot gives only the instruction with no examples; few-shot adds example (input, output) pairs; CoT is orthogonal to both - it's about eliciting explicit reasoning steps and can be combined with zero-shot ("zero-shot CoT") or few-shot prompting.
What is self-consistency prompting?
Instead of taking one CoT reasoning path, the model generates multiple independent reasoning paths (via sampling with temperature) for the same question, and the final answer is chosen by majority vote across paths - improving reliability on reasoning-heavy tasks at the cost of multiple generations.
How do you design a robust system prompt for production?
- Be explicit about role, tone, and boundaries rather than assuming implicit understanding.
- Give concrete positive and negative examples, not just abstract rules.
- Specify output format precisely (e.g. exact JSON schema) if downstream code parses the output.
- Put the most important constraints near the start and end of the prompt (models weight the edges of context more reliably than the middle - the "lost in the middle" effect).
- Test against adversarial/edge-case inputs, not just happy-path examples.
What is prompt injection and how do you defend against it?
Prompt injection is when untrusted input (e.g., a webpage or document the model reads) contains text designed to override the system's actual instructions ("ignore previous instructions and..."). Defenses include: clearly delimiting untrusted content, instructing the model to treat retrieved/tool content as data not commands, using a separate, more privileged system prompt channel where supported, output filtering, and least-privilege tool permissions so even a hijacked model can't cause damage.
Why does temperature and top-p (nucleus) sampling matter, and how do you pick values?
Temperature scales the logits before softmax - low temperature (near 0) makes output deterministic and greedy, high temperature increases randomness/diversity. Top-p sampling restricts sampling to the smallest set of tokens whose cumulative probability exceeds p, avoiding low-probability "tail" tokens that greedy or high-temperature sampling might pick. For factual/deterministic tasks (extraction, code, classification) use low temperature (0-0.3); for creative tasks use higher temperature (0.7-1.0) - often combined with a moderate top-p (~0.9) rather than temperature alone.