CertBrain
CoursesBlog
Sign in
← Back to decks
Sign in to save your progress, vote, and build your own decks.Sign in

Context Engineering

A foundation model only knows two things when it answers: what it learned in training, and what is in the request in front of it. Context engineering is the practice of deciding what goes into that request, so the model has the right information, in the right amount, at the right time. This lesson…

7 cards
Study this deck

Context Engineering

A foundation model only knows two things when it answers: what it learned in training, and what is in the request in front of it. Context engineering is the practice of deciding what goes into that request, so the model has the right information, in the right amount, at the right time. This lesson explains what fills the context window, why the window is a limited resource, and how good applications manage it.

What the context window holds

The context window is the total number of tokens a model can work with in one request; each model has its own size, and on many models the response shares the same window as the input. Everything the model uses to answer must fit inside it:

  • Instructions: the system prompt that sets the model's role, rules and output format.
  • Retrieved data: relevant chunks pulled from documents or databases, as in Retrieval Augmented Generation (RAG).
  • Conversation history: earlier turns, so the model can follow a dialogue.
  • Tool results: data returned when an application or agent calls an API or runs a search.
  • The user's request: the actual question or task.

Prompt engineering focuses on wording the instructions well. Context engineering is broader: it decides which of all this material to include at all.

Why the window is limited

Models are stateless. They don't remember earlier requests on their own, so anything the model should know must be sent again each time. But the window has a fixed maximum size, every token sent costs money and adds processing time, and stuffing in loosely related material can bury the important details and make answers worse. More context isn't automatically better context.

Managing context well

TechniqueWhat it does
Retrieve selectivelySend only the chunks relevant to this question, not whole documents
Summarize historyReplace long past conversations with a short summary
Use memory storesSave key facts and preferences outside the model and bring back only what's needed
Order deliberatelyPut stable instructions first and the specific request last
Trim tool outputPass the model the parts of an API result it needs, not the raw dump

On AWS, Amazon Bedrock AgentCore Memory gives agents short-term memory, which keeps track of turns within one session, and long-term memory, which extracts facts and preferences that carry across sessions. Bedrock Knowledge Bases handles selective retrieval.

Context engineering and agents

Agents make context engineering more important. An agent may run many steps, calling tools and collecting results as it goes, and each step adds to its context. Without care, the window fills with stale or irrelevant material, costs rise and the agent loses track of its goal. Well-designed agents keep their working context lean, store what they may need later in memory, and pull it back only when relevant.

On the exam

If a model "forgets" what a user said in an earlier session, the fix is a memory store, not a bigger model. If answers degrade after a long conversation or after adding lots of documents, the problem is too much or poorly chosen context, and the fix is selective retrieval or summarizing. If cost rises with conversation length, remember that history is resent on every turn.

Quiz me on this
A travel agent assistant built on a foundation model forgets a customer's seating preference as soon as they start a new chat session the next day. Which approach BEST solves this?
  1. Switch to a model with a larger context window
  2. Raise the temperature so the model recalls more details
  3. ✓ Save the preference in long-term memory and supply it in later sessions
  4. Fine-tune the model on every customer's past conversations each night so it learns them
A team building a policy assistant pastes the full 300-page employee handbook into every prompt. Answers have become slower, more expensive and less accurate. What is the BEST change?
  1. ✓ Retrieve only the handbook sections relevant to each question
  2. Add a second copy of the handbook so the model sees every policy twice
  3. Ask the model to answer in more words to be thorough
  4. Remove the system instructions to make room for the handbook
An agent that handles multi-step expense approvals calls several tools per request. Halfway through long tasks it starts repeating steps and losing track of the goal, and its token costs climb each step. Which approach addresses the root cause?
  1. Switch to a model with more parameters
  2. Lower the maximum output length to a few words
  3. Disable all of the tools so the agent works only from its own built-in knowledge
  4. ✓ Keep only relevant tool results in context; store the rest in memory
Context engineering
Building each request from budgeted parts (instructions, history, retrieved knowledge, tool schemas), since a model only knows what that request includes.
Overstuffed vs too little context
Too much context makes each request slower and costlier; too little leaves the model guessing and invites errors. Include only what the task needs.
Stateless model
A model keeps nothing between requests, so anything it needs to know must be sent in the context each time.
Short-term vs long-term memory (AgentCore Memory)
Short-term tracks turns within one session; long-term keeps facts and preferences across sessions.