You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Compact intermediate tool calls in message history - #25
This change improves message history storage by compacting intermediate tool calls within a single agent turn. Instead of storing full tool call messages with arguments and results, only the tool names are preserved in a (used tools: ...) summary line appended to the assistant's text. This reduces storage overhead while maintaining conversation context.
Key Changes
New strip_tool_summary() function in app/core/agent.py: Removes the (used tools: ...) line from assistant messages, allowing callers to recover the original text if needed.
New Agent._compact_turn() classmethod: Processes a turn's message chain (assistant messages with tool calls + tool results) and produces a compacted list of assistant text messages. Intermediate text is preserved, tool names are collected into a summary line, and tool arguments/results/reasoning are dropped (kept only in traces).
Updated agent_loop() in both CliAgent and BackgroundAgent: Now calls _compact_turn() to process the turn's messages before appending to self.messages and MessageHistory. This ensures both the in-memory message list and the persistent history store the compacted format.
Updated documentation: MessageHistory docstring clarified to reflect that tool calls are now reduced to a summary line rather than stored in full.
Implementation Details
The compaction logic groups consecutive tool calls by their preceding assistant text, preserving the semantic flow of the conversation.
A turn that ends mid-tool-call (due to stop or max iterations) is handled correctly—the final message is appended only if the turn doesn't already end on a tool-call message.
The regex pattern _TOOL_SUMMARY_RE matches the summary line only at the end of a message (after optional \n\n), ensuring similar text elsewhere in the message is not removed.
All four test cases cover edge cases: normal multi-tool turns, tool-only messages, cut-short turns, and turns with no tool calls.
Instead of saving only the user prompt and final reply, each turn now
stores intermediate assistant text plus a one-line summary of the tool
calls that followed it (name + truncated args). Tool results and
reasoning are dropped; traces and logs still have everything.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01E7PTP6WSGTKfqwPiL2wvBc
Replace the [tool calls: name(args)] summary, which looks like call
syntax the model might imitate, with a plain trailing line listing the
deduped tool names. Add strip_tool_summary() to remove it again.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01E7PTP6WSGTKfqwPiL2wvBc
Auto-naming omits final answer after expanded tool messages
app/cli/cli_agent.py:105
This expanded list is also passed unchanged to _auto_name(), which only examines messages[:4] (app/core/agent.py:269-271). On a first turn with three text-bearing tool rounds, those slots become the user plus three intermediate messages, so the final answer is excluded from title generation. Please make auto-naming select the first user and final assistant response explicitly rather than relying on the first four stored rows.
🧠 Review effort: Balanced
Give feedback about Copilot approvals in this survey to enter a drawing for a $150 gift card.
This reverts commit fc91b8d because persisted tool summaries leaked into later model responses. Keep only the user prompt and final assistant reply in durable history.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This change improves message history storage by compacting intermediate tool calls within a single agent turn. Instead of storing full tool call messages with arguments and results, only the tool names are preserved in a
(used tools: ...)summary line appended to the assistant's text. This reduces storage overhead while maintaining conversation context.Key Changes
New
strip_tool_summary()function inapp/core/agent.py: Removes the(used tools: ...)line from assistant messages, allowing callers to recover the original text if needed.New
Agent._compact_turn()classmethod: Processes a turn's message chain (assistant messages with tool calls + tool results) and produces a compacted list of assistant text messages. Intermediate text is preserved, tool names are collected into a summary line, and tool arguments/results/reasoning are dropped (kept only in traces).Updated
agent_loop()in bothCliAgentandBackgroundAgent: Now calls_compact_turn()to process the turn's messages before appending toself.messagesandMessageHistory. This ensures both the in-memory message list and the persistent history store the compacted format.Updated documentation:
MessageHistorydocstring clarified to reflect that tool calls are now reduced to a summary line rather than stored in full.Implementation Details
stopor max iterations) is handled correctly—the final message is appended only if the turn doesn't already end on a tool-call message._TOOL_SUMMARY_REmatches the summary line only at the end of a message (after optional\n\n), ensuring similar text elsewhere in the message is not removed.https://claude.ai/code/session_01E7PTP6WSGTKfqwPiL2wvBc