Skip to content

Comments

feat(anthropic): Add raw usage object to Anthropic provider metadata#7566

Closed
jacoblee93 wants to merge 9 commits intovercel:mainfrom
jacoblee93:jacob/anthropic_caching
Closed

feat(anthropic): Add raw usage object to Anthropic provider metadata#7566
jacoblee93 wants to merge 9 commits intovercel:mainfrom
jacoblee93:jacob/anthropic_caching

Conversation

@jacoblee93
Copy link
Contributor

@jacoblee93 jacoblee93 commented Jul 26, 2025

Background

Anthropic supports several different flavors of prompt caching in beta. These are priced differently. It would be lovely to give tracing providers a way to accurately track this.

Summary

Adds the raw returned Anthropic usage object to provider metadata. A subset of this is returned as providerMetadata.anthropic.cache_creation_input_tokens, but this is not enough granularity to tell whether it was a 5 minute cache or a 1 hr cache.

Verification

See added example.

Tasks

  • Tests have been added / updated (for bug fixes / features)
  • Documentation has been added / updated (for bug fixes / features)
  • A patch changeset for relevant packages has been added (for bug fixes / features - run pnpm changeset in the project root)
  • Formatting issues have been fixed (run pnpm prettier-fix in the project root)

@jacoblee93 jacoblee93 marked this pull request as ready for review July 26, 2025 01:04
Comment on lines 77 to 78
/** @deprecated Access via usage instead. */
cacheCreationInputTokens?: number;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this be removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the prop from the type entirely but left the code populating it, not sure of what degree of backcompat you're looking for.


providerMetadata = {
anthropic: {
usage: value.message.usage as Record<string, JSONValue>,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The JSONValue type doesn't seem to like unknown as a type even though it's accurate here... cast seemed appropriate? Open to other ideas

@lgrammel
Copy link
Collaborator

Continued in #7591

@lgrammel lgrammel closed this Jul 28, 2025
lgrammel added a commit that referenced this pull request Jul 28, 2025
…7591)

## Background

Anthropic supports several different flavors of prompt caching in beta.
These are priced differently. It would be lovely to give tracing
providers a way to accurately track this.

## Summary

Adds the raw returned Anthropic `usage` object to provider metadata. A
subset of this is returned as
`providerMetadata.anthropic.cache_creation_input_tokens`, but this is
not enough granularity to tell whether it was a 5 minute cache or a 1 hr
cache.

## Verification

See added example.

## Related Issues
Continues #7566

---------

Co-authored-by: jacoblee93 <jacoblee93@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants