Build an LLM Benchmarking Platform (Python Project)
Define test suites, run them across LLM providers with raw HTTP, score with an LLM judge, and build reports that show which model wins, all from scratch.
You spent two weeks building a customer support chatbot. Your manager asks: “Should we use GPT-4o-mini, Claude Haiku, or Gemini Flash?” You check public leaderboards. They test math puzzles and trivia, nothing like your support tickets. So you guess. And your guess costs the company money every month on the wrong model.
What you need is your own benchmarking system, one that tests your prompts on your tasks and tells you which model actually wins. With numbers. That’s what we’ll build here.
Before we write a single class, let me show you how the pieces fit together.
You start with a test suite, prompts paired with expected answers. Each test case defines what a “good” response looks like. The benchmarking platform fires every prompt at every provider, OpenAI, Anthropic, Google, using raw HTTP. No SDKs. No external packages. Just urllib.request.
Once responses come back, a judge evaluates them. The judge is itself an LLM. It reads each response alongside the expected answer and scores it on criteria you pick: accuracy, completeness, clarity.
With scores in hand, the stats engine finds the mean, spread, and a 95% range for each model. This tells you not just which model scored highest, but whether the gap is real or just noise from too few test cases.
Finally, the report builder puts out a ranked table, per-metric winners, and a check on whether the top models truly differ. You’ll know which model to pick, with numbers to back it up.
Let’s build it.
What Do You Need Before Starting?
- Python version: 3.10+
- Required libraries: None beyond the standard library (
json,urllib.request,math,statistics,time,os) - API keys: You need at least one LLM provider API key. We use OpenAI, Anthropic, and Google Gemini. Get them from:
- OpenAI: platform.openai.com/api-keys
- Anthropic: console.anthropic.com
- Google Gemini: aistudio.google.com/apikey
- Time to complete: 35–40 minutes
- Runs in the browser: Yes, all code runs in-browser (API calls need real keys)
That single import block is everything. Zero external packages. The entire LLM benchmarking platform runs on Python’s standard library.
How Do You Define a Test Suite for LLM Evaluation?
A test suite is a list of test cases. Each one has a prompt, a expected answer, and a category. Groups let you compare models per task, maybe GPT excels at code but Gemini wins at summaries.
I keep the data classes minimal on purpose. TestCase holds three fields. TestSuite wraps a list and gives you iteration.
Here’s a suite that tests four different skills. Each category targets a real capability you’d care about.
Quick check: What happens if you skip the category parameter? The default is "general", all results land in one bucket. Always set categories when you want per-task analysis.
How Do You Compare LLM Providers with Raw HTTP?
Most guides use SDK packages like openai or anthropic. We’re going a different route on purpose. Raw HTTP means you see what goes over the wire, headers, body, and reply format. You learn what the SDK hides. And you get zero outside packages.
The LLMProvider class wraps one API endpoint. It holds the URL, API key, model name, and two helper functions. One builds the JSON body. The other pulls text from the reply. Each provider has a different JSON shape, so these helpers handle those gaps.
The call method builds the request, fires it, measures round-trip time, and pulls out the text. Clean and predictable.
Now let’s create factory functions for each provider. I’ll show them one at a time so you see how the JSON payloads differ.
OpenAI uses /v1/chat/completions. The body takes a messages array with role/content pairs. The API key goes in the Authorization header.
Anthropic uses /v1/messages. It needs a special x-api-key header plus an anthropic-version header. The response nests text inside content[0].text.
Google Gemini puts the API key in the URL itself, not in headers. The body uses a contents array with parts.
See the pattern? Each factory defines two inner functions, then returns an LLMProvider. The only changes are the URL, headers, and JSON shape. Adding a new provider follows the same steps.
Set up providers with your keys. Environment variables keep secrets out of code.
temperature=0.0 when benchmarking LLM providers. Non-zero temperature adds randomness. You want score differences to reflect model quality, not sampling luck.How Does the Multi-Provider Benchmark Runner Work?
The runner loops through every test case and every provider. For each pair, it calls the API, saves the reply and timing, and moves on. If a call fails, bad network, rate limit, timeout, it logs the error and keeps going.
BenchmarkResult holds one reply. BenchmarkRunner runs the loop. Together they cover the full grid of test cases times providers.
Predict the output: You have 4 test cases and 3 providers. How many API calls? Think first.
Answer: 4 x 3 = 12. Every test case hits every provider.
Let’s run it.
Progress prints as each call completes:
Running 4 cases x 3 providers...
[1/12] OpenAI (gpt-4o-mini) <- What is the capital of Australia?...
[2/12] Anthropic (claude-3-5-haiku-20241022) <- What is the capital of Australia?...
[3/12] Google (gemini-2.0-flash) <- What is the capital of Australia?...
[4/12] OpenAI (gpt-4o-mini) <- Explain why the sky is blue in 2-3...
[5/12] Anthropic (claude-3-5-haiku-20241022) <- Explain why the sky is blue in 2-3...
[6/12] Google (gemini-2.0-flash) <- Explain why the sky is blue in 2-3...
[7/12] OpenAI (gpt-4o-mini) <- Write a Python function that return...
[8/12] Anthropic (claude-3-5-haiku-20241022) <- Write a Python function that return...
[9/12] Google (gemini-2.0-flash) <- Write a Python function that return...
[10/12] OpenAI (gpt-4o-mini) <- Summarize gradient descent in exac...
[11/12] Anthropic (claude-3-5-haiku-20241022) <- Summarize gradient descent in exac...
[12/12] Google (gemini-2.0-flash) <- Summarize gradient descent in exac...
Done: 12 successes, 0 failures
time.sleep(1) between calls. The runner’s try/except catches failures gracefully, errors get logged, not lost.How Does LLM-as-Judge Scoring Work?
Here’s the part I find most fun. Instead of string matching or BLEU scores, we use another LLM to grade each reply. I prefer this because it catches meaning, not just word overlap. Did the reply actually answer the question well? A simple metric can’t tell.
The judge reads three things: the original prompt, the expected answer, and the model’s response. Then it scores on accuracy (1-5), completeness (1-5), and clarity (1-5).
The LLMJudge class builds a tight prompt that forces JSON output. It strips markdown fences the judge might add and pins scores to the 1-5 range.
Why force JSON? It’s easy to parse. The “ONLY a JSON object” line works well across most models. The fallback catches edge cases where the judge wraps output in code fences or sends back odd text.
Score all benchmark results.
The judge makes one call per result:
Scoring responses with LLM judge...
Judging: OpenAI (gpt-4o-mini) on [factual]
Judging: Anthropic (claude-3-5-haiku-20241022) on [factual]
Judging: Google (gemini-2.0-flash) on [factual]
Judging: OpenAI (gpt-4o-mini) on [explanation]
Judging: Anthropic (claude-3-5-haiku-20241022) on [explanation]
Judging: Google (gemini-2.0-flash) on [explanation]
Judging: OpenAI (gpt-4o-mini) on [code]
Judging: Anthropic (claude-3-5-haiku-20241022) on [code]
Judging: Google (gemini-2.0-flash) on [code]
Judging: OpenAI (gpt-4o-mini) on [summarization]
Judging: Anthropic (claude-3-5-haiku-20241022) on [summarization]
Judging: Google (gemini-2.0-flash) on [summarization]
Scored 12 results
What if you swap the judge model? Try using gpt-4o instead of gpt-4o-mini as the judge. Stronger judges give sharper scores, they tell a 3 from a 4 more clearly. Weaker judges tend to rate everything 4 or 5.
How Do You Compute Statistical Comparisons Between LLM Models?
Raw averages are tempting. “Model A scored 4.2, Model B scored 4.0, A wins!” But with only 4 test cases, that gap could be pure noise.
This is where confidence intervals save you from bad conclusions.
A confidence interval (CI) gives you a range where the true average likely sits. The 95% CI says: “Run this test 100 times, and 95 of those times the real mean lands in this range.”
For small samples, and 4 test cases is small, we use the t-table instead of the normal curve. Why? The t-table has fatter tails. It makes wider ranges when you have less data. With only 4 data points, you should be less sure, and the t-table forces that.
The StatisticalAnalyzer groups results by provider and works out stats for each score type plus an overall blend.
The analyze method ties it all up. It works out per-metric stats, an overall blend (mean of all three scores), and mean latency for each provider.
Run the analysis.
analyzer = StatisticalAnalyzer(scored_results)
summary = analyzer.analyze()
print("=" * 65)
print("STATISTICAL SUMMARY")
print("=" * 65)
for provider, stats in summary.items():
print(f"\n{provider}")
print("-" * 40)
for metric in ("accuracy", "completeness", "clarity", "overall"):
s = stats[metric]
print(f" {metric:14s}: {s['mean']:.2f} +/- {s['std']:.2f} "
f"95% CI [{s['ci_low']:.2f}, {s['ci_high']:.2f}]")
print(f" {'latency':14s}: {stats['latency_avg']:.3f}s avg")
#> =================================================================
#> STATISTICAL SUMMARY
#> =================================================================
#>
#> OpenAI (gpt-4o-mini)
#> ----------------------------------------
#> accuracy : 4.50 +/- 0.58 95% CI [3.70, 5.00]
#> completeness : 4.25 +/- 0.50 95% CI [3.56, 4.94]
#> clarity : 4.75 +/- 0.50 95% CI [4.06, 5.00]
#> overall : 4.50 +/- 0.41 95% CI [3.93, 5.00]
#> latency : 1.234s avg
#>
#> Anthropic (claude-3-5-haiku-20241022)
#> ----------------------------------------
#> accuracy : 4.75 +/- 0.50 95% CI [4.06, 5.00]
#> completeness : 4.50 +/- 0.58 95% CI [3.70, 5.00]
#> clarity : 4.50 +/- 0.58 95% CI [3.70, 5.00]
#> overall : 4.58 +/- 0.38 95% CI [4.05, 5.00]
#> latency : 0.987s avg
#>
#> Google (gemini-2.0-flash)
#> ----------------------------------------
#> accuracy : 4.25 +/- 0.96 95% CI [2.92, 5.00]
#> completeness : 4.00 +/- 0.82 95% CI [2.86, 5.14]
#> clarity : 4.50 +/- 0.58 95% CI [3.70, 5.00]
#> overall : 4.25 +/- 0.63 95% CI [3.37, 5.13]
#> latency : 0.756s avg
Look at those ranges. With 4 test cases, they span over a full point. We can’t safely pick a winner yet. That’s honest math, and that’s the whole point of using CIs.
How Do You Generate an LLM Comparison Report?
Stats in a dict aren’t useful to your boss. You need a report that tells a story: who won, who’s fastest, and whether the gap is real.
I find auto-reports save a ton of time. Run the test, get a report. No spreadsheet work.
The ReportGenerator takes the summary from the analyzer. It builds a leaderboard, flags per-metric winners, and checks whether the top two providers’ CIs overlap.
Generate and print.
report_gen = ReportGenerator(summary)
print(report_gen.generate())
#> =================================================================
#> LLM BENCHMARKING REPORT
#> =================================================================
#>
#> ## LEADERBOARD
#>
#> Rank Provider Score Latency
#> -----------------------------------------------------------------
#> 1 Anthropic (claude-3-5-haiku-20241022) 4.58 0.987s
#> 2 OpenAI (gpt-4o-mini) 4.50 1.234s
#> 3 Google (gemini-2.0-flash) 4.25 0.756s
#>
#> ## PER-METRIC WINNERS
#>
#> accuracy : Anthropic (claude-3-5-haiku-20241022) (4.75)
#> completeness : Anthropic (claude-3-5-haiku-20241022) (4.50)
#> clarity : OpenAI (gpt-4o-mini) (4.75)
#> speed : Google (gemini-2.0-flash) (0.756s)
#>
#> ## SIGNIFICANCE CHECK
#>
#> No significant difference between top 2.
#> -> Run more test cases for conclusive results.
#>
#> =================================================================
That last check is the most useful line. Without it, you’d call Anthropic the winner from a 0.08-point lead. The ranges overlap heavily, that lead is noise with only 4 test cases.
How Do You Run the Complete LLM Benchmark Pipeline?
The BenchmarkPipeline chains everything. One call runs benchmarks, scores them, computes statistics, and generates the report. No manual wiring.
Quick demo with a fresh suite.
quick_suite = TestSuite("Quick Test", "Sanity check")
quick_suite.add_case(
prompt="What is 15% of 200? Show your work.",
expected_answer="15% of 200 = 0.15 * 200 = 30",
category="math"
)
quick_suite.add_case(
prompt="Name three sorting algorithms with their average time complexity.",
expected_answer="Bubble Sort O(n^2), Merge Sort O(n log n), Quick Sort O(n log n)",
category="factual"
)
pipeline = BenchmarkPipeline(
suite=quick_suite,
providers=providers,
judge_provider=make_openai_provider(OPENAI_KEY, "gpt-4o-mini"),
)
report_text, all_results, all_summary = pipeline.execute()
print(report_text)
How Does This Compare to Existing LLM Evaluation Frameworks?
You might wonder: why build from scratch when DeepEval, RAGAS, and LangSmith exist?
| Feature | Our Platform | DeepEval | RAGAS |
|---|---|---|---|
| Dependencies | Zero (stdlib only) | 15+ packages | 10+ packages |
| Setup time | 5 minutes | 30+ minutes | 20+ minutes |
| Learning value | High (you built it) | Low (black box) | Low (black box) |
| Statistical analysis | Built-in CIs | Basic metrics | Basic metrics |
| Provider flexibility | Any HTTP endpoint | SDK-based | SDK-based |
| Production use | Prototyping | Production | RAG-specific |
| Customization | Total control | Plugin system | Limited |
Our tool isn’t meant to replace those. It’s meant to show you how they work under the hood. Once you get the core ideas, you can pick the right one, or grow this one.
Common Mistakes When Benchmarking LLM Models
Mistake 1: Non-Zero Temperature for Benchmarks
❌ Wrong:
"temperature": 0.7, # randomness in every response
Why: Random output means the same prompt gives a different reply each run. Your scores lose meaning.
✅ Fix:
"temperature": 0.0, # deterministic for fair comparison
Mistake 2: Same Model as Contestant and Judge
❌ Wrong:
judge = LLMJudge(make_openai_provider(key, model="gpt-4o-mini"))
providers = [make_openai_provider(key, model="gpt-4o-mini")]
Why: Models like their own style, they rate their own output higher. Zheng et al. (2023) proved this.
✅ Fix:
judge = LLMJudge(make_openai_provider(key, model="gpt-4o"))
providers = [make_openai_provider(key, model="gpt-4o-mini")]
Use a stronger, different model as judge.
Mistake 3: Declaring a Winner from Overlapping CIs
❌ Wrong:
Why: Overlapping CIs mean the difference could be random. You can’t declare a winner.
✅ Fix:
if model_a_ci_low > model_b_ci_high:
print("A is significantly better")
else:
print("Need more test cases to conclude")
When Should You NOT Use This Platform?
Every tool has real limits. Know yours before you lean on it.
Don’t use it for live speed tests. Our runner tracks wall-clock time with network hops baked in. Real speed depends on where your server sits, your link, and the load at the time. Use load-test tools for that.
Don’t use it for cost math. Token counts differ per provider and per reply. A true cost check needs token tracking with per-model pricing, and those prices shift each month.
Don’t use it with fewer than 15 test cases. With 4 cases, the ranges are too wide to draw clear lines. The tool works, but the results won’t drive real choices.
Summary
You built a complete LLM benchmarking platform from scratch. Here’s what each piece does:
- TestSuite / TestCase, stores prompts, expected answers, and categories
- LLMProvider, wraps raw HTTP calls to any LLM API
- BenchmarkRunner, runs every test case against every provider
- LLMJudge, uses an LLM to score responses on accuracy, completeness, clarity
- StatisticalAnalyzer, computes means, standard deviations, and 95% confidence intervals
- ReportGenerator, ranks providers and checks statistical significance
- BenchmarkPipeline, chains everything into a single call
Zero outside packages. Grow it by adding new providers (Mistral, Cohere, Ollama), custom score types, per-task breakdowns, or weighted scoring.
Frequently Asked Questions
Can I add a local model like Ollama as a provider?
Yes. Write a factory function pointing to Ollama’s endpoint, http://localhost:11434/api/generate. The request and response JSON differ from cloud APIs, so you need custom callables. The LLMProvider class handles any HTTP endpoint.
How many test cases give reliable benchmark results?
At least 15-20 per group. Fewer gives wide ranges. For choices that affect real costs, aim for 50+ cases across your true use cases.
What scoring criteria work best for code generation tasks?
Add “correctness” (does the code run?) and “efficiency” as criteria. Modify JUDGE_PROMPT to include them and update StatisticalAnalyzer to loop over the new metric names.
How do I handle rate limits with large test suites?
Add time.sleep(1) between calls in BenchmarkRunner.run. For heavy usage, add exponential backoff to LLMProvider.call, start at 1 second, double on each retry, cap at 30 seconds.
References
- OpenAI API Documentation, Chat Completions. Link
- Anthropic API Documentation, Messages. Link
- Google Gemini API Documentation, generateContent. Link
- Zheng, L. et al., “Judging LLM-as-a-Judge with MT-Bench and Chatbot Arena.” NeurIPS 2023. Link
- Sebastian Raschka, “Understanding the 4 Main Approaches to LLM Evaluation.” Link
- Python Documentation, statistics module. Link
- Python Documentation, urllib.request. Link
- Evidently AI, “30 LLM Evaluation Benchmarks and How They Work.” Link
- Confident AI, “How to Build an LLM Evaluation Framework from Scratch.” Link