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

# Batches

> Perform batch inference with Azure OpenAI

With Prisma AIRS AI Gateway, you can perform [Azure OpenAI Batch Inference](https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/batch?tabs=global-batch%2Cstandard-input%2Cpython-secure\&pivots=rest-api) operations.
This is the most efficient way to

* Test your data with different foundation models
* Perform A/B testing with different foundation models
* Perform batch inference with different foundation models

<Info>
  The AI Gateway supports **two modes** on Azure OpenAI:

  * **[Provider Batch API](#using-azure-openai-batch-api-through-the-ai-gateway)** (cheapest, completion\_window: `24h`, `48h`, etc.)
  * **[AI Gateway Batch API](/docs/aigw/product/ai-gateway/batches#portkey-batch-api-mode)** (fast, provider-agnostic, completion\_window: `immediate`)
</Info>

## Using Azure OpenAI Batch API through the AI Gateway

### Create Batch Job

<CodeGroup>
  ```bash curl theme={"system"}
  curl --location 'https://aigw.portkey.ai/v1/batches' \
  --header 'Authorization: Bearer <portkey_api_key>' \
  --header 'x-portkey-provider: @provider' \
  --header 'Content-Type: application/json' \
  --data '{
      "input_file_id": "<file_id>",
      "endpoint": "<endpoint>",
      "completion_window": "<completion_window>",
      "metadata": {}
  }'
  ```

  ```javascript OpenAI NodeJS theme={"system"}
  import OpenAI from 'openai'; // We're using the v4 SDK

  const openai = new OpenAI({
    apiKey: "PORTKEY_API_KEY",  // defaults to process.env["PORTKEY_API_KEY"]
    baseURL: "https://aigw.portkey.ai/v1",
    defaultHeaders: {
        "x-portkey-provider": "openai",
    }
  });

  const startBatch = async () => {
    const startBatchResponse = await openai.batches.create({
      input_file_id: "file_id", // file id of the input file
      endpoint: "endpoint", // ex: /v1/chat/completions
      completion_window: "completion_window", // ex: 24h
      metadata: {} // metadata for the batch
    });

    console.log(startBatchResponse);
  }

  await startBatch();
  ```

  ```python OpenAI Python theme={"system"}
  from openai import OpenAI

  openai = OpenAI(
      api_key="PORTKEY_API_KEY",
      base_url="https://aigw.portkey.ai/v1",
      default_headers={"x-portkey-provider": "openai"}
  )

  start_batch_response = openai.batches.create(
    input_file_id="file_id", # file id of the input file
    endpoint="endpoint", # ex: /v1/chat/completions
    completion_window="completion_window", # ex: 24h
    metadata={} # metadata for the batch
  )

  print(start_batch_response)
  ```
</CodeGroup>

### Create Batch Job with Blob Storage

<CodeGroup>
  ```bash curl theme={"system"}
  curl --location 'https://aigw.portkey.ai/v1/batches' \
  --header 'Authorization: Bearer <portkey_api_key>' \
  --header 'x-portkey-provider: @provider' \
  --header 'Content-Type: application/json' \
  --data '{
      "endpoint": "<endpoint>",
      "completion_window": "<completion_window>",
      "metadata": {},
      "input_blob": "<blob_url>",
      "output_folder": {
        "url": "<output_blob_folder>" # both error file and output file will be saved in this folder
      }
  }'
  ```

  ```javascript OpenAI NodeJS theme={"system"}
  import OpenAI from 'openai'; // We're using the v4 SDK

  const openai = new OpenAI({
    apiKey: "PORTKEY_API_KEY",  // defaults to process.env["PORTKEY_API_KEY"]
    baseURL: "https://aigw.portkey.ai/v1",
    defaultHeaders: {
        "x-portkey-provider": "openai",
    }
  });

  const startBatch = async () => {
    const startBatchResponse = await openai.batches.create({
      endpoint: "endpoint", // ex: /v1/chat/completions
      completion_window: "completion_window", // ex: 24h
      metadata: {}, // metadata for the batch
      extra_body: {
        input_blob: "<blob_url>",
        output_folder: {
          url: "<output_blob_folder>" // both error file and output file will be saved in this folder
        }
      }
    });

    console.log(startBatchResponse);
  }

  await startBatch();
  ```

  ```python OpenAI Python theme={"system"}
  from openai import OpenAI

  openai = OpenAI(
      api_key="PORTKEY_API_KEY",
      base_url="https://aigw.portkey.ai/v1",
      default_headers={"x-portkey-provider": "openai"}
  )

  start_batch_response = openai.batches.create(
    endpoint="endpoint",  # ex: /v1/chat/completions
    completion_window="completion_window",  # ex: 24h
    metadata={},  # metadata for the batch
    extra_body={
      "input_blob": "<blob_url>",
      "output_folder": {
        "url": "<output_blob_folder>"  # both error file and output file will be saved in this folder
      }
    }
  )

  print(start_batch_response)
  ```
</CodeGroup>

### List Batch Jobs

<CodeGroup>
  ```bash curl theme={"system"}
  curl --location 'https://aigw.portkey.ai/v1/batches' \
  --header 'Authorization: Bearer <portkey_api_key>' \
  --header 'x-portkey-provider: @provider'
  ```

  ```javascript OpenAI NodeJS theme={"system"}
  import OpenAI from 'openai'; // We're using the v4 SDK

  const openai = new OpenAI({
    apiKey: "PORTKEY_API_KEY",  // defaults to process.env["PORTKEY_API_KEY"]
    baseURL: "https://aigw.portkey.ai/v1",
    defaultHeaders: {
        "x-portkey-provider": "openai",
    }
  });

  const listBatches = async () => {
    const batches = await openai.batches.list();

    console.log(batches);
  }

  await listBatches();
  ```

  ```python OpenAI Python theme={"system"}
  from openai import OpenAI

  openai = OpenAI(
      api_key="PORTKEY_API_KEY",
      base_url="https://aigw.portkey.ai/v1",
      default_headers={"x-portkey-provider": "openai"}
  )

  batches = openai.batches.list()

  print(batches)
  ```
</CodeGroup>

### Get Batch Job Details

<CodeGroup>
  ```bash curl theme={"system"}
  curl --location 'https://aigw.portkey.ai/v1/batches/<batch_id>' \
  --header 'Authorization: Bearer <portkey_api_key>' \
  --header 'x-portkey-provider: @provider'
  ```

  ```javascript OpenAI NodeJS theme={"system"}
  import OpenAI from 'openai'; // We're using the v4 SDK

  const openai = new OpenAI({
    apiKey: "PORTKEY_API_KEY",  // defaults to process.env["PORTKEY_API_KEY"]
    baseURL: "https://aigw.portkey.ai/v1",
    defaultHeaders: {
        "x-portkey-provider": "openai",
    }
  });

  const getBatch = async () => {
    const batch = await openai.batches.retrieve(batch_id="batch_id");

    console.log(batch);
  }

  await getBatch();
  ```

  ```python OpenAI Python theme={"system"}
  from openai import OpenAI

  openai = OpenAI(
      api_key="PORTKEY_API_KEY",
      base_url="https://aigw.portkey.ai/v1",
      default_headers={"x-portkey-provider": "openai"}
  )

  batch = openai.batches.retrieve(batch_id="batch_id")

  print(batch)
  ```
</CodeGroup>

### Get Batch Output

<CodeGroup>
  ```bash curl theme={"system"}
  curl --location 'https://aigw.portkey.ai/v1/batches/<batch_id>/output' \
  --header 'Authorization: Bearer <portkey_api_key>' \
  --header 'x-portkey-provider: @provider'
  ```
</CodeGroup>

### List Batch Jobs

<CodeGroup>
  ```bash curl theme={"system"}
  curl --location 'https://aigw.portkey.ai/v1/batches' \
  --header 'Authorization: Bearer <portkey_api_key>' \
  --header 'x-portkey-provider: @provider'
  ```

  ```javascript OpenAI NodeJS theme={"system"}
  import OpenAI from 'openai'; // We're using the v4 SDK

  const openai = new OpenAI({
    apiKey: "PORTKEY_API_KEY",  // defaults to process.env["PORTKEY_API_KEY"]
    baseURL: "https://aigw.portkey.ai/v1",
    defaultHeaders: {
        "x-portkey-provider": "openai",
    }
  });

  const listBatchingJobs = async () => {
    const batching_jobs = await openai.batches.list();

    console.log(batching_jobs);
  }

  await listBatchingJobs();
  ```

  ```python OpenAI Python theme={"system"}
  from openai import OpenAI

  openai = OpenAI(
      api_key="PORTKEY_API_KEY",
      base_url="https://aigw.portkey.ai/v1",
      default_headers={"x-portkey-provider": "openai"}
  )

  batching_jobs = openai.batches.list()

  print(batching_jobs)
  ```
</CodeGroup>

### Cancel Batch Job

<CodeGroup>
  ```bash curl theme={"system"}
  curl --request POST --location 'https://aigw.portkey.ai/v1/batches/<batch_id>/cancel' \
  --header 'Authorization: Bearer <portkey_api_key>' \
  --header 'x-portkey-provider: @provider'
  ```

  ```javascript OpenAI NodeJS theme={"system"}
  import OpenAI from 'openai'; // We're using the v4 SDK

  const openai = new OpenAI({
    apiKey: "PORTKEY_API_KEY",  // defaults to process.env["PORTKEY_API_KEY"]
    baseURL: "https://aigw.portkey.ai/v1",
    defaultHeaders: {
        "x-portkey-provider": "openai",
    }
  });

  const cancelBatch = async () => {
    const cancel_batch_response = await openai.batches.cancel(batch_id="batch_id");

    console.log(cancel_batch_response);
  }

  await cancelBatch();
  ```

  ```python OpenAI Python theme={"system"}
  from openai import OpenAI

  openai = OpenAI(
      api_key="PORTKEY_API_KEY",
      base_url="https://aigw.portkey.ai/v1",
      default_headers={"x-portkey-provider": "openai"}
  )

  cancel_batch_response = openai.batches.cancel(batch_id="batch_id")

  print(cancel_batch_response)
  ```
</CodeGroup>


## Related topics

- [Batches](/docs/aigw/integrations/llms/claude-platform-aws/batches.md)
