CertSafari
    Databricks Certified Context Engineer Associate· Lessons

    Domain 6 · Lesson 37/45

    Trimming vs compaction: matching context management to relevance

    Given a long-running Databricks agent task where context window pressure is building, evaluate whether hard-coded trimming heuristics are sufficient for the task's information relevance pattern, or if it requires a more sophisticated compaction approach.

    12 min read
    2.2% of exam
    5 sources
    Published 22 Oct 2026
    Docs as of 17 Oct 2026

    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?

    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?

    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.

    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?

    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?

    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?

    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?

    Sources34

    Sources

    (5)

    Every claim above is drawn from one of these pages, quoted as it was written on the date shown.

    1. 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. 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. 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. 4.
      “Group traces from multi-turn conversations to analyze the full conversational flow”
      ↩︎ Escalate to compaction on evidence, not by default

    Also cited

    Ready to test yourself?

    Practise Databricks Certified Context Engineer Associate in quiz mode.

    Spotted a mistake, or was something unclear? Tell us.