Skip to content

feat(context): Tell tools how large their result may be - #336

Closed
funana777 wants to merge 1 commit into
feat/shorten-json-objectsfrom
feat/shorten-nested-lists
Closed

funana777 wants to merge 1 commit into
feat/shorten-json-objectsfrom
feat/shorten-nested-lists

Conversation

@funana777

@funana777 funana777 commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Lets tools know how large their result may be before it is shortened.

Base: #338 (feat/shorten-json-objects). Merge #338 first. #335 builds on this PR.

Why

A tool result larger than half of what is left of the context budget is shortened before the model sees it. A tool that can size its own answer, for example by leaving out older articles of a ticket, does better than a cut afterwards, but it has no way to know the limit, so it would have to guess a fixed number that only fits the default context budget.

What

  • tool_result_budget() returns how many characters a tool result of the current run can have without being shortened. Outside of a run it returns None.
  • The agent updates it for every request to the model, from the same calculation _compact_current_turn() uses (max_tool_result_chars()), so tools read the value of the latest request.

The first user is the Zammad tools in #335, which pass the budget to integration_zammad so a ticket is cut at article boundaries.

80 lines added, 4 removed.

How to test

  1. python -m pytest tests. New tests in tests/test_context_window.py check that the budget reaches tools that run in tasks of their own, and that the agent sets it below half the budget once instructions and tools are counted.

🤖 AI (if applicable)

  • The content of this PR was partly or fully generated using AI

Code, tests and this description were drafted with Claude Code (claude-opus-5-5) under my direction.


Generated by Claude Code

@funana777
funana777 force-pushed the feat/shorten-nested-lists branch 3 times, most recently from 996e687 to a1c6586 Compare November 19, 2026 17:23
@funana777 funana777 changed the title feat(context): Keep shortened JSON results valid and tell tools their budget feat(context): Tell tools how large their result may be Nov 19, 2026
@funana777
funana777 changed the base branch from main to feat/shorten-json-objects November 19, 2026 17:25
@funana777
funana777 force-pushed the feat/shorten-nested-lists branch from a1c6586 to 7712906 Compare November 19, 2026 17:35
@funana777
funana777 force-pushed the feat/shorten-json-objects branch from 0295fd7 to 1b6395a Compare November 19, 2026 17:35
@funana777
funana777 force-pushed the feat/shorten-nested-lists branch 2 times, most recently from b9e6489 to 7cbab3d Compare November 19, 2026 18:23
@funana777
funana777 force-pushed the feat/shorten-json-objects branch from 0edbab2 to 2d686ca Compare November 19, 2026 18:23
A tool result larger than half of what is left of the context budget
is shortened before the model sees it. A tool that can size its own
answer, e.g. by leaving out older articles of a ticket, does better
than a cut afterwards, but it cannot know the limit.

tool_result_budget() returns how many characters a tool result of the
current run can have. The agent updates it for every request to the
model, from the same calculation that shortens results, so tools read
the value of the latest request. It is None outside of a run.

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: Christian Fu Mueller <christian-fu.mueller@nextcloud.com>
@funana777
funana777 force-pushed the feat/shorten-json-objects branch from 2d686ca to 3dbac77 Compare November 19, 2026 18:52
@funana777
funana777 force-pushed the feat/shorten-nested-lists branch 2 times, most recently from 868eb9c to 54967ca Compare November 19, 2026 18:58
@funana777
funana777 force-pushed the feat/shorten-json-objects branch from 3dbac77 to a9feebb Compare November 19, 2026 18:58
@funana777
funana777 force-pushed the feat/shorten-nested-lists branch from 54967ca to 58d2da5 Compare November 24, 2026 13:37
@marcelklehr

Copy link
Copy Markdown
Member

Thanks, this is cleanly done, and taking max_tool_result_chars() out of _compact_current_turn() keeps the value tools are told and the limit actually applied in one place. But I don't think tools should be sizing their output to the context budget. That's the agent's job:

  • Only the agent knows the real limit. The value a tool reads comes from the last request, not the next one. It ignores the room other results in the same turn take up. It's also measured in different units: message_size() counts JSON-escaped text, so a result of exactly tool_result_budget() characters still gets shortened. And when older turns are summarized, reserved is raised after the budget was set, so tools see a number larger than the limit applied. Tools would get a value that's only roughly right, and the agent still has to truncate afterwards.
  • It hides a dependency. The output depends on how long the conversation is, through a ContextVar, not only on the tool's arguments. That makes tools harder to test and their results harder to reproduce.
  • It behaves differently over MCP. Through mcp_server.py, tool_result_budget() is None, so the same call returns the whole ticket to an MCP client and a shortened one to the Assistant.

For Zammad I'd rather follow the pattern other tools here already use (list_bookmarks(page, limit), the count_limit arguments): get_zammad_ticket with an article limit or cursor and a reasonable default, returning the newest articles plus a note like "N older articles, call again with before=…". The model decides whether it needs more, the output only depends on the arguments, and since #338 the agent drops whole articles if the result is still too big.

If we later need content-aware cutting, such as always keeping the first customer message, that should go into the agent's truncation, for example as a hint from the tool about which items to keep. That way the budget stays with the agent.

So I'd suggest closing this and changing #335 to use paging.

@funana777

Copy link
Copy Markdown
Contributor Author

Great feedback! I'll do exactly what you proposed now, thank you

@funana777 funana777 closed this Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants