Sub topic 10
Testing with a Small Pilot Batch Before Scaling
This chapter assumes you can build a JSONL request file, submit a batch, poll it, and read the output and error files, as covered in the earlier chapters. It also assumes basic Python and a working OpenAI API key.
A batch can take up to 24 hours to finish. A mistake in your prompt or request format can waste a full day and the money spent on the whole job. A pilot batch is a small copy of the real job, run first, to catch those mistakes cheaply.
What a pilot should prove
A pilot is not a quality benchmark. It answers four practical questions:
- Does every request pass validation? Wrong URLs, missing fields, and duplicate
custom_idvalues fail early. - Does the output parse? Your collection code should handle both the output file and the error file.
- Is the answer quality acceptable on real data? Read the outputs yourself.
- Does the cost match your estimate? Compare token usage with your plan.
Choosing the pilot sample
Do not take the first 50 rows. Files are often sorted, so the first rows may be unrepresentative. Take a random sample, and add a few edge cases on purpose: empty text, very long text, non-English text, and text with odd characters.
A sample of 20 to 100 requests is usually enough. Fix the random seed so the pilot is repeatable.
import json
import random
def build_pilot(source_path, pilot_path, size=50, seed=42):
with open(source_path, encoding="utf-8") as f:
lines = [line for line in f if line.strip()]
rng = random.Random(seed)
sample = rng.sample(lines, min(size, len(lines)))
ids = [json.loads(line)["custom_id"] for line in sample]
assert len(ids) == len(set(ids)), "custom_id values must be unique"
with open(pilot_path, "w", encoding="utf-8") as f:
f.writelines(sample)
return ids
pilot_ids = build_pilot("batch_input.jsonl", "pilot_input.jsonl")
print(len(pilot_ids), "requests in the pilot")The pilot file uses the same request lines as the full job. That matters: you are testing the exact prompts and parameters you will scale.
Running the pilot
Submit the pilot with the same code you will use for the full job. Do not write a separate test path. If the pilot uses different code, it proves nothing about the real run.
import time
from openai import OpenAI
client = OpenAI()
pilot_file = client.files.create(
file=open("pilot_input.jsonl", "rb"),
purpose="batch",
)
batch = client.batches.create(
input_file_id=pilot_file.id,
endpoint="/v1/chat/completions",
completion_window="24h",
)
terminal = {"completed", "failed", "expired", "cancelled"}
while batch.status not in terminal:
time.sleep(30)
batch = client.batches.retrieve(batch.id)
print(batch.status, batch.request_counts)Small batches often finish much faster than the window allows, but there is no guarantee. Treat the pilot's turnaround as a rough hint, not a promise for the full run.
Checking the results
After completion, run a short report. It should count successes and failures, show token usage, and print a few outputs for reading.
import json
total_tokens = 0
ok = 0
errors = 0
samples = []
text = client.files.content(batch.output_file_id).text
for line in text.splitlines():
record = json.loads(line)
response = record.get("response")
if record.get("error") is None and response and response["status_code"] == 200:
ok += 1
total_tokens += response["body"]["usage"]["total_tokens"]
if len(samples) < 5:
content = response["body"]["choices"][0]["message"]["content"]
samples.append((record["custom_id"], content))
else:
errors += 1
print(ok, "ok;", errors, "errors;", total_tokens, "tokens")
for custom_id, content in samples:
print(custom_id, "->", content)Divide the token count by the number of successes to get tokens per request. Multiply by the full job size to project the total cost.
Decision criteria before scaling
Scale up only when each of these holds:
| Check | Pass condition |
|---|---|
| Failed requests | Zero, or each failure is understood |
| Output format | Parses without special cases |
| Quality | Reviewed by a person on real samples |
| Projected cost | Within budget |
| Retry path | Failed `custom_id` values were resubmitted successfully |
Common mistakes
- Using a different prompt or model in the pilot than in the full job.
- Sampling only easy rows, so failures on hard rows appear only at scale.
- Skipping the manual read of outputs because the status says
completed. A completed batch can still hold poor answers. - Extrapolating cost from a pilot that was too small to include long inputs.
Exercise
Your pilot of 50 requests used 40,000 total tokens and all succeeded. The full job has 20,000 requests. What is the projected token count, and why might the real number differ?
Show answer
40,000 / 50 = 800 tokens per request, so 800 × 20,000 = 16,000,000 tokens. The real number can differ if the pilot sample did not represent the long or short inputs in the full data, which is why the sample should be random and include edge cases.