What you will be able to do
- Explain what a compaction prompt controls when an agent trace is summarized near the context-window limit
- Apply the Databricks precision and recall definitions to the output of a compaction summary
- Build an expected-facts evaluation dataset from agent traces and use it to drive a recall-first prompt revision
- Tighten a compaction prompt for precision without losing the facts the recall pass secured
Key concept
Compaction prompt — The instruction given to the summarization step that rewrites an agent's history once the context grows too long. It decides which facts from the trace stay active and which are dropped, so tuning it means tuning what the agent remembers.
1.What a compaction prompt actually controls
Base models keep nothing beyond their current context window, so a long-running agent depends on its harness to manage memory. When the conversation grows long, the harness decides what stays active and what gets summarized. Databricks calls this context compaction. The summarizer works from an instruction, and that instruction is the compaction prompt. Whatever it fails to ask for is gone once the agent restarts from the summary.
On Databricks, the input to that summary is an agent trace. An MLflow trace records the initial input, the final output, and every intermediate step in between: model calls, tool calls, retrieval, plus each step's inputs, outputs, latency, token usage and cost. A compaction prompt turns that record into something much smaller. Tuning it means deciding which parts of the trace the next stretch of work actually needs.
The final answer doesn't show which intermediate facts the agent relied on to get there. Databricks makes the same point about debugging: with only an input and a final response, you can't tell which intermediate decision caused a failure. The facts a compaction summary must keep (a tool result, a constraint the user stated, a resolved sub-task's outcome) live in the intermediate steps of the trace, so the trace is the material you evaluate against.
Checkpoint 1 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?
Correct answer: A — Recall: make sure the summary captures every fact, decision, and tool result that could still be relevant later
2.Recall and precision as the measuring stick
The objective asks for two passes: get recall high first, then raise precision. Both terms have exact definitions in the Databricks evaluation material. They're written for retrieved chunks, and they carry over directly when the output is a summary instead of a set of chunks.
Recall is about completeness: of everything you know is relevant, how much made it into the output. Databricks describes it as a measure of the completeness of the results. Precision runs the other way: of everything the output contains, how much is actually relevant. The two have different costs. Recall needs a ground truth that lists every relevant item. Precision doesn't, and Databricks notes that an LLM judge can assess the relevance of each item.
That difference in cost explains the order of the passes. A compaction summary that drops a fact can't be repaired later, because the agent no longer sees it. A summary that keeps a few extra lines just costs some tokens. So you first make sure nothing needed is missing, which requires the full ground truth. Only then do you trim.
| Metric | Databricks definition | What it means for a compaction summary |
|---|---|---|
| Recall | Proportion of the ground truth documents represented in the retrieved chunks; a measure of completeness | Share of the facts the agent will need later that survive in the summary |
| Precision | Proportion of retrieved documents that are actually relevant to the request | Share of the summary's content that is still needed, as opposed to superfluous output |
| Ground truth needed? | Recall: yes, all relevant items. Precision: no | Recall needs an expected-facts list per trace; precision can be judged item by item |
Checkpoint 2 of 6· Check yourself
A compaction summary keeps every fact on your expected list. It also restates three tool outputs that later steps superseded. Which metric suffers?
Recall is fully met, since every expected fact is there. The superseded outputs are irrelevant content in the output, and that is what precision measures.
“Precision is the proportion of retrieved documents that are actually relevant to the user's request.”Source: docs.databricks.com
Sources3
3.Pass 1: maximize recall with expected facts
The recall pass starts with ground truth. MLflow's guide to evaluating prompt versions does this with a dataset of inputs, each paired with the facts that should appear in a good summary. For compaction, each input is the content of an agent trace (or the part about to be compacted). The expected facts are the things the agent must still know after the restart. Traces are the right source for these examples, because Databricks recommends turning representative failures into evaluation and golden datasets.
"expectations": {
"expected_facts": [
"remote work changed collaboration",
"digital tools adoption",
"productivity remained stable",
"challenges with company culture",
"work-life balance issues",
"global talent recruitment"
]
}Checkpoint 3 of 6· Fill the gap
Which call creates the Unity Catalog-backed evaluation dataset that holds these expected facts?
eval_dataset = mlflow.genai.datasets. ? (
uc_table_name=EVAL_DATASET_NAME
)mlflow.genai.datasets.create_dataset creates the dataset in a Unity Catalog table. merge_records adds the examples to it afterwards, and register_prompt versions the prompt, not the data.
The prompt itself goes into the MLflow Prompt Registry, and each revision becomes a new version. The guide begins with a deliberately bare v1 that tells the model what to do but nothing about what to keep.
prompt_v1 = mlflow.genai.register_prompt(
name=PROMPT_NAME,
template="Summarize this text: {{content}}",
commit_message="v1: Basic summarization prompt"
)A compaction prompt written like v1 lets the model choose what matters, so recall depends on luck. The recall revision states coverage as an explicit requirement. The guide's v2 includes the line "Include ALL core facts and key findings" along with "Cover all main points mentioned". You then score each version with a scorer that compares the output against the expected facts. In the guide, that scorer is Correctness(), commented as the one that checks expected facts.
scorers = [
Correctness(), # Checks expected facts
sentence_count_judge, # Custom sentence count judge
]Make the recall dataset hard on purpose. Databricks advises testing edge cases and including challenging examples in your evaluation dataset. For compaction, that means traces where the important fact is easy to miss: something stated once, early on, and never repeated.
Checkpoint 4 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?
Correct answer: A — Iterate on the prompt to omit content confirmed to be superfluous, then re-check that recall is still preserved
4.Pass 2: improve precision without giving back recall
Once the coverage score is where you want it, the second pass removes what doesn't need to be there. The guide's v2 already pairs its coverage rule with a strict output constraint. The constraint is a form of precision pressure, because it leaves no room for filler.
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"
)The MLflow tutorial on evaluating and improving an agent shows the same move in a different prompt. After reading its evaluation results, it adds the guideline "Keep the email CONCISE - only include information directly relevant to the user's request." The compaction version says the same thing about the agent's remaining task: keep only what the next steps need.
The discipline that makes this a precision pass and not a gamble is to evaluate every version against the same dataset. Re-run the expected-facts scorer next to whatever measures the new constraint; the guide pairs Correctness() with a custom sentence-count judge. If tightening the prompt lowers the expected-facts score, you've traded recall for brevity and need to back off. Each change is logged as a new registry version with a commit message that records why it was made, so you can trace a later regression to the edit that caused it.
eval_results = mlflow.genai.evaluate(
predict_fn=create_summary_function(PROMPT_NAME, version),
data=eval_dataset,
scorers=scorers,
)Checkpoint 5 of 6· Put it in order
Put the prompt-version evaluation loop in the order the MLflow guide follows
- 1.Build an evaluation dataset with expected facts
- 2.Register prompt versions in the Prompt Registry
- 3.Evaluate each version with mlflow.genai.evaluate and the scorers
- 4.Compare versions across metrics to pick the best prompt
The guide creates prompt versions, then builds a dataset of expected facts, runs MLflow's evaluation framework on each version, and compares the results.
“create prompt versions, build evaluation datasets with expected facts, and use MLflow's evaluation framework to compare performance.”Source: docs.databricks.com
The work doesn't end at deployment. Databricks recommends continuing to evaluate prompts after deployment to catch degradation. For compaction, production traces will keep turning up new kinds of fact the summary drops. Each one becomes a new expected-facts record, which starts another recall pass.
Checkpoint 6 of 6· Exam question
When tuning a compaction prompt for an agent trace, what is the recommended order of the two tuning objectives?
Correct answer: A — Maximize recall first, then iterate afterward to improve precision
Exam traps
Each one states something that sounds right. Open it to see what is actually true.
1.You can start by making the compaction prompt as terse as possible and add facts back if the agent misbehaves.Why is that wrong?
Compaction is recall first. A fact the summary drops is gone for the agent, so you secure coverage against expected facts before you add brevity constraints, and you re-check coverage after every tightening.
Covered in Pass 2: improve precision without giving back recall
2.Precision and recall both require a complete ground-truth list of relevant items.Why is that wrong?
Only recall needs the complete list. Precision asks whether each produced item is relevant, which can be judged item by item.
Covered in Recall and precision as the measuring stick
3.A new compaction prompt version can be validated on a fresh set of traces, as long as its score is higher than the previous version's.Why is that wrong?
Scores are only comparable when every version runs against the same dataset. Otherwise a higher score may just reflect easier traces.
Covered in Pass 2: improve precision without giving back recall
Sources
Every claim above is drawn from one of these pages, quoted as it was written on the date shown.
- 1.https://www.databricks.com/blog/ai-harnessSecondary source
“the harness decides what stays active and what gets summarized, a process known as context compaction”
↩︎ What a compaction prompt actually controls“the harness decides what stays active and what gets summarized, a process known as context compaction”
↩︎ Key concept - 2.
“plus every intermediate step — such as model calls, tool calls, and retrieval”
↩︎ What a compaction prompt actually controls“With only an input and final response, you cannot tell which intermediate decision caused a failure.”
↩︎ What a compaction prompt actually controls“turn representative failures into evaluation and golden datasets”
↩︎ Pass 1: maximize recall with expected facts - 3.https://docs.databricks.com/aws/en/agents/tutorials/ai-cookbook/evaluate-assess-performanceOfficial docs
“Recall is the proportion of the ground truth documents that are represented in the retrieved chunks.”
↩︎ Recall and precision as the measuring stick“This is a measure of the completeness of the results.”
↩︎ Recall and precision as the measuring stick“An LLM judge can be used to assess the relevance of each retrieved chunk to the user's request.”
↩︎ Recall and precision as the measuring stick“Computing precision does not require knowing all relevant items.”
↩︎ Exam trap 2“Computing recall requires your ground-truth to contain all relevant items.”
↩︎ Prediction“Precision is the proportion of retrieved documents that are actually relevant to the user's request.”
↩︎ Checkpoint - 4.https://docs.databricks.com/aws/en/mlflow3/genai/prompt-version-mgmt/prompt-registry/evaluate-promptsOfficial docs
“Build a dataset with expected facts that should appear in good summaries”
↩︎ Pass 1: maximize recall with expected facts“Checks expected facts”
↩︎ Pass 1: maximize recall with expected facts“Test edge cases: Include challenging examples in your evaluation dataset.”
↩︎ Pass 1: maximize recall with expected facts“Use consistent datasets: Evaluate all versions against the same data for fair comparison.”
↩︎ Pass 2: improve precision without giving back recall“Document changes: Use meaningful commit messages to track why changes were made.”
↩︎ Pass 2: improve precision without giving back recall“Monitor production: Continue evaluating prompts after deployment to catch degradation.”
↩︎ Pass 2: improve precision without giving back recall“Include ALL core facts and key findings”
↩︎ Exam trap 1“Use consistent datasets: Evaluate all versions against the same data for fair comparison.”
↩︎ Exam trap 3“create prompt versions, build evaluation datasets with expected facts, and use MLflow's evaluation framework to compare performance.”
↩︎ Checkpoint - 5.
“Keep the email CONCISE - only include information directly relevant to the user's request”
↩︎ Pass 2: improve precision without giving back recall