LESSON 18 Β· TOKENS & CONTEXT

Why Context Matters

Context is the information you give an LLM for a particular task. Learn why the same question can produce different answers when the context changes, and how to build focused context for reliable AI applications.

20 min readβ€’Beginnerβ€’Generative AI

By the end of this lesson, you will be able to:

  • Explain why context can change an LLM answer.
  • Compare the same question with and without useful context.
  • Distinguish relevant context from irrelevant context.
  • Build a simple Python context builder.
  • Test and debug an AI application by changing its context.
1

The Same Question Can Have Different Answers

An LLM does not answer a question in isolation. The instructions, documents, conversation history, and other information you place in the request can change what the model should say.

WITHOUT CONTEXT

Question

β€œCan I work from home on Friday?”

Possible answerIt depends on your company's policy.
WITH CONTEXT

Same question + policy

β€œRemote work is allowed on Fridays for employees who are not scheduled for an on-site shift.”

Possible answerYes, if you are not scheduled for an on-site shift.
Core idea:The question stayed the same. The useful evidence changed. Better context gives the model information it can use to produce a more grounded answer.
2

Context Is the Model's Working Information

Think of the model as an analyst who needs the right evidence on the desk before making a decision. Your application decides what evidence to place there.

QuestionWhat does the user want?
+
Relevant contextWhat evidence helps?
+
InstructionsHow should it respond?
β†’
LLMGenerate an answer

Important: Context is not just β€œmore text.” It is selected information intended to help the model perform the current task.

3

Experiment: Change the Context

Here is a small experiment you can reproduce with an LLM API. The application asks the same question twice but supplies different company-policy context.

</> Python
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
question = "Can I work from home on Friday?"

def ask(policy):
    response = client.responses.create(
        model="gpt-5-mini",
        input=[
            {"role": "system", "content": "Answer using only the supplied policy."},
            {"role": "user", "content": f"Policy:
{policy}

Question: {question}"},
        ],
    )
    return response.output_text

policy_a = "Remote work is allowed on Fridays."
policy_b = "Remote work is not allowed on Fridays."

print("Context A:", ask(policy_a))
print("Context B:", ask(policy_b))

What to observe: the user question does not change. The policy context changes, so the evidence available to the model changes. Keep API keys in environment variables and use the model/API version available in your account.

4

Relevant Context Beats Irrelevant Context

Suppose the user asks about parental leave. Adding the company's laptop policy, cafeteria menu, and office parking rules makes the prompt longer without making the answer better.

RELEVANT

Keep

  • Parental leave policy
  • Eligibility rules
  • Employee location if the policy differs by country
  • Current question
IRRELEVANT

Remove

  • Laptop replacement policy
  • Office parking rules
  • Cafeteria hours
  • Unrelated old announcements
Rule of thumb:Before adding a piece of context, ask: β€œCan this information change or support the answer to the current question?” If not, it probably does not belong in the working context.
5

Context Quality Has Four Dimensions

RelevantIt relates directly to the task.
AccurateIt reflects the current source of truth.
ClearFacts and instructions are easy to interpret.
ConsistentConflicting facts are minimized or resolved.

Imagine two retrieved documents: one says β€œReturns accepted within 30 days,” while an old document says β€œReturns accepted within 14 days.” Simply putting both into the prompt can create ambiguity. Retrieval and application logic should prefer the current authoritative policy.

6

Build a Simple Context Builder

A context builder turns application data into a small, structured package for the model. Start simple: select the relevant records, label them, and add the user's question.

</> Python
def build_context(question, policy, employee_type):
    return f"""Company Policy

Employee type: {employee_type}
Policy:
{policy}

Employee question:
{question}
"""

context = build_context(
    question="How many parental leave days do I have?",
    policy="Eligible employees receive 16 weeks of parental leave.",
    employee_type="full-time",
)

print(context)

In a production system, the policy value could come from a database, search result, or RAG retriever. The important design step is selecting useful evidence before calling the model.

7

Context Can Be Changed Deliberately

Changing context is not only a debugging technique. It is how many AI applications adapt to different users, products, policies, permissions, and tasks.

USER ACountry: UK

Retrieve the UK policy.

β†’
CONTEXTUK evidence

Add the relevant policy.

β†’
LLMAnswer

Use the supplied evidence.

USER BCountry: India

Retrieve the India policy.

β†’
CONTEXTIndia evidence

Replace the relevant evidence.

β†’
LLMAnswer

Answer from the new evidence.

Key idea: the application controls context selection. The model does not automatically know which internal policy, database row, or private document your application wants it to use.

8

Break the Context on Purpose

A useful engineering exercise is to intentionally remove or corrupt the evidence and observe the result. Then restore the correct context.

WORKING

Correct context

β€œParental leave: 16 weeks for eligible full-time employees.”

Expected: grounded answer.
BROKEN

Wrong context

β€œLaptop policy: employees receive a replacement every 3 years.”

Expected: insufficient evidence.
FIXED

Restore context

Retrieve the current parental-leave policy again.

Expected: grounded answer returns.

This kind of failure test helps you distinguish a model problem from a context-selection problem. If the correct evidence is missing, changing the prompt wording alone may not solve the real issue.

9

Mini-Project: Company Policy Assistant

Build a small assistant that answers employee questions from a set of policy documents. Start with a few local Python strings, then replace the manual selection with retrieval as you learn RAG.

1. Store policiesKeep HR policies as structured text records.
2. Receive questionExample: β€œHow much parental leave is available?”
3. Select evidenceChoose only the policy sections relevant to the question.
4. Build contextLabel the evidence and include the question.
5. Call the LLMAsk it to answer from the supplied evidence.
6. Test failuresRemove, replace, and restore the context.
Success criteria:The assistant should give different answers when you intentionally change the supplied policy, and it should avoid inventing a policy when the relevant evidence is missing.
10

Context Is a Design Decision

Reliable LLM applications are not built by sending every piece of available information to the model. They are built by deciding what information the model needs for the task.

QUESTIONWhat is being asked?
EVIDENCEWhat facts support the answer?
FILTERWhat can be removed?
STRUCTUREHow should the evidence be presented?
VERIFYDid the answer use the right context?
PRACTICE

Context Experiment

Create a question and two policy contexts that give opposite answers. Run the same LLM function with each context. Then replace both with an irrelevant policy and see what happens.

βœ“ same questionβœ“ context Aβœ“ context Bβœ“ irrelevant contextβœ“ compare outputs
QUICK QUIZ

Test yourself

Why can the same question produce different answers?

30-Second Recap

  • The same question can produce different answers when the supplied context changes.
  • Good context is relevant, accurate, clear, and consistent.
  • More context is not automatically better; irrelevant information can add noise.
  • A simple context builder can select evidence, label it, and include the current question.
  • RAG systems use retrieval to select useful evidence from a much larger knowledge base.
  • Intentionally breaking and restoring context is a practical way to debug AI applications.