Wire through token cache statistics
This commit is contained in:
parent
fb2941a10f
commit
00dc34fcf6
3 changed files with 23 additions and 1 deletions
|
|
@ -118,6 +118,8 @@ export type AnthropicToolChoice =
|
|||
export type AnthropicStreamUsage = {
|
||||
input_tokens: number;
|
||||
output_tokens: number;
|
||||
cache_creation_input_tokens?: number;
|
||||
cache_read_input_tokens?: number;
|
||||
};
|
||||
|
||||
export type AnthropicStreamChunk =
|
||||
|
|
|
|||
|
|
@ -325,6 +325,13 @@ export const createAnthropicProxy = ({
|
|||
usage: {
|
||||
input_tokens: usage.inputTokens,
|
||||
output_tokens: usage.outputTokens,
|
||||
// OpenAI provides cached tokens via cachedInputTokens or in experimental_providerMetadata
|
||||
// Map to Anthropic's cache_read_input_tokens
|
||||
cache_creation_input_tokens: 0, // OpenAI doesn't report cache creation separately
|
||||
cache_read_input_tokens: usage.cachedInputTokens ??
|
||||
(typeof (response as any).experimental_providerMetadata?.openai?.cached_tokens === 'number'
|
||||
? (response as any).experimental_providerMetadata.openai.cached_tokens
|
||||
: 0),
|
||||
},
|
||||
})
|
||||
);
|
||||
|
|
|
|||
|
|
@ -27,7 +27,12 @@ export function convertToAnthropicStream(
|
|||
model: "claude-4-sonnet-20250514",
|
||||
stop_reason: null,
|
||||
stop_sequence: null,
|
||||
usage: { input_tokens: 0, output_tokens: 0 },
|
||||
usage: {
|
||||
input_tokens: 0,
|
||||
output_tokens: 0,
|
||||
cache_creation_input_tokens: 0,
|
||||
cache_read_input_tokens: 0,
|
||||
},
|
||||
},
|
||||
});
|
||||
break;
|
||||
|
|
@ -42,6 +47,14 @@ export function convertToAnthropicStream(
|
|||
usage: {
|
||||
input_tokens: chunk.usage.inputTokens ?? 0,
|
||||
output_tokens: chunk.usage.outputTokens ?? 0,
|
||||
// OpenAI provides cached tokens via cachedInputTokens or in providerMetadata
|
||||
cache_creation_input_tokens: 0, // OpenAI doesn't report cache creation separately
|
||||
cache_read_input_tokens:
|
||||
chunk.usage.cachedInputTokens ??
|
||||
(typeof chunk.providerMetadata?.openai?.cached_tokens ===
|
||||
"number"
|
||||
? chunk.providerMetadata.openai.cached_tokens
|
||||
: 0),
|
||||
},
|
||||
});
|
||||
break;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue