Generation

Look up a generation by its response or stream ID to retrieve confirmed token usage, the USD amount charged, and its interruption status.

GET /v1/generation looks up the confirmed ledger charge and usage for a Chat Completions, Messages, or Responses request. Use the response ID, or save the ID from the first stream event so you can reconcile an interrupted stream. This call is free: it is not billed and does not record usage.

Endpoint and authentication#

ItemValue
Method and pathGET https://apis.opengateway.ai/v1/generation
AuthenticationRequired: Authorization: Bearer <key>
Query parameterid (required): the client-visible generation ID from the response body or first stream event
Request bodyNone

A key can look up only generations belonging to its team. An unknown ID and another team's ID both return 404. For an interrupted stream, see reconciling incomplete streams for the ID field to save and a backoff example.

Bash
curl -G 'https://apis.opengateway.ai/v1/generation' \
-H "Authorization: Bearer $OPENGATEWAY_API_KEY" \
--data-urlencode "id=$GENERATION_ID"

Response#

JSON
{
"data": {
"id": "chatcmpl_example",
"model": "openai/gpt-5-mini",
"created_at": "2026-11-19T05:00:00Z",
"tokens_prompt": 123,
"tokens_completion": 45,
"total_cost": 0.000123,
"stream_interrupted": false
}
}
FieldTypeMeaning
data.idstringClient-visible generation ID used in the query.
data.modelstringModel ID of the final attempt.
data.created_atstringTime of the first attempt, in ISO 8601 format.
data.tokens_promptintegerPrompt tokens summed over charged attempts.
data.tokens_completionintegerCompletion tokens summed over charged attempts.
data.total_costnumberConfirmed total debited from the team balance in USD, including charged fallback attempts.
data.stream_interruptedbooleantrue when the final attempt did not succeed, such as when a stream was cut or the attempt failed.

The response contains no provider information or cost breakdown. Unlike response usage.cost, total_cost is the confirmed ledger value. Immediately after a price or discount change, usage.cost can differ until the price table refreshes (up to 5 minutes); use total_cost for budget reconciliation.

Errors and retries#

StatusCodeWhen
404generation_not_foundThe ID is unknown or belongs to another team. A just-finished generation can also take a few seconds to be recorded, so retry shortly.
404generation_pendingThe generation has been recorded but its charge is not settled. Retry after the Retry-After header, which is 2 seconds.

A generation remains pending while an attempt is awaiting price matching. A pending response is not a settled zero-dollar charge.

A pending response has this shape:

HTTP
HTTP/1.1 404 Not Found
Retry-After: 2
Content-Type: application/json
{"error":{"message":"Generation chatcmpl_example is not recorded yet; retry shortly.","type":"not_found_error","code":"generation_pending"}}

An unknown or not-yet-recorded ID returns:

JSON
{
"error": {
"message": "Generation chatcmpl_example not found. A just-finished generation can take a few seconds to be recorded; retry shortly.",
"type": "not_found_error",
"code": "generation_not_found"
}
}

Poll with backoff for either of these 404 codes when a recent request may still be settling. Use a retry limit: an unknown ID or an ID from another team will not become available to this key.

Python example#

This example performs one lookup. For polling and Retry-After handling, see the streaming guide.

Python
import json
import os
from urllib.parse import urlencode
from urllib.request import Request, urlopen
query = urlencode({"id": os.environ["GENERATION_ID"]})
request = Request(
f"https://apis.opengateway.ai/v1/generation?{query}",
headers={"Authorization": f"Bearer {os.environ['OPENGATEWAY_API_KEY']}"},
)
with urlopen(request) as response:
generation = json.load(response)["data"]
print(generation["total_cost"])

JavaScript example#

JavaScript
const url = new URL("https://apis.opengateway.ai/v1/generation");
url.searchParams.set("id", process.env.GENERATION_ID);
const response = await fetch(url, {
headers: { Authorization: `Bearer ${process.env.OPENGATEWAY_API_KEY}` },
});
const body = await response.json();
if (!response.ok) throw new Error(`${body.error.code}: ${body.error.message}`);
console.log(body.data.total_cost);
Generation — OpenGateway