Managing and Reducing Costs › Strategy 4: Batch Similar Requests
Michael BrenndoerferAugust 26, 20252 min read
AI Agent HandbookAll chapters Part 15 of 15Performance and Cost Optimization - Doing More with Less
Managing and Reducing Costs › Strategy 4: Batch Similar RequestsOne section · Sections in this chapter7 / 15 ↓
Sections in this chapter15
If you need to process multiple similar queries, batch them into a single API call. This reduces overhead and can be more cost-effective.
Example: Batch Processing (GPT-5)
In[8]:
Codefrom openai import OpenAI
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
def process_individually(queries):
"""Process each query separately (expensive)."""
results = []
total_cost = 0.0
for query in queries:
response = client.chat.completions.create(
model="gpt-5",
max_completion_tokens=100,
messages=[{"role": "user", "content": query}]
)
# Estimate cost (rough approximation)
tokens = response.usage.total_tokens
cost = (tokens / 1_000_000) * 6.25 # Average of input/output rates
total_cost += cost
results.append(response.choices[0].message.content)
return results, total_cost
def process_batched(queries):
"""Process all queries in a single call (cheaper)."""
# Combine queries into a single prompt
batch_prompt = "Answer each of the following questions concisely:\n\n"
for i, query in enumerate(queries, 1):
batch_prompt += f"{i}. {query}\n"
response = client.chat.completions.create(
model="gpt-5",
max_completion_tokens=500,
messages=[{"role": "user", "content": batch_prompt}]
)
# Estimate cost
tokens = response.usage.total_tokens
cost = (tokens / 1_000_000) * 6.25
# Parse the batched response
result = response.choices[0].message.content
return result, cost
## Test both approaches
queries = [
"What is Python?",
"What is JavaScript?",
"What is Ruby?",
"What is Go?",
"What is Rust?"
]
print("Individual processing:")
results_individual, cost_individual = process_individually(queries)
print(f"Cost: ${cost_individual:.4f}")
print()
print("Batched processing:")
result_batched, cost_batched = process_batched(queries)
print(f"Cost: ${cost_batched:.4f}")
print(f"Savings: ${cost_individual - cost_batched:.4f} ({((cost_individual - cost_batched) / cost_individual * 100):.1f}%)")
print()
print("Batched response:")
print(result_batched)Out[8]:
ConsoleIndividual processing:
Cost: $0.0034 Batched processing:
Cost: $0.0034 Savings: $0.0000 (0.7%) Batched response:
Batching can save 30-50% on costs for similar queries because you eliminate the overhead of multiple API calls and can share context more efficiently.