Search⌘ K
AI Features

From Prompt Engineering to Context Engineering

Explore how context engineering extends prompt engineering by controlling the information an AI agent receives at inference time. You’ll identify the eight parts of context, diagnose retrieval and state problems, decide when a prompt is enough, and prepare to build more reliable, efficient on-call assistants.

In this course, we’ll build an on-call assistant: an AI agent that helps engineers handle production incidents. It searches the team’s runbooks and past incident reports, checks recent deploys and metrics through tools, and cites the documents behind every step it suggests. Here’s a typical question an engineer might ask it in the middle of an incident:

“Checkout latency spiked at 14:05. What changed, and what should we roll back?”

When the assistant is small, with a few runbooks and one tool, a well-written system prompt is enough to answer this question well. As it grows, though, it searches thousands of documents, calls several tools, works through long incident threads, and has to remember what the team already tried. At that point, the prompt alone can’t keep the answers accurate, and we need context engineering. In this lesson, we’ll see what context engineering is and when we need it.

Does a great prompt scale?

To see why a prompt alone isn’t enough, let’s look at what the model actually receives when it answers our question. The model doesn’t see only our prompt. It sees everything we send in the request, and all of that together is called its context. For the on-call assistant, the context has eight parts:

  • Instructions: The system prompt telling it to answer only from runbooks and cite every step.

  • Tool schemas: The definitions of the tools it can call: document search, the deploy log, and metrics.

  • Few-shot examples: One or two sample answers showing the step-by-step, cited format we want.

  • Retrieved records: The runbooks and postmortems that search just returned.

  • Conversation history: The recent messages in the incident thread, including how the question was first asked.

  • Scratchpads: A running note of which runbooks were already checked during this incident.

  • User preferences: A stored rule that this engineer wants copy-ready commands with each step.

  • API outputs: The raw JSON the deploy-log tool just handed back, before anyone trims it.

We write the instructions once, and they stay the same on every call. The other seven parts change from call to call, depending on what the search returns, what the user said earlier, and what the tools send back. A prompt written in advance can’t control any of these. So as the assistant grows, the prompt alone can’t keep all eight parts right.

What is context engineering, concretely?

Context engineering is the work of deciding what goes into the model’s context on each call. A helpful way to picture it is to think of the model as a computer’s processor (CPU) and its context window as the computer’s memory (RAM). The CPU can only work with what’s loaded into RAM right now. In the same way, the model can only reason about what’s in its context window. Andrej Karpathy describes context engineering as “the delicate art and science of filling the context window with just the right information for the next step.”

On a computer, the operating system decides which programs and data sit in RAM at any moment. Context engineering does the same job for the model: on every call, it decides what gets loaded into the context window and what stays out.

The CPU and RAM analogy: context engineering decides what loads into memory.
The CPU and RAM analogy: context engineering decides what loads into memory.

Note: The analogy has one important difference. On a computer, the operating system manages RAM for you. In an LLM application, nothing does this automatically. You, or the framework you use, have to decide what goes into the window on every call.

This also shows how context engineering relates to prompt engineering. Prompt engineering is about writing good instructions: we write the system prompt once and reuse it. Context engineering covers everything the model sees: on every call, it decides which of the eight parts to include, how to format them, and in what order.

Eight components assembling into what the model sees for one call.
Eight components assembling into what the model sees for one call.

Isn’t this just better prompting?

It’s tempting to think a better prompt can fix any problem. Let’s test that idea with the on-call assistant. We’ll ask it the same latency question twice: first when its document store holds only 3 notes, and again after the team imports all of its runbooks and postmortems, so the store holds 30,000.

With 3 notes in the store, the eight parts look like this:

  • Instructions: Answer only from runbooks and cite every step.

  • Tool schemas: The search, deploy-log, and metrics tool definitions.

  • Examples: One or two sample answers.

  • Retrieved records: All 3 notes come back, including the current checkout runbook.

  • Conversation history: Two short messages: the question and one follow-up.

  • Scratchpad: A line noting that note 2 and note 3 were already checked.

  • User preference: Copy-ready commands.

  • API output: A small JSON object listing the last 3 deploys.

After the import, the instructions, the tool schemas, and the examples stay exactly the same. Everything else changes:

  • Retrieved records: Search now picks a handful of notes out of 30,000. One of them is the old 2024 checkout runbook, which says to restart the Redis cache first. It sits right next to the current runbook, which says to turn off the newest feature flag first.

  • Scratchpad and conversation history: Both are longer. If many follow-up messages bury the original question, the model may answer the wrong part of it.

  • API outputs: The deploy-log JSON now lists hundreds of deploys and has to be trimmed before the model reads it.

How a crowded retrieval box leads the engine to cite the stale duplicate note.
How a crowded retrieval box leads the engine to cite the stale duplicate note.

Now suppose the assistant follows the old runbook and tells the engineer to restart the cache. In the middle of an incident, that empties the cache and can double the load on the database. Our first instinct might be to improve the system prompt, for example by adding “Always double-check your sources.” The answer stays the same. The problem is in retrieval: search put the old runbook in front of the model, and the model used it. No instruction can remove a document that’s already in the context.

The fix belongs in the retrieved records: we need to remove the old runbook, or rank it lower, before the model ever sees it.

Let’s practice this kind of diagnosis. Match each symptom below to the part of the context where the problem actually is.

Where Does the Fault Live?
Symptom

A 40 KB search response fills most of the call with fields nobody reads

After a long follow-up chat, the assistant answers a side question instead of the original one

The answer tells the engineer to restart the cache, a step from the 2024 runbook

The engineer asked for copy-ready commands last week but gets long prose today

Context part

Retrieved records

API outputs

Conversation history

User preferences

Draw a line from each symptom to the part of the context where the problem actually is.

Do you find this helpful?

Notice that rewording the instructions wouldn’t fix any of these. Each problem lives in one of the seven parts that change on every call.

When is a prompt enough?

Not every task needs context engineering, and a simple rule helps us decide. If a task is a single question with no tools, no retrieval, and nothing to remember afterward, a good prompt is enough. As soon as any of the following appears, we need context engineering:

  • Multiple turns: The conversation continues past one call.

  • A tool whose result changes: The same tool returns different data on a later call.

  • Retrieved documents that change: What search returns shifts as the documents change.

  • State that must persist: Something has to be remembered after this call.

The on-call assistant has all four. It searches documents, calls tools, works through long incident threads, and remembers what the team already tried. So it needs context engineering from the very first version.

Note: A one-off question with no documents and no tools really is just prompt engineering. That changes as soon as a second message or a first document arrives. The rule is about what each call needs, whatever the project is called.

Let’s apply the rule to three quick scenarios.

Prompting or Context Engineering?

Q1. An engineer asks the on-call assistant one question while its document store is empty. No tools run, and nothing needs to persist afterward. What does this call need?

Do you find this helpful?

What will the finished assistant do?

By the end of this course, our on-call assistant will answer the same latency question with much more behind it. It will:

  • Search runbooks, postmortems, and deploy data using keyword, semantic, and structured search together.

  • Order the most useful passages and quote them back, so the model cites the right sources.

  • Split big tasks across helper agents, each with its own small, clean context.

  • Remember what the team learned across sessions, with a record of where each memory came from and a way to undo bad ones.

  • Check its own answers with automated tests before an engineer acts on them.

A version built on a prompt alone has none of this. Its accuracy and cost depend entirely on whatever lands in a single call, which is exactly the problem we saw with the old runbook.

How is the course organized?

The course has eight chapters. Each one adds a new skill to the on-call assistant and builds on the chapters before it. For example, we first learn to diagnose why a context goes wrong (Chapter 2) before we improve retrieval (Chapter 4). The map below shows every chapter and its lessons.

Course structure

Who is this course for?

This course is for engineers who build with LLMs in backend, platform, or AI and ML roles. It’s especially useful if your answers get worse as contexts grow longer, your token costs keep rising, or your tool calls are unreliable. It also helps if you’re preparing for interviews on designing AI agents.

You should be comfortable with the following:

  • Python: Functions, classes, JSON serialization, file I/O, and subprocess calls.

  • LLM APIs: Calling a chat completion API with JSON-schema tool calling.

  • Retrieval basics: What vector embeddings and a RAG pipeline are.

We won’t cover basic prompting or explain what a chat completion call is.

How will you run the code?

All the code in this course runs in the browser, so there’s nothing to install. We call models through LiteLLM, a library that lets the same completion() call work with many providers. By default, the code uses gemini/gemini-2.5-flash, which works with a free Gemini API key. If you’d rather use a model you already pay for, set the LLM_MODEL environment variable to any LiteLLM model name, such as openai/gpt-4o-mini or anthropic/claude-haiku-4-5, and add that provider’s API key.

Can you assemble the context yourself?

Now it’s your turn. Below, you’ll get the on-call assistant’s question and the material available for one call, including several runbooks and postmortems. One of them is the old 2024 runbook. Decide what should go into the context and what should stay out.

AI Powered
Saved
Reset
Try the curation job yourself: decide what belongs in the context for this one call.

Ed, your AI tutor, will review your plan for the on-call assistant's checkout latency question and guide you with hints if needed.

Hi, I'm Ed! An engineer asks the on-call assistant: “Checkout latency spiked at 14:05. What changed, and what should we roll back?” Search returned the current checkout runbook, the 2024 version of the same runbook, a March postmortem, the search-latency runbook, and the on-call rotation note. The deploy-log tool returned 6,000 tokens of raw JSON, and the incident thread has 14 messages. What would you put into the context for this call, and what would you leave out?

Decide what goes into the context for one incident call

Deciding what belongs in the context for a single call, and explaining why the old runbook stays out, is the core skill of context engineering. We’ll practice it throughout the course.

What’s next?

In this lesson, we learned what context engineering is, looked at the eight parts of a model’s context, and used a simple rule to decide when a task needs more than a prompt. In the next lesson, we’ll measure how much room the context window really has and see why the cost of filling it grows much faster than its size.