Writing and Tuning a Compaction Prompt
Databricks Context Engineer Associate · Lesson 16 of 18 · 2:02:33
YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗
Learning with these videos? Subscribe for new courses and exam-style questions.
Subscribe on YouTube (opens in a new tab)In this lesson
Prepare for the Databricks Certified Context Engineer Associate exam. In this lesson: what a compaction prompt asks for, tuning it on agent traces (recall first, then precision), finding the category of information a summary discarded, and revising the prompt without inflating the summary. Includes three original practice questions.
Study notes
The text of this lesson, to read at your own pace. Information as of November 2026.
Leases it was told to skip
Thistledown's migration agent compacts its context every few hours. After the third compaction, it migrates leases that the manager had told it to skip, because the summary kept the progress, but not the exclusions. This lesson covers writing a compaction prompt, tuning it for recall and then precision, and repairing a prompt that discards the wrong category.
What a compaction prompt asks for
A compaction prompt tells the model how to summarize a trace, so that the agent can continue with the summary alone. A good one names the categories to keep: the goal and its constraints, decisions with their reasons, the current state of the task, open issues, and exact IDs, values, and table names. It also names what to drop: raw tool outputs already used, dead ends, and repeated content. And it asks for a compact structure, like short lists under fixed headings, which keeps the summary small and easy for the agent to follow. The test is simple: can the agent continue the task from the summary, as if nothing had been removed?
Recall first, then precision
Tune a compaction prompt on real traces, the long and complex ones where it matters most. First, maximize recall: make sure the summary captures every piece of information that later steps used, even if it comes out too long. Then iterate for precision: remove superfluous content, like raw outputs and repeated details, while checking that recall does not drop. The order matters, since a missing fact breaks the task, while an extra line only costs a few tokens.
Finding the discarded category
When an agent loses coherence after compaction, compare the trace before and after, to see what the summary discarded. Repeated work points to a discarded task state. Reopened or contradicted choices point to discarded decisions, and their reasons. Ignored rules point to discarded user constraints, while wrong rows and broken calls point to discarded IDs and values. Name the category first, since the fix depends on it.
Revising without inflating
Once the category is known, revise the prompt to preserve it explicitly, under its own heading. Ask for it in a compact form: a list of constraints, each stated once, or of decisions with a one line reason each, not the full exchange where they appeared. Avoid fixes that inflate the summary, like keeping the whole history, or raising the summary length for everything. Then rerun the same traces, and check that the failure is gone, and that the summary grew only slightly.
Recap
Let's recap. A compaction prompt names what to keep, what to drop, and a compact structure. Tune it on real, complex traces: recall first, then precision. When coherence fails after compaction, find the category the summary discarded. Repeated work means task state, reopened choices mean decisions, and ignored rules mean constraints. And revise the prompt to keep that category in a compact form, then rerun the same traces.
Quick checks for this lesson
The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.
A compaction prompt says only: “Summarize the conversation briefly.” After compaction, the agent repeats work it already did. What is missing from the prompt?
- A demand for an even shorter summary
- The current task state: what is done, what is next
- A larger model to write the summary
- An instruction to keep all raw tool outputs
Show the answer
The current task state: what is done, what is next
Without the task's current state, what is done and what comes next, the agent cannot tell which work is finished. “A demand for an even shorter summary” is wrong, since a shorter summary would lose even more. “A larger model to write the summary” is wrong, since a stronger model still follows a prompt that never asks for the task state. And “An instruction to keep all raw tool outputs” is wrong, since keeping all raw outputs defeats the purpose of compaction.
After compaction, the migration agent migrates leases the manager said to skip, although it tracks its progress correctly. Which category did the summary discard?
- The progress of the task
- The raw tool outputs
- The agent’s role
- The user’s constraints
Show the answer
The user’s constraints
The instruction to skip some leases is a user constraint, and the summary dropped it, while it kept the progress. “The progress of the task” is wrong, since the progress was tracked correctly. “The raw tool outputs” is wrong, since raw outputs are meant to be dropped, and do not hold the instruction. And “The agent’s role” is wrong, since the agent's role is in the system prompt, which compaction does not summarize.
The summary keeps discarding the decisions the agent made, and why. Which revision preserves them without a large rise in token cost?
- A heading for decisions, one line of reason each
- Keep the full conversation, with no compaction
- Double the summary length for every category
- Have the agent decide again after each compaction
Show the answer
A heading for decisions, one line of reason each
A dedicated heading with one line per decision keeps the category at a small cost. “Keep the full conversation, with no compaction” is wrong, since dropping compaction lets the context grow without limit. “Double the summary length for every category” is wrong, since doubling everything raises the cost far more than needed. And “Have the agent decide again after each compaction” is wrong, since deciding again wastes calls, and may produce different, conflicting choices.