RUNLOCALAIv38
->Will it run?Best GPUCompareTroubleshootStartLearnPulseModelsHardwareToolsBench
Run check
RUNLOCALAI

Independently operated catalog for local-AI hardware and software. Hand-written verdicts. Source-cited claims. Reproducible commands when we have them.

OP·Eruo Fredoline
DIR
  • Models
  • Hardware
  • Tools
  • Benchmarks
TOOLS
  • Will it run?
  • Compare hardware
  • Cost vs cloud
  • Choose my GPU
  • Prompting kits
  • Quick answers
REF
  • All buyer guides
  • Learn local AI
  • Methodology
  • Glossary
  • Errors KB
  • Trust
EDITOR
  • About
  • Author
  • How we make money
  • Editorial policy
  • Contact
LEGAL
  • Privacy
  • Terms
  • Sitemap
MAIL · MONTHLY DIGEST
Get monthly local AI changes
Monthly recap. No spam.
DISCLOSURE

Some links on this site are affiliate links (Amazon Associates and other first-class retailers). When you buy through them, we earn a small commission at no extra cost to you. Affiliate links do not influence our verdicts — there are cards we rate highly that we don't have affiliate relationships with, and cards that sell well that we refuse to recommend. Read more →

© 2026 runlocalai.coIndependently operated
RUNLOCALAI · v38
  1. >
  2. Home
  3. /Learn
  4. /Courses
  5. /RAG Systems: Part 1
  6. /Ch. 16
RAG Systems: Part 1

16. Prompt with Retrieved Context

Chapter 16 of 22 · 25 min
KEY INSIGHT

Explicit citation requirements in prompts reduce hallucination by forcing the model to explicitly attribute claims to retrieved context.

The prompt determines how the model uses retrieved context. Even excellent retrieval fails with poorly designed prompts. This chapter covers prompt engineering for RAG systems.

Basic RAG Prompt Template

RAG_PROMPT_TEMPLATE = """You are a helpful assistant that answers questions based ONLY on the provided context.

CONTEXT:
{context}

INSTRUCTIONS:
1. Answer the question using ONLY the context provided above.
2. If the context does not contain enough information to fully answer the question, say "Based on the provided context, I cannot fully answer this question."
3. Do NOT use any knowledge outside the provided context.
4. Quote relevant parts of the context when answering.

QUESTION: {question}

ANSWER:"""

This template includes guardrails preventing the model from hallucinating information not present in context. The "DO NOT use any knowledge outside" instruction significantly reduces fabrication.

Chain-of-Thought for Complex Queries

Complex analytical questions benefit from step-by-step reasoning:

ANALYTICAL_RAG_PROMPT = """You are an analyst answering questions based ONLY on the provided context.

CONTEXT:
{context}

Analyze the following question step by step:
"{question}"

Think step by step:
1. What information does the context provide that is relevant to this question?
2. How does this information combine to form an answer?
3. What specific parts of the context support each part of the answer?

Based on your analysis, provide a final answer:
"""

response = llm.generate(
    prompt=ANALYTICAL_RAG_PROMPT,
    context=assembled_context,
    question=user_question,
    temperature=0.3  # Lower temperature for factual tasks
)

Lower temperature (0.2-0.4) reduces creative liberties and keeps responses grounded in provided context.

Citation-Based Prompts

Force explicit citations to improve answer verifiability and reduce hallucination:

CITATION_PROMPT = """Answer the question using ONLY the provided context. Cite sources explicitly.

CONTEXT:
{context}

QUESTION: {question}

Format your answer as follows:
Answer: [Your answer here]
Sources: [Source 1], [Source 2]

Example:
Answer: The error occurs when memory exceeds 512MB.
Sources: troubleshooting.md (line 45), error_codes.md (line 12)
"""

The citation format forces the model to explicitly reference sources, making it easier to verify answers and identify hallucination.

System Prompt for Source Attribution

SYSTEM_PROMPT = """You are a technical support assistant. Your role is to:
1. Answer questions using ONLY information from the provided context
2. Always cite the specific source document when stating facts
3. Distinguish clearly between what the context says vs. what you know
4. If asked about topics not covered in context, state that the information is not available

Context is authoritative. Never contradict context even if it seems outdated."""

System prompts set persistent behavior expectations. The "never contradict context" instruction handles cases where the model knows more recent information than the retrieved documents contain.

EXERCISE

Design a prompt template that (1) prevents hallucination by requiring source citations, (2) handles unanswerable questions gracefully, and (3) supports multi-document synthesis by explicitly grouping information by source.

← Chapter 15
Context Assembly
Chapter 17 →
Basic Generation Pipeline