openrouter-analytics
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseOpenRouter Analytics
OpenRouter Analytics
Query your OpenRouter usage data programmatically. Answer questions like "What was my spend this month?", "Which models cost the most?", and "How can I reduce my bill?" using the Analytics API.
以编程方式查询你的OpenRouter使用数据。使用Analytics API回答诸如“我本月的支出是多少?”、“哪些模型成本最高?”以及“如何降低我的账单?”之类的问题。
Prerequisites
前置条件
- An OpenRouter management key (provisioning key). Regular API keys will get a 403.
- Get a management key at https://openrouter.ai/settings/management-keys (separate from the regular API keys page)
- Pass it via or set the
--api-key <key>environment variableOPENROUTER_API_KEY
- 一个OpenRouter management key(配置密钥)。普通API密钥会返回403错误。
- 在https://openrouter.ai/settings/management-keys获取management key(与常规API密钥页面分开)
- 通过传递密钥,或设置
--api-key <key>环境变量OPENROUTER_API_KEY
First-Time Setup
首次设置
bash
cd <skill-path>/scripts && npm installbash
cd <skill-path>/scripts && npm installDecision Tree
决策树
| User wants to… | Do this |
|---|---|
| Know what data is available | Run |
| See spend / usage / volume | Run |
| Break down by model, provider, API key | Add |
| See trends over time | Add |
| Reduce costs | Run |
| Inspect individual generations | Add |
| Break down by classifier labels | Use |
| Filter by classifier labels | Use |
| Answer a specific question | Map the question → metrics + dimensions, then query |
| 用户想要… | 操作步骤 |
|---|---|
| 了解可用数据 | 运行 |
| 查看支出/使用情况/请求量 | 使用合适的指标运行 |
| 按模型、提供商、API密钥细分 | 在查询中添加 |
| 查看时间趋势 | 添加 |
| 降低成本 | 运行 |
| 检查单个生成任务 | 添加 |
| 按分类器标签细分 | 在请求体中使用 |
| 按分类器标签筛选 | 在请求体中使用 |
| 回答特定问题 | 将问题映射为指标+维度,然后执行查询 |
Workflow
工作流程
The recommended workflow for answering a user's analytics question:
- Discover — Call to see available metrics and dimensions
discover-schema.ts - Map — Translate the user's question into metrics, dimensions, filters, and time range
- Query — Execute via
query-analytics.ts - Interpret — Analyze the returned data and explain the results
For common questions, provides ready-made query templates.
suggest-queries.ts回答用户分析问题的推荐工作流程:
- 发现 — 调用查看可用的指标和维度
discover-schema.ts - 映射 — 将用户的问题转换为指标、维度、筛选条件和时间范围
- 查询 — 通过执行查询
query-analytics.ts - 解读 — 分析返回的数据并解释结果
对于常见问题,提供了现成的查询模板。
suggest-queries.tsDiscover Available Data
发现可用数据
bash
cd <skill-path>/scripts && npx tsx discover-schema.tsReturns the full schema: metrics, dimensions, filter operators, and granularities.
Filter to a specific section:
bash
npx tsx discover-schema.ts --section metrics
npx tsx discover-schema.ts --section dimensions
npx tsx discover-schema.ts --section operators
npx tsx discover-schema.ts --section granularitiesSee the skill for detailed guidance on interpreting the schema response and mapping user questions to the right metrics and dimensions.
openrouter-analytics-schemabash
cd <skill-path>/scripts && npx tsx discover-schema.ts返回完整的 schema:指标、维度、筛选运算符和时间粒度。
筛选特定部分:
bash
npx tsx discover-schema.ts --section metrics
npx tsx discover-schema.ts --section dimensions
npx tsx discover-schema.ts --section operators
npx tsx discover-schema.ts --section granularities如需了解解读schema响应以及将用户问题映射到正确指标和维度的详细指南,请查看技能。
openrouter-analytics-schemaQuery Analytics Data
查询分析数据
bash
cd <skill-path>/scripts && npx tsx query-analytics.ts --metrics request_count,total_usageSee the skill for the full parameter reference and query construction guide.
openrouter-analytics-querybash
cd <skill-path>/scripts && npx tsx query-analytics.ts --metrics request_count,total_usage如需完整的参数参考和查询构建指南,请查看技能。
openrouter-analytics-queryQuick Examples
快速示例
Spend over the last 7 days, broken down by day:
bash
npx tsx query-analytics.ts --metrics total_usage --granularity dayTop 10 models by cost:
bash
npx tsx query-analytics.ts --metrics total_usage,request_count --dimensions model --order-by total_usage --limit 10Usage by API key:
bash
npx tsx query-analytics.ts --metrics request_count,tokens_total --dimensions api_key_id --order-by request_count --limit 10Latency by provider (limited to 31-day range):
bash
npx tsx query-analytics.ts --metrics avg_latency,p90_latency --dimensions provider --order-by p90_latencyUsage cost breakdown (credits, BYOK, upstream, cache, data logging, web search):
bash
npx tsx query-analytics.ts --metrics credits_usage,byok_usage,byok_fees,usage_upstream,usage_cache,usage_data,usage_web --granularity day过去7天的支出,按天细分:
bash
npx tsx query-analytics.ts --metrics total_usage --granularity day按成本排序的前10个模型:
bash
npx tsx query-analytics.ts --metrics total_usage,request_count --dimensions model --order-by total_usage --limit 10按API密钥统计的使用情况:
bash
npx tsx query-analytics.ts --metrics request_count,tokens_total --dimensions api_key_id --order-by request_count --limit 10按提供商统计的延迟(限制为31天范围):
bash
npx tsx query-analytics.ts --metrics avg_latency,p90_latency --dimensions provider --order-by p90_latency使用成本细分( credits、BYOK、上游、缓存、数据日志、网页搜索):
bash
npx tsx query-analytics.ts --metrics credits_usage,byok_usage,byok_fees,usage_upstream,usage_cache,usage_data,usage_web --granularity dayCommon Query Templates
常用查询模板
bash
cd <skill-path>/scripts && npx tsx suggest-queries.tsReturns a list of pre-built query templates for common questions, each with:
- The natural-language question it answers
- The query parameters (metrics, dimensions, filters, time range)
- The CLI flags to pass to
query-analytics.ts - Interpretation guidance (where applicable)
bash
cd <skill-path>/scripts && npx tsx suggest-queries.ts返回针对常见问题的预构建查询模板列表,每个模板包含:
- 它能回答的自然语言问题
- 查询参数(指标、维度、筛选条件、时间范围)
- 传递给的CLI标志
query-analytics.ts - 解读指南(如适用)
Interpreting Results
解读结果
The query endpoint returns an array of data rows. Each row is a flat object with keys matching the requested metrics and dimensions.
When interpreting results for the user:
- Spend metrics (,
total_usage,credits_usage,openrouter_usage,byok_usage,byok_fees,usage_upstream,usage_cache,usage_web,usage_upstream_web,usage_file,usage_upstream_file,usage_web_fetch) are in USD.usage_upstream_web_fetchincludes BYOK inference cost.total_usageis typically negative (a data logging discount)usage_data - Token counts (,
tokens_total,tokens_prompt) are in native model tokenstokens_completion - Latency (,
avg_latency, etc.) is in millisecondsp50_latency - Rates () are 0–1 ratios
cache_hit_rate - Throughput () is tokens per second
avg_throughput - When is set, rows include a
granularityfield for the time bucket (e.g.,date__<granularity>,date__day,date__hour)date__month - Label resolution: dimensions ,
api_key_id,app, anduserhave their raw IDs replaced with human-readable names (key name, app title, user name, workspace name) directly in the data rows;workspaceandgeneration_idreturn raw valuessession_id - Truncation: when consuming output programmatically, check . If
metadata.truncated, the result was capped attrueand is a partial dataset — raise--limitor paginate before reporting totals or rankings--limit
查询端点返回一个数据行数组。每行是一个扁平对象,键与请求的指标和维度匹配。
为用户解读结果时:
- 支出指标(、
total_usage、credits_usage、openrouter_usage、byok_usage、byok_fees、usage_upstream、usage_cache、usage_web、usage_upstream_web、usage_file、usage_upstream_file、usage_web_fetch)以美元为单位。usage_upstream_web_fetch包含BYOK推理成本。total_usage通常为负值(数据日志折扣)usage_data - Token计数(、
tokens_total、tokens_prompt)以模型原生Token为单位tokens_completion - 延迟(、
avg_latency等)以毫秒为单位p50_latency - 比率()为0–1的数值
cache_hit_rate - 吞吐量()为每秒Token数
avg_throughput - 设置后,行中会包含时间桶的
granularity字段(例如date__<granularity>、date__day、date__hour)date__month - 标签解析:维度、
api_key_id、app和user的原始ID会在数据行中直接替换为人类可读的名称(密钥名称、应用标题、用户名、工作区名称);workspace和generation_id返回原始值session_id - 截断:以编程方式处理输出时,请检查。如果为
metadata.truncated,结果已被限制在true数量内,是部分数据集——在报告总数或排名前,提高--limit或进行分页--limit
Cost Optimization Guidance
成本优化指南
When the user asks "How can I spend less?" or similar:
- Query top models by spend:
--metrics total_usage,tokens_total,cache_hit_rate,request_count --dimensions model --order-by total_usage --limit 10 - Query cost breakdown: to see where spend goes
--metrics credits_usage,byok_usage,byok_fees,usage_upstream,usage_cache,usage_data,usage_web,usage_file --granularity day - Look for:
- Models with high spend but low — prompt caching can help
cache_hit_rate - Expensive models that could be replaced by cheaper alternatives for specific tasks
- High token counts with low request counts — may indicate oversized prompts
- Models where are a large fraction of total — consider disabling extended thinking if not needed
reasoning_tokens - High or
usage_webrelative tousage_file— web search and file processing add-on costs may be significantusage_upstream
- Models with high spend but low
当用户询问“如何减少支出?”或类似问题时:
- 查询按支出排序的热门模型:
--metrics total_usage,tokens_total,cache_hit_rate,request_count --dimensions model --order-by total_usage --limit 10 - 查询成本细分:,查看支出去向
--metrics credits_usage,byok_usage,byok_fees,usage_upstream,usage_cache,usage_data,usage_web,usage_file --granularity day - 关注以下情况:
- 支出高但低的模型——提示缓存可能会有所帮助
cache_hit_rate - 可被更便宜的替代模型完成特定任务的昂贵模型
- Token计数高但请求量低的情况——可能提示提示词过大
- 占总Token数比例较大的模型——如果不需要,考虑禁用扩展思考功能
reasoning_tokens - 或
usage_web相对于usage_file占比高的情况——网页搜索和文件处理的附加成本可能很高usage_upstream
- 支出高但
Drilling Down to Individual Generations
深入到单个生成任务
To inspect specific generations or sessions from your analytics results, add or as a dimension. Both are generations-only dimensions (31-day limit). returns the unique ID for each generation in the result set. groups and filters sessionless requests as the literal : the ClickHouse column defaults to an empty string, and the query builder coalesces it to . Use to exclude sessionless requests; filtering on matches nothing.
generation_idsession_idgeneration_idsession_idnonenoneneq 'none'''bash
npx tsx query-analytics.ts --metrics total_usage,tokens_total --dimensions generation_id --order-by total_usage --limit 10Once you have a generation ID (e.g., ), use the skill to get detailed information:
gen-aBcDeFgHiJkLmNoPqRsTopenrouter-generations- — Fetch full request metadata: cost breakdown, token counts, latency, provider routing chain, finish reason, and more
get-generation - — Fetch the stored prompt and completion text (unless Zero Data Retention was enabled)
get-generation-content
bash
cd <openrouter-generations-skill-path>/scripts
npx tsx get-generation.ts gen-aBcDeFgHiJkLmNoPqRsT
npx tsx get-generation-content.ts gen-aBcDeFgHiJkLmNoPqRsTThis workflow is useful for identifying your most expensive or slowest requests via analytics, then inspecting the actual prompt/completion to understand why.
要从分析结果中检查特定的生成任务或会话,添加或作为维度。两者都是仅适用于生成任务的维度(31天限制)。返回结果集中每个生成任务的唯一ID。将无会话请求分组并筛选为字面量:ClickHouse列默认为空字符串,查询构建器将其合并为。使用排除无会话请求;筛选不会匹配任何内容。
generation_idsession_idgeneration_idsession_idnonenoneneq 'none'''bash
npx tsx query-analytics.ts --metrics total_usage,tokens_total --dimensions generation_id --order-by total_usage --limit 10一旦你获得了生成任务ID(例如),使用技能获取详细信息:
gen-aBcDeFgHiJkLmNoPqRsTopenrouter-generations- — 获取完整的请求元数据:成本细分、Token计数、延迟、提供商路由链、结束原因等
get-generation - — 获取存储的提示词和完成文本(除非启用了零数据保留)
get-generation-content
bash
cd <openrouter-generations-skill-path>/scripts
npx tsx get-generation.ts gen-aBcDeFgHiJkLmNoPqRsT
npx tsx get-generation-content.ts gen-aBcDeFgHiJkLmNoPqRsT此工作流程有助于通过分析识别出最昂贵或最慢的请求,然后检查实际的提示词/完成内容以了解原因。
API Reference
API参考
Both endpoints require a management key via .
Authorization: Bearer sk-or-v1-...| Endpoint | Method | Description |
|---|---|---|
| GET | Returns available metrics, dimensions, operators, granularities |
| POST | Executes an analytics query and returns structured data |
Full documentation: https://openrouter.ai/docs/api/api-reference/analytics
两个端点都需要通过传递management key。
Authorization: Bearer sk-or-v1-...| 端点 | 方法 | 描述 |
|---|---|---|
| GET | 返回可用的指标、维度、运算符、时间粒度 |
| POST | 执行分析查询并返回结构化数据 |