What Is Context Engineering? Going Beyond Prompts
Context engineering decides everything a model sees: instructions, history, documents and tool results. Learn how it differs from prompt engineering, with a runnable example.
Short answer: Context engineering is the practice of deciding everything a language model sees when it answers: instructions, conversation history, retrieved documents, tool results and examples, and how they are selected, ordered and trimmed to fit. Prompt engineering focuses on wording one request; context engineering designs the whole flow of information around it.
What is context engineering, and why did the term appear?
Context engineering is a name for work that AI app builders were already doing. A model only knows two things when it answers: what it learned in training, and the text in front of it right now, called the context. In a simple chat the context is mostly your message. In a real app it is assembled by code from many sources, and the quality of that assembly often matters more than the wording of any single instruction. As apps and agents grew more complex, people started calling this assembly work context engineering.
How is context engineering different from prompt engineering?
Context engineering includes prompt engineering but covers a wider area. Prompt engineering asks "how should I phrase this?". Context engineering asks "what should the model see at this moment, and what should it not see?"
Prompt engineering | Context engineering | |
|---|---|---|
Main question | How do I word the instruction? | What information goes in, in what order? |
Unit of work | One prompt | The whole context, rebuilt for every model call |
Typical tools | Clear wording, examples, format rules | Retrieval, history trimming, summaries, tool results, budgets |
Who does it | Anyone using AI | Mostly people building AI apps and agents |
What goes into a model's context?
Instructions: usually a system prompt with the role, rules and output format.
The user's request for this turn.
Conversation history: earlier messages, in full or summarised.
Retrieved knowledge: passages found by search or RAG.
Tools: descriptions of the tools the model may call, and the results of earlier calls.
Examples and memory: sample answers, saved user preferences or the state of a task.
Why does context engineering matter?
Context engineering matters because the context is limited and every part of it competes for attention. A context window holds only so many tokens, and more is not automatically better: irrelevant text can distract the model, details buried deep in a long context can be overlooked, and every extra token adds cost and waiting time. Missing context is the opposite problem: without the right document, the model guesses, which leads to hallucinations.
What does context engineering look like in code?
This small script, which runs as-is, shows the core decisions. It keeps the rules and the question, adds only relevant documents (score at least 0.5, best first), then adds as much recent chat as fits a word budget. Real apps count tokens instead of words, but the logic is the same.
def words(text):
return len(text.split()) # rough stand-in for a token count
def build_context(system, question, docs, history, budget=60):
"""Choose what the model sees: rules and question first, then the best docs, then recent chat."""
used = words(system) + words(question)
chosen_docs, chosen_turns = [], []
for doc in sorted(docs, key=lambda d: d["score"], reverse=True):
if doc["score"] >= 0.5 and used + words(doc["text"]) <= budget:
chosen_docs.append(doc["text"])
used += words(doc["text"])
for turn in reversed(history): # newest turn first
if used + words(turn) > budget:
break
chosen_turns.insert(0, turn)
used += words(turn)
context = "\n".join(
[f"RULES: {system}"]
+ [f"DOC: {d}" for d in chosen_docs]
+ [f"CHAT: {t}" for t in chosen_turns]
+ [f"QUESTION: {question}"]
)
return context, used
docs = [
{"text": "Refunds are allowed within 14 days of purchase with a receipt.", "score": 0.91},
{"text": "Our office is closed on public holidays.", "score": 0.32},
{"text": "Refunds go back to the original payment method within 5 working days.", "score": 0.84},
]
history = [
"User: Hi, I bought headphones last week.",
"Assistant: Thanks! How can I help with them?",
"User: They stopped working.",
]
context, used = build_context(
system="Answer only from the DOC lines. If they do not cover it, say so.",
question="Can I get my money back?",
docs=docs,
history=history,
)
print(context)
print("words used:", used)Running it prints:
RULES: Answer only from the DOC lines. If they do not cover it, say so.
DOC: Refunds are allowed within 14 days of purchase with a receipt.
DOC: Refunds go back to the original payment method within 5 working days.
CHAT: Assistant: Thanks! How can I help with them?
CHAT: User: They stopped working.
QUESTION: Can I get my money back?
words used: 55Notice what was left out: the office-hours document scored too low to be relevant, and the oldest chat turn did not fit the budget. Those two choices are context engineering. Changing the score threshold or the budget changes what the model can know.
What are the main context engineering techniques?
Technique | What it means | Example |
|---|---|---|
Select | Include only what helps this step | Retrieve the top passages, not the whole manual |
Order | Put things where the model uses them well | Rules first, the question clearly at the end |
Compress | Shrink what is still needed | Summarise old chat turns into a few lines |
Isolate | Give separate sub-tasks separate contexts | A research step whose notes, not raw pages, go to the main step |
Label and separate | Mark where each part comes from | Tag documents as DOC and keep untrusted text apart from instructions, to limit prompt injection |
The free chapter on context assembly and trust boundaries builds these steps into a working app.
Common mistakes
Stuffing the whole document set or the full chat history into every call.
Rewriting the instructions endlessly when the real problem is a missing document.
Mixing untrusted user or web text with instructions, without labels.
Never logging the final context, so you cannot see what the model actually received.
Frequently Asked Questions
Is context engineering replacing prompt engineering?
No. Context engineering includes prompt engineering: clear instructions are still one part of the context. The term simply widens the focus to everything else the model sees.
Is a bigger context window enough on its own?
No. A bigger context window lets you include more, but irrelevant or badly ordered text can still lower answer quality and raises cost. Choosing what to include still matters.
Why is context engineering important for AI agents?
AI agents call a model many times in a loop, and each call adds tool results and notes to the context. Without trimming and summarising, the context fills with old details and the agent loses track of its goal.
Can I practise context engineering without writing code?
Yes. In any chat assistant you can choose which documents to paste, start a fresh chat when the old one is cluttered, and summarise long discussions yourself before continuing.
Key Takeaway
Context engineering decides what a model sees: instructions, history, documents and tool results, selected and ordered within a budget. Good wording still matters, but the right context usually matters more.
Ready to start? You will learn advanced prompting and context techniques, including long-context testing, reasoning controls and evaluation. Begin with the free course Prompt Engineering for AI: Advanced.
Related articles
More in Prompt Engineering
How to Write Prompts for AI Image Generation
A practical guide to AI image prompts: the elements to include, a before-and-after example, how to refine results one change at a time, and the limits and responsible-use rules to know.
4 min readNov 19, 2026Writing Your CV and Cover Letter With ChatGPT
How to use ChatGPT to improve your CV and draft a cover letter without inventing facts: working prompts, a safety checklist and common mistakes.
4 min readNov 16, 2026What Is Prompt Injection?
Prompt injection tricks an AI app into following an attacker's instructions. Learn the types, why it is dangerous and the layers of defence that reduce the risk.
4 min readNov 12, 2026What Is a System Prompt and Why Does It Matter?
A system prompt is the standing instruction that sets an AI model's role, rules and format. Learn how it differs from a user prompt, what to put in it and its limits.
3 min readNov 10, 2026Editing Photos With ChatGPT: Prompts That Work and Their Limits
Copy-ready ChatGPT prompts for photo editing, a simple formula for writing your own, and an honest look at what ChatGPT cannot do well.
3 min readNov 6, 202610 Reusable ChatGPT Prompt Templates
Ten fill-in-the-blank ChatGPT prompt templates you can copy and paste, plus the five-part pattern (role, task, context, format, constraints) that makes any prompt work better.
5 min readNov 2, 2026
Comments
Sign in to join the discussion.



