Spaces:
Sleeping
Sleeping
| """gpt-4o-mini cost calculator. | |
| Pricing as of 2026-04. Verify against https://openai.com/api/pricing | |
| before recording Video 3 — these constants are the only thing the VP | |
| will see translated into dollars. | |
| """ | |
| from pydantic import BaseModel, ConfigDict | |
| INPUT_COST_PER_M_USD = 0.15 | |
| OUTPUT_COST_PER_M_USD = 0.60 | |
| class CostBreakdown(BaseModel): | |
| model_config = ConfigDict(frozen=True) | |
| input_tokens: int | |
| output_tokens: int | |
| requests: int | |
| usd: float | |
| def format(self) -> str: | |
| return ( | |
| f"${self.usd:.5f} " | |
| f"({self.input_tokens} in + {self.output_tokens} out, " | |
| f"{self.requests} req)" | |
| ) | |
| def cost_from_usage(usage) -> CostBreakdown: | |
| """Convert an openai-agents Usage object to a dollar breakdown.""" | |
| usd = ( | |
| usage.input_tokens * INPUT_COST_PER_M_USD | |
| + usage.output_tokens * OUTPUT_COST_PER_M_USD | |
| ) / 1_000_000 | |
| return CostBreakdown( | |
| input_tokens=usage.input_tokens, | |
| output_tokens=usage.output_tokens, | |
| requests=usage.requests, | |
| usd=usd, | |
| ) | |