Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fix(deepagents): keep audio and video blocks out of tool result eviction
main now keeps image blocks (#980). Audio and video blocks were still
counted toward the eviction limit and dropped from an evicted result;
treat them the same as images.
  • Loading branch information
burrows99 committed Nov 24, 2026
commit c777fdaacac3df5d088a7bab37e9563de908a461
2 changes: 1 addition & 1 deletion .changeset/tool-eviction-keeps-media.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
"deepagents": patch
---

Fix tool result eviction dropping media: image, audio and video blocks no longer count toward `toolTokenLimitBeforeEvict`, so a screenshot returned by a tool (for example over MCP) no longer forces eviction on its own, and when a tool result's text is evicted its media blocks stay on the replacement message, as they already do for evicted human messages.
Tool result eviction now treats audio and video blocks like image blocks: they no longer count toward `toolTokenLimitBeforeEvict`, and when a tool result's text is evicted they stay on the replacement message instead of being dropped.
36 changes: 36 additions & 0 deletions libs/deepagents/src/middleware/fs.eviction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -739,4 +739,40 @@ describe("tool result eviction with image blocks", () => {
expect(image).toEqual(imageBlock);
expect(messages[0].content).toHaveLength(2);
});

it("should keep audio and video blocks when evicting large text", async () => {
const audioBlock = {
type: "audio",
data: "A".repeat(200_000),
mimeType: "audio/wav",
};
const videoBlock = {
type: "video",
data: "A".repeat(200_000),
mimeType: "video/mp4",
};
const largeText = "large result\n".repeat(500);
const wrapped = await wrapToolResult([
{ type: "text", text: largeText },
audioBlock,
videoBlock,
]);

expect(isCommand(wrapped)).toBe(true);
const { files, messages } = (wrapped as any).update;
const evictedPath = "/large_tool_results/call_screenshot.txt";
expect(files[evictedPath].content).toBe(largeText);
expect(messages[0].content.slice(1)).toEqual([audioBlock, videoBlock]);
});

it("should not count audio or video blocks toward the eviction limit", async () => {
const content = [
{ type: "text", text: "Recording captured" },
{ type: "audio", data: "A".repeat(200_000), mimeType: "audio/wav" },
];
const wrapped = await wrapToolResult(content);

expect(isCommand(wrapped)).toBe(false);
expect(wrapped.content).toEqual(content);
});
});
22 changes: 15 additions & 7 deletions libs/deepagents/src/middleware/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -500,17 +500,25 @@ function extractTextFromMessage(message: {
return String(message.content);
}

const MEDIA_BLOCK_TYPES: ReadonlySet<unknown> = new Set([
"image",
"image_url",
"audio",
"video",
]);

/**
* Whether a content block is an image. Tool-result eviction skips images when
* sizing a result and keeps them when evicting it: base64 length says little
* about what a provider charges for an image.
* Whether a content block is an image, audio or video block. Tool-result
* eviction skips them when sizing a result and keeps them when evicting it:
* base64 length says little about what a provider charges for media, and it is
* not text the model could page through.
*/
function isImageBlock(block: unknown): boolean {
function isMediaBlock(block: unknown): boolean {
return (
typeof block === "object" &&
block !== null &&
"type" in block &&
(block.type === "image" || block.type === "image_url")
MEDIA_BLOCK_TYPES.has(block.type)
);
}

Expand All @@ -520,7 +528,7 @@ function stringifyToolContent(content: unknown): string {
}
if (Array.isArray(content)) {
return content
.filter((block) => !isImageBlock(block))
.filter((block) => !isMediaBlock(block))
.map((block) => {
if (
typeof block === "object" &&
Expand Down Expand Up @@ -2601,7 +2609,7 @@ export function createFilesystemMiddleware(
.replace("{content_sample}", contentSample);

const truncatedMessage = new ToolMessage({
content: buildEvictedContent(msg, replacementText, isImageBlock),
content: buildEvictedContent(msg, replacementText, isMediaBlock),
tool_call_id: msg.tool_call_id,
name: msg.name,
id: msg.id,
Expand Down
Loading