Skip to main content
11

Context engineering vs prompt engineering, and memory

MediumContext engineeringMemoryAgents

What is context engineering, how is it different from prompt engineering, and how do you give an LLM app memory?

TL;DR

Prompt engineering is writing good instructions. Context engineering is the broader job of deciding everything that goes into the context window on each call: system instructions, conversation history, retrieved documents, tool definitions and tool results, user profile and memory, and the output format, all within a token budget. It matters most for agents, where the context grows with every step and quality drops as it fills with noise. LLMs are stateless, so memory is something you build: short-term (the current conversation, trimmed or summarized) and long-term (facts and preferences stored outside the model and retrieved when relevant).

Key points

  • More context isn't better. Irrelevant tokens distract the model, raise cost and latency, and bury the important parts ("context rot").
  • Techniques:
    • Select: retrieve only relevant docs, memories and tools (don't expose 80 tools when 5 are relevant).
    • Compress: summarize old turns and long tool outputs; keep key facts.
    • Isolate: give sub-agents their own clean context and return only their conclusions.
    • Order: stable content first (good for prompt caching), the current task last.
  • Long-term memory: extract facts ("prefers Python", "works at a fintech") into a store, retrieve them by relevance, and let users view and delete them (privacy).
  • Agent scratchpads: let a long-running agent write notes or a plan to a file and re-read them, instead of keeping everything in context.

Example

text
Context for step 12 of a coding agent:
  system rules (stable, cached)
+ relevant memories: "repo uses pnpm; tests run with vitest"
+ summary of steps 1–10 (300 tokens instead of 20,000)
+ full output of step 11 only
+ current task: "fix the failing test in auth.test.ts"

Common follow-ups

  • How do you stop a chat from overflowing the window? Keep a rolling summary plus the last N turns, and retrieve older details on demand.
  • What should not be stored as memory? Sensitive data the user didn't agree to store, or one-off details that will mislead later conversations.

What interviewers look for

Tip

Context as a scarce budget you curate, concrete techniques (select, compress, isolate), and memory as an explicit system you design.

  • Red flag: "just put the whole history and every document in the context."

Was this helpful?

Related questions