Skip to content

Instantly share code, notes, and snippets.

@FeepingCreature
Created February 22, 2026 18:01
Show Gist options
  • Select an option

  • Save FeepingCreature/471f622e8f8d9f931044c46e9ff689a5 to your computer and use it in GitHub Desktop.

Select an option

Save FeepingCreature/471f622e8f8d9f931044c46e9ff689a5 to your computer and use it in GitHub Desktop.
generation api bug repro
import requests
import os
import time
OPENROUTER_API_KEY = os.environ.get("OPENROUTER_API_KEY", "your-api-key-here")
HEADERS = {"Authorization": f"Bearer {OPENROUTER_API_KEY}"}
# Step 1: Make a simple chat completion
print("Making a chat completion...")
resp = requests.post(
"https://openrouter.ai/api/v1/chat/completions",
headers={**HEADERS, "Content-Type": "application/json"},
json={
"model": "openai/gpt-4o-mini",
"messages": [{"role": "user", "content": "Say hello in one word."}],
},
)
resp.raise_for_status()
completion = resp.json()
gen_id = completion.get("id")
print(f"Generation ID: {gen_id}")
# Step 2: Fetch generation metadata (may need a short delay)
time.sleep(1)
print("\nFetching generation metadata...")
gen_resp = requests.get(
"https://openrouter.ai/api/v1/generation",
headers=HEADERS,
params={"id": gen_id},
)
print(f"Status: {gen_resp.status_code}")
data = gen_resp.json()
print(f"Raw response: {data}")
if "data" in data:
cost = data["data"].get("total_cost")
print(f"\nTotal cost: ${cost}")
else:
print("\nNo 'data' field in response — API may be broken or returning unexpected format.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment