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

# Batch Processing

> Learn to send multiple prompts using OpenAI’s Python client, covering setup, code structure, and best practices for efficient batch processing.

Learn how to send multiple prompts in one go using OpenAI’s Python client. This guide covers setup, code structure, and best practices for efficient batch processing.

## Table of Contents

1. [Prerequisites](#prerequisites)
2. [Install & Import](#install--import)
3. [Define Your Prompts](#define-your-prompts)
4. [Helper Function for a Single Prompt](#helper-function-for-a-single-prompt)
5. [Batch Processing Loop](#batch-processing-loop)
6. [Inspecting Results](#inspecting-results)
7. [Run the Script](#run-the-script)
8. [Reference Links](#reference-links)

## Prerequisites

* Python 3.7+
* An OpenAI API key
* `openai` Python package

<Callout icon="lightbulb" color="#1CB2FE">
  Store your API key as an environment variable for security:

  ```bash theme={null}
  export OPENAI_API_KEY="sk-your-api-key-here"
  ```

  Alternatively, pass it directly in code (not recommended for production).
</Callout>

## Install & Import

Install the OpenAI Python client:

```shell theme={null}
pip install openai
```

Then import and initialize the client:

```python theme={null}
from openai import OpenAI

client = OpenAI(api_key="sk-your-api-key-here")
```

## Define Your Prompts

Build a list of user prompts to process in batch:

```python theme={null}
prompts = [
    "Tell me a story about a warrior princess",
    "Generate a list of 5 business ideas",
    "Explain the theory of relativity in simpler terms",
    "Write a poem about Michael Jordan"
]
```

Feel free to extend this list to dozens or hundreds of items.

## Helper Function for a Single Prompt

Encapsulate the API call in a reusable function:

```python theme={null}
def process_prompt(prompt: str) -> str:
    response = client.chat.completions.create(
        model="gpt-4",
        messages=[{"role": "user", "content": prompt}],
        max_tokens=250,
        temperature=0.8
    )
    return response.choices[0].message.content
```

### Model Parameters

| Parameter   | Description                           | Example |
| ----------- | ------------------------------------- | ------- |
| model       | The chat model to use                 | `gpt-4` |
| messages    | Conversation history for the model    | `[...]` |
| max\_tokens | Maximum number of tokens in the reply | `250`   |
| temperature | Controls randomness (0.0–1.0)         | `0.8`   |

## Batch Processing Loop

Iterate through all prompts and collect responses:

```python theme={null}
results = []

for prompt in prompts:
    reply = process_prompt(prompt)
    results.append(reply)
```

<Callout icon="triangle-alert" color="#FF6B6B">
  Batch requests can incur higher costs and rate limits. Monitor your usage in the [OpenAI Dashboard](https://platform.openai.com/usage).
</Callout>

## Inspecting Results

Print each prompt alongside its generated response:

```python theme={null}
for idx, (prompt, reply) in enumerate(zip(prompts, results), start=1):
    print(f"Prompt {idx}: {prompt}")
    print(f"Response {idx}:\n{reply}\n")
```

Sample output:

```text theme={null}
Prompt 1: Tell me a story about a warrior princess
Response 1:
Once upon a time in the verdant kingdom of Eldoria, a fierce warrior princess...

Prompt 2: Generate a list of 5 business ideas
Response 2:
1. Eco-friendly packaging startup
2. Virtual event planning service
...

Prompt 3: Explain the theory of relativity in simpler terms
Response 3:
The theory of relativity, proposed by Albert Einstein, shows how time and space are linked...

Prompt 4: Write a poem about Michael Jordan
Response 4:
In courts of hardwood, he stood so tall...
```

## Run the Script

Save the code to `batch_processing.py` and execute:

```shell theme={null}
python batch_processing.py
```

Extend or customize the `process_prompt` function to add streaming output, error handling, or alternative model parameters as needed.

## Reference Links

* [OpenAI Python SDK](https://github.com/openai/openai-python)
* [Chat Completions API](https://platform.openai.com/docs/api-reference/chat)
* [Pricing & Rate Limits](https://platform.openai.com/pricing)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/introduction-to-openai/module/42afe984-cd3e-4b3c-b1e0-8e9093f57a63/lesson/ed59d365-b517-4f70-ac1d-6b3ed61cc88d" />

  <Card title="Practice Lab" icon="installation" cta="Learn more" href="https://learn.kodekloud.com/user/courses/introduction-to-openai/module/42afe984-cd3e-4b3c-b1e0-8e9093f57a63/lesson/2806fb1b-967e-44b8-ac17-6724eda29c0f" />
</CardGroup>
