Skip to content

Commit b8933ce

Browse files
Justin Torreclaude
authored andcommitted
fix: always store to S3 for PTB when usage extraction fails
For pass-through billing requests, if we fail to extract usage from the response body in the Worker, we must store the body to S3 so Jawn can extract it for accurate billing. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 1052431 commit b8933ce

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

worker/src/lib/dbLogger/DBLoggable.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -820,10 +820,13 @@ export class DBLoggable {
820820
reasoning_tokens?: number;
821821
} = {};
822822
let extractedModel: string | undefined;
823+
let failedToGetUsage = true;
823824
try {
824825
const responseText = rawResponseBody.join("");
825826
const parsedResponse = JSON.parse(responseText);
826827
extractedUsage = this.getDetailedUsage(parsedResponse);
828+
// Check if we actually got usage tokens
829+
failedToGetUsage = !extractedUsage.prompt_tokens && !extractedUsage.completion_tokens;
827830
// Extract model from response (OpenAI format)
828831
if (
829832
typeof parsedResponse === "object" &&
@@ -833,12 +836,15 @@ export class DBLoggable {
833836
extractedModel = (parsedResponse as { model?: string }).model;
834837
}
835838
} catch {
836-
// Ignore parsing errors - usage will be extracted later by Jawn if body is stored
839+
// Parsing failed - Jawn will need to extract usage from the body
840+
failedToGetUsage = true;
837841
}
838842

839843
// Skip S3 storage only if BOTH request and response are omitted
840844
// If only one is omitted, still store to S3 - Jawn will respect the omit flags
845+
// IMPORTANT: For PTB, if we failed to get usage, always store to S3 so Jawn can extract it for billing
841846
const skipS3Storage =
847+
!(isPassthroughBilling && failedToGetUsage) &&
842848
requestHeaders?.omitHeaders?.omitRequest === true &&
843849
requestHeaders?.omitHeaders?.omitResponse === true;
844850

0 commit comments

Comments
 (0)