aproli90 commited on
Commit
0672ba5
·
verified ·
1 Parent(s): 8732d54

Upload api.chat.ts

Browse files
Files changed (1) hide show
  1. app/routes/api.chat.ts +12 -4
app/routes/api.chat.ts CHANGED
@@ -57,13 +57,21 @@ async function chatAction({ context, request }: ActionFunctionArgs) {
57
  try {
58
  const options: StreamingOptions = {
59
  toolChoice: 'none',
60
- onFinish: async ({ text: content, finishReason, usage }) => {
 
61
  logger.debug('usage', JSON.stringify(usage));
62
 
 
 
 
63
  if (usage) {
64
- cumulativeUsage.completionTokens += usage.completionTokens || 0;
65
- cumulativeUsage.promptTokens += usage.promptTokens || 0;
66
- cumulativeUsage.totalTokens += usage.totalTokens || 0;
 
 
 
 
67
  }
68
 
69
  if (finishReason !== 'length') {
 
57
  try {
58
  const options: StreamingOptions = {
59
  toolChoice: 'none',
60
+ // eslint-disable-next-line @typescript-eslint/naming-convention
61
+ onFinish: async ({ text: content, finishReason, usage, experimental_providerMetadata }) => {
62
  logger.debug('usage', JSON.stringify(usage));
63
 
64
+ const cacheUsage = experimental_providerMetadata?.anthropic;
65
+ console.debug({ cacheUsage });
66
+
67
  if (usage) {
68
+ cumulativeUsage.completionTokens += Math.round(usage.completionTokens || 0);
69
+ cumulativeUsage.promptTokens += Math.round(
70
+ (usage.promptTokens || 0) +
71
+ ((cacheUsage?.cacheCreationInputTokens as number) || 0) * 1.25 +
72
+ ((cacheUsage?.cacheReadInputTokens as number) || 0) * 0.1,
73
+ );
74
+ cumulativeUsage.totalTokens = cumulativeUsage.completionTokens + cumulativeUsage.promptTokens;
75
  }
76
 
77
  if (finishReason !== 'length') {