OneRouterにおける使用会計
AIモデルのトークン使用量を追跡する
By アンドリュー・ジェン •
AIモデルのトークン使用量を追跡する



2025/12/21
アンドリュー・ジェン
OneRouter APIは、追加のAPI呼び出しを行うことなくAIモデルの使用状況を追跡できるビルトインの使用状況の計算を提供します。この機能は、APIレスポンスの中でトークンのカウント、コスト、キャッシングの状態に関する詳細な情報を提供します。
有効化されると、APIは以下を含む詳細な使用状況の情報を返します:
モデルのネイティブトークナイザーを使用したプロンプトおよび完成トークンのカウント
クレジットのコスト
推論トークンのカウント(該当する場合)
キャッシュされたトークンのカウント(利用可能な場合)
この情報は、ストリーミングレスポンスの最後のSSEメッセージに含まれるか、非ストリーミングリクエストの完全なレスポンスに含まれます。
リクエストにusageパラメータを含めることで、使用状況の計算を有効化できます:
import requests import json response = requests.post( url="https://llm.onerouter.pro/v1/chat/completions", headers={ "Authorization": "Bearer <<API Keys>>", "Content-Type": "application/json" }, data=json.dumps({ "model": "google-ai-studio/gemini-2.5-flash-preview-09-2025", "messages": [ { "role": "user", "content": "What is the meaning of life?" } ], "usage": { "include": True } }) ) print(response.json())
使用状況の計算が有効化されると、レスポンスには詳細なトークン情報を含むusageオブジェクトと、詳細なコストを含むcost項目及びcost_detailsオブジェクトが含まれます:
{ 'choices': [{ 'finish_reason': 'stop', 'index': 0, 'logprobs': None, 'message': { 'content': 'This is perhaps the most profound and widely debated question in human history. The truth is, there is no single, universally accepted answer.\n\nThe "meaning of life" is a concept that is approached very differently across disciplines, cultures, and individual beliefs. Here is an overview of the major perspectives:\n\n---\n\n## 1. Philosophical Perspectives\n\nPhilosophy attempts to logically define existence and purpose.\n\n| Perspective | Core Idea | Key Concept |\n| :--- | :--- | :--- |\n| **Nihilism** | Life is inherently without objective meaning, purpose, or intrinsic value. Any purpose is an illusion. | Meaninglessness, Absurdity (often a precursor to Existentialism). |\n| **Absurdism** | The conflict between humanity\'s inherent search for meaning and the universe\'s silent, objective meaninglessness (**The Absurd**). The meaning is found in rebelling against the Absurd by embracing life fully despite its lack of ultimate purpose. | Revolt, Freedom, Passion (Albert Camus). |\n| **Existentialism** | Existence precedes essence. Humans are born a "blank slate" and must create their own meaning through choices, freedom, and responsibility. Meaning is subjective and individually constructed. | Radical Freedom, Responsibility, Subjectivity (Sartre, de Beauvoir). |\n| **Hedonism/Utilitarianism** | The ultimate meaning or good is the maximization of pleasure and the minimization of pain (utility). Life\'s purpose is to seek happiness. | Utility, Pleasure, The Greatest Good for the Greatest Number. |\n| **Stoicism** | Meaning is found in living in accordance with nature and reason. Focus on what you can control (your judgments and reactions) and accept what you cannot. Virtue is the sole good. | Virtue, Resilience, Acceptance. |\n\n---\n\n## 2. Religious and Spiritual Perspectives\n\nReligions generally posit that life has a transcendent, divinely ordained purpose.\n\n* **Christianity/Islam/Judaism:** Meaning is found in serving God, following divine law, achieving salvation, and preparing for an afterlife. The purpose is to fulfill the covenant or will of the Creator.\n* **Buddhism:** The meaning of life is to escape suffering (Dukkha) caused by desire, achieve enlightenment (Nirvana), and break the cycle of reincarnation (Samsara). This is often achieved through compassion and ethical conduct.\n* **Hinduism:** Meaning is tied to *Dharma* (righteous conduct/duty) and *Moksha* (liberation/union with the ultimate reality, Brahman). The goal is to fulfill one’s duty within the cosmic order.\n* **Spirituality (Non-denominational):** Meaning is often found in connection—to nature, to other beings, or to a higher power—and in personal growth or evolution.\n\n---\n\n## 3. Scientific and Biological Perspectives\n\nScience approaches the question from an empirical viewpoint, focusing on observable functions.\n\n* **Biological Imperative:** From an evolutionary standpoint, the meaning of life is the perpetuation of the species. The goal of any organism is to survive long enough to reproduce and pass on its genetic material.\n* **Cognitive Science:** The function of human life is the processing of information, learning, adaptation, and the creation of culture, knowledge, and complex systems. Meaning can be seen as the ultimate emergent property of a highly complex brain interacting with the environment.\n* **Cosmic Perspective (Atheistic/Naturalistic):** In the vastness of the cosmos, human life has no inherent cosmic significance. Meaning, if it exists, is purely self-imposed and human-centered.\n\n---\n\n## 4. Psychological Perspectives\n\nPsychology focuses on the internal experience of purpose, fulfillment, and well-being.\n\n* **Logotherapy (Viktor Frankl):** Meaning is found in the relentless search for purpose, even amidst suffering. Frankl, a Holocaust survivor, argued that meaning can be found in three primary ways:\n 1. Creating a work or doing a deed.\n 2. Experiencing something or loving someone.\n 3. The attitude we take toward unavoidable suffering.\n* **Humanistic Psychology (Maslow/Rogers):** The meaning of life is **Self-Actualization**—the realization of one\'s full potential and inherent capabilities. It is the drive to become the best possible version of oneself.\n* **Positive Psychology (Seligman):** Meaning is a core component of flourishing (**PERMA Model**). It is achieved by belonging to and serving something larger than oneself.\n\n---\n\n## Conclusion: The Answer May Be a Verb\n\nSince there is no objective meaning decreed by the universe, most contemporary thought (philosophical and psychological) suggests that **meaning is not something to be discovered, but something to be created.**\n\nIf you accept that the universe is indifferent, the meaning of life becomes:\n\n### 1. The Meaning You Choose to Create\n\nIt is the subjective purpose you assign to your own existence. This could be love, art, justice, connection, knowledge, or helping others.\n\n### 2. The Experience of Living\n\nThe ultimate meaning might simply be the experience itself—the engagement with the world, the appreciation of beauty, the capacity for connection, and the pursuit of growth.\n\nIn essence, **The meaning of life is the life you lead.**', 'role': 'assistant' } }], 'cost': 0.002835, 'cost_details': { 'audio_cost': 0, 'cache_prompt_cost': 0, 'cache_write_cost': 0, 'generation_cost': 0, 'image_cost': 0, 'input_prompt_cost': 2.4e-06, 'output_prompt_cost': 0.002832727, 'tools_cost': 0, 'video_cost': 0 }, 'created': 1766026675, 'discounted': '1', 'id': 'chatcmpl-202512180257506444719362giBMqDX', 'model': 'gemini-2.5-flash-preview-09-2025', 'object': 'chat.completion', 'request_id': '8dce2dd0fd4c4bfeb4374df8fb32a8a6', 'usage': { 'completion_tokens': 1133, 'input_tokens': 0, 'output_tokens': 0, 'prompt_tokens': 8, 'prompt_tokens_details': { 'text_tokens': 8 }, 'server_tool_use': { 'web_search_requests': '' }, 'total_tokens': 1141, 'ttft': 0 } }
OneRouter APIは、追加のAPI呼び出しを行うことなくAIモデルの使用状況を追跡できるビルトインの使用状況の計算を提供します。この機能は、APIレスポンスの中でトークンのカウント、コスト、キャッシングの状態に関する詳細な情報を提供します。
有効化されると、APIは以下を含む詳細な使用状況の情報を返します:
モデルのネイティブトークナイザーを使用したプロンプトおよび完成トークンのカウント
クレジットのコスト
推論トークンのカウント(該当する場合)
キャッシュされたトークンのカウント(利用可能な場合)
この情報は、ストリーミングレスポンスの最後のSSEメッセージに含まれるか、非ストリーミングリクエストの完全なレスポンスに含まれます。
リクエストにusageパラメータを含めることで、使用状況の計算を有効化できます:
import requests import json response = requests.post( url="https://llm.onerouter.pro/v1/chat/completions", headers={ "Authorization": "Bearer <<API Keys>>", "Content-Type": "application/json" }, data=json.dumps({ "model": "google-ai-studio/gemini-2.5-flash-preview-09-2025", "messages": [ { "role": "user", "content": "What is the meaning of life?" } ], "usage": { "include": True } }) ) print(response.json())
使用状況の計算が有効化されると、レスポンスには詳細なトークン情報を含むusageオブジェクトと、詳細なコストを含むcost項目及びcost_detailsオブジェクトが含まれます:
{ 'choices': [{ 'finish_reason': 'stop', 'index': 0, 'logprobs': None, 'message': { 'content': 'This is perhaps the most profound and widely debated question in human history. The truth is, there is no single, universally accepted answer.\n\nThe "meaning of life" is a concept that is approached very differently across disciplines, cultures, and individual beliefs. Here is an overview of the major perspectives:\n\n---\n\n## 1. Philosophical Perspectives\n\nPhilosophy attempts to logically define existence and purpose.\n\n| Perspective | Core Idea | Key Concept |\n| :--- | :--- | :--- |\n| **Nihilism** | Life is inherently without objective meaning, purpose, or intrinsic value. Any purpose is an illusion. | Meaninglessness, Absurdity (often a precursor to Existentialism). |\n| **Absurdism** | The conflict between humanity\'s inherent search for meaning and the universe\'s silent, objective meaninglessness (**The Absurd**). The meaning is found in rebelling against the Absurd by embracing life fully despite its lack of ultimate purpose. | Revolt, Freedom, Passion (Albert Camus). |\n| **Existentialism** | Existence precedes essence. Humans are born a "blank slate" and must create their own meaning through choices, freedom, and responsibility. Meaning is subjective and individually constructed. | Radical Freedom, Responsibility, Subjectivity (Sartre, de Beauvoir). |\n| **Hedonism/Utilitarianism** | The ultimate meaning or good is the maximization of pleasure and the minimization of pain (utility). Life\'s purpose is to seek happiness. | Utility, Pleasure, The Greatest Good for the Greatest Number. |\n| **Stoicism** | Meaning is found in living in accordance with nature and reason. Focus on what you can control (your judgments and reactions) and accept what you cannot. Virtue is the sole good. | Virtue, Resilience, Acceptance. |\n\n---\n\n## 2. Religious and Spiritual Perspectives\n\nReligions generally posit that life has a transcendent, divinely ordained purpose.\n\n* **Christianity/Islam/Judaism:** Meaning is found in serving God, following divine law, achieving salvation, and preparing for an afterlife. The purpose is to fulfill the covenant or will of the Creator.\n* **Buddhism:** The meaning of life is to escape suffering (Dukkha) caused by desire, achieve enlightenment (Nirvana), and break the cycle of reincarnation (Samsara). This is often achieved through compassion and ethical conduct.\n* **Hinduism:** Meaning is tied to *Dharma* (righteous conduct/duty) and *Moksha* (liberation/union with the ultimate reality, Brahman). The goal is to fulfill one’s duty within the cosmic order.\n* **Spirituality (Non-denominational):** Meaning is often found in connection—to nature, to other beings, or to a higher power—and in personal growth or evolution.\n\n---\n\n## 3. Scientific and Biological Perspectives\n\nScience approaches the question from an empirical viewpoint, focusing on observable functions.\n\n* **Biological Imperative:** From an evolutionary standpoint, the meaning of life is the perpetuation of the species. The goal of any organism is to survive long enough to reproduce and pass on its genetic material.\n* **Cognitive Science:** The function of human life is the processing of information, learning, adaptation, and the creation of culture, knowledge, and complex systems. Meaning can be seen as the ultimate emergent property of a highly complex brain interacting with the environment.\n* **Cosmic Perspective (Atheistic/Naturalistic):** In the vastness of the cosmos, human life has no inherent cosmic significance. Meaning, if it exists, is purely self-imposed and human-centered.\n\n---\n\n## 4. Psychological Perspectives\n\nPsychology focuses on the internal experience of purpose, fulfillment, and well-being.\n\n* **Logotherapy (Viktor Frankl):** Meaning is found in the relentless search for purpose, even amidst suffering. Frankl, a Holocaust survivor, argued that meaning can be found in three primary ways:\n 1. Creating a work or doing a deed.\n 2. Experiencing something or loving someone.\n 3. The attitude we take toward unavoidable suffering.\n* **Humanistic Psychology (Maslow/Rogers):** The meaning of life is **Self-Actualization**—the realization of one\'s full potential and inherent capabilities. It is the drive to become the best possible version of oneself.\n* **Positive Psychology (Seligman):** Meaning is a core component of flourishing (**PERMA Model**). It is achieved by belonging to and serving something larger than oneself.\n\n---\n\n## Conclusion: The Answer May Be a Verb\n\nSince there is no objective meaning decreed by the universe, most contemporary thought (philosophical and psychological) suggests that **meaning is not something to be discovered, but something to be created.**\n\nIf you accept that the universe is indifferent, the meaning of life becomes:\n\n### 1. The Meaning You Choose to Create\n\nIt is the subjective purpose you assign to your own existence. This could be love, art, justice, connection, knowledge, or helping others.\n\n### 2. The Experience of Living\n\nThe ultimate meaning might simply be the experience itself—the engagement with the world, the appreciation of beauty, the capacity for connection, and the pursuit of growth.\n\nIn essence, **The meaning of life is the life you lead.**', 'role': 'assistant' } }], 'cost': 0.002835, 'cost_details': { 'audio_cost': 0, 'cache_prompt_cost': 0, 'cache_write_cost': 0, 'generation_cost': 0, 'image_cost': 0, 'input_prompt_cost': 2.4e-06, 'output_prompt_cost': 0.002832727, 'tools_cost': 0, 'video_cost': 0 }, 'created': 1766026675, 'discounted': '1', 'id': 'chatcmpl-202512180257506444719362giBMqDX', 'model': 'gemini-2.5-flash-preview-09-2025', 'object': 'chat.completion', 'request_id': '8dce2dd0fd4c4bfeb4374df8fb32a8a6', 'usage': { 'completion_tokens': 1133, 'input_tokens': 0, 'output_tokens': 0, 'prompt_tokens': 8, 'prompt_tokens_details': { 'text_tokens': 8 }, 'server_tool_use': { 'web_search_requests': '' }, 'total_tokens': 1141, 'ttft': 0 } }
AIモデルのトークン使用量を追跡する
By アンドリュー・ジェン •

エンタープライズLLMルーティングの複雑さを管理する

エンタープライズLLMルーティングの複雑さを管理する

OneRouter アンスロポシック クロード API

OneRouter アンスロポシック クロード API

OneRouter OpenAI 応答 API

OneRouter OpenAI 応答 API