Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 21

Augmented Prompt Construction

Recommended resources

Welcome back. In our last lesson, we successfully implemented the retrieval step, creating a script that takes a user's query and fetches the most relevant document chunks from our vector store. You now have the raw materials—the context—needed to answer a question.

This lesson tackles the critical "Augmentation" stage of the RAG pipeline. Our goal is to construct an augmented prompt that incorporates this retrieved context into the LLM input. We will learn how to strategically combine the user's query with the context you just learned to retrieve, crafting a precise set of instructions that guides the LLM to generate a factual, grounded answer. This step is where we move from simply finding information to telling the model exactly how to use it.

From Prompting to Context Engineering

In the first module of this course, you learned the fundamentals of prompting: structuring a request with system and user roles. That's a solid foundation, but RAG requires a more dynamic and sophisticated approach. We aren't just giving the model a static instruction; we are dynamically engineering a context at query time.

The diagram below perfectly captures this shift. On the left is basic prompt engineering, where a simple user message and system prompt are sent to the model. On the right is what we're doing in RAG: curating various sources of information (documents, memory, tools) and injecting them into the context window to create a rich, detailed prompt.

This image contrasts a simple, single-turn prompt with the more complex process of context engineering for an agent or a RAG system, where multiple information sources are curated and combined into the final prompt.

This process of dynamically injecting retrieved information is the essence of building an "augmented prompt." It's not just about passing along data; it's about structuring that data to maximize its usefulness for the model.

To see this idea in action, the following video explains how runtime information, including RAG retrievals, is used to populate a base prompt.

Context Engineering vs. Prompt Engineering: Smarter AI with RAG & Agents

This clip from IBM Technology clearly explains the concept of context engineering.

Watch the segment from this point, where the presenter describes how a static base prompt is dynamically injected with context from RAG retrievals and other sources. Notice the key insight: the final prompt sent to the LLM can be composed mostly of this dynamic content.


The Anatomy of an Augmented Prompt

So, what does a well-structured augmented prompt look like? While variations exist, a robust and effective prompt generally consists of three core components, typically arranged in a specific order:

  1. Instructions: This is a system-level directive that tells the LLM its role, its constraints, and its goal. This is where you program the model's behavior, instructing it to prioritize the provided context.
  2. Context: This is the content of the document chunks you retrieved in the previous step. You need to format this information clearly so the model can distinguish between different pieces of evidence.
  3. Query: This is the original, unchanged question from the user.

Placing the query last often helps focus the model's attention on the specific task after it has processed all the provided evidence. The following article provides an excellent overview of this structure and the reasoning behind it.

Combining Retrieved Context with Prompts in RAG

This resource explains the fundamental principles of constructing an augmented prompt.

Read the entire short article, from the introduction down to the example. Pay close attention to the three-part structure it proposes (Instructions, Context, Query) and the Python f-string example that demonstrates how to assemble them.

Let's dissect each component with a bit more detail, drawing on best practices from production systems.

1. Crafting Effective Instructions

The instruction set is your primary tool for controlling the LLM's behavior and preventing hallucination. Your goal is to force the model to be "faithful" to the provided context. A simple instruction like "Answer the question" is not enough. You need to be explicit.

Consider these examples of strong grounding instructions:

  • Strict: "You are an assistant for question-answering tasks. Use only the following pieces of retrieved context to answer the question. If you don't know the answer from the context provided, just say that you don't know. Don't try to make up an answer."
  • With Citations: "Answer the user's question based on the provided documents. For each statement in your answer, you must cite the document number it came from (e.g., [Document 1])."

This technique of setting explicit boundaries is a key part of prompt engineering. The article below discusses this in the context of fixing a common RAG failure: hallucination even when the correct context is present.

Building RAG pipelines in Python: A step-by-step guide for ...

This blog post on production RAG systems offers practical advice for common failure modes.

Find the section "Common RAG failures and their fixes" and read the entry for Failure 3. Note how the primary fix is to strengthen the grounding instructions in the system prompt.

2. Formatting the Context

You can't just smash all the retrieved text chunks together. The model needs to see where one document chunk ends and another begins. As a software engineer, you can think of this as serializing a list of objects into a human-readable (or in this case, LLM-readable) string format.

Common formatting strategies include:

  • Using clear separators like ---.
  • Numbering each chunk, like [Document 1], [Document 2], etc. This is especially useful if you ask the model to provide citations.
  • Including metadata, such as the source filename, if available.

Here's how you might format the chunks in Python:




# Assume retrieved_nodes is a list of objects from LlamaIndex,
# where each object has a .get_content() method.

context_chunks = [node.get_content() for node in retrieved_nodes]




# Format with numbering and separators
formatted_context = ""
for i, chunk in enumerate(context_chunks):
    formatted_context += f"[Document {i+1}]:\n{chunk}\n\n---\n\n"

Putting It All Together: From Retrieval to Prompt

Now, let's combine these pieces into a complete workflow. You have the retrieved_nodes from the previous lesson's script. You have the query. Now you can construct the final prompt string.

The following Python function demonstrates the complete augmentation step:

def create_augmented_prompt(retrieved_nodes: list, query: str) -> str:
    """
    Creates an augmented prompt string from retrieved context and a query.
    """



    # 1. Format the context
    context_chunks = [node.get_content() for node in retrieved_nodes]
    formatted_context = ""
    for i, chunk in enumerate(context_chunks):
        formatted_context += f"--- Context Document {i+1} ---\n{chunk}\n"




    # 2. Define the instruction template
    # Note the strong grounding instructions.
    prompt_template = f"""
You are a helpful assistant. Your task is to answer the user's query based ONLY on the provided context documents.
- Do not use any of your own prior knowledge.
- If the context does not contain the answer, you MUST state that you cannot answer the question with the given information.

CONTEXT:
{formatted_context}

USER QUERY:
{query}

ANSWER:
"""



    # 3. Return the final prompt
    return prompt_template




# --- Example Usage (building on the previous lesson) ---
# retrieved_nodes = retriever.retrieve("Summarize Python naming conventions.")
# query = "Summarize Python naming conventions."
#
# final_prompt = create_augmented_prompt(retrieved_nodes, query)
# print(final_prompt)

This script cleanly separates the logic for formatting the context and assembling it into a final prompt with clear, firm instructions. The resulting final_prompt string is what you will send to the LLM.

For a live demonstration of this process, let's revisit the pixegami video. The following segment shows exactly how a prompt template is defined and then populated with retrieved chunks and the user's query.

RAG + Langchain Python Project: Easy AI/Chat For Your Docs

This video walks through building a RAG project using LangChain, and this specific part covers prompt construction.

Watch the segment from this point. The presenter first defines a prompt template with placeholders for context and question. Then, they format this template with the retrieved data to create the final, complete prompt string that will be sent to the LLM. This is the exact process we are implementing.

This step, while seemingly simple string manipulation, is one of the highest-leverage activities in building a RAG system. The quality of your prompt directly governs the faithfulness and accuracy of the final generated answer.


Conclusion

In this lesson, you've bridged the gap between retrieval and generation. You've learned how to move beyond simple prompting to "context engineering," where you dynamically construct a detailed, augmented prompt at query time.

Here are the key takeaways:

  • Augmented prompts are essential: They provide the necessary instructions and evidence for an LLM to generate a grounded response, rather than relying on its internal, potentially outdated knowledge.
  • Structure is key: A well-designed prompt contains clear instructions, well-formatted context, and the user's query.
  • Instructions enforce faithfulness: By explicitly telling the model to use only the provided context and what to do if the answer isn't present, you significantly reduce the risk of hallucination.
  • Context formatting matters: Using separators or numbering helps the LLM distinguish between different pieces of evidence, improving its ability to synthesize information and provide citations.

You have now assembled a complete, augmented prompt. The final step in the pipeline is to take this prompt and pass it to an LLM to generate the final answer. In the next lesson, we will do just that, completing our end-to-end RAG query implementation.