openai / openai/openai-openapi

[Proposal] Introducing an API Endpoint for Token Count and Cost Estimation

Open
#205 1 comment 7 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

api enhancement
Dominant language
No language data
Stars
2.5k
Forks
527
Avg merge
1h 46m
Merged PRs (30d)
2

Description

As the development community continues to leverage OpenAI’s advanced models, the need for more transparent and accessible budgeting tools has become apparent. The introduction of a programmatic way to estimate token usage and associated costs would mark a significant step forward in this regard, particularly with the utilization of cutting-edge models like GPT-4o.

This proposal outlines the introduction of a dedicated API endpoint for token count and cost estimation and suggests enhancing model objects with pricing information to aid developers in making more informed decisions.

The Proposal

  1. API Endpoint for Token Count and Cost Estimation (count_tokens): This endpoint aims to provide developers with an efficient tool for estimating the number of tokens generated by their text inputs, alongside the expected cost, for a specific model, namely GPT-4o.
  2. Incorporate Pricing Information into Model Endpoints: To further aid decision-making processes, it is proposed that model detail endpoints be updated to include essential pricing information.

Benefits

  • Accurate Cost Management: Enables developers to accurately manage and forecast their expenditures on the OpenAI platform.
  • Seamless Developer Workflow: Integrates directly into development workflows, allowing for real-time cost estimations without manual intervention.
  • Transparent Pricing: Offers clear visibility into pricing structures, promoting trust and reliability in the OpenAI ecosystem.

Suggested Implementation

Here is how the count_tokens endpoint could be implemented, using GPT-4o as an example:

POST /v1/count_tokens
Content-Type: application/json

{
    "model": "gpt-4o",
    "text": "Your sample text goes here."
}

Expected Response:

{
    "tokens": 150,
    "cost": 0.001
}

To include model-specific pricing details transparently:

GET /v1/models

Sample Response:

{
    "models": [
        {
            "id": "gpt-4o",
            "object": "model",
            "token_cost_per_thousand": "0.08"
        },
        // More model objects...
    ]
}

The introduction of the count_tokens endpoint, particularly with support for GPT-4o, represents a critical enhancement to the OpenAI API, streamlining development processes and facilitating better budget management. Community feedback on this proposal is invaluable for refining and implementing these suggestions effectively.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No files or tests are named. Start by locating the existing endpoint and model definitions in the OpenAPI specification, then review how similar API proposals are represented. Done means the project has an agreed scope for token counting, cost estimation, and model pricing before implementation begins.

Written by the indexing model from the issue text.

Assessment

Domain
api, backend-api-design
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.