-
Notifications
You must be signed in to change notification settings - Fork 114
fix: surface anthropic + bedrock prompt cache tokens #1992
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5403,6 +5403,7 @@ chat.openapi(completions, async (c) => { | |
| let totalTokens = null; | ||
| let reasoningTokens = null; | ||
| let cachedTokens = null; | ||
| let cacheCreationTokens: number | null = null; | ||
| let streamingToolCalls = null; | ||
| let imageByteSize = 0; // Track total image data size for token estimation | ||
| let outputImageCount = 0; // Track number of output images for cost calculation | ||
|
|
@@ -6570,6 +6571,9 @@ chat.openapi(completions, async (c) => { | |
| if (usage.cachedTokens !== null) { | ||
| cachedTokens = usage.cachedTokens; | ||
| } | ||
| if (usage.cacheCreationTokens !== null) { | ||
| cacheCreationTokens = usage.cacheCreationTokens; | ||
| } | ||
|
|
||
| // Estimate tokens if not provided and we have a finish reason | ||
| if (finishReason && (!promptTokens || !completionTokens)) { | ||
|
|
@@ -7103,9 +7107,15 @@ chat.openapi(completions, async (c) => { | |
| 1, | ||
| Math.round(adjPrompt + adjCompletion), | ||
| ), | ||
| ...(cachedTokens !== null && { | ||
| ...((cachedTokens !== null || | ||
| (cacheCreationTokens !== null && | ||
| cacheCreationTokens > 0)) && { | ||
| prompt_tokens_details: { | ||
| cached_tokens: cachedTokens, | ||
| cached_tokens: cachedTokens ?? 0, | ||
| ...(cacheCreationTokens !== null && | ||
| cacheCreationTokens > 0 && { | ||
| cache_creation_tokens: cacheCreationTokens, | ||
| }), | ||
|
Comment on lines
+7110
to
+7118
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The normal This addition only affects the late 🤖 Prompt for AI Agents |
||
| }, | ||
| }), | ||
| cost_usd_total: streamingCostsEarly.totalCost, | ||
|
|
@@ -8720,6 +8730,7 @@ chat.openapi(completions, async (c) => { | |
| completionTokens, | ||
| reasoningTokens, | ||
| cachedTokens, | ||
| cacheCreationTokens, | ||
| toolResults, | ||
| images, | ||
| annotations, | ||
|
|
@@ -8897,6 +8908,7 @@ chat.openapi(completions, async (c) => { | |
| routingAttempts.length > 0 ? routingAttempts : null, | ||
| requestId, | ||
| usedRegion, | ||
| cacheCreationTokens, | ||
| ); | ||
|
|
||
| // Extract plugin IDs for logging | ||
|
|
||
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.
Add the cache usage fields to
message_starttoo.This path now treats
cache_creation_input_tokensandcache_read_input_tokensas always-present, but themessage_startpayload at Line 603 still emitsusage: { input_tokens, output_tokens }only. Native streaming clients that inspectmessage_start.message.usagewill still seeundefinedfor the new fields.Possible fix
usage: { input_tokens: 0, output_tokens: 0, + cache_creation_input_tokens: 0, + cache_read_input_tokens: 0, },Also applies to: 592-604, 739-758
🤖 Prompt for AI Agents