-
-
Notifications
You must be signed in to change notification settings - Fork 333
fix(amp): use message.usage as primary data source #820
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Amp changed its data format around Jan 13, 2026 to store usage data in messages[].usage instead of usageLedger.events[]. - Make message.usage the primary source for token data - Fall back to usageLedger for legacy sessions (pre-Jan 13, 2026) - Add timestamp field support from message.usage - Update CLAUDE.md with current and legacy data structure docs - Add test for message-level usage extraction
📝 WalkthroughWalkthroughUpdated token usage tracking in AMP to primarily extract usage from message-level data instead of ledger-based events, with fallback support for legacy data format from before January 13, 2026. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@apps/amp/src/data-loader.ts`:
- Around line 185-188: The timestamp fallback currently sets timestamp =
usage.timestamp ?? (threadCreated != null ? new
Date(threadCreated).toISOString() : new Date().toISOString()), which risks
assigning the current time when both usage.timestamp and threadCreated are
missing; change the fallback in the timestamp assignment to use a sentinel
(e.g., null or a constant like UNKNOWN_TIMESTAMP) instead of new
Date().toISOString(), and add a warning log when the sentinel is used so callers
can detect missing event time; update the code around the timestamp variable and
the consuming logic to handle the sentinel rather than assuming a real ISO
timestamp.
| // Use timestamp from usage if available, otherwise estimate from thread created time | ||
| const timestamp = | ||
| usage.timestamp ?? | ||
| (threadCreated != null ? new Date(threadCreated).toISOString() : new Date().toISOString()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Timestamp fallback to current time may produce inaccurate data.
When both usage.timestamp and threadCreated are missing, using new Date().toISOString() assigns the current timestamp at parse time rather than the actual event time. This could skew time-based sorting and reports, especially if files are re-parsed later.
Consider using a sentinel value or logging a warning when this fallback is triggered.
Suggested improvement
// Use timestamp from usage if available, otherwise estimate from thread created time
+ // Note: Using current time as last resort - data may be inaccurate
const timestamp =
usage.timestamp ??
- (threadCreated != null ? new Date(threadCreated).toISOString() : new Date().toISOString());
+ (threadCreated != null ? new Date(threadCreated).toISOString() : (() => {
+ logger.debug('Missing timestamp for message usage, using current time', { threadId, messageId: message.messageId });
+ return new Date().toISOString();
+ })());🤖 Prompt for AI Agents
In `@apps/amp/src/data-loader.ts` around lines 185 - 188, The timestamp fallback
currently sets timestamp = usage.timestamp ?? (threadCreated != null ? new
Date(threadCreated).toISOString() : new Date().toISOString()), which risks
assigning the current time when both usage.timestamp and threadCreated are
missing; change the fallback in the timestamp assignment to use a sentinel
(e.g., null or a constant like UNKNOWN_TIMESTAMP) instead of new
Date().toISOString(), and add a warning log when the sentinel is used so callers
can detect missing event time; update the code around the timestamp variable and
the consuming logic to handle the sentinel rather than assuming a real ISO
timestamp.
Amp seems to have changed its data format around Jan 13, 2026 to store usage data in messages[].usage instead of usageLedger.events[].
Summary by CodeRabbit
Release Notes
Documentation
Refactor
✏️ Tip: You can customize this high-level summary in your review settings.