CertSafari
    Databricks Certified Context Engineer Associate· Lessons

    Domain 5 · Lesson 32/45

    Clearing raw tool outputs from a deep agent history

    Given an agent with a deep message history where the context window is approaching capacity, evaluate which raw tool outputs are candidates for clearing.

    13 min read
    2.17% of exam
    5 sources
    Published 22 Oct 2026
    Docs as of 17 Oct 2026

    What you will be able to do

    • Explain why raw tool outputs are the first place to look for room in a deep message history
    • Classify a raw tool output as consumed, superseded, re-fetchable or still needed
    • Rank several clearing candidates and pick the one to clear first
    • Distill the facts from an output and keep a pointer to it before dropping the payload

    Key concept

    Clearing candidate — A raw tool output in the message history that no pending step still reads, because the agent has already used it, a later call has replaced it, or it can be fetched again. You clear it once the facts the agent took from it are recorded elsewhere in the history.

    1.Why raw tool outputs are the first place to look

    Once an agent's context window is nearly full, the next turn either fails or pushes useful material out. Something has to go, and the useful question is which item frees the most room while costing the task nothing. In a deep history, the answer is nearly always a raw tool output.

    The reason is how agents grow their context. Every turn re-sends the full history to the model, so each item stays in every later request (Per-turn context accumulation). Most of that history is small: a user question is a sentence, an assistant plan is a paragraph, a tool call is a name plus some arguments. Tool results are a different size. One SQL query through a Unity Catalog function can return tens of thousands of rows. A document fetch can return a whole file. A schema lookup can return hundreds of column definitions. A handful of these can outweigh everything else in the history combined.

    Most of that bulk has also stopped working by the time the window fills. An agent calls a tool to learn something specific: which three rows match, whether a column exists, what an error code means. After the next reasoning step it has the answer and has written it down in its own words. The raw payload stays in the history afterwards. It still costs tokens on every turn but no longer contributes information.

    Checkpoint 1 of 7· Check yourself

    An agent is 30 turns into a task and its context window is nearly full. Which item in its history should it look at first to free room without hurting the rest of the task?

    Checkpoint 2 of 7· Exam question

    An agent invoked a Unity Catalog function that returned a 50,000-row raw query result. The agent has already extracted the three relevant rows and written a summary of its findings into its response earlier in the conversation. The context window is now approaching capacity. Which item in the message history is the best candidate for clearing?

    Sources1

    2.Consumed, superseded, re-fetchable — or still needed

    A raw tool output is a clearing candidate when at least one of three things is true about it and nothing ahead still reads it.

    • Consumed. The agent has already pulled what it needed from the output into its own reasoning or response. A 50,000-row query result whose three relevant rows are already summarised in an earlier assistant message fits this case.
    • Superseded. A later call has replaced the output. A failed call followed by a successful retry, or an older version of a file read again after an edit, leaves the earlier output carrying nothing the agent will act on.
    • Re-fetchable. The same call with the same arguments would return the same content. If the agent needs it again it can ask again, so the history only has to remember that the call happened.

    The exception overrides all three: an output that a pending step still reads is not a candidate, however large it is and however long it has sat there. Take an agent on a multi-part data-cleaning task. It fetched table A's schema, wrote and ran a cleaning query against it, and confirmed the query succeeded. It then moved on to formatting table B and fetched table B's schema. Table A's schema is consumed and finished. Table B's schema has been used once too, but the formatting work is reading it right now. Clearing it would make the agent guess at column names or call the tool again in the middle of the step.

    So "already used" and "done with" are different things. The question to ask is whether any remaining step still depends on the output, not whether the agent has looked at it. Removing context that is still needed has a real cost. Genie warns about the same thing when it filters context under token pressure.

    Notice what the success confirmation is. It is not a payload waiting to be distilled. It is already the distilled outcome: one line saying the cleaning query ran. Clearing it frees almost nothing and removes the agent's record of what it has done, which is how agents end up re-running finished work.

    Checkpoint 3 of 7· Check yourself

    Which of these raw tool outputs should stay in the history when the agent clears context to free room?

    Checkpoint 4 of 7· Exam question

    An agent is working through a multi-part data-cleaning task. It first called a tool to fetch a table's schema, used that schema to write and run a cleaning query, and confirmed the query executed successfully. The agent has now moved on to an unrelated formatting sub-task for a different table and will not need the earlier schema again. The context window is close to its limit. Which raw tool output is the strongest candidate for clearing?

    Sources21

    3.Which candidate to clear first

    When more than one output qualifies, clear first the one that loses the least information per token freed. That gives a fairly stable order.

    1. Failed and superseded outputs. Nothing in them needs saving, so they can go without any distillation step.
    2. Large consumed outputs whose findings are already written down. The facts survive in the agent's own messages, so the raw rows are pure overhead.
    3. Re-fetchable outputs the agent may want again later. Clear them, but keep the call so the agent can repeat it.

    Never on the list: anything a pending step reads, plus the non-tool items that steer the remaining work.

    Retries show why the order matters. Agents routinely call a tool again after an error until they get valid data. Suppose an MCP tool call fails with a transient network error, the retry succeeds, and the agent uses the returned data in its next step. Both outputs have now been "consumed" in a loose sense. But the error output told the agent only to try again, and that instruction is spent. The successful output holds the data the task is built on, and the agent may want to check it again. The failed call's error output is the first to clear. The successful output, if it is large, comes later, after its facts are recorded.

    Checkpoint 5 of 7· Check yourself

    Context is nearly full. Three outputs qualify for clearing. The first is a transient network error from an MCP call. The second is the large result of the successful retry, which the agent used in its next step but has not yet restated. The third is a large query result whose findings the agent has already written down. Which should go first?

    Checkpoint 6 of 7· Exam question

    While completing a task, an agent's MCP tool call failed with a transient network error. The agent retried, and the second call succeeded, returning the data it needed, which it then used in its next step. The failed call's error output still sits in the message history and the context window is nearly full. Which output should be cleared first?

    Sources3

    4.Distill first, then drop the payload

    Clearing is safe only once the history keeps everything later steps need from the output. That means three things stay when the payload goes.

    • The facts the agent extracted. For a 50,000-row result, that is the three matching rows and the conclusion drawn from them, written into an assistant message or a short note that replaces the raw result.
    • The call record. The tool name and arguments show that the call happened and what it asked for, so the agent does not repeat finished work.
    • A pointer to fetch it again. The same call, a query, a table name or a file path that brings back the full content if a later step needs a detail that was not distilled.

    With those three in place, a raw output passes the downstream dependency test: later steps get the facts they need from the distillate, and the pointer covers anything nobody foresaw. Without the distillate, clearing a consumed output turns into losing it. Without the pointer, a detail that turns out to matter later can't be recovered.

    Clearing an output from the working context also does not have to destroy the record. On Databricks, an agent session can keep the full transcript, including tool calls and results, apart from what the agent sends to the model on a given turn, and MLflow tracing records every step of every execution. The payload you drop from the model's input can still be found in the session or the trace when someone needs to debug the run.

    Checkpoint 7 of 7· Check yourself

    A team adds a context-clearing step to their Databricks agent. Their agent uses MLflow tracing. For a consumed 50,000-row query result, which version of the step clears it safely?

    Sources45

    Sources

    (5)

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

    1. 1.
      “Each turn re-sends the whole chat's context to the model, and an agentic task runs several turns to plan, act, and check its work.”
      ↩︎ Why raw tool outputs are the first place to look
      “so the twentieth message in a chat costs more than the first”
      ↩︎ Why raw tool outputs are the first place to look
      “restating table names or earlier results in each prompt adds tokens without adding information”
      ↩︎ Consumed, superseded, re-fetchable — or still needed
      “The goal is to keep each chat's context relevant to the task at hand.”
      ↩︎ Key concept
    2. 2.
      “quality may be reduced if important context is filtered out”
      ↩︎ Consumed, superseded, re-fetchable — or still needed
      “quality may be reduced if important context is filtered out”
      ↩︎ Exam trap 2
      “quality may be reduced if important context is filtered out.”
      ↩︎ Checkpoint
    3. 3.
      “calling an LLM or tools repeatedly until an objective is achieved or a certain condition is met, such as receiving valid data”
      ↩︎ Which candidate to clear first
      “You still need carefully designed prompts and constraints to keep the agent focused and relevant.”
      ↩︎ Exam trap 1
      “calling an LLM or tools repeatedly until an objective is achieved or a certain condition is met, such as receiving valid data”
      ↩︎ Exam trap 3
      “or tools repeatedly until an objective is achieved or a certain condition is met, such as receiving valid data or resolving an error.”
      ↩︎ Checkpoint
    4. 4.
      “Reconstruct full context (including tool calls and reasoning) on a follow-up message.”
      ↩︎ Distill first, then drop the payload
    5. 5.
      “Agent tracing captures a complete record of every agent execution.”
      ↩︎ Distill first, then drop the payload
      “Agent tracing captures a complete record of every agent execution.”
      ↩︎ Exam trap 4
      “every intermediate step — such as model calls, tool calls, and retrieval — with the inputs, outputs, latency, token usage, and cost”
      ↩︎ Checkpoint

    Ready to test yourself?

    Practise Databricks Certified Context Engineer Associate in quiz mode.

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