Cost Tracking

Every completion carries a cost estimate derived from reported token counts

Every completion carries a cost estimate โ€” PraisonAI derives it from the token counts your provider reports.

Diagram

Cost Estimation

๐Ÿ“ Prompt

๐Ÿค– LLM

๐Ÿ“Š Usage input/output

๐Ÿ’ฒ Pricing Table

โœ… Cost USD

Cost is computed from the input_tokens and output_tokens your provider returns, multiplied by a per-model rate. When litellm is installed it covers 1000+ models; otherwise a built-in fallback table prices the most common models.

Quick Start

  1. Run an agent and read its token counts

    A bare Agent(llm="gpt-4o-mini") records usage automatically. Read the counts back with the global collector:

    python
    from praisonaiagents import Agent
    from praisonaiagents.telemetry.token_collector import get_token_collector
    
    agent = Agent(llm="gpt-4o-mini")
    agent.start("Summarise the theory of relativity in one paragraph")
    
    summary = get_token_collector().get_session_summary()
    print(summary["total_metrics"]["input_tokens"], summary["total_metrics"]["output_tokens"])
    # cost is derived from these counts via calculate_llm_cost()
  2. Estimate the cost from those counts

    Pass the counts and the model name to calculate_llm_cost():

    python
    from praisonaiagents.utils.cost_utils import calculate_llm_cost
    
    cost = calculate_llm_cost(
        prompt_tokens=1000,
        completion_tokens=500,
        model="gpt-4o-mini",
    )
    print(f"${cost}")  # $0.00045  โ†’  (1000/1M ร— $0.15) + (500/1M ร— $0.60)
  3. Read cost from an async agent

    An async run reports the same telemetry as sync โ€” every tool-loop iteration emits llm_end with tokens, cost, and latency.

    python
    import asyncio
    from praisonaiagents import Agent
    from praisonaiagents.telemetry.token_collector import get_token_collector
    
    agent = Agent(llm="gpt-4o-mini")
    
    async def main():
        await agent.astart("Summarise the theory of relativity in one paragraph")
    
    asyncio.run(main())
    
    summary = get_token_collector().get_session_summary()
    print(summary["total_metrics"]["input_tokens"], summary["total_metrics"]["output_tokens"])
    # Same non-zero counts you would see from a sync agent.start(...)

How It Works

Cost is input_tokens ร— input_rate + output_tokens ร— output_rate. The only question is which rate applies to a given model name.

Diagram

Longest exact/prefix key

No match

๐Ÿท๏ธ Model name

โœ‚๏ธ Strip provider prefix

๐Ÿ” Match fallback table

๐Ÿ’ฒ Model rate

๐Ÿ’ฒ default rate

โœ… Cost


How Pricing Is Resolved

The fallback matcher resolves a model name to a rate in three steps:

  1. Strip the provider prefix. openai/gpt-4o-mini becomes gpt-4o-mini, so provider-qualified names resolve the same as bare names.
  2. Match longest-key-first, on exact match or prefix boundary. The table is searched from the longest key down, and a key only matches when the model name is exactly that key or starts with <key>-โ€ฆ. So gpt-4o-mini is billed at $0.15/1M, not gpt-4o‘s $2.50/1M, and o1-mini no longer matches o1.
  3. Fall back to default. If no key matches, the default rate applies. default is never matched by name โ€” it is only the last resort.

Fallback Pricing Table

These are the built-in rates the SDK ships in _FALLBACK_PRICING (USD per 1M tokens). They apply when litellm is not installed.

ModelInput ($/1M)Output ($/1M)
gpt-4o2.5010.00
gpt-4o-mini0.150.60
gpt-4-turbo10.0030.00
gpt-430.0060.00
gpt-3.5-turbo0.501.50
o115.0060.00
o1-mini1.104.40
o1-preview15.0060.00
o3-mini1.104.40
claude-3-5-sonnet3.0015.00
claude-3-5-haiku0.804.00
claude-3-opus15.0075.00
claude-3-sonnet3.0015.00
claude-3-haiku0.251.25
gemini-1.5-pro1.255.00
gemini-1.5-flash0.0750.30
gemini-2.0-flash0.100.40
gemini-2.0-flash-exp0.100.40
gemini-pro0.501.50
deepseek-chat0.140.28
deepseek-reasoner0.552.19
default1.003.00

Common Patterns

Price a whole session

Read the session totals, then price them once:

python
from praisonaiagents import Agent
from praisonaiagents.telemetry.token_collector import get_token_collector
from praisonaiagents.utils.cost_utils import calculate_llm_cost

agent = Agent(llm="gpt-4o-mini")
agent.start("Draft a product announcement")

totals = get_token_collector().get_session_summary()["total_metrics"]
cost = calculate_llm_cost(totals["input_tokens"], totals["output_tokens"], model="gpt-4o-mini")
print(f"${cost}")
Read spend from a provider/model agent
python
from praisonaiagents import Agent

agent = Agent(llm="anthropic/claude-3-5-sonnet-20240620")
agent.start("Summarise the theory of relativity in one paragraph")

print(agent.total_cost)                 # non-zero
print(agent.cost_summary["llm_calls"])  # >= 1
Provider-prefixed model names resolve correctly

openai/gpt-4o-mini is stripped to gpt-4o-mini before matching, so it prices at the mini rate:

python
from praisonaiagents.utils.cost_utils import calculate_llm_cost

print(calculate_llm_cost(1000, 500, model="openai/gpt-4o-mini"))  # 0.00045
print(calculate_llm_cost(1000, 500, model="gpt-4o-mini"))         # 0.00045 โ€” same

Best Practices

Use realistic model names

Pass the exact model string you run with (e.g. gpt-4o-mini, claude-3-5-sonnet). The longest-key-first matcher relies on the full name to avoid billing a -mini model at its larger sibling’s rate.

Install litellm for uncommon models

The fallback table covers common models only. For anything outside it, install litellm and call calculate_llm_cost(..., use_litellm=True) for accurate, up-to-date pricing.

Cost is an estimate, not a bill

These numbers estimate spend from token counts and published rates. Use your provider’s dashboard for authoritative billing.


Related