tokencost

AgentOps-AIMIT长期未更新

为 400 多个大模型提供 token 价格估算,帮你在调用 OpenAI、Claude 等模型前算清成本,方便做预算和用量监控。

Easy token price estimates for 400+ LLMs. TokenOps.

0.1.26发布于 2025-08-13 06:28 UTC2,004star108fork同步于 2026-11-07 07:30 UTC

怎么上手

由 README 提炼,执行前请确认
pip install tokencost
硬件要求按 README 说明

README 未说明,纯 Python 客户端库,无需 GPU

下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

客户端统计 LLM 提示与补全的 token 数并估算美元成本,覆盖 400+ 模型

为 400 多个大模型提供 token 价格估算,帮你在调用 OpenAI、Claude 等模型前算清成本,方便做预算和用量监控。

核心能力

  • 用 calculate_prompt_cost 一行算出提示成本
  • 用 calculate_completion_cost 估算补全成本
  • 用 count_message_tokens 统计 ChatML 消息 token
  • 用 count_string_tokens 统计纯字符串 token
  • 内置价格表跟踪主流 LLM 最新定价

容易踩的坑

Anthropic 3 代以上模型计数会调用其 beta token 计数 API,需要网络与相应访问权限;旧版 Claude 模型用 cl100k_base 近似计数,结果不精确;价格表随 README 更新,模型定价变动时需升级版本

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

5 个版本0.1.26
  1. 0.1.26最新正式版2025-08-13 · 2025-08-13 06:28 UTC

    Add AWS Bedrock model cost tracking

    This release includes comprehensive support for AWS Bedrock models with flexible pricing pattern matching and model configuration.

    Changes:

    • Added AWS Bedrock model cost tracking functionality
    • Enhanced pricing pattern matching capabilities
    • Updated model configuration system
    • Fixed version bump issue (pyproject.toml now correctly shows 0.1.26)

    Technical Details:

    • 7 files changed with 3,709 additions and 36 deletions
    • Comprehensive model pricing data updates
    • Enhanced cost calculation algorithms
  2. 0.1.252025-07-22 · 2025-07-22 22:39 UTC
    查看更新说明

    What's Changed

    New Contributors

    Full Changelog: https://github.com/AgentOps-AI/tokencost/compare/0.1.24...0.1.25

  3. 0.1.242025-06-24 · 2025-06-24 22:21 UTC
    查看更新说明

    What's Changed

    Full Changelog: https://github.com/AgentOps-AI/tokencost/compare/0.1.23...0.1.24

  4. 0.1.232025-06-09 · 2025-06-09 16:09 UTC
    查看更新说明

    What's Changed

    Full Changelog: https://github.com/AgentOps-AI/tokencost/compare/0.1.22...0.1.23

  5. 0.1.222025-05-23 · 2025-05-23 00:34 UTC
    查看更新说明

    What's Changed

    New Contributors

    Full Changelog: https://github.com/AgentOps-AI/tokencost/compare/0.1.21...0.1.22

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    TokenCost

    License: MIT PyPI - Version X (formerly Twitter) Follow

    Tokencost helps calculate the USD cost of using major Large Language Model (LLMs) APIs by calculating the estimated cost of prompts and completions.

    Building AI agents? Check out AgentOps

    Features

    • LLM Price Tracking Major LLM providers frequently add new models and update pricing. This repo helps track the latest price changes
    • Token counting Accurately count prompt tokens before sending OpenAI requests
    • Easy integration Get the cost of a prompt or completion with a single function

    Example usage:

    from tokencost import calculate_prompt_cost, calculate_completion_cost
    
    model = "gpt-3.5-turbo"
    prompt = [{ "role": "user", "content": "Hello world"}]
    completion = "How may I assist you today?"
    
    prompt_cost = calculate_prompt_cost(prompt, model)
    completion_cost = calculate_completion_cost(completion, model)
    
    print(f"{prompt_cost} + {completion_cost} = {prompt_cost + completion_cost}")
    # 0.0000135 + 0.000014 = 0.0000275
    

    Installation

    Recommended: PyPI:
    pip install tokencost
    

    Usage

    Cost estimates

    Calculating the cost of prompts and completions from OpenAI requests

    from openai import OpenAI
    
    client = OpenAI()
    model = "gpt-3.5-turbo"
    prompt = [{ "role": "user", "content": "Say this is a test"}]
    
    chat_completion = client.chat.completions.create(
        messages=prompt, model=model
    )
    
    completion = chat_completion.choices[0].message.content
    # "This is a test."
    
    prompt_cost = calculate_prompt_cost(prompt, model)
    completion_cost = calculate_completion_cost(completion, model)
    print(f"{prompt_cost} + {completion_cost} = {prompt_cost + completion_cost}")
    # 0.0000180 + 0.000010 = 0.0000280
    

    Calculating cost using string prompts instead of messages:

    from tokencost import calculate_prompt_cost
    
    prompt_string = "Hello world" 
    response = "How may I assist you today?"
    model= "gpt-3.5-turbo"
    
    prompt_cost = calculate_prompt_cost(prompt_string, model)
    print(f"Cost: ${prompt_cost}")
    # Cost: $3e-06
    

    Counting tokens

    from tokencost import count_message_tokens, count_string_tokens
    
    message_prompt = [{ "role": "user", "content": "Hello world"}]
    # Counting tokens in prompts formatted as message lists
    print(count_message_tokens(message_prompt, model="gpt-3.5-turbo"))
    # 9
    
    # Alternatively, counting tokens in string prompts
    print(count_string_tokens(prompt="Hello world", model="gpt-3.5-turbo"))
    # 2
    
    

    How tokens are counted

    Under the hood, strings and ChatML messages are tokenized using Tiktoken, OpenAI's official tokenizer. Tiktoken splits text into tokens (which can be parts of words or individual characters) and handles both raw strings and message formats with additional tokens for message formatting and roles.

    For Anthropic models above version 3 (i.e. Sonnet 3.5, Haiku 3.5, and Opus 3), we use the Anthropic beta token counting API to ensure accurate token counts. For older Claude models, we approximate using Tiktoken with the cl100k_base encoding.

    Cost table

    Units denominated in USD. All prices can be located here.

    Model NamePrompt Cost (USD) per 1M tokensCompletion Cost (USD) per 1M tokensMax Prompt TokensMax Output Tokens
    gpt-4$30$6081924096
    gpt-4o$2.5$10128,00016384
    gpt-4o-audio-preview$2.5$10128,00016384
    gpt-4o-audio-preview-2024-10-01$2.5$10128,00016384
    gpt-4o-mini$0.15$0.6128,00016384
    gpt-4o-mini-2024-07-18$0.15$0.6128,00016384
    o1-mini$1.1$4.4128,00065536
    o1-mini-2024-09-12$3$12128,00065536
    o1-preview$15$60128,00032768
    o1-preview-2024-09-12$15$60128,00032768
    chatgpt-4o-latest$5$15128,0004096
    gpt-4o-2024-05-13$5$15128,0004096
    gpt-4o-2024-08-06$2.5$10128,00016384
    gpt-4-turbo-preview$10$30128,0004096
    gpt-4-0314$30$608,1924096
    gpt-4-0613$30$608,1924096
    gpt-4-32k$60$12032,7684096
    gpt-4-32k-0314$60$12032,7684096
    gpt-4-32k-0613$60$12032,7684096
    gpt-4-turbo$10$30128,0004096
    gpt-4-turbo-2024-04-09$10$30128,0004096
    gpt-4-1106-preview$10$30128,0004096
    gpt-4-0125-preview$10$30128,0004096
    gpt-4-vision-preview$10$30128,0004096
    gpt-4-1106-vision-preview$10$30128,0004096
    gpt-3.5-turbo$1.5$216,3854096
    gpt-3.5-turbo-0301$1.5$24,0974096
    gpt-3.5-turbo-0613$1.5$24,0974096
    gpt-3.5-turbo-1106$1$216,3854096
    gpt-3.5-turbo-0125$0.5$1.516,3854096
    gpt-3.5-turbo-16k$3$416,3854096
    gpt-3.5-turbo-16k-0613$3$416,3854096
    ft:gpt-3.5-turbo$3$616,3854096
    ft:gpt-3.5-turbo-0125$3$616,3854096
    ft:gpt-3.5-turbo-1106$3$616,3854096
    ft:gpt-3.5-turbo-0613$3$64,0964096
    ft:gpt-4-0613$30$608,1924096
    ft:gpt-4o-2024-08-06$3.75$15128,00016384
    ft:gpt-4o-mini-2024-07-18$0.3$1.2128,00016384
    ft:davinci-002$2$216,3844096
    ft:babbage-002$0.4$0.416,3844096
    text-embedding-3-large$0.13$08,191nan
    text-embedding-3-small$0.02$08,191nan
    text-embedding-ada-002$0.1$08,191nan
    text-embedding-ada-002-v2$0.1$08,191nan
    text-moderation-stable$0$032,7680
    text-moderation-007$0$032,7680
    text-moderation-latest$0$032,7680
    256-x-256/dall-e-2----nannan
    512-x-512/dall-e-2----nannan
    1024-x-1024/dall-e-2----nannan
    hd/1024-x-1792/dall-e-3----nannan
    hd/1792-x-1024/dall-e-3----nannan
    hd/1024-x-1024/dall-e-3----nannan
    standard/1024-x-1792/dall-e-3----nannan
    standard/1792-x-1024/dall-e-3----nannan
    standard/1024-x-1024/dall-e-3----nannan
    whisper-1----nannan
    tts-1----nannan
    tts-1-hd----nannan
    azure/tts-1----nannan
    azure/tts-1-hd----nannan
    azure/whisper-1----nannan
    azure/o1-mini$1.21$4.84128,00065536
    azure/o1-mini-2024-09-12$1.1$4.4128,00065536
    azure/o1-preview$15$60128,00032768
    azure/o1-preview-2024-09-12$15$60128,00032768
    azure/gpt-4o$2.5$10128,00016384
    azure/gpt-4o-2024-08-06$2.5$10128,00016384
    azure/gpt-4o-2024-05-13$5$15128,0004096
    azure/global-standard/gpt-4o-2024-08-06$2.5$10128,00016384
    azure/global-standard/gpt-4o-mini$0.15$0.6128,00016384
    azure/gpt-4o-mini$0.16$0.66128,00016384
    azure/gpt-4-turbo-2024-04-09$10$30128,0004096
    azure/gpt-4-0125-preview$10$30128,0004096
    azure/gpt-4-1106-preview$10$30128,0004096
    azure/gpt-4-0613$30$608,1924096
    azure/gpt-4-32k-0613$60$12032,7684096
    azure/gpt-4-32k$60$12032,7684096
    azure/gpt-4$30$608,1924096
    azure/gpt-4-turbo$10$30128,0004096
    azure/gpt-4-turbo-vision-preview$10$30128,0004096
    azure/gpt-35-turbo-16k-0613$3$416,3854096
    azure/gpt-35-turbo-1106$1$216,3844096
    azure/gpt-35-turbo-0613$1.5$24,0974096
    azure/gpt-35-turbo-0301$0.2$24,0974096
    azure/gpt-35-turbo-0125$0.5$1.516,3844096
    azure/gpt-35-turbo-16k$3$416,3854096
    azure/gpt-35-turbo$0.5$1.54,0974096
    azure/gpt-3.5-turbo-instruct-0914$1.5$24,097nan
    azure/gpt-35-turbo-instruct$1.5$24,097nan
    azure/gpt-35-turbo-instruct-0914$1.5$24,097nan
    azure/mistral-large-latest$8$2432,000nan
    azure/mistral-large-2402$8$2432,000nan
    azure/command-r-plus$3$15128,0004096
    azure/ada$0.1$08,191nan
    azure/text-embedding-ada-002$0.1$08,191nan
    azure/text-embedding-3-large$0.13$08,191nan
    azure/text-embedding-3-small$0.02$08,191nan
    azure/standard/1024-x-1024/dall-e-3--$0nannan
    azure/hd/1024-x-1024/dall-e-3--$0nannan
    azure/standard/1024-x-1792/dall-e-3--$0nannan
    azure/standard/1792-x-1024/dall-e-3--$0nannan
    azure/hd/1024-x-1792/dall-e-3--$0nannan
    azure/hd/1792-x-1024/dall-e-3--$0nannan
    azure/standard/1024-x-1024/dall-e-2--$0nannan
    azure_ai/jamba-instruct$0.5$0.770,0004096
    azure_ai/mistral-large$4$1232,0008191
    azure_ai/mistral-small$1$332,0008191
    azure_ai/Meta-Llama-3-70B-Instruct$1.1$0.378,1922048
    azure_ai/Meta-Llama-3.1-8B-Instruct$0.3$0.61128,0002048
    azure_ai/Meta-Llama-3.1-70B-Instruct$2.68$3.54128,0002048
    azure_ai/Meta-Llama-3.1-405B-Instruct$5.33$16128,0002048
    azure_ai/cohere-rerank-v3-multilingual$0$04,0964096
    azure_ai/cohere-rerank-v3-english$0$04,0964096
    azure_ai/Cohere-embed-v3-english$0.1$0512nan
    azure_ai/Cohere-embed-v3-multilingual$0.1$0512nan
    babbage-002$0.4$0.416,3844096
    davinci-002$2$216,3844096
    gpt-3.5-turbo-instruct$1.5$28,1924096
    gpt-3.5-turbo-instruct-0914$1.5$28,1924097
    claude-instant-1$1.63$5.51100,0008191
    mistral/mistral-tiny$0.25$0.2532,0008191
    mistral/mistral-small$0.1$0.332,0008191
    mistral/mistral-small-latest$0.1$0.332,0008191
    mistral/mistral-medium$2.7$8.132,0008191
    mistral/mistral-medium-latest$0.4$2131,0728191
    mistral/mistral-medium-2312$2.7$8.132,0008191
    mistral/mistral-large-latest$2$6128,000128000
    mistral/mistral-large-2402$4$1232,0008191
    mistral/mistral-large-2407$3$9128,000128000
    mistral/pixtral-12b-2409$0.15$0.15128,000128000
    mistral/open-mistral-7b$0.25$0.2532,0008191
    mistral/open-mixtral-8x7b$0.7$0.732,0008191
    mistral/open-mixtral-8x22b$2$665,3368191
    mistral/codestral-latest$1$332,0008191
    mistral/codestral-2405$1$332,0008191
    mistral/open-mistral-nemo$0.3$0.3128,000128000
    mistral/open-mistral-nemo-2407$0.3$0.3128,000128000
    mistral/open-codestral-mamba$0.25$0.25256,000256000
    mistral/codestral-mamba-latest$0.25$0.25256,000256000
    mistral/mistral-embed$0.1--8,192nan
    deepseek-chat$0.14$0.28128,0004096
    codestral/codestral-latest$0$032,0008191
    codestral/codestral-2405$0$032,0008191
    text-completion-codestral/codestral-latest$0$032,0008191
    text-completion-codestral/codestral-2405$0$032,0008191
    deepseek-coder$0.14$0.28128,0004096
    groq/llama2-70b-4096$0.7$0.84,0964096
    groq/llama3-8b-8192$0.05$0.088,1928192
    groq/llama3-70b-8192$0.59$0.798,1928192
    groq/llama-3.1-8b-instant$0.05$0.08128,0008192
    groq/llama-3.1-70b-versatile$0.59$0.798,1928192
    groq/llama-3.1-405b-reasoning$0.59$0.798,1928192
    groq/mixtral-8x7b-32768$0.24$0.2432,76832768
    groq/gemma-7b-it$0.07$0.078,1928192
    groq/gemma2-9b-it$0.2$0.28,1928192
    groq/llama3-groq-70b-8192-tool-use-preview$0.89$0.898,1928192
    groq/llama3-groq-8b-8192-tool-use-preview$0.19$0.198,1928192
    cerebras/llama3.1-8b$0.1$0.1128,000128000
    cerebras/llama3.1-70b$0.6$0.6128,000128000
    friendliai/mixtral-8x7b-instruct-v0-1$0.4$0.432,76832768
    friendliai/meta-llama-3-8b-instruct$0.1$0.18,1928192
    friendliai/meta-llama-3-70b-instruct$0.8$0.88,1928192
    claude-instant-1.2$0.16$0.55100,0008191
    claude-2$8$24100,0008191
    claude-2.1$8$24200,0008191
    claude-3-haiku-20240307$0.25$1.25200,0004096
    claude-3-haiku-latest$0.25$1.25200,0004096
    claude-3-opus-20240229$15$75200,0004096
    claude-3-opus-latest$15$75200,0004096
    claude-3-sonnet-20240229$3$15200,0004096
    claude-3-5-sonnet-20240620$3$15200,0008192
    claude-3-5-sonnet-20241022$3$15200,0008192
    claude-3-5-sonnet-latest$3$15200,0008192
    text-bison----8,1922048
    text-bison@001----8,1921024
    text-bison@002----8,1921024
    text-bison32k$0.12$0.128,1921024
    text-bison32k@002$0.12$0.128,1921024
    text-unicorn$10$288,1921024
    text-unicorn@001$10$288,1921024
    chat-bison$0.12$0.128,1924096
    chat-bison@001$0.12$0.128,1924096
    chat-bison@002$0.12$0.128,1924096
    chat-bison-32k$0.12$0.1232,0008192
    chat-bison-32k@002$0.12$0.1232,0008192
    code-bison$0.12$0.126,1441024
    code-bison@001$0.12$0.126,1441024
    code-bison@002$0.12$0.126,1441024
    code-bison32k$0.12$0.126,1441024
    code-bison-32k@002$0.12$0.126,1441024
    code-gecko@001$0.12$0.122,04864
    code-gecko@002$0.12$0.122,04864
    code-gecko$0.12$0.122,04864
    code-gecko-latest$0.12$0.122,04864
    codechat-bison@latest$0.12$0.126,1441024
    codechat-bison$0.12$0.126,1441024
    codechat-bison@001$0.12$0.126,1441024
    codechat-bison@002$0.12$0.126,1441024
    codechat-bison-32k$0.12$0.1232,0008192
    codechat-bison-32k@002$0.12$0.1232,0008192
    gemini-pro$0.5$1.532,7608192
    gemini-1.0-pro$0.5$1.532,7608192
    gemini-1.0-pro-001$0.5$1.532,7608192
    gemini-1.0-ultra$0.5$1.58,1922048
    gemini-1.0-ultra-001$0.5$1.58,1922048
    gemini-1.0-pro-002$0.5$1.532,7608192
    gemini-1.5-pro$1.25$52,097,1528192
    gemini-1.5-pro-002$1.25$52,097,1528192
    gemini-1.5-pro-001$1.25$51,000,0008192
    gemini-1.5-pro-preview-0514$0.08$0.311,000,0008192
    gemini-1.5-pro-preview-0215$0.08$0.311,000,0008192
    gemini-1.5-pro-preview-0409$0.08$0.311,000,0008192
    gemini-1.5-flash$0.08$0.31,000,0008192
    gemini-1.5-flash-exp-0827$0$01,000,0008192
    gemini-1.5-flash-002$0.08$0.31,048,5768192
    gemini-1.5-flash-001$0.08$0.31,000,0008192
    gemini-1.5-flash-preview-0514$0.08$01,000,0008192
    gemini-pro-experimental$0$01,000,0008192
    gemini-flash-experimental$0$01,000,0008192
    gemini-pro-vision$0.5$1.516,3842048
    gemini-1.0-pro-vision$0.5$1.516,3842048
    gemini-1.0-pro-vision-001$0.5$1.516,3842048
    medlm-medium----32,7688192
    medlm-large----8,1921024
    vertex_ai/claude-3-sonnet@20240229$3$15200,0004096
    vertex_ai/claude-3-5-sonnet@20240620$3$15200,0008192
    vertex_ai/claude-3-5-sonnet-v2@20241022$3$15200,0008192
    vertex_ai/claude-3-haiku@20240307$0.25$1.25200,0004096
    vertex_ai/claude-3-opus@20240229$15$75200,0004096
    vertex_ai/meta/llama3-405b-instruct-maas$0$032,00032000
    vertex_ai/meta/llama3-70b-instruct-maas$0$032,00032000
    vertex_ai/meta/llama3-8b-instruct-maas$0$032,00032000
    vertex_ai/meta/llama-3.2-90b-vision-instruct-maas$0$0128,0002048
    vertex_ai/mistral-large@latest$2$6128,0008191
    vertex_ai/mistral-large@2407$2$6128,0008191
    vertex_ai/mistral-nemo@latest$0.15$0.15128,000128000
    vertex_ai/jamba-1.5-mini@001$0.2$0.4256,000256000
    vertex_ai/jamba-1.5-large@001$2$8256,000256000
    vertex_ai/jamba-1.5$0.2$0.4256,000256000
    vertex_ai/jamba-1.5-mini$0.2$0.4256,000256000
    vertex_ai/jamba-1.5-large$2$8256,000256000
    vertex_ai/mistral-nemo@2407$3$3128,000128000
    vertex_ai/codestral@latest$0.2$0.6128,000128000
    vertex_ai/codestral@2405$0.2$0.6128,000128000
    vertex_ai/imagegeneration@006----nannan
    vertex_ai/imagen-3.0-generate-001----nannan
    vertex_ai/imagen-3.0-fast-generate-001----nannan
    text-embedding-004$0.1$02,048nan
    text-multilingual-embedding-002$0.1$02,048nan
    textembedding-gecko$0.1$03,072nan
    textembedding-gecko-multilingual$0.1$03,072nan
    textembedding-gecko-multilingual@001$0.1$03,072nan
    textembedding-gecko@001$0.1$03,072nan
    textembedding-gecko@003$0.1$03,072nan
    text-embedding-preview-0409$0.01$03,072nan
    text-multilingual-embedding-preview-0409$0.01$03,072nan
    palm/chat-bison$0.12$0.128,1924096
    palm/chat-bison-001$0.12$0.128,1924096
    palm/text-bison$0.12$0.128,1921024
    palm/text-bison-001$0.12$0.128,1921024
    palm/text-bison-safety-off$0.12$0.128,1921024
    palm/text-bison-safety-recitation-off$0.12$0.128,1921024
    gemini/gemini-1.5-flash-002$0.08$0.31,048,5768192
    gemini/gemini-1.5-flash-001$0.08$0.31,048,5768192
    gemini/gemini-1.5-flash$0.08$0.31,048,5768192
    gemini/gemini-1.5-flash-latest$0.08$0.31,048,5768192
    gemini/gemini-1.5-flash-8b-exp-0924$0$01,048,5768192
    gemini/gemini-1.5-flash-exp-0827$0$01,048,5768192
    gemini/gemini-1.5-flash-8b-exp-0827$0$01,000,0008192
    gemini/gemini-pro$0.35$1.0532,7608192
    gemini/gemini-1.5-pro$3.5$10.52,097,1528192
    gemini/gemini-1.5-pro-002$3.5$10.52,097,1528192
    gemini/gemini-1.5-pro-001$3.5$10.52,097,1528192
    gemini/gemini-1.5-pro-exp-0801$3.5$10.52,097,1528192
    gemini/gemini-1.5-pro-exp-0827$0$02,097,1528192
    gemini/gemini-1.5-pro-latest$3.5$1.051,048,5768192
    gemini/gemini-pro-vision$0.35$1.0530,7202048
    gemini/gemini-gemma-2-27b-it$0.35$1.05nan8192
    gemini/gemini-gemma-2-9b-it$0.35$1.05nan8192
    command-r$0.15$0.6128,0004096
    command-r-08-2024$0.15$0.6128,0004096
    command-light$0.3$0.64,0964096
    command-r-plus