In this recipe you build a small script that sends the same prompts to several models and prints their answers, response time and cost in one table. Because Spro gives you every model behind one key and one API, you can compare them without changing any code.
What you need
- A Spro API key. See API keys.
- Python 3.10 or newer, and two packages:
pip install openai httpx.
Bash
export SPRO_API_KEY="YOUR_SPRO_API_KEY"The code
Save this as compare_models.py.
Python
import osimport time
import httpxfrom openai import OpenAI
BASE_URL = "https://spro.hridaai.com/v1"API_KEY = os.environ["SPRO_API_KEY"]
client = OpenAI(base_url=BASE_URL, api_key=API_KEY)
# Any models from the catalog. Add more to widen the comparison.MODELS = ["gpt-oss-20b", "gpt-oss-120b"]
PROMPTS = [ "Explain what an API rate limit is in two sentences.", "Write a one-line subject for an email announcing a delayed shipment.",]
def get_cost(request_id: str) -> float | None: """Look up what a request cost. Tries a few times in case the record isn't ready yet.""" for _ in range(3): resp = httpx.get( f"{BASE_URL}/generation/{request_id}", headers={"Authorization": f"Bearer {API_KEY}"}, ) if resp.status_code == 200: return resp.json().get("total_cost") time.sleep(1) return None
for prompt in PROMPTS: print(f"\nPROMPT: {prompt}\n") for model in MODELS: started = time.perf_counter() raw = client.chat.completions.with_raw_response.create( model=model, messages=[{"role": "user", "content": prompt}], ) seconds = time.perf_counter() - started
answer = raw.parse().choices[0].message.content.strip() cost = get_cost(raw.headers["x-spro-request-id"]) cost_text = f"${cost:.6f}" if cost is not None else "n/a"
print(f"[{model}] {seconds:.1f}s {cost_text}") print(f" {answer}\n")Run it
Bash
python compare_models.pyText
PROMPT: Explain what an API rate limit is in two sentences.
[gpt-oss-20b] 1.2s $0.000021 An API rate limit caps how many requests ...
[gpt-oss-120b] 2.4s $0.000048 A rate limit is a rule that restricts ...Your numbers will differ, since speed and cost depend on the models and load.
How it works
- One key, many models. The only thing that changes between runs is the
modelname. See Models for what's available. - Timing. The script measures the whole round trip with
time.perf_counter. For a time-to-first-token figure, use the generation lookup below. - Cost. Every response carries an
x-spro-request-idheader. The script passes it to the Get Generation endpoint, which returns the tokens, cost and latency for that exact call.with_raw_responseis what lets you read the header. - Retries. The cost lookup tries three times, in case the record isn't ready on the first try.
Ideas to extend it
- Add
generation["ttft_ms"]to the table to compare time to first token. - Pin each model to a provider (for example
openai/gpt-oss-20b) to compare providers serving the same model. - Score the answers automatically with a second model acting as a judge.
