> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/portkey-AI/gateway/llms.txt
> Use this file to discover all available pages before exploring further.

# Retrieve Batch

> Retrieve the status and results of a batch request

## GET /v1/batches/:id

Retrieve information about a specific batch, including its status and results.

## Authentication

Requires provider authentication headers:

```bash theme={null}
x-portkey-provider: openai
Authorization: Bearer YOUR_OPENAI_API_KEY
```

## Request

### Headers

<ParamField header="x-portkey-provider" type="string" required>
  The provider to route the request to (e.g., `openai`)
</ParamField>

<ParamField header="Authorization" type="string" required>
  Bearer token for the provider API
</ParamField>

### Path Parameters

<ParamField path="id" type="string" required>
  The ID of the batch to retrieve
</ParamField>

## Response

<ResponseField name="id" type="string">
  The batch identifier
</ResponseField>

<ResponseField name="object" type="string">
  The object type, always "batch"
</ResponseField>

<ResponseField name="endpoint" type="string">
  The endpoint used for the batch
</ResponseField>

<ResponseField name="errors" type="object">
  Error information if any requests failed
</ResponseField>

<ResponseField name="input_file_id" type="string">
  The ID of the input file
</ResponseField>

<ResponseField name="completion_window" type="string">
  The completion time frame
</ResponseField>

<ResponseField name="status" type="string">
  The current status of the batch:

  * `validating` - Checking the input file format
  * `in_progress` - Processing the requests
  * `finalizing` - Generating output files
  * `completed` - All requests processed
  * `failed` - Batch processing failed
  * `cancelled` - Batch was cancelled
</ResponseField>

<ResponseField name="output_file_id" type="string">
  The ID of the file containing the outputs (available when status is `completed`)
</ResponseField>

<ResponseField name="error_file_id" type="string">
  The ID of the file containing errors (if any)
</ResponseField>

<ResponseField name="created_at" type="integer">
  Unix timestamp of when the batch was created
</ResponseField>

<ResponseField name="in_progress_at" type="integer">
  Unix timestamp of when the batch started processing
</ResponseField>

<ResponseField name="completed_at" type="integer">
  Unix timestamp of when the batch completed
</ResponseField>

<ResponseField name="request_counts" type="object">
  Statistics about the batch requests

  <Expandable title="request_counts object">
    <ResponseField name="total" type="integer">
      Total number of requests in the batch
    </ResponseField>

    <ResponseField name="completed" type="integer">
      Number of successfully completed requests
    </ResponseField>

    <ResponseField name="failed" type="integer">
      Number of failed requests
    </ResponseField>
  </Expandable>
</ResponseField>

<ResponseField name="metadata" type="object">
  Custom metadata attached to the batch
</ResponseField>

## Example

<CodeGroup>
  ```bash cURL theme={null}
  curl https://localhost:8787/v1/batches/batch_abc123 \
    -H "x-portkey-provider: openai" \
    -H "Authorization: Bearer $OPENAI_API_KEY"
  ```

  ```python Python theme={null}
  from portkey_ai import Portkey
  import time

  client = Portkey(
      provider="openai",
      Authorization="sk-***"
  )

  # Poll for batch completion
  batch_id = "batch_abc123"
  while True:
      batch = client.batches.retrieve(batch_id)
      print(f"Status: {batch.status}")
      
      if batch.status == "completed":
          print(f"Batch completed!")
          print(f"Output file: {batch.output_file_id}")
          break
      elif batch.status in ["failed", "cancelled"]:
          print(f"Batch {batch.status}")
          break
      
      time.sleep(60)  # Wait 1 minute before checking again

  # Download results
  if batch.status == "completed":
      output_content = client.files.retrieve_content(batch.output_file_id)
      with open("batch_results.jsonl", "wb") as f:
          f.write(output_content)
  ```

  ```javascript JavaScript theme={null}
  import Portkey from 'portkey-ai';

  const client = new Portkey({
      provider: "openai",
      Authorization: "sk-***"
  });

  // Poll for batch completion
  const batchId = "batch_abc123";
  const checkBatch = async () => {
      while (true) {
          const batch = await client.batches.retrieve(batchId);
          console.log(`Status: ${batch.status}`);
          
          if (batch.status === "completed") {
              console.log(`Batch completed!`);
              console.log(`Output file: ${batch.output_file_id}`);
              
              // Download results
              const outputContent = await client.files.retrieveContent(batch.output_file_id);
              // Process output content
              break;
          } else if (["failed", "cancelled"].includes(batch.status)) {
              console.log(`Batch ${batch.status}`);
              break;
          }
          
          await new Promise(resolve => setTimeout(resolve, 60000)); // Wait 1 minute
      }
  };

  checkBatch();
  ```
</CodeGroup>

### Response Example

```json theme={null}
{
  "id": "batch_abc123",
  "object": "batch",
  "endpoint": "/v1/chat/completions",
  "errors": null,
  "input_file_id": "file-abc123",
  "completion_window": "24h",
  "status": "completed",
  "output_file_id": "file-xyz789",
  "error_file_id": "file-err456",
  "created_at": 1713894800,
  "in_progress_at": 1713894900,
  "completed_at": 1713898500,
  "request_counts": {
    "total": 100,
    "completed": 98,
    "failed": 2
  },
  "metadata": {
    "description": "Daily batch job"
  }
}
```

## Output File Format

Once the batch is completed, download the output file:

```json theme={null}
{"id": "batch_req_abc123", "custom_id": "request-1", "response": {"status_code": 200, "body": {"id": "chatcmpl-123", "object": "chat.completion", "created": 1713894800, "model": "gpt-4o-mini", "choices": [{"index": 0, "message": {"role": "assistant", "content": "2+2 equals 4."}, "finish_reason": "stop"}]}}, "error": null}
{"id": "batch_req_def456", "custom_id": "request-2", "response": {"status_code": 200, "body": {"id": "chatcmpl-456", "object": "chat.completion", "created": 1713894801, "model": "gpt-4o-mini", "choices": [{"index": 0, "message": {"role": "assistant", "content": "The capital of France is Paris."}, "finish_reason": "stop"}]}}, "error": null}
```

## Best Practices

<Note>
  Batch processing typically takes several hours. Implement polling logic with appropriate intervals (1-5 minutes) rather than continuous polling.
</Note>

<Tip>
  Use webhooks (if supported by your provider) to get notified when batches complete instead of polling.
</Tip>

## Related Endpoints

<CardGroup cols={2}>
  <Card title="Create Batch" icon="plus" href="/api/batches/create">
    Create a new batch
  </Card>

  <Card title="Cancel Batch" icon="xmark" href="/api/batches/cancel">
    Cancel a running batch
  </Card>

  <Card title="List Batches" icon="list" href="/api/batches/list">
    View all batches
  </Card>
</CardGroup>
