CostEstimator¶
Estimate costs based on token usage.
Quick Example¶
from mamba_agents.tokens import CostEstimator
estimator = CostEstimator()
# Estimate cost
breakdown = estimator.estimate(
prompt_tokens=1000,
completion_tokens=500,
model="gpt-4o",
)
print(f"Total: ${breakdown.total_cost:.4f}")
# Custom rates
estimator.set_rate("my-model", 0.002)
# Get rates
all_rates = estimator.get_all_rates()
Default Rates¶
| Model | Input (per 1M) | Output (per 1M) |
|---|---|---|
| gpt-4o | $2.50 | $10.00 |
| gpt-4o-mini | $0.15 | $0.60 |
| claude-3-5-sonnet | $3.00 | $15.00 |
| Local models | $0.00 | $0.00 |
API Reference¶
CostEstimator
¶
Estimate costs based on token usage.
Provides cost estimation for various models with configurable rates.
Initialize the cost estimator.
| PARAMETER | DESCRIPTION |
|---|---|
custom_rates
|
Custom cost rates per 1000 tokens. Overrides defaults for specified models.
TYPE:
|
Source code in src/mamba_agents/tokens/cost.py
get_rate
¶
Get the cost rate for a model.
| PARAMETER | DESCRIPTION |
|---|---|
model
|
Model name or identifier.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
float
|
Cost per 1000 tokens. |
Source code in src/mamba_agents/tokens/cost.py
estimate
¶
estimate(
usage: TokenUsage, model: str | None = None
) -> CostBreakdown
Estimate cost for token usage.
| PARAMETER | DESCRIPTION |
|---|---|
usage
|
Token usage to estimate cost for.
TYPE:
|
model
|
Model to use for rate lookup.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
CostBreakdown
|
CostBreakdown with detailed costs. |
Source code in src/mamba_agents/tokens/cost.py
estimate_tokens
¶
Estimate cost for a token count.
| PARAMETER | DESCRIPTION |
|---|---|
token_count
|
Number of tokens.
TYPE:
|
model
|
Model to use for rate lookup.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
float
|
Estimated cost in USD. |
Source code in src/mamba_agents/tokens/cost.py
set_rate
¶
Set a custom rate for a model.
| PARAMETER | DESCRIPTION |
|---|---|
model
|
Model name.
TYPE:
|
rate
|
Cost per 1000 tokens.
TYPE:
|
get_all_rates
¶
Get all configured rates.
| RETURNS | DESCRIPTION |
|---|---|
dict[str, float]
|
Dictionary of model names to rates. |
CostBreakdown
dataclass
¶
CostBreakdown(
prompt_cost: float,
completion_cost: float,
total_cost: float,
model: str | None = None,
)
Cost breakdown by category.
| ATTRIBUTE | DESCRIPTION |
|---|---|
prompt_cost |
Cost for prompt tokens.
TYPE:
|
completion_cost |
Cost for completion tokens.
TYPE:
|
total_cost |
Total cost.
TYPE:
|
model |
Model used for calculation.
TYPE:
|