What you will be able to do
- Distinguish trimming from compaction by what happens to the content that leaves the context window
- Decide from a task's information relevance pattern whether a hard-coded, recency-based trimming rule is sufficient
- Identify the evidence that justifies escalating a long-running agent from trimming to compaction
Key concept
Trimming vs compaction — Trimming removes context by a fixed rule, such as dropping the oldest N turns, and keeps nothing of what it removes. Compaction asks an LLM to summarize the history and carries that summary forward. Trimming is only safe when relevance follows recency.
1.Two ways to relieve context pressure
A long-running agent's context grows with every turn, and every turn pays for all of it. Databricks puts this plainly for Genie Code: the whole chat is re-sent to the model on each turn, so the twentieth message costs more than the first. A multi-hour task, such as a data migration, a pipeline monitor or a research loop, eventually fills the window, and something has to leave. There are two ways to handle that, and the difference between them is what happens to the content that leaves.
Trimming removes content by a hard-coded rule. Typical rules are: keep only the last 20 turns (a sliding window), drop the oldest 50 turns once a token threshold is crossed, or truncate any tool output longer than a fixed length. No model reads the content before it goes. The rule applies the same way whatever the turns contain, and a dropped turn is gone completely, with no trace of it left in context. This makes trimming cheap and deterministic. You can say in advance exactly what the agent will see at turn 200.
Compaction passes the history to an LLM with instructions to summarize it. The agent then continues with that summary in place of the original turns. The original text still leaves the window, but a condensed version of it stays in context. That retention costs a model call each time compaction runs. It also means the agent's view of the past now depends on a compaction prompt, which can drop the wrong things in ways a fixed rule never does. Diagnosing those losses is covered in diagnosing what compaction discarded.
Neither strategy is tied to a message type. A trimming rule can drop user turns as easily as tool results, and compaction can summarize tool output as well as dialogue. What separates them is the operation: trimming discards, while compaction condenses and keeps. Production harnesses often use both. Some writing uses "compaction" loosely for any kind of context reduction. In this certification, compaction always means summarizing.
Checkpoint 1 of 6· Check yourself
A data-migration agent has run for hours and its context window is nearly full. The team is choosing between trimming and compaction. Which statement correctly describes the difference between them?
The two strategies differ in what they do with the content that leaves the window. Trimming throws it away under a hard-coded rule. Compaction has an LLM condense it and keeps the summary in context. Neither one is limited to a message type, and the last option has the two reversed.
“As conversations grow longer, the harness decides what stays active and what gets summarized, a process known as context compaction.”Source: www.databricks.com
Checkpoint 2 of 6· Exam question
What is the fundamental difference between trimming and compaction as strategies for managing a long-running agent's context window?
Correct answer: A — Trimming discards older or lower-priority turns outright, while compaction summarizes them into a condensed form that is kept in context.
- A. Correct. Trimming is a hard-coded heuristic that drops turns outright (e.g., oldest-first), whereas compaction summarizes the discarded material and retains the condensed summary in context.
- B. Incorrect. This reverses the two strategies: trimming is the one that discards content outright, while compaction is the one that summarizes and retains a condensed version.
- C. Incorrect. Compaction does not discard content outright; it produces and retains a summarized version, which is the key distinction from trimming.
- D. Incorrect. Neither strategy is restricted to a single message type; both can be applied across tool outputs, user turns, and assistant turns depending on configuration.
Sources1
2.Does relevance follow recency?
One question decides between the two strategies: does a piece of context become less relevant as it ages? A rule like "keep the last 20 turns" bets that it does. When that bet holds, trimming is not a compromise. It is the right tool, because it removes precisely the content no future step will use, and it does so without a model call.
The polling agent is the clearest case of relevance following recency. The status from turn 400 is replaced by the status from turn 401. Relevance falls off predictably and evenly with age, and nothing old ever becomes important again. Compacting those turns would pay for a summary of numbers that are already wrong. Leaving context unmanaged is not a third option, because the agent fails when it reaches the hard limit, and every turn before that costs more than the last.
A data migration has a different shape. In turn 3, the user specifies the target schema, and every transformation through turn 150 must conform to it. Meanwhile, turns 4 to 100 fill up with exploratory queries that stop mattering almost immediately. This relevance is non-monotonic: one early turn stays critical while the turns around it lose value. A drop-oldest-50 rule removes the schema along with the noise, and the next transformation violates it. The rule did exactly what it was written to do. What failed is its assumption, because recency does not describe relevance for this task. That calls for compaction, with a prompt that keeps constraints like this one.
The two examples fall on either side of a line Databricks also draws when it distills long-term memory. Memory pipelines keep facts the user states explicitly, but skip live values that change over time and record how to re-fetch them instead. A user-stated constraint exists only in the transcript. Once trimmed, it cannot be recovered. A live status can always be fetched again and goes stale anyway. Trimming breaks when content of the first kind is still needed after its turn has aged out of the window. When that content is one item you know about in advance, you can also keep it outside the window, for example in a Delta-backed state object. When no fixed rule can predict which early content will matter, compaction is the answer.
No. Findings from file 1 matter just as much to the final report as findings from file 40. A drop-oldest rule would quietly lose the early findings, so this task needs compaction (or the findings written out somewhere outside the context window), not a recency rule.
Checkpoint 3 of 6· Check yourself
A migration agent uses a drop-oldest-50 rule. Around turn 120, a transformation breaks the target schema the user gave in turn 3. What explains the failure, and what should change?
The trimming rule did exactly what it was written to do. Its assumption that relevance follows recency fails for this task. A schema the user stated exists only in the transcript and cannot be fetched again like a live value, so the task needs compaction with a prompt that keeps such constraints.
“Facts that the user states explicitly and preferences that they express, without inferring beyond what the conversation says.”Source: docs.databricks.com
Checkpoint 4 of 6· Exam question
A Databricks agent continuously polls a streaming pipeline's status every few seconds over a multi-hour run. Only the most recent one or two poll results are ever referenced by the agent's next action; results older than a few turns are never revisited. As context window pressure builds, which approach best fits this task's information relevance pattern?
Correct answer: A — Apply hard-coded trimming to drop the oldest poll turns, since relevance decays predictably and uniformly with recency here.
- A. Correct. When relevance decays monotonically and predictably with recency, a hard-coded trimming heuristic that drops the oldest turns is sufficient and avoids the extra cost of summarization.
- B. Incorrect. Since old poll data is never revisited by this task, summarizing it provides no benefit and only adds unnecessary latency and cost.
- C. Incorrect. Letting the context window grow unmanaged guarantees eventual failure and ignores the fact that a simple, low-cost mitigation is available.
- D. Incorrect. Restricting compaction to reasoning turns while ignoring the actual relevance pattern of the poll data does not match the described task's needs.
Checkpoint 5 of 6· Exam question
A long-running Databricks agent performs a multi-step data migration. In an early turn, the user specifies a target schema that every later transformation step must conform to. By turn 150, context pressure triggers a hard-coded trimming heuristic that drops the oldest 50 turns, including the one containing the schema definition. The agent then produces a transformation that violates the schema. What does this failure indicate about the task's information relevance pattern?
Correct answer: A — The pattern is non-monotonic: a fact stated early stays critical throughout, so recency-only trimming misses it and compaction is needed here.
- A. Correct. The schema fact remains critical long after it ages out of recency-based trimming, showing the relevance pattern is not purely recency-driven and requires compaction to preserve durable facts.
- B. Incorrect. If the pattern were purely recency-based, dropping the schema turn would not have caused a downstream failure; the observed failure demonstrates the opposite.
- C. Incorrect. This assumes all older turns are equally safe to discard, which is exactly the assumption the failure disproves for this task.
- D. Incorrect. The scenario states context pressure did trigger trimming; the failure is about what was dropped, not whether pressure existed.
Sources2
3.Escalate to compaction on evidence, not by default
Compaction is not an upgrade to apply just in case. Each time it fires, it adds a model call and latency. It also makes the agent's history depend on a summary that nobody wrote or checked by hand. Trimming has the same strengths Databricks lists for deterministic chains: it is predictable, easy to audit and easy to test. The same design advice applies: start simple, and add model-driven behaviour only when the simple version has been shown to fail.
For a long-running agent whose task looks recency-shaped, start with a trimming rule. The trigger to move to compaction is an observed failure: a later step contradicts, repeats or forgets something that existed only in turns the rule dropped. That evidence usually looks like one of these:
- a violated constraint whose definition (a schema, a budget cap, an excluded region) appears only in a trimmed turn
- the agent repeating work or re-asking a question that was settled in a dropped turn
- an identifier, such as a table name or run ID, used wrongly once the turn that introduced it falls outside the window
Confirm the failure in traces before changing strategy. Tag each turn's trace with the mlflow.trace.session metadata field so a conversation's traces group together, then compare the failing step with where the trimming boundary was at that moment. If the needed content was outside the window, recency does not describe relevance for this task, and compaction is justified. If the content was still inside the window, trimming did not cause the failure, so look at context-length degradation instead. After you switch to compaction, deciding how hard to compress is a separate tradeoff, covered in aggressive vs conservative compaction.
Checkpoint 6 of 6· Check yourself
Before any failure has been observed, a team lead proposes LLM compaction for every new long-running agent "to be safe." Following the lesson's guidance, what is the better plan?
Each compaction run adds a model call and latency, and it makes the agent's history depend on a summary nobody checked. A predictable trimming rule comes first. A threshold being crossed is not evidence that trimming failed. The trigger is a confirmed loss: traces show that something a later step needed had fallen outside the window.
“When building any AI-powered application, start simple. Introduce more complex agentic behaviors when you truly need them”Source: docs.databricks.com
Sources
(5)
Every claim above is drawn from one of these pages, quoted as it was written on the date shown.
- 1.
“Consumption grows with both the length of a chat and the amount of context it carries”
↩︎ Two ways to relieve context pressure“Genie Code automatically summarizes, compacts, and truncates long context, but only a new chat clears it completely.”
↩︎ Two ways to relieve context pressure“Genie Code automatically summarizes, compacts, and truncates long context”
↩︎ Key concept - 2.
“Facts that the user states explicitly and preferences that they express, without inferring beyond what the conversation says.”
↩︎ Does relevance follow recency?“Values from live sources that change over time”
↩︎ Does relevance follow recency?“It records how to find or compute the value instead.”
↩︎ Exam trap 2“Facts that the user states explicitly and preferences that they express, without inferring beyond what the conversation says.”
↩︎ Exam trap 3 - 3.
“start simple. Introduce more complex agentic behaviors when you truly need them for better flexibility or model-driven decisions.”
↩︎ Escalate to compaction on evidence, not by default“Highest predictability and auditability.”
↩︎ Escalate to compaction on evidence, not by default“start simple. Introduce more complex agentic behaviors when you truly need them for better flexibility or model-driven decisions.”
↩︎ Exam trap 4“When building any AI-powered application, start simple. Introduce more complex agentic behaviors when you truly need them”
↩︎ Checkpoint - 4.
“Group traces from multi-turn conversations to analyze the full conversational flow”
↩︎ Escalate to compaction on evidence, not by default
Also cited
- https://www.databricks.com/blog/ai-harnessOfficial docs
“the harness decides what stays active and what gets summarized, a process known as context compaction.”
↩︎ Exam trap 1“As conversations grow longer, the harness decides what stays active and what gets summarized, a process known as context compaction.”
↩︎ Checkpoint