Skip to content
AI Engineering
Prompting & Context

Compaction, isolation and long context

When a task outgrows the window you have three tools: shrink what is there, split the work across contexts, or use a bigger window carefully.

Chapter 4: Prompting & ContextLesson 8 of 8Level: CoreAbout 12 min
Words on this page (8)
tool
: A function the model can ask your program to run, described by a name, description and argument schema.
compaction
: Summarizing older conversation so a long task fits in the context window.
subagent
: A helper agent started by another agent for a focused job, with its own context.
agent
: A model run in a loop that chooses its own tools and steps until the task is done.
token
: A piece of text the model reads and writes, usually a word or part of a word. About 4 characters of English on average.
prompt
: The input text you give a model, including instructions, examples and data.
retrieval
: Finding the most relevant pieces of text for a query.
logarithm
: The power you raise a base to in order to get a number. log base 2 of 8 is 3 because 2 to the power 3 is 8.

Compaction

Summarize the older part of the conversation into a short note and continue from there. Keep decisions, open questions, file paths and failures. Drop raw output. The risk is losing a detail you later need, so write important facts to a file as well. A reset, which starts a fresh context with a written handoff, avoids carrying accumulated confusion.

Isolation

Give each subtask its own context. A that reads fifty files returns a three-line summary, keeping the main context small. Sharing context across several is harder: pass explicit summaries and file references rather than full transcripts.

Long context

Windows of hundreds of thousands of exist, but longer inputs cost more, slow responses and can degrade accuracy. Use them for tasks that truly need many documents at once. caching cuts the cost of a repeated long prefix. is usually better than pasting everything.

A worked example: a handoff note

After two hours the agent writes: "Goal: fix login bug. Done: found cause in auth.js line 42 (token expiry). Tried: extending expiry (broke tests). Next: add refresh logic. Files: auth.js, auth.test.js. Do not touch: billing." The next context starts with only this note. It is about 60 tokens instead of 80,000.

Isolation in practice

Ask a subagent to "find every place the config is read and report file names". It reads fifty files and returns ten lines. Your main context stays tidy. For work that depends on shared decisions, isolation is harder, so keep tasks that must agree in one context.

Writing a good compaction, step by step

works best as a routine, not an emergency.

  1. Stop at a clean point, such as after a test run, not in the middle of an edit.
  2. Write down the goal, what is done, what was tried and failed, what is next, and the key files.
  3. Save the note to a file as well as the context.
  4. Start the new context with only that note.
  5. Ask the new context what it will do next and compare it with your plan. If they differ, the note is missing something.

A second example: contract question, paste or retrieve

You need an answer from a very long contract. Pasting it all costs more, slows the reply and can hurt accuracy. Retrieving the three relevant sections is cheaper and usually better. But if the task is to find contradictions between any two clauses, you cannot know in advance which parts matter. Here a long context is the right tool, and prompt caching helps if you ask many questions about the same text.

What goes wrong

After a poor compaction, an agent redoes finished work because the note lacked a "done" list, or retries an approach that already failed because the note lacked a "tried" list. To confirm, ask the fresh context what has been decided and compare it with your . With isolation, the failure is conflicting choices: two subagents invent different names for the same function, and the merge breaks. Keep one context in charge of shared decisions.

Common mistake

Summaries that lose the one detail you need later. Save critical facts to a file as well.

Check yourself

What should a compaction note keep?

Why give a subtask its own context?

An agent runs for two hours and starts repeating itself. What helps most?

Go deeper

Spotted a mistake or something unclear? Report it on GitHub.

On this page