Trimming vs Compaction | What Is Safe to Remove
Databricks Context Engineer Associate · Lesson 15 of 18 · 1:56:53
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: hard-coded trimming heuristics versus compaction, what is safe to remove from a trace without harming later steps, and aggressive versus conservative compaction. Includes three original practice questions.
Study notes
The text of this lesson, to read at your own pace. Information as of November 2026.
The budget cap that vanished
Thistledown's renovation agent works with a manager for hours on a building refit. To stay within its window, it keeps only the last twenty messages, and quietly drops the budget cap the manager set at the start. This lesson covers trimming and compaction, what is safe to remove, and how aggressive compaction should be.
Trimming heuristics
As a long task fills the context window, something has to go. The simplest answer is a hard coded trimming heuristic: keep the last few messages, drop tool outputs older than a set number of turns, or cut the oldest text first. Trimming is cheap and predictable, and it is enough when only recent information matters, like a chat that handles one question at a time. But it fails when an early message still holds a constraint, a decision, or an ID that a later step needs, since trimming removes by position, not by importance.
When to compact
Compaction summarizes the conversation as it nears the limit, and continues with the summary in place of the history. It keeps information by importance, not by position, so early constraints and decisions survive, while the bulk of the raw exchanges goes. It suits long tasks where early context stays relevant, like a refit planned over many hours, or a migration with rules set at the start. The cost is an extra model call to write the summary, and the risk that a poorly written summary drops something that matters.
What is safe to remove
To compact well, sort the trace into what can go, and what must stay. Safe to remove: raw tool outputs already used, exploratory attempts that were abandoned, repeated messages, and pleasantries. Must stay: decisions and their reasons, the user's constraints, open issues, and the exact IDs and values still in use. Also keep the current state of the task: what is done, and what comes next. When in doubt, ask whether a later step could fail without it.
Aggressive or conservative
How much to compress is a trade off. Aggressive compaction keeps a short summary: it cuts the token cost of every later call, but risks losing subtle context whose importance only appears later. Conservative compaction keeps more detail: it stays faithful to the original, but it costs more on every call, and frees less room. Choose from the task: lean conservative when small details decide later steps, like exact figures and conditions, and lean aggressive when the work is repetitive, and the details are easy to fetch again.
Recap
Let's recap. Trimming heuristics remove by position, which is enough when only recent information matters. Compaction summarizes by importance, for long tasks where early context stays relevant. Safe to remove: raw outputs already used, abandoned attempts, and repeats. Keep decisions, constraints, open issues, exact IDs, and the task's current state. And aggressive compaction saves tokens but risks subtle losses, while conservative compaction keeps fidelity at a higher cost.
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 support chat answers independent questions about house rules, one at a time; nothing from early turns is used later. The context grows long. Which approach fits?
- A carefully tuned compaction prompt
- Split the chat across several agents
- Trimming: keep the most recent turns
- A model with a larger context window
Show the answer
Trimming: keep the most recent turns
When only recent turns matter, a simple trimming heuristic is enough, cheap, and predictable. “A carefully tuned compaction prompt” is wrong, since compaction would work, but adds cost and complexity the task does not need. “Split the chat across several agents” is wrong, since splitting independent questions across agents adds overhead for nothing. And “A model with a larger context window” is wrong, since a larger window only delays the problem, and keeps paying for old turns.
The renovation trace holds: the rule no work before 9, a decision to use supplier B for lead times, the raw output of a supplier search, already summarized, and the ID of the purchase order being drafted. Which item is safe to remove?
- The supplier search output, already summarized
- The rule that no work starts before 9
- The decision to use supplier B, and why
- The ID of the purchase order in progress
Show the answer
The supplier search output, already summarized
The raw search output was already used and summarized, so removing it does not affect later steps. “The rule that no work starts before 9” is wrong, since the start time rule is a constraint that every later schedule must respect. “The decision to use supplier B, and why” is wrong, since dropping the decision and its reason invites the agent to reopen it, or contradict it. And “The ID of the purchase order in progress” is wrong, since the next steps still need the exact ID of the order in progress.
After an aggressive compaction, the renewal agent still quotes the right rents, but forgets special conditions agreed with some tenants, like a waived pet fee. Which change fits?
- Compact more often, to stay well under the limit
- Switch to trimming the oldest messages
- Drop the summary, keep all raw tool outputs
- Compact less aggressively, keeping such conditions
Show the answer
Compact less aggressively, keeping such conditions
These subtle details decide later steps, so a more conservative compaction that keeps them is worth its extra tokens. “Compact more often, to stay well under the limit” is wrong, since compacting more often loses even more detail. “Switch to trimming the oldest messages” is wrong, since trimming by position would drop the early agreements entirely. And “Drop the summary, keep all raw tool outputs” is wrong, since raw outputs without a summary fill the window again, and still bury the conditions.