The endpoint does not seem to work:
async def openrouter_get_usage(response_id):
url = f"https://openrouter.ai/api/v1/generation?id={response_id}"
headers = { "Authorization": f"Bearer {constants.OPENROUTER_API_KEY}" }
async with httpx.AsyncClient() as client:
response = await client.get(url, headers=headers)
response_json = response.json()
if response.status_code != 200:
return None, None, None
data = response_json.get("data", {})
input_tokens = data.get("tokens_prompt")
output_tokens = data.get("tokens_completion")
total_cost = data.get("total_cost")
return input_tokens, output_tokens, total_cost
stream = await openrouter_client.chat.completions.create(
model=model,
messages=messages,
stream=True,
extra_body={
"transforms": [],
}
)
response = ""
chunk = None
finish_reason = None
async for chunk in stream:
if chunk.choices:
if chunk.choices[0].delta.content is not None:
response += chunk.choices[0].delta.content
if chunk.choices[0].finish_reason:
finish_reason = chunk.choices[0].finish_reason
break
if chunk:
input_tokens, output_tokens, total_cost = await openrouter_get_usage(chunk.id)
Am I doing something wrong?