openrouter-generations
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chineseopenrouter-generations
OpenRouter生成任务查询
Retrieve detailed metadata and stored content for individual OpenRouter generations. Use this skill when you need to inspect a specific request — its cost, latency, token usage, provider routing, or the actual prompt/completion text.
检索单个OpenRouter生成任务的详细元数据和存储内容。当你需要检查特定请求(包括成本、延迟、token使用量、服务商路由情况,或实际的提示词/补全文本)时,使用此技能。
Prerequisites
前提条件
- Any valid OpenRouter API key (regular or management key). Get one at openrouter.ai/settings/keys.
- Pass it via or set the
--api-key <key>environment variableOPENROUTER_API_KEY - Generation IDs look like or
gen-1234567890.gen-aBcDeFgHiJkLmNoPqRsT
- 任何有效的OpenRouter API密钥(常规密钥或管理密钥)。可在openrouter.ai/settings/keys获取。
- 通过传入密钥,或设置
--api-key <key>环境变量OPENROUTER_API_KEY - 生成任务ID格式类似或
gen-1234567890。gen-aBcDeFgHiJkLmNoPqRsT
First-Time Setup
首次设置
bash
cd <skill-path>/scripts && npm installbash
cd <skill-path>/scripts && npm installEndpoints
接口端点
| Endpoint | Method | Purpose |
|---|---|---|
| GET | Request metadata and usage (tokens, cost, latency, model, provider) |
| GET | Stored prompt and completion text |
Both take a single query parameter: (the generation ID).
idFull API reference: openrouter.ai/docs/api/api-reference/generations/get-generation
| 端点 | 请求方法 | 用途 |
|---|---|---|
| GET | 请求元数据和使用情况(token数量、成本、延迟、模型、服务商) |
| GET | 获取存储的提示词和补全文本 |
两个接口都接受一个查询参数:(生成任务ID)。
idWorkflow
操作流程
1. Get generation metadata
1. 获取生成任务元数据
Retrieves everything about a generation except the actual prompt/completion text:
bash
cd <skill-path>/scripts && npx tsx get-generation.ts gen-1234567890
npx tsx get-generation.ts --id gen-1234567890 --jsonWhat you get back:
- Model & routing: ,
model,provider_name,routerservice_tier - Tokens: ,
tokens_prompt,tokens_completion,native_tokens_reasoningnative_tokens_cached - Cost: ,
total_cost,usage,upstream_inference_costcache_discount - Performance: ,
latency,generation_timemoderation_latency - Status: ,
finish_reason,streamed,cancelledis_byok - Context: ,
created_at,app_id,external_user,session_idrequest_id - Provider chain: array showing fallback attempts with per-provider latency and status
provider_responses
检索生成任务的所有信息(不包含实际的提示词/补全文本):
bash
cd <skill-path>/scripts && npx tsx get-generation.ts gen-1234567890
npx tsx get-generation.ts --id gen-1234567890 --json返回内容:
- 模型与路由:、
model、provider_name、routerservice_tier - Token统计:、
tokens_prompt、tokens_completion、native_tokens_reasoningnative_tokens_cached - 成本信息:、
total_cost、usage、upstream_inference_costcache_discount - 性能数据:、
latency、generation_timemoderation_latency - 状态信息:、
finish_reason、streamed、cancelledis_byok - 上下文信息:、
created_at、app_id、external_user、session_idrequest_id - 服务商调用链:数组展示重试尝试,包含每个服务商的延迟和状态
provider_responses
2. Get generation content
2. 获取生成任务内容
Retrieves the stored prompt and completion:
bash
cd <skill-path>/scripts && npx tsx get-generation-content.ts gen-1234567890
npx tsx get-generation-content.ts --id gen-1234567890 --jsonWhat you get back:
- Input: (raw text) and/or
prompt(array ofmessages){role, content} - Output: (the model's response) and
completion(chain-of-thought, if applicable)reasoning
Note: Content is only available if the generation was not made with Zero Data Retention (ZDR) enabled. If ZDR was on, this endpoint returns empty/null content.
检索存储的提示词和补全文本:
bash
cd <skill-path>/scripts && npx tsx get-generation-content.ts gen-1234567890
npx tsx get-generation-content.ts --id gen-1234567890 --json返回内容:
- 输入:(原始文本)和/或
prompt(messages数组){role, content} - 输出:(模型响应结果)和
completion(思维链内容,如有)reasoning
注意:仅当生成任务未启用Zero Data Retention(ZDR,零数据保留)时,才能获取内容。如果启用了ZDR,此接口将返回空值或null。
Direct API Usage (curl)
直接API调用(curl)
Get metadata
获取元数据
bash
curl -G https://openrouter.ai/api/v1/generation \
-H "Authorization: Bearer $OPENROUTER_API_KEY" \
-d id=gen-1234567890bash
curl -G https://openrouter.ai/api/v1/generation \
-H "Authorization: Bearer $OPENROUTER_API_KEY" \
-d id=gen-1234567890Get content
获取内容
bash
curl -G https://openrouter.ai/api/v1/generation/content \
-H "Authorization: Bearer $OPENROUTER_API_KEY" \
-d id=gen-1234567890bash
curl -G https://openrouter.ai/api/v1/generation/content \
-H "Authorization: Bearer $OPENROUTER_API_KEY" \
-d id=gen-1234567890Response Schemas
响应格式
Metadata response (/api/v1/generation
)
/api/v1/generation元数据响应(/api/v1/generation
)
/api/v1/generationjson
{
"data": {
"id": "gen-3bhGkxlo4XFrqiabUM7NDtwDzWwG",
"api_type": "completions",
"model": "openai/gpt-4o",
"provider_name": "OpenAI",
"created_at": "2024-07-15T23:33:19.433273+00:00",
"tokens_prompt": 10,
"tokens_completion": 25,
"native_tokens_reasoning": 5,
"native_tokens_cached": 3,
"total_cost": 0.0015,
"usage": 0.0015,
"upstream_inference_cost": 0.0012,
"latency": 1250,
"generation_time": 1200,
"finish_reason": "stop",
"streamed": true,
"is_byok": false,
"cancelled": false,
"router": "openrouter/auto",
"service_tier": "priority",
"provider_responses": [
{
"provider_name": "OpenAI",
"model_permaslug": "openai/gpt-4o",
"status": 200,
"latency": 1200,
"is_byok": false
}
]
}
}json
{
"data": {
"id": "gen-3bhGkxlo4XFrqiabUM7NDtwDzWwG",
"api_type": "completions",
"model": "openai/gpt-4o",
"provider_name": "OpenAI",
"created_at": "2024-07-15T23:33:19.433273+00:00",
"tokens_prompt": 10,
"tokens_completion": 25,
"native_tokens_reasoning": 5,
"native_tokens_cached": 3,
"total_cost": 0.0015,
"usage": 0.0015,
"upstream_inference_cost": 0.0012,
"latency": 1250,
"generation_time": 1200,
"finish_reason": "stop",
"streamed": true,
"is_byok": false,
"cancelled": false,
"router": "openrouter/auto",
"service_tier": "priority",
"provider_responses": [
{
"provider_name": "OpenAI",
"model_permaslug": "openai/gpt-4o",
"status": 200,
"latency": 1200,
"is_byok": false
}
]
}
}Content response (/api/v1/generation/content
)
/api/v1/generation/content内容响应(/api/v1/generation/content
)
/api/v1/generation/contentjson
{
"data": {
"input": {
"prompt": "What is the meaning of life?",
"messages": [
{
"content": "What is the meaning of life?",
"role": "user"
}
]
},
"output": {
"completion": "The meaning of life is a philosophical question...",
"reasoning": null
}
}
}json
{
"data": {
"input": {
"prompt": "What is the meaning of life?",
"messages": [
{
"content": "What is the meaning of life?",
"role": "user"
}
]
},
"output": {
"completion": "The meaning of life is a philosophical question...",
"reasoning": null
}
}
}Common Use Cases
常见使用场景
Debug a failed generation
调试失败的生成任务
bash
undefinedbash
undefinedCheck what happened — look at finish_reason, provider_responses, and cancelled
查看详情——检查finish_reason、provider_responses和cancelled字段
cd <skill-path>/scripts && npx tsx get-generation.ts gen-abc123 --json
Look for:
- `finish_reason` = `"length"` means the model hit max tokens
- `finish_reason` = `"content_filter"` means content was filtered
- `cancelled` = `true` means the request was cancelled by the client
- `provider_responses` with multiple entries means fallbacks occurredcd <skill-path>/scripts && npx tsx get-generation.ts gen-abc123 --json
重点关注:
- `finish_reason` = `"length"` 表示模型达到了最大token限制
- `finish_reason` = `"content_filter"` 表示内容被过滤
- `cancelled` = `true` 表示请求被客户端取消
- `provider_responses`包含多个条目表示发生了重试Check cost of a specific request
检查特定请求的成本
bash
cd <skill-path>/scripts && npx tsx get-generation.ts gen-abc123Check (what you were charged) vs (what the provider charged OpenRouter).
total_costupstream_inference_costbash
cd <skill-path>/scripts && npx tsx get-generation.ts gen-abc123对比(你的实际付费金额)和(服务商向OpenRouter收取的费用)。
total_costupstream_inference_costReview what was actually sent/received
查看实际发送/接收的内容
bash
cd <skill-path>/scripts && npx tsx get-generation-content.ts gen-abc123Useful for debugging unexpected outputs — verify the actual prompt sent and completion received.
bash
cd <skill-path>/scripts && npx tsx get-generation-content.ts gen-abc123用于调试不符合预期的输出——验证实际发送的提示词和接收的补全文本。
Trace a multi-generation session
追踪多生成任务会话
If you have a or from one generation, you can find related generations via the analytics query endpoint (see skill).
request_idsession_idopenrouter-analytics如果你有某个生成任务的或,可以通过分析查询接口找到相关生成任务(参考技能)。
request_idsession_idopenrouter-analyticsError Handling
错误处理
| Status | Meaning |
|---|---|
| 401 | Invalid or missing API key |
| 403 | You don't have access to this generation (belongs to another user) |
| 404 | Generation ID not found |
| 429 | Rate limited — wait and retry |
| 500 | Server error — retry |
| 502 | Upstream failure — retry |
| 状态码 | 含义 |
|---|---|
| 401 | API密钥无效或缺失 |
| 403 | 你无权访问此生成任务(属于其他用户) |
| 404 | 生成任务ID不存在 |
| 429 | 请求频率超限——请等待后重试 |
| 500 | 服务器错误——请重试 |
| 502 | 上游服务故障——请重试 |
Key Fields Reference
关键字段参考
Metadata fields
元数据字段
| Field | Type | Description |
|---|---|---|
| string | Generation ID ( |
| string | Model permaslug (e.g., |
| string|null | Provider that served the request |
| string | One of: |
| int|null | Prompt token count |
| int|null | Completion token count |
| int|null | Reasoning/thinking tokens |
| int|null | Cached input tokens |
| number | Total cost in USD |
| number | Usage amount in USD |
| number|null | Provider's cost in USD |
| number|null | Discount from caching |
| number|null | Total latency in ms |
| number|null | Model generation time in ms |
| number|null | Moderation check time in ms |
| string|null | Why generation stopped ( |
| string|null | Raw finish reason from provider |
| bool|null | Whether response was streamed |
| bool | Whether user's own provider key was used |
| bool|null | Whether request was cancelled |
| int|null | OAuth app ID |
| string|null | External user identifier (X-External-User header) |
| string|null | Session grouping ID |
| string|null | Request grouping ID (all gens from one API call) |
| string|null | Router used (e.g., |
| string|null | Provider service tier |
| string|null | Search engine used (e.g., |
| int|null | Number of search results included |
| array|null | Provider attempt chain with per-provider latency/status |
| 字段 | 类型 | 描述 |
|---|---|---|
| string | 生成任务ID( |
| string | 模型永久标识(例如: |
| string|null | 处理请求的服务商 |
| string | 类型之一: |
| int|null | 提示词token数量 |
| int|null | 补全token数量 |
| int|null | 推理/思考token数量 |
| int|null | 缓存输入token数量 |
| number | 总成本(美元) |
| number | 使用费用(美元) |
| number|null | 服务商收取的费用(美元) |
| number|null | 缓存带来的折扣 |
| number|null | 总延迟(毫秒) |
| number|null | 模型生成时间(毫秒) |
| number|null | 内容审核时间(毫秒) |
| string|null | 生成停止原因( |
| string|null | 服务商返回的原始停止原因 |
| bool|null | 是否以流方式返回响应 |
| bool | 是否使用了用户自己的服务商密钥 |
| bool|null | 请求是否被取消 |
| int|null | OAuth应用ID |
| string|null | 外部用户标识(X-External-User请求头) |
| string|null | 会话分组ID |
| string|null | 请求分组ID(同一API调用产生的所有生成任务) |
| string|null | 使用的路由策略(例如: |
| string|null | 服务商服务等级 |
| string|null | 使用的搜索引擎(例如: |
| int|null | 包含的搜索结果数量 |
| array|null | 服务商调用链,包含每个服务商的延迟和状态 |
Content fields
内容字段
| Field | Type | Description |
|---|---|---|
| string|null | Raw prompt text |
| array|null | Messages array ( |
| string|null | Model's completion text |
| string|null | Chain-of-thought reasoning |
| 字段 | 类型 | 描述 |
|---|---|---|
| string|null | 原始提示词文本 |
| array|null | 消息数组( |
| string|null | 模型的补全文本 |
| string|null | 思维链推理内容 |