#Generation gen-xxx-xxxxx not found (https://openrouter.ai/api/v1/generation?id=gen-xxx-xxxxx)

8 messages · Page 1 of 1 (latest)

low pelican
#

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?

celest shoal
#

Sleep a few seconds in your script before querying /generation and it will work

low pelican
#

Thanks, is there a way to get these info without adding extra latency? We aim to have a low latency solution

low pelican
#

Btw even minutes after still have the same error, are they removed after some delay?

celest shoal
low pelican
#

Even with stream=True?