{"id":46781119,"url":"https://github.com/orkunkinay/openai_cost_calculator","last_synced_at":"2026-03-10T00:06:37.912Z","repository":{"id":282576056,"uuid":"949049492","full_name":"orkunkinay/openai_cost_calculator","owner":"orkunkinay","description":"Calculate exact USD cost of each OpenAI API call — no guesswork.","archived":false,"fork":false,"pushed_at":"2025-09-18T12:42:49.000Z","size":71,"stargazers_count":7,"open_issues_count":1,"forks_count":3,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-09-19T23:29:59.932Z","etag":null,"topics":["azureopenai","chatgpt","cost-estimation","cost-estimator","openai","openai-api","pricing","python-library"],"latest_commit_sha":null,"homepage":"https://orkunkinay.github.io/openai_cost_calculator/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/orkunkinay.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-03-15T15:03:14.000Z","updated_at":"2025-09-18T08:25:46.000Z","dependencies_parsed_at":"2025-06-11T12:20:34.052Z","dependency_job_id":"0b53dcc4-b9d0-4b41-83b9-13ada2f0480c","html_url":"https://github.com/orkunkinay/openai_cost_calculator","commit_stats":null,"previous_names":["orkunkinay/openai_cost_calculator"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/orkunkinay/openai_cost_calculator","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/orkunkinay%2Fopenai_cost_calculator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/orkunkinay%2Fopenai_cost_calculator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/orkunkinay%2Fopenai_cost_calculator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/orkunkinay%2Fopenai_cost_calculator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/orkunkinay","download_url":"https://codeload.github.com/orkunkinay/openai_cost_calculator/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/orkunkinay%2Fopenai_cost_calculator/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30317689,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-09T20:05:46.299Z","status":"ssl_error","status_checked_at":"2026-03-09T19:57:04.425Z","response_time":61,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["azureopenai","chatgpt","cost-estimation","cost-estimator","openai","openai-api","pricing","python-library"],"created_at":"2026-03-10T00:06:37.275Z","updated_at":"2026-03-10T00:06:37.906Z","avatar_url":"https://github.com/orkunkinay.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# openai_cost_calculator\n\n[![PyPI version](https://img.shields.io/pypi/v/openai-cost-calculator)](https://pypi.org/project/openai-cost-calculator/)\n[![License](https://img.shields.io/badge/license-MIT-blue)](LICENSE)\n\nInstant, accurate **USD cost estimates** for OpenAI \u0026 Azure OpenAI API calls. Works with **Chat Completions** and the **Responses API**, streaming or not. Offers a **typed** `Decimal`-based API for finance-safe math and a **legacy** string API for drop-ins.\n\n**Docs:** https://orkunkinay.github.io/openai_cost_calculator/\n\n---\n\n## Installation\n\n```bash\npip install openai-cost-calculator\n```\n\n\u003e Import name uses underscores: `import openai_cost_calculator`\n\n---\n\n## Quickstart\n\n**Typed (recommended)**\n\n```python\nfrom openai import OpenAI\nfrom openai_cost_calculator import estimate_cost_typed\n\nclient = OpenAI()\nresp = client.chat.completions.create(\n    model=\"gpt-4o-mini\",\n    messages=[{\"role\": \"user\", \"content\": \"Hi there!\"}],\n)\n\ncost = estimate_cost_typed(resp)  # -\u003e CostBreakdown (Decimal fields)\nprint(cost.total_cost)            # Decimal('0.00000750')\nprint(cost.as_dict(stringify=True))  # 8-dp strings if you prefer\n```\n\n**Legacy (string output)**\n\n```python\nfrom openai_cost_calculator import estimate_cost\nprint(estimate_cost(resp))  # dict of 8-dp strings\n```\n\n**Responses API**\n\n```python\nresp = client.responses.create(model=\"gpt-4.1-mini\", input=[{\"role\":\"user\",\"content\":\"Hi\"}])\nfrom openai_cost_calculator import estimate_cost_typed\nprint(estimate_cost_typed(resp))\n```\n\n**Streaming**\n\n```python\nstream = client.chat.completions.create(\n  model=\"gpt-4o-mini\",\n  messages=[{\"role\":\"user\",\"content\":\"Hi\"}],\n  stream=True,\n  stream_options={\"include_usage\": True},\n)\nfrom openai_cost_calculator import estimate_cost_typed\nprint(estimate_cost_typed(stream))\n```\n\n---\n\n## Highlights\n\n- **Typed API:** `CostBreakdown` dataclass with `Decimal` precision  \n- **Drop-in legacy API:** 8-decimal strings (backward compatible)  \n- **Handles edge cases:** cached tokens, undated model strings, streaming generators, Azure deployment names  \n- **Pricing sources:** Remote CSV (24h cache) + **local overrides** and **offline mode**\n\n---\n\n## Pricing utilities\n\n```python\nfrom openai_cost_calculator import (\n  refresh_pricing, set_offline_mode,\n  add_pricing_entry, add_pricing_entries, clear_local_pricing\n)\n\n# Force refresh (bypasses 24h cache)\nrefresh_pricing()\n\n# Run fully offline (no network calls)\nset_offline_mode(True)\n\n# Teach custom prices (per 1M tokens)\nadd_pricing_entry(\n  \"ollama/qwen3:30b\", \"2025-08-01\",\n  input_price=0.20, output_price=0.60, cached_input_price=0.04,\n  minimum_tokens=0,  # optional tier floor; default is 0\n)\n```\n\nIf a model has tiered pricing by prompt/input size, add multiple rows for the same\n`(model_name, model_date)` with different `minimum_tokens` values. The calculator picks\nthe highest matching tier where `minimum_tokens \u003c= prompt_tokens`.\n\nRemote CSV (auto-fetched, cached 24h):  \n`https://raw.githubusercontent.com/orkunkinay/openai_cost_calculator/refs/heads/main/data/gpt_pricing_data.csv`\n\n---\n\n## Errors\n\nRecoverable issues raise `CostEstimateError` with a clear message (missing pricing row, unexpected input shape, etc.).\n\n---\n\n## Troubleshooting\n\n- **“Pricing not found”** → confirm row exists in the CSV; call `refresh_pricing()`.  \n- **`cached_tokens = 0`** → ensure `include_usage_details=True` (classic) or `stream_options={\"include_usage\": True}` (streaming).  \n- **Model string has no date** → the latest row with `date ≤ today` is used.\n\n---\n\n## Links\n\n- **Docs \u0026 examples:** https://orkunkinay.github.io/openai_cost_calculator/  \n- **Source:** https://github.com/orkunkinay/openai_cost_calculator  \n- **Issues:** https://github.com/orkunkinay/openai_cost_calculator/issues\n\n---\n\n## License\n\nMIT © 2025 Orkun Kınay \u0026 Murat Barkın Kınay\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Forkunkinay%2Fopenai_cost_calculator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Forkunkinay%2Fopenai_cost_calculator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Forkunkinay%2Fopenai_cost_calculator/lists"}