Cancel batch
Batch batches().cancel(BatchCancelParamsparams = BatchCancelParams.none(), RequestOptionsrequestOptions = RequestOptions.none())
post /batches/{batch_id}/cancel
Cancels an in-progress batch. The batch will be in status cancelling for up to 10 minutes, before changing to cancelled, where it will have partial results (if any) available in the output file.
Parameters
-
BatchCancelParams paramsOptional<String> batchId
Returns
-
class Batch:-
String id -
String completionWindowThe time frame within which the batch should be processed.
-
long createdAtThe Unix timestamp (in seconds) for when the batch was created.
-
String endpointThe OpenAI API endpoint used by the batch.
-
String inputFileIdThe ID of the input file for the batch.
-
JsonValue; object_ "batch"constantThe object type, which is always
batch.BATCH("batch")
-
Status statusThe current status of the batch.
-
VALIDATING("validating") -
FAILED("failed") -
IN_PROGRESS("in_progress") -
FINALIZING("finalizing") -
COMPLETED("completed") -
EXPIRED("expired") -
CANCELLING("cancelling") -
CANCELLED("cancelled")
-
-
Optional<Long> cancelledAtThe Unix timestamp (in seconds) for when the batch was cancelled.
-
Optional<Long> cancellingAtThe Unix timestamp (in seconds) for when the batch started cancelling.
-
Optional<Long> completedAtThe Unix timestamp (in seconds) for when the batch was completed.
-
Optional<String> errorFileIdThe ID of the file containing the outputs of requests with errors.
-
Optional<Errors> errors-
Optional<List<BatchError>> data-
Optional<String> codeAn error code identifying the error type.
-
Optional<Long> lineThe line number of the input file where the error occurred, if applicable.
-
Optional<String> messageA human-readable message providing more details about the error.
-
Optional<String> paramThe name of the parameter that caused the error, if applicable.
-
-
Optional<String> object_The object type, which is always
list.
-
-
Optional<Long> expiredAtThe Unix timestamp (in seconds) for when the batch expired.
-
Optional<Long> expiresAtThe Unix timestamp (in seconds) for when the batch will expire.
-
Optional<Long> failedAtThe Unix timestamp (in seconds) for when the batch failed.
-
Optional<Long> finalizingAtThe Unix timestamp (in seconds) for when the batch started finalizing.
-
Optional<Long> inProgressAtThe Unix timestamp (in seconds) for when the batch started processing.
-
Optional<Metadata> metadataSet of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format, and querying for objects via API or the dashboard.
Keys are strings with a maximum length of 64 characters. Values are strings with a maximum length of 512 characters.
-
Optional<String> modelModel ID used to process the batch, like
gpt-5-2025-08-07. OpenAI offers a wide range of models with different capabilities, performance characteristics, and price points. Refer to the model guide to browse and compare available models. -
Optional<String> outputFileIdThe ID of the file containing the outputs of successfully executed requests.
-
Optional<BatchRequestCounts> requestCountsThe request counts for different statuses within the batch.
-
long completedNumber of requests that have been completed successfully.
-
long failedNumber of requests that have failed.
-
long totalTotal number of requests in the batch.
-
-
Optional<BatchUsage> usageRepresents token usage details including input tokens, output tokens, a breakdown of output tokens, and the total tokens used. Only populated on batches created after September 7, 2025.
-
long inputTokensThe number of input tokens.
-
InputTokensDetails inputTokensDetailsA detailed breakdown of the input tokens.
-
long cachedTokensThe number of tokens that were retrieved from the cache. More on prompt caching.
-
-
long outputTokensThe number of output tokens.
-
OutputTokensDetails outputTokensDetailsA detailed breakdown of the output tokens.
-
long reasoningTokensThe number of reasoning tokens.
-
-
long totalTokensThe total number of tokens used.
-
-
Example
package com.openai.example;
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.batches.Batch;
import com.openai.models.batches.BatchCancelParams;
public final class Main {
private Main() {}
public static void main(String[] args) {
OpenAIClient client = OpenAIOkHttpClient.fromEnv();
Batch batch = client.batches().cancel("batch_id");
}
}
Response
{
"id": "id",
"completion_window": "completion_window",
"created_at": 0,
"endpoint": "endpoint",
"input_file_id": "input_file_id",
"object": "batch",
"status": "validating",
"cancelled_at": 0,
"cancelling_at": 0,
"completed_at": 0,
"error_file_id": "error_file_id",
"errors": {
"data": [
{
"code": "code",
"line": 0,
"message": "message",
"param": "param"
}
],
"object": "object"
},
"expired_at": 0,
"expires_at": 0,
"failed_at": 0,
"finalizing_at": 0,
"in_progress_at": 0,
"metadata": {
"foo": "string"
},
"model": "model",
"output_file_id": "output_file_id",
"request_counts": {
"completed": 0,
"failed": 0,
"total": 0
},
"usage": {
"input_tokens": 0,
"input_tokens_details": {
"cached_tokens": 0
},
"output_tokens": 0,
"output_tokens_details": {
"reasoning_tokens": 0
},
"total_tokens": 0
}
}