@@ -17,7 +17,7 @@ import { getModelParams } from "../transform/model-params"
1717
1818import { BaseProvider } from "./base-provider"
1919import type { SingleCompletionHandler , ApiHandlerCreateMessageMetadata } from "../index"
20- import { calculateApiCostAnthropic } from "../../shared/cost"
20+ import { calculateApiCostAnthropic , applyBatchApiDiscount } from "../../shared/cost"
2121
2222// Batch API polling configuration
2323const BATCH_POLL_INTERVAL_MS = 5000 // Poll every 5 seconds
@@ -271,13 +271,7 @@ export class AnthropicHandler extends BaseProvider implements SingleCompletionHa
271271
272272 // Apply 50% discount for Batch API (applies after 1M context pricing if both enabled)
273273 if ( this . options . anthropicUseBatchApi ) {
274- info = {
275- ...info ,
276- inputPrice : typeof info . inputPrice === "number" ? info . inputPrice * 0.5 : undefined ,
277- outputPrice : typeof info . outputPrice === "number" ? info . outputPrice * 0.5 : undefined ,
278- cacheWritesPrice : typeof info . cacheWritesPrice === "number" ? info . cacheWritesPrice * 0.5 : undefined ,
279- cacheReadsPrice : typeof info . cacheReadsPrice === "number" ? info . cacheReadsPrice * 0.5 : undefined ,
280- }
274+ info = applyBatchApiDiscount ( info )
281275 }
282276
283277 const params = getModelParams ( {
@@ -368,6 +362,8 @@ export class AnthropicHandler extends BaseProvider implements SingleCompletionHa
368362 {
369363 requests : [
370364 {
365+ // Using Date.now() is sufficient since we only send one request per batch
366+ // If we support multiple requests per batch in the future, consider using crypto.randomUUID()
371367 custom_id : `req_${ Date . now ( ) } ` ,
372368 params : batchRequest ,
373369 } ,
@@ -388,8 +384,10 @@ export class AnthropicHandler extends BaseProvider implements SingleCompletionHa
388384 break
389385 }
390386
391- // Handle non-processing states (API may return states not in current SDK types)
392- if ( status . processing_status !== "in_progress" && status . processing_status !== "canceling" ) {
387+ // Only fail on truly failed states; continue polling for all valid transitional states
388+ // Note: SDK types may not include all possible states, so we check the actual string value
389+ const statusStr = status . processing_status as string
390+ if ( statusStr === "errored" || statusStr === "expired" || statusStr === "canceled" ) {
393391 throw new Error ( `Batch processing failed with status: ${ status . processing_status } ` )
394392 }
395393
@@ -442,8 +440,10 @@ export class AnthropicHandler extends BaseProvider implements SingleCompletionHa
442440 ) ,
443441 }
444442 } else if ( result . result . type === "errored" ) {
445- const errorType = result . result . error . type
446- throw new Error ( `Batch request failed: ${ errorType } ` )
443+ const error = result . result . error
444+ // ErrorResponse only has 'type' field in SDK types, but may have 'message' at runtime
445+ const errorDetails = JSON . stringify ( error )
446+ throw new Error ( `Batch request failed: ${ error . type } - ${ errorDetails } ` )
447447 }
448448 }
449449 }
0 commit comments