Repository navigation
feat(vertex_ai): bill explicit context cache storage per token-hour #45019
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
Open
roasted-penguin
wants to merge
9
commits into
BerriAI:main
Choose a base branch
from
roasted-penguin:feat/vertex-context-cache-storage-cost
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+624
−8
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9dd2835
feat(vertex_ai): bill explicit context cache storage per token-hour
roasted-penguin db5c264
fix(cost_map): keep schema in sync with base generator and limit test…
roasted-penguin c231b7f
fix(vertex_ai): keep token pricing for storage-only overrides and pri…
roasted-penguin 95d7152
Merge remote-tracking branch 'origin/main' into feat/vertex-context-c…
roasted-penguin 2ce6744
fix(vertex_ai): keep basedpyright counts flat for cache storage pricing
roasted-penguin 2ef85f8
test: annotate new cost-map test locals as Final
roasted-penguin a5b6d84
Merge remote-tracking branch 'origin/main' into feat/vertex-context-c…
roasted-penguin 45c52ad
refactor(vertex_ai): take the deployment storage rate from model_info
roasted-penguin 0b07cb5
test(vertex_ai): cover cache storage joining provider additional costs
roasted-penguin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| from datetime import datetime | ||
| from typing import Final | ||
|
|
||
| from pydantic import BaseModel, ConfigDict, ValidationError | ||
|
|
||
| DEFAULT_CONTEXT_CACHE_TTL_SECONDS: Final = 3600.0 | ||
| _SECONDS_PER_HOUR: Final = 3600.0 | ||
|
|
||
|
|
||
| class _CachedContentUsage(BaseModel): | ||
| model_config = ConfigDict(frozen=True) | ||
|
|
||
| totalTokenCount: int = 0 | ||
|
|
||
|
|
||
| class _CreatedCachedContent(BaseModel): | ||
| model_config = ConfigDict(frozen=True) | ||
|
|
||
| usageMetadata: _CachedContentUsage = _CachedContentUsage() | ||
| createTime: datetime | None = None | ||
| expireTime: datetime | None = None | ||
|
|
||
|
|
||
| def _reported_lifetime_seconds(created: _CreatedCachedContent) -> float | None: | ||
| if created.createTime is None or created.expireTime is None: | ||
| return None | ||
| seconds: Final = (created.expireTime - created.createTime).total_seconds() | ||
| return seconds if seconds > 0 else None | ||
|
|
||
|
|
||
| def _requested_ttl_seconds(requested_ttl: str | None) -> float | None: | ||
| if requested_ttl is None or not requested_ttl.endswith("s"): | ||
| return None | ||
| try: | ||
| seconds: Final = float(requested_ttl[:-1]) | ||
| except ValueError: | ||
| return None | ||
| return seconds if seconds > 0 else None | ||
|
|
||
|
|
||
| def context_cache_storage_token_hours(created_cached_content: object, requested_ttl: str | None) -> float: | ||
| """Token-hours a newly created cachedContents resource is billed for over its whole TTL. | ||
|
|
||
| The lifetime comes from the create response (expireTime - createTime), then the TTL that was | ||
| requested, then the provider default of one hour. | ||
| """ | ||
| try: | ||
| created: Final = _CreatedCachedContent.model_validate(created_cached_content) | ||
| except ValidationError: | ||
| return 0.0 | ||
| lifetime_seconds: Final = ( | ||
| _reported_lifetime_seconds(created) | ||
| or _requested_ttl_seconds(requested_ttl) | ||
| or DEFAULT_CONTEXT_CACHE_TTL_SECONDS | ||
| ) | ||
| return created.usageMetadata.totalTokenCount * lifetime_seconds / _SECONDS_PER_HOUR |
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.