Definitive Guide • 20 min read
How to Write AI Prompts: The Complete Guide to Getting Better Results
Writing effective AI prompts means structuring your instructions rather than typing free-form requests. The STCO framework (System → Task → Context → Output) produces valid output on 100% of calls vs 85% for unstructured prompts, reduces hallucinations by 40-60%, and cuts API costs by up to 90%. This guide walks you through every step with before/after examples.
Why Most AI Prompts Fail
Most people write AI prompts like text messages — vague, context-free, and hoping the AI "just knows" what they mean. The result? Generic outputs, hallucinated facts, wasted tokens, and endless copy-paste-retry cycles. Research shows that unstructured prompts produce valid, usable output only 85% of the time (rel-029). That means 1 in 7 responses is broken before you even evaluate quality.
The fix isn't better wording — it's better structure. Structured prompts with explicit output schemas achieve 100% valid output, eliminate retry loops, and save 75% on token costs. This guide shows you exactly how.
Before vs After: The Difference Structure Makes
❌ Unstructured Prompt
Write me a summary of this article. Make it good and not too long. Include the key points.
- No role definition
- Ambiguous length ("not too long")
- No output format
- 85% success rate
- Requires 2-3 rewrites
✅ STCO Structured Prompt
[System] Senior research analyst
[Task] Extract exactly 5 findings
[Context] {article text}
[Output] JSON array:
{finding, evidence, impact}- Clear expert role
- Specific deliverable
- Schema-validated output
- 100% success rate
- First-attempt quality
The structured version uses fewer tokens (150 vs 600 — 75% cheaper) and produces valid output on every single call.
How to Write AI Prompts: 6-Step Process
The STCO framework breaks every prompt into four structured sections, plus examples and iteration. Follow these steps in order:
Step 1: Define the System Role
Step 1/6Tell the AI WHO it is. This sets the expertise level, communication style, and behavioural boundaries. A system role eliminates generic responses by anchoring the AI in a specific professional context.
❌ Don't write:
Summarise this report.
✅ Write this:
[System] You are a senior financial analyst with 15 years of experience in SaaS metrics. You communicate in precise, data-driven language. Never speculate — only state what the data supports.
💡 Pro tip: Be specific about expertise domain, years of experience, and communication constraints. "You are a helpful assistant" is almost worthless.
View supporting research →Step 2: Specify the Task
Step 2/6State ONE clear action in a single sentence. Compound tasks ("summarise AND critique AND suggest improvements") produce confused output. If you need multiple actions, chain separate prompts.
❌ Don't write:
Tell me about this code and fix the bugs and suggest improvements.
✅ Write this:
[Task] Identify all security vulnerabilities in the following code. For each vulnerability, state the line number, severity (critical/high/medium/low), and a one-line fix.
💡 Pro tip: Start with a strong verb: Extract, Classify, Generate, Analyse, Compare. Avoid "Tell me about" or "Help me with".
View supporting research →Step 3: Provide Context
Step 3/6Include ALL relevant data the AI needs. Don't assume it knows your project, your codebase, or your business rules. Context grounding reduces hallucinations by 58% compared to relying on training data alone.
❌ Don't write:
Review our Q4 performance.
✅ Write this:
[Context] - Revenue: £2.1M (up 23% QoQ) - Churn: 4.2% (target: <3%) - NPS: 67 (industry avg: 45) - New enterprise deals: 12 - Support tickets: 340 (up 15%)
💡 Pro tip: Paste actual data, not references to data. The AI can't access your files, databases, or previous conversations unless you include them.
View supporting research →Step 4: Define the Output Format
Step 4/6This is the most impactful step. Specifying a JSON schema, markdown structure, or explicit format eliminates 100% of structural errors. Output tokens cost 3× input tokens — constraining length saves money.
❌ Don't write:
Give me the results in a nice format.
✅ Write this:
[Output] Respond in this exact JSON schema:
{
"findings": [{
"metric": string,
"status": "on_track" | "at_risk" | "critical",
"action": string
}],
"summary": string (max 100 words)
}💡 Pro tip: Use JSON schemas for machine-readable output, markdown for human-readable. Always set max length constraints to avoid paying for verbose responses.
View supporting research →Step 5: Add Few-Shot Examples
Step 5/62-3 input/output examples teach the AI your exact quality standard. Few-shot examples in 150 tokens outperform 600-token verbose instructions — saving 75% on input costs while producing better results.
❌ Don't write:
Make it professional and detailed.
✅ Write this:
[Example]
Input: "Revenue grew 15%"
Output: {"metric":"Revenue","status":"on_track","action":"Maintain current acquisition strategy"}
Input: "Churn hit 5.1%"
Output: {"metric":"Churn","status":"critical","action":"Launch retention campaign targeting Month 3 drop-off"}💡 Pro tip: Show one "normal" case and one "edge" case. The AI learns the pattern, not just the rule.
View supporting research →Step 6: Test, Measure, Iterate
Step 6/6Run your prompt 3-5 times. Check output validity (does it match the schema?), factual accuracy (are claims grounded in context?), and usefulness (would you send this to a stakeholder?). Then refine the weakest section.
❌ Don't write:
It looks about right, ship it.
✅ Write this:
Validation checklist: ✅ JSON parses without errors ✅ All required fields present ✅ No claims unsupported by context ✅ Summary under 100 words ✅ Consistent across 5 runs
💡 Pro tip: Use constrained decoding (Outlines, LMQL) for 0% retry rates in production. For exploration, manual review is fine.
View supporting research →Complete Prompt Example: All 4 STCO Sections
[System]
You are a senior product analyst at a B2B SaaS company.
You only make claims supported by the provided data.
You flag metrics that deviate >10% from target as "at_risk".
[Task]
Analyse Q4 2025 performance metrics and produce an
executive summary with actionable recommendations.
[Context]
- MRR: £185K (target: £200K) — 7.5% below target
- Net Revenue Retention: 108% (target: 110%)
- New logos: 23 (target: 30) — 23% below target
- Support CSAT: 4.6/5.0 (target: 4.5) — exceeding
- Avg deal size: £8,200 (up 12% QoQ)
[Output]
Respond in JSON:
{
"executive_summary": string (max 150 words),
"metrics": [{
"name": string,
"actual": string,
"target": string,
"status": "on_track" | "at_risk" | "exceeding",
"recommendation": string
}],
"top_priority": string
}This prompt uses ~250 tokens and produces valid, actionable JSON on the first attempt. An unstructured version ("analyse our Q4 numbers") would require 2-3 follow-ups and produce unstructured prose that can't be programmatically processed.
The Research: Why Structure Beats Freestyle
Go Deeper: Specialised Guides
Now that you know how to write structured prompts, dive into specific topics:
Free Tools to Practice
📌 Key Takeaways
- Structure beats wording. The STCO framework (System-Task-Context-Output) produces 100% valid output vs 85% for unstructured prompts.
- Be specific, not verbose. 3 few-shot examples in 150 tokens outperform 600-token instructions — 75% cheaper with better quality.
- Define the output format. JSON schemas eliminate parse errors and enable automated pipelines. Output costs 3× input — constrain it.
- Ground in context. Paste actual data, not references. RAG grounding reduces hallucinations by 58%.
- Test and iterate. Run 3-5 times, check schema conformance, refine the weakest section.
Skip the Learning Curve
AI Prompt Architect generates STCO-structured prompts with JSON schemas automatically — 100% valid output on every call.
Start Writing Better Prompts →How to Write AI Prompts: The Research
Every claim below is sourced from peer-reviewed research and industry reports.Browse all 141 citations →
Prompt caching reduces static context costs.
Cached prompt tokens cost $0.30/MTok vs $3.00/MTok uncached on Claude 3.5 Sonnet — a 90% reduction on repeated system instructions.
Without prompt caching, enterprise pipelines re-tokenise and re-bill the same system prompt across thousands of requests, paying 10x more for identical static context.
Anthropic, 'Prompt Caching (Beta)' documentation, 2024Model downshifting lowers inference costs.
Structured prompts enable GPT-3.5-class models to match GPT-4 output quality on 78% of classification tasks, at 1/30th the per-token cost ($0.0005 vs $0.03/1K tokens).
Without quality prompts, smaller models produce unusable output, forcing developers to default to expensive frontier models.
Khattab et al., 'DSPy: Compiling Declarative Language Model Calls', Stanford NLP, 2023Output tokens are significantly more expensive than input tokens.
GPT-4o charges $15.00/MTok for output vs $5.00/MTok for input — a 3x premium. Constraining max_tokens from 4096 to 500 saves $11.25 per million requests.
Without output length constraints, LLMs generate verbose responses that consume the most expensive billing vector — output tokens — at 3x the input rate.
OpenAI, 'API Pricing' page, updated 2024Constrained decoding eliminates retry loops via grammar-guided generation.
Outlines' grammar-guided generation produces valid JSON on every call with 0% retry rate, versus 15% retry rates with unconstrained generation — eliminating the 2-3x token cost multiplier from failed parses.
Without constrained decoding, each failed JSON generation consumes the full input + output token budget before retrying, compounding costs exponentially across high-volume pipelines.
Outlines, '.txt: Structured Generation with Grammar-Guided Constrained Decoding' documentation, 2024