feat: add metadata field to messages for stateful context tracking#2125
feat: add metadata field to messages for stateful context tracking#2125lizradway wants to merge 2 commits intostrands-agents:mainfrom
Conversation
…trands-agents#1532) Attach usage and metrics from model responses directly to assistant messages, enabling downstream features like smart truncation and per-message cost analysis.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as off-topic.
This comment was marked as off-topic.
|
/strands review Also tell me about how this impacts serialization/deserialization of messages, and if it can impact session managers. |
|
Assessment: Comment All issues from previous review rounds have been addressed. The PR checklist is now fully checked, and the code is in good shape. Session Manager Serialization/Deserialization ImpactPer @mkmeral's request, here is the serialization analysis: Save path: Load path: Backward compatibility: Old sessions persisted without Conversation managers: One gap: Previously Raised Items
|
Description
Adds an optional
metadatafield to theMessageTypedDict that carries per-messageusage,metrics, and arbitrarycustomdata from model responses. This is a foundational piece for the context management roadmap — downstream features like proactive compression (#555), smart truncation, and per-message cost analysis need this information attached directly to messages.What it does:
MessageMetadataTypedDict andmetadata: NotRequired[MessageMetadata]onMessagemetadataon assistant messages immediately after stream processing, beforeAfterModelCallEventfires (so all hook consumers see consistent state)roleandcontentbefore model calls — metadata (and any future non-model fields) never leak to providersMessage.__annotations__.keys()withMessage.__required_keys__inagent.pyfor message detection, so the optionalmetadatafield doesn't affect input type inferenceget_message_metadata()convenience accessorNote: The existing test
test_event_loop_cycle_tool_resultpreviously asserted 4 messages in themodel.streamcall, but the 4th message was the response FROM the model, not input TO it. This was a pre-existing bug in the test that was corrected as part of this change.Related Issues
Closes #1532
Documentation PR
N/A — no new public API surface;
metadatais framework-internal.Type of Change
New feature
Testing
How have you tested the change?
test_event_loop_metadata.pywith 3 tests: metadata population with real usage, default values when no metadata event, and verification that metadata is stripped before model callstest_message_metadata.pywith 4 unit tests for theMessageMetadatatype andget_message_metadataaccessormetadatapresence on assistant messageshatch run prepareChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.