Repository navigation
Python: fix(compaction): ignore MCP host payload in token estimates - #10382
Conversation
Strip host-only additional properties from function results before token estimation so large MCP host payloads do not inflate compaction token counts and drop user messages. Signed-off-by: LuYuan-Zjyy <your-email@example.com>
|
Patronum (@LuYuan-Zjyy) please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
1 similar comment
|
Patronum (@LuYuan-Zjyy) please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
There was a problem hiding this comment.
🟡 Changes recommended
The unchanged token-count basis version allows inflated cached basis-2 counts to remain valid, so restored conversations can still unnecessarily lose earlier user messages.
1 open issue
What changed in this PR
This PR addresses #10277 by excluding host-only MCP payloads from Python compaction token estimates, helping prevent unnecessary removal of earlier messages.
Changes:
- Filters the MCP host-payload key from serialized additional properties without importing the MCP stack.
- Adds a regression test verifying that host payloads do not affect estimated token counts.
| File | Description |
|---|---|
python/packages/core/tests/core/test_compaction.py |
Tests token-count equality with and without an MCP host payload. |
python/packages/core/agent_framework/_compaction.py |
Excludes host-only MCP data from token-estimation serialization. |
Review effort: Balanced
| for key in _HOST_ONLY_ADDITIONAL_PROPERTY_KEYS: | ||
| filtered_properties.pop(key, None) |

What does this PR do?
Closes #10277.
Compaction token estimation serializes
function_result.additional_propertiesas-is, so the host-only_mcp_tool_result_host_payloadkey is counted as model-visible text. Large MCP host payloads inflate token estimates, which can causeTruncationStrategyto drop earlier user messages.This PR strips host-only keys from
additional_propertiesin_serialize_contentbefore estimating tokens, using a small module-level frozenset kept in sync with_mcp._MCP_TOOL_RESULT_HOST_PAYLOAD_KEY.Why a local constant
_mcppulls in the full MCP/OpenTelemetry stack; importing it from_compactionwould add a heavy runtime dependency. The key is stable, so a frozenset with a keep-in-sync comment is the lightweight option.Verification
pytest python/packages/core/tests/core/test_compaction.py- passed.ruff checkandruff format --checkon the changed files - passed.git diff --check- clean.