CertSafari
    Databricks Certified Context Engineer Associate· Lessons

    Domain 6 · Lesson 36/45

    Tuning a compaction prompt: recall first, then precision

    Tune a compaction prompt for a Databricks agent trace: maximize recall first (capture everything relevant), then iterate to improve precision (remove superfluous outputs).

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

    What you will be able to do

    • Explain why the compaction prompt is the main lever for changing what a compacted summary keeps and drops
    • Build an expected-facts list from real Databricks agent traces and use it to drive a recall-first tuning pass
    • Run a precision pass that removes superfluous content one class at a time while re-checking recall against the same expected facts

    Key concept

    Recall-first compaction tuning — Tune the compaction prompt until its summaries contain every fact the agent will still need, measured against an expected-facts list taken from real traces. Only after that, cut superfluous content, checking after every cut that recall has not dropped.

    1.The compaction prompt decides what survives

    Compaction replaces a long agent history with a summary, and the agent carries on from that summary as if it were the history. For a Databricks agent that has spent two hours profiling Unity Catalog tables, opening tickets and testing SQL fixes, the summary is all it has left of that work. Whatever the summary leaves out is gone. The agent cannot ask about it, because it no longer knows the thing ever existed.

    The compaction prompt is the instruction that produces the summary, so it is the lever you tune. The model that writes the summary keeps what the prompt asks for and drops the rest. Changing the trigger threshold or the context window size changes when compaction runs. Changing the prompt changes what comes out of it. Treat the prompt as a versioned artifact: register it, evaluate each version, and keep the history, the same as you would for any other prompt in the agent.

    Tuning has two objectives, and you take them in a fixed order: maximize recall first, then improve precision. Recall asks whether the summary contains everything the agent still needs. Precision asks whether everything in the summary earns its tokens. The order comes from an asymmetry in what each failure costs. When recall fails, the agent reopens a ticket it already closed or breaks a constraint the user set an hour ago, and nothing in its context shows that anything went missing. When precision fails, you pay for extra tokens. The first failure is silent and does damage. The second is visible and cheap to fix later.

    There is also a practical reason. You can only judge recall against a complete list of what has to survive. Precision can be judged locally, by looking at one sentence and asking whether it matters, so it can wait. If you prune first, you end up with a short summary and no way of telling which of the deleted facts mattered. Tuning both at once, weighted equally, has the same problem: every cut is a guess.

    Checkpoint 1 of 6· Check yourself

    A teammate wants to tune a new compaction prompt for recall and precision in one pass, with both weighted equally, so that the summaries come out short right away. What is the main problem with this plan?

    Checkpoint 2 of 6· Exam question

    A context engineer is tuning a compaction prompt that summarizes a long-running Databricks agent trace whenever the context window nears its limit. This is the first draft of the prompt. What should the engineer optimize for first, before worrying about how concise the summary is?

    Checkpoint 3 of 6· Exam question

    When tuning a compaction prompt for an agent trace, what is the recommended order of the two tuning objectives?

    Sources123

    2.The recall pass: expected facts from real traces

    Recall tuning starts from real traces, not invented conversations. MLflow Tracing records every LLM call, retriever lookup and tool invocation in an agent run, so a long production or staging trace shows exactly what history the compaction step would face. Choose several long traces that actually hit the context limit, and include ones where the agent changed direction, hit errors, or received new constraints partway through.

    For each trace, write down the facts the summary must contain. These are expected facts, the same idea MLflow evaluation uses for the required content of a good response. Categories compaction must keep (goals and constraints, decisions with their rationale, open issues, identifiers and paths, errors that still matter) give you the skeleton. Your job here is to turn each category into concrete, checkable lines for this specific trace. A line like "keep decisions" can't be checked. A line like "chose to rebuild sales.orders_daily rather than patch it, because the source schema changed" can.

    The loop is simple. Run the draft prompt on each trace, compare the summary with the expected-facts list, and for each missing fact, add or sharpen an instruction that asks for its category. Then run again. Stop when every expected fact appears in every summary. A scorer can do the comparison so that each prompt version gets a recall number instead of an impression.

    Leave length limits out of the prompt during this pass. The summarization prompt below, registered as a new version, shows why. One line asks for every core fact, which is a recall instruction. Another forces the output into exactly two sentences, which is a hard precision limit. When the two conflict, the length limit usually wins and facts get dropped. A first-draft compaction prompt should contain the recall instruction only.

    A summarization prompt version that mixes a recall instruction ("Include ALL core facts") with a hard length cap. Leave the cap out of a compaction prompt until recall is proven.python
    prompt_v2 = mlflow.genai.register_prompt(
        name=PROMPT_NAME,
        template="""You are an expert summarizer. Create a summary of the following content in *exactly* 2 sentences (no more, no less - be very careful about the number of sentences).
    
    Guidelines:
    - Include ALL core facts and key findings
    - Use clear, concise language
    - Maintain factual accuracy
    - Cover all main points mentioned
    - Write for a general audience
    - Use exactly 2 sentences
    
    Content: {{content}}
    
    Summary:""",
        commit_message="v2: Added comprehensive fact coverage with 2-sentence requirement"
    )

    Checkpoint 4 of 6· Fill the gap

    This registered prompt version mixes two kinds of guideline. Which word completes the guideline that works as the recall instruction, the only kind a first-draft compaction prompt should keep?

    prompt_v2 = mlflow.genai.register_prompt(
        name=PROMPT_NAME,
        template="""You are an expert summarizer. Create a summary of the following content in *exactly* 2 sentences (no more, no less - be very careful about the number of sentences).
    
    Guidelines:
    - Include  ?  core facts and key findings
    - Use clear, concise language
    - Maintain factual accuracy
    - Cover all main points mentioned
    - Write for a general audience
    - Use exactly 2 sentences
    
    Content: {{content}}
    
    Summary:""",
        commit_message="v2: Added comprehensive fact coverage with 2-sentence requirement"
    )

    Sources456

    3.The precision pass: cut what never matters, then re-check recall

    Precision work starts only once the recall-tuned prompt keeps every expected fact. At that point, read the summaries for content that never changes what the agent does later. Typical examples are tool-call arguments repeated next to the result they produced, the same "job still running" status written five times, and raw output the agent could simply fetch again. Removing this kind of content safely is covered in safe content removal. In this pass you turn each of those classes into an omission instruction in the prompt.

    Make one cut per prompt version, then re-run the same traces against the same expected-facts list. An instruction such as "omit tool arguments" can quietly remove a table path the agent needs later. Only the recall check catches that. If recall drops, roll back to the previous version and narrow the instruction, for example "omit tool arguments except identifiers and paths". Prompt versions are immutable, so the version that passed is always there to go back to.

    The two tuning passes compared
    PassQuestion it answersSignal to watchWhat you change in the prompt
    Recall (first)Does the summary contain every expected fact from the trace?Share of expected facts present in each summaryAdd or sharpen instructions to keep missing categories; no length cap
    Precision (second)Does every part of the summary affect later agent behavior?Summary size, e.g. total_token_count, with recall held at its passing levelAdd omission instructions one class at a time; roll back any version that loses a fact

    Three tempting responses to a bloated summary each undo the method. Rewriting the prompt from scratch with a new strategy throws away the instructions that earned full recall, so you start over. Leaving the prompt alone because "more detail is always safer" pays for useless tokens on every compaction, and filler competes with real facts for the model's attention. Shrinking the context window so compaction runs less often doesn't touch the summary's content at all. The correct move is iterative: remove content confirmed as superfluous, and confirm recall after each removal.

    Checkpoint 5 of 6· Check yourself

    Your recall-tuned compaction prompt, v5, keeps every expected fact. You register v6, which adds "omit tool arguments". total_token_count drops by 30%, but on one trace the summary no longer contains a table path that the agent uses later. What should you do next?

    Checkpoint 6 of 6· Exam question

    After the recall-first draft of a compaction prompt, an engineer reviews summarized agent traces and finds the summaries reliably retain all necessary facts, but they are bloated with restated tool-call arguments and duplicate status messages that never affect later agent behavior. What is the appropriate next tuning step?

    Sources73

    Sources

    (8)

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

    1. 1.
      “As conversations grow longer, the harness decides what stays active and what gets summarized, a process known as context compaction.”
      ↩︎ The compaction prompt decides what survives
    2. 2.
      “By treating prompts as first-class artifacts alongside your agent code, you can iterate confidently and ensure every prompt change is tracked and measurable.”
      ↩︎ The compaction prompt decides what survives
    3. 3.
      “Computing precision does not require knowing all relevant items.”
      ↩︎ The compaction prompt decides what survives
      “Precision is the proportion of retrieved documents that are actually relevant”
      ↩︎ The precision pass: cut what never matters, then re-check recall
      “Recall is the proportion of the ground truth documents that are represented in the retrieved chunks.”
      ↩︎ Key concept
      “Computing recall requires your ground-truth to contain all relevant items.”
      ↩︎ Exam trap 1
      “Computing recall requires your ground-truth to contain all relevant items.”
      ↩︎ Checkpoint
    4. 4.
      “Tracing records the inputs, outputs, latency, token usage, and cost of every intermediate step”
      ↩︎ The recall pass: expected facts from real traces
    5. 6.
      “build evaluation datasets with expected facts”
      ↩︎ The recall pass: expected facts from real traces
      “Start simple: Begin with basic prompts and iteratively improve based on evaluation results.”
      ↩︎ Exam trap 2
      “Use consistent datasets: Evaluate all versions against the same data for fair comparison.”
      ↩︎ Exam trap 3
    6. 7.
      “This Git-like versioning maintains complete history and enables rollbacks.”
      ↩︎ The precision pass: cut what never matters, then re-check recall
      “To edit a prompt, you must create a new version. This Git-like versioning maintains complete history and enables rollbacks.”
      ↩︎ Checkpoint

    Also cited

    Ready to test yourself?

    Practise Databricks Certified Context Engineer Associate in quiz mode.

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