Adding Context to a Prompt
What Is Context? Context is the background information that tells the model about the situation in which a task is being performed.
In simple terms, the instruction says what to do; the context explains the circumstances around it: who it is for, why it is needed, what already exists, and what rules apply.
In technical terms, context is any information placed in the prompt that is not the task itself and not the data being processed, but that changes how the task should be carried out.
Example Without context:
Suggest a database for my project. With context:
I am building an internal attendance tracker for a training institute with about 300 students. Three staff members will use it from one office. The backend is Django, I am the only developer, and I want minimal server maintenance.
Suggest a database for this project and explain why it fits. Example Explanation Without context, the model must answer for an imaginary average project. It may list several databases with general pros and cons, which is accurate but does not help you decide.
With context, the model knows:
The scale is small (300 students, 3 users). The framework is Django, which has built-in support for specific databases. The team is one developer. The priority is low maintenance. These facts make a specific recommendation possible. Notice that the instruction barely changed. The improvement came almost entirely from context.
Why Context Is Needed The Model Answers for the "Average" Situation A model has learned from text written for many audiences and situations. When your prompt does not describe your situation, the most likely response is one that fits a generic situation. Generic answers are rarely wrong, but they are often not useful.
The Same Instruction Has Different Correct Answers Consider the instruction: Explain what a REST API is.
Context What a Good Answer Looks Like A beginner's first week of web development Simple analogy, no HTTP details A job interview preparation session Precise definition, HTTP methods, statelessness, status codes A presentation to non-technical managers Business value, examples of integrations, no code Documentation for a team's internal API Reference to the team's actual endpoints and conventions All four answers are correct for their context and wrong for the others. Context tells the model which one you need.
Types of Context Context is not one kind of information. Recognizing the different types helps you notice what is missing from a prompt.
- Purpose Context What it describes: Why the output is needed and how it will be used.
This summary will be read aloud at the start of a 10-minute team meeting. Effect: The model favors spoken-style sentences, a short length, and the most important points first.
- Audience Context What it describes: Who will read or use the output, and what they already know.
The readers are second-year engineering students who know Python but have never used a web framework. Effect: Controls vocabulary, depth, and what can be assumed without explanation.
- Domain and Background Context What it describes: Facts about the subject area, business, or project that the model cannot know.
Our platform offers three plans: Free (2 courses), Pro (all courses), and Team (all courses plus a progress dashboard for managers). Effect: The model can give accurate, specific answers instead of guessing your business details.
- Technical Environment Context What it describes: Versions, tools, platforms, and limitations of the environment.
The project uses Django 5.1, Python 3.12, and PostgreSQL 16. It is deployed on a single Linux server without Docker. Effect: Prevents answers that use the wrong syntax, unavailable features, or tools you cannot use.
- State Context What it describes: What has already happened or been decided.
We already tried increasing the Gunicorn worker count from 2 to 4, and response times did not improve. Effect: Stops the model from suggesting solutions you have already tried and focuses it on new possibilities.
- Time Context What it describes: The current date or relevant time frame.
Today's date is 2026-10-23. Effect: Models do not reliably know the current date. Without it, phrases such as "last month", "this year", or "recent" may be interpreted using the model's training period instead of the actual present.
Quick Reference Type Key Question Purpose Why is this needed, and how will it be used? Audience Who will use it, and what do they know? Domain What facts about this subject or business are required? Technical environment What versions, tools, and limits apply? State What has already been tried or decided? Time What is the current date or relevant period? Not every prompt needs every type. Use the list as a checklist when a response is too generic or contains wrong assumptions.
How Context Changes the Response Lesson 2 explained that a model generates text by predicting likely next tokens. Context works by shifting those predictions toward responses that fit the described situation.
Demonstration Prompt A:
How should I store user passwords? Prompt B:
I am maintaining a legacy PHP 5.6 application that cannot be upgraded this year. Passwords are currently stored as unsalted MD5 hashes.
How should I store user passwords, and how can I migrate the existing ones? What changes:
Prompt A typically produces general advice: use a dedicated password-hashing algorithm, never store plain text. Prompt B makes the model address a specific situation: the PHP version, the insecure existing hashes, and the migration problem. The question how to migrate only makes sense because the state context (unsalted MD5) was provided. Context does not just add detail. It changes which problem the model solves.
How Much Context Should You Add? More context is not always better. Irrelevant context has real costs.
The Cost of Irrelevant Context Distraction: The model may give weight to details that do not matter. If you mention that your team uses a particular project management tool while asking about database indexing, the answer may unnecessarily mention that tool. Diluted instructions: Important requirements become harder to notice in a long prompt. Higher cost and slower responses: Every token of context is processed on every request. The Relevance Test For each piece of context, ask:
"Would a correct answer be different if this information were missing or different?"
If changing the fact would change the correct answer, include it. If the correct answer would be the same either way, leave it out. Example Question: "How do I add pagination to my Django REST Framework API?"
Context Item Changes the Answer? Include? Uses Django REST Framework Yes ✓ Wants page numbers rather than cursor-based pagination Yes ✓ Frontend is built with React Only if the question includes frontend handling Usually no Office is in Chennai No ✗ The developer likes clean code Does not change the specific answer ✗ Where to Place Context Short Prompts: Context Before the Instruction For most prompts, place context first. It frames the instruction that follows.
[Context] We sell online coding courses to beginners in India.
[Instruction] Write three subject lines for an email announcing a free Python workshop. Long Documents: Document First, Instruction Last When context includes a long document, place the document first and the instruction and question after it, as discussed in Lesson 2.
Applications: Stable Context in Instructions, Changing Context in Input In applications, separate context that stays the same from context that changes per request:
Context Type Example Where It Goes Stable Company description, rules, product plans System/developer instructions Per-user User's name, plan, progress Inserted into the prompt for each user Per-request Current date, the user's current page Inserted for each request Adding Context Dynamically in an Application In real applications, context often comes from a database or the current environment rather than being typed by hand. The program builds the prompt by inserting the relevant facts for each request.
Example: A Student Help Assistant Requirements: the openai package (pip install openai) and an OPENAI_API_KEY environment variable.
from datetime import date
from openai import OpenAI
client = OpenAI() MODEL = "gpt-5.4" # Replace with a model available in your account
PLATFORM_CONTEXT = """You answer questions from students on an online coding course platform.
Platform facts:
- Plans: Free (2 courses), Pro (all courses), Team (all courses plus a manager dashboard).
- Certificates are issued only after all modules and the final project are completed.
- Support hours: Monday to Friday, 10:00 to 18:00 IST.
If a question cannot be answered from these facts or the student details, say that you will forward it to the support team. Do not guess policies."""
def build_instructions(student: dict) -> str: student_context = ( f"Today's date: {date.today().isoformat()}\n" f"Student name: {student['name']}\n" f"Plan: {student['plan']}\n" f"Current course: {student['course']}\n" f"Progress: module {student['module']} of {student['total_modules']}" ) return f"{PLATFORM_CONTEXT}\n\nStudent details:\n{student_context}"
def answer_question(student: dict, question: str) -> str: response = client.responses.create( model=MODEL, instructions=build_instructions(student), input=question, ) return response.output_text
student = { "name": "Arun", "plan": "Free", "course": "Python Fundamentals", "module": 7, "total_modules": 10, }
print(answer_question(student, "When will I get my certificate?")) Possible output:
Hi Arun! You're on module 7 of 10 in Python Fundamentals. Certificates are issued once you've completed all modules and the final project, so you'll receive yours after finishing the remaining 3 modules and the final project. Code Explanation PLATFORM_CONTEXT Stable domain context that is the same for every student: plans, certificate rules, support hours. It also contains a rule for missing information, which tells the model to forward the question instead of inventing a policy.
build_instructions(student) Combines the stable context with per-student context. The student's plan and progress come from your database in a real application.
date.today().isoformat() Inserts the real current date. Without it, a question such as "Is support open today?" cannot be answered correctly, because the model does not know which day it is.
Why the answer is specific: The model can mention that Arun has 3 modules left only because the progress data is in the context. Without it, the answer would be a general description of the certificate policy.
What Happens If You Remove Parts of the Context Removed Effect Platform facts The model invents a certificate policy that may not match yours Student progress The answer becomes generic: "complete all modules to get a certificate" Today's date Time-based questions ("Is support open now?") become unreliable Missing-information rule Unanswerable questions get guessed answers Warning: Only include personal data that the task actually requires. Every piece of data in a prompt is sent to the model provider and may appear in logs. Do not include passwords, API keys, full payment details, or unnecessary personal information in prompts.
Context Versus Training Knowledge A model already has general knowledge from its training. Context is needed when:
The information is specific to you: your company, project, users, decisions. The information is newer than the model's training data: a new library version, recent prices, current events. The information is niche: rarely written about, so the model's knowledge is weak or unreliable. You need the model to use a particular version of the facts: your company's definition of a term, your team's coding standard. When you provide context that conflicts with the model's general knowledge, say explicitly which should be used:
Use the pricing in the table below, even if it differs from information you may have seen elsewhere. Providing large amounts of reference information automatically, such as searching a document collection and inserting the relevant parts, is called Retrieval-Augmented Generation (RAG), covered in its own section later in this series.
Common Mistakes Mistake 1: Providing No Context for Situation-Dependent Questions What it is: Asking "What is the best framework?" or "How should I structure this?" without describing the project.
Why it happens: The situation is clear in your mind.
What it causes: Generic "it depends" answers, or a confident recommendation suited to someone else's project.
How to fix it: Use the six context types as a checklist and add the ones that change the correct answer.
Mistake 2: Adding Irrelevant Context What it is: Including background details that do not affect the task.
Why it happens: The belief that more information always helps.
What it causes: Responses that mention irrelevant details, and important instructions that get less attention.
How to fix it: Apply the relevance test to each piece of context.
Mistake 3: Leaving Out Versions and Environment What it is: Asking technical questions without mentioning versions or the platform.
Why it happens: Versions seem like minor details.
What it causes: Code that uses removed features, syntax from a different version, or tools that are not available in your environment.
How to fix it: State language, framework, and major tool versions for any technical task where behavior differs across versions.
Mistake 4: Contradictory or Outdated Context What it is: Context that conflicts with itself or describes an old state, especially in reusable prompts or long chats.
Why it happens: Prompts are edited over time, and old context is not removed.
What it causes: Inconsistent answers, because the model receives two versions of the truth.
How to fix it: Keep stable context in one maintained place, review it when facts change, and start a new conversation when a chat contains outdated information.
Mistake 5: Not Providing the Current Date What it is: Asking time-relative questions ("this year", "last week", "is it still valid?") without giving the date.
Why it happens: People assume the model knows the date.
What it causes: Answers based on the wrong time period.
How to fix it: Include the current date as context whenever time matters.
Best Practices Describe the situation before the task. Context frames the instruction, so it should usually come first.
Include context that changes the correct answer. Apply the relevance test and remove everything else.
State technical versions explicitly. Language, framework, and database versions affect syntax and available features.
Include what has already been tried. State context prevents repeated suggestions and saves iterations.
Insert the current date for time-dependent tasks. Generate it in code for applications.
Separate stable context from per-request context. Maintain stable context in one place; insert per-user and per-request context programmatically.
Tell the model what to do when the context is insufficient. A forwarding or "not specified" rule prevents invented facts.
Minimize sensitive data. Include only the personal or business information the task needs, and never include secrets.
Try It Yourself Exercise 1: Add Context in Layers Start with:
How should I deploy my web application? Add one type of context at a time and run the prompt after each addition:
Technical environment (framework, language, database) Audience and team (who will maintain it, their experience) Purpose and scale (expected traffic, budget) State (what you already tried or already have) Note how the answer changes from generic options to a specific plan.
Exercise 2: Apply the Relevance Test Here is a prompt with mixed context:
I live in Coimbatore and I like working at night. I use VS Code with a dark theme. My Flask app uses SQLAlchemy 2.0 with PostgreSQL. A query that loads users with their orders is very slow and I can see many separate SELECT statements in the logs.
How can I make this query faster? Identify which context items change the correct answer, remove the rest, and compare the two versions. (Hint: the many separate SELECT statements are the most important clue.)
Exercise 3: Build Dynamic Context Modify the Python example in this lesson so that the student dictionary includes a last_login date. Then ask the assistant, "Have I been inactive for a long time?" and observe how the answer depends on both last_login and today's date being present in the context.