liveBook · Manning

Context

As explained in AI Agents and Applications

Prompt context is background text or information supplied alongside a request to help a language model interpret it and formulate an answer.

A question alone may not give a chatbot enough information to answer. Context is the background text or information placed in the prompt alongside the question. The model uses both to formulate an answer. section 6.1.1

In a structured prompt, context explains the situation surrounding a request. Keep it distinct from the instruction, which says what to do, and from specific input, such as a passage or list of points to process. Include the parts that help the model understand the request; the prompt structure is a checklist, not a requirement to fill every field. section 2.6

Ask about a document

To ask about the temples at Paestum, put a question about their number, builders, and architectural style before a passage describing the site. The passage supplies context; the question identifies what to extract from it. A follow-up question can narrow an initial answer. section 6.1.1

Listing 7.3 Initial RAG setup before incorporating message history
from langchain_core.runnables import RunnablePassthrough
from langchain_core.prompts import PromptTemplate
from langchain_openai import ChatOpenAI

rag_prompt_template = """Use the following pieces of context
to answer the question at the end. 
If you don't know the answer, just say that you don't know, 
don't try to make up an answer.
Use three sentences maximum and keep the 
answer as concise as possible.
{context}
Question: {question}
Helpful Answer:"""

rag_prompt = PromptTemplate.from_template(rag_prompt_template)

retriever = vector_db.as_retriever()

question_feeder = RunnablePassthrough()

chatbot = ChatOpenAI(openai_api_key=OPENAI_API_KEY, 
                     model_name="gpt-5-nano")

rag_chain = {"context": retriever, 
             "question": question_feeder}|rag_prompt|chatbot

def execute_chain(chain, question):
    answer = rag_chain.invoke(question)
    return answer

The RAG prompt has separate {context} and {question} slots. A retriever supplies context, while the user's question flows into the question slot. The shown setup precedes the addition of message history. section 7.4

For an ongoing chat, the current document is not the only useful context. Earlier questions and answers can help the chatbot interpret a new prompt, so a RAG chat setup can be amended to include message history. section 7.4

How AI Agents and Applications covers it

How deep it goes
Defines context through a single-document Q&A workflow and shows separate context and question slots in an initial RAG prompt.
Examples
Uses a Paestum passage and temple questions, plus a LangChain prompt template with context and question slots.
What it assumes
Starts with a no-code chatbot interaction before showing context in a LangChain RAG setup.
What you can do after
Apply it via a prompt template that pairs a question with document context, then consider message history for follow-up questions.
How it connects
Connects context to prompt structure, document Q&A, retrieval-augmented generation, and chatbot message history.

Also explained in

Compare how 18 books explain context →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of AI Agents and Applications. The section links point to the parts of the book it draws on.