AI & LLM Basics
Prompting Basics: How to Get More Reliable Answers from AI (with Templates)
Beginner 15 min For: Beginners / Developers
Key takeaways
- ▸A good prompt = goal + context + output format + constraints. Treat the model as a brilliant new colleague who knows nothing about your situation.
- ▸Few-shot examples — 2 to 5 “input → output” pairs in the prompt — steer the model better than long descriptions.
- ▸Asking the model to work through steps before answering (chain of thought) clearly helps on maths and multi-step reasoning; sampling several answers and taking the majority (self-consistency) helps further.
- ▸When code will read the result, use the platform's structured-output (JSON Schema) feature and validate it again in your code.
- ▸Prompting cannot supply what the model does not know: use retrieval (RAG) for fresh or private data and tools for exact calculations or actions.
The short answer
A prompt is everything you give a large language model: instructions, material, examples and format requirements. The model can only predict the next token from the context you provide (see What Is a Large Language Model?), so the clearer the prompt, the more reliable the answer. The official guides from OpenAI, Anthropic and Google agree closely: be clear, give examples, give structure, and define what “good” looks like.
Step 1: instructions, roles and context
Picture the model as a brilliant new colleague who knows nothing about your situation (Anthropic's guide uses the same image). A complete prompt usually has four parts:
| Part | What to write | Example |
|---|---|---|
| Goal | What to produce and for whom | “Write a refund-process guide for newly hired support staff” |
| Context | Information the model cannot know | The policy text, product names, an existing template |
| Format | What the output looks like | “A numbered list of 5 steps, each under 20 words” |
| Constraints | What not to do, what to do when unsure | “If the policy doesn't cover a case, answer ‘escalate to a human’; don't guess” |
Practical tips:
- Separate instructions from material. Wrap the content to process in headings, triple quotes or XML tags (for example
<policy>…</policy>) so the model is less likely to treat sentences in it as instructions. - Long documents first, question last. Anthropic's docs recommend placing long documents at the top of the prompt and the query at the end.
- Add a role, but don't rely on it. “You are a senior financial analyst” adjusts tone and depth; the specific task and material still decide quality.
- Use the system prompt for rules that stay the same, and the user message for each specific task.
Step 2: few-shot examples
Instead of describing the style you want at length, show 2–5 examples. The GPT-3 paper showed that a model can perform tasks it was never specifically trained for just from a few examples in the prompt — “in-context learning” (Brown et al., 2020).
Classify user feedback as: feature request / bug / praise.
Feedback: The export button does nothing when I click it → bug
Feedback: Please let me filter by date → feature request
Feedback: Your support agent was so patient, thanks! → praise
Feedback: The mobile app shows a blank screen after login →
Make examples cover different cases (including edge cases) and match the exact output format you want. Avoid near-identical examples, or the model learns only the surface pattern.
Step 3: let the model think step by step
For maths, logic or multi-step problems, asking the model to write out its reasoning before the answer improves accuracy noticeably. This is chain-of-thought prompting (Wei et al., 2022). Even adding “Let's think step by step” helps (Kojima et al., 2022). Going further, have the model solve the problem several times and take the most common answer — self-consistency (Wang et al., 2022).
Two caveats:
- Reasoning models (which “think” before answering) already do this internally. OpenAI's guide likens a reasoning model to a senior co-worker who can be given a goal and trusted with the details, while GPT models need more explicit instructions.
- A written-out chain of reasoning is not guaranteed to be correct; still check the final answer.
Step 4: structured output (JSON)
If a program will consume the result, ask for a fixed format such as JSON.
- Prefer the platform feature. OpenAI's Structured Outputs, for example, makes the output conform to a JSON Schema you supply (OpenAI docs).
- Without such a feature, list the field names and types with a full example, and say “output JSON only, no extra text”.
- Always validate in code. If parsing fails or fields are missing, retry or raise an error rather than using it as is.
{"category": "bug", "product_area": "login", "urgency": "high", "summary": "Blank screen after mobile login"}
Step 5: iterate on prompts like code
Prompts are rarely right the first time. A dependable loop:
- Collect 5–20 real test questions (including hard and edge cases) and write down what a good answer looks like.
- After every prompt change, run the same set and compare results.
- Split complex tasks into steps (extract, then analyse, then write), one prompt each — easier to control than one giant prompt.
For evaluating model outputs systematically, see How to Evaluate LLMs.
When prompting is not enough
Prompting can only draw on what the model already can do; it cannot add what the model doesn't know:
| Problem | Better approach |
|---|---|
| You need fresh information or internal documents | Retrieval-augmented generation (RAG), web search or file upload |
| You need exact calculation, database lookups or system actions | Tool calling and MCP, a code interpreter |
| You need multi-step execution that adapts to results | AI agents |
| High-volume tasks with strict, stable style requirements | Fine-tuning |
Also, when processing external content — web pages, emails, documents — it may contain text like “ignore previous instructions”. That is prompt injection, and prompting alone cannot defend against it.
Try it yourself
- Pick a task you repeat every week, write it as a goal + context + format + constraints template, test it on 5 real cases, and revise it three times, noting what went wrong each time.
- Work through Anthropic's interactive prompt-engineering tutorial.
- Compare the OpenAI prompting guide, Gemini prompting strategies and the Prompt Engineering Guide and note what they agree on.
FAQ
Are longer prompts better?
No. What matters is complete information, not word count. Irrelevant background distracts; put key requirements somewhere clear and separate instructions from material with headings or XML/Markdown tags.
Does giving the AI a role (“You are a senior lawyer”) help?
Somewhat: it helps the model pick the right tone and depth, and Anthropic's guide recommends setting a role in the system prompt. But a role is no substitute for a specific task description and the relevant material.
Do reasoning models still need “think step by step”?
Usually not. OpenAI's guide notes that reasoning models do better with high-level guidance. For ordinary chat models, asking for step-by-step reasoning still helps.
Why does the same prompt give different answers?
Generation involves random sampling. For stable results, lower the temperature, add examples and a fixed format, and re-run a set of test questions after every change.