feat(base): per-message / per-role analytics on RenderedTokens#38
Merged
Conversation
Adds four methods, derived from existing message_indices / sampled_mask plus a new message_roles field every renderer populates: tokens_per_message(*, sampled_only=False) tokens_by_role(*, sampled_only=False) message_token_spans() role_token_spans() Use cases — per-role length penalties for RL trainers, per-turn statistics on logprobs / attention / gradients via (start, end) spans. Also fixes attribution loss in every bridge_to_next_turn: bridges previously discarded msg_idx / is_sampled even though the local emit helpers received them. Now bridges populate message_indices (relative to new_messages), sampled_mask (uniformly False — bridge output is a prompt), and message_roles. Consumers can run the new analytics on bridge output for incremental per-message accounting without re-rendering the conversation. 17 renderers updated. 35 new parametrized tests; 1310 total passing. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
snimu
reviewed
May 16, 2026
snimu
reviewed
May 16, 2026
snimu
reviewed
May 16, 2026
Frame RL length-penalty as an example rather than the use, and rewrite the scaffolding sentence with explicit subject + concrete token examples so it's no longer a run-on. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
snimu
reviewed
May 16, 2026
| """ | ||
| if n_messages is None: | ||
| n_messages = len(self.message_roles) | ||
| out = [0] * n_messages |
Contributor
There was a problem hiding this comment.
this isn't truncated to a max of len(self.message_roles) though, but it's returned directly below. So len(out) > len(self.message_roles) is possible. Is that intended?
Previously, passing n_messages > len(message_roles) padded the result with trailing zeros for phantom messages the renderer never attributed. The docstring framed the parameter as a truncation knob only, so this was an undocumented footgun. Clamp to len(message_roles) and document it; add a regression test. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
snimu
approved these changes
May 16, 2026
This was referenced May 16, 2026
Merged
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
RenderedTokensmethods —tokens_per_message,tokens_by_role,message_token_spans,role_token_spans— derived frommessage_indices/sampled_maskand a newmessage_rolesfield every renderer now populates. Use cases: per-role length penalties for RL trainers (e.g. prime-rl), per-turn statistics over logprobs / attention / gradients via(start, end)spans.bridge_to_next_turn: bridges previously discardedmsg_idx/is_sampledeven though local emit helpers received them. Now bridges populatemessage_indices(relative tonew_messages),sampled_mask(uniformlyFalse— bridge output is a prompt), andmessage_roles. Consumers can run the new analytics on bridge output for incremental per-message accounting without re-rendering.Example
Test plan
pytest tests/— 1310 passed, 52 skipped (gpt-oss HF parity), 1 xfailed (pre-existing)tests/test_tokens_per_message.py🤖 Generated with Claude Code