{"id":25750478,"url":"https://github.com/prajwalsrinvas/llm_api_cost_calc","last_synced_at":"2025-06-27T03:11:24.267Z","repository":{"id":247834491,"uuid":"804321191","full_name":"Prajwalsrinvas/llm_api_cost_calc","owner":"Prajwalsrinvas","description":"LLM API Cost Calculator","archived":false,"fork":false,"pushed_at":"2025-04-22T09:50:47.000Z","size":70,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-22T11:04:33.939Z","etag":null,"topics":["llm","llm-cost","streamlit"],"latest_commit_sha":null,"homepage":"https://llm-api-cost.streamlit.app/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Prajwalsrinvas.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2024-05-22T11:28:07.000Z","updated_at":"2025-04-22T09:50:50.000Z","dependencies_parsed_at":"2024-08-28T01:33:32.441Z","dependency_job_id":"f4637b72-14aa-4d16-bdeb-76ef3e94a32c","html_url":"https://github.com/Prajwalsrinvas/llm_api_cost_calc","commit_stats":null,"previous_names":["prajwalsrinvas/llm_api_cost_calc"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Prajwalsrinvas/llm_api_cost_calc","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Prajwalsrinvas%2Fllm_api_cost_calc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Prajwalsrinvas%2Fllm_api_cost_calc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Prajwalsrinvas%2Fllm_api_cost_calc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Prajwalsrinvas%2Fllm_api_cost_calc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Prajwalsrinvas","download_url":"https://codeload.github.com/Prajwalsrinvas/llm_api_cost_calc/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Prajwalsrinvas%2Fllm_api_cost_calc/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262181059,"owners_count":23271319,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["llm","llm-cost","streamlit"],"created_at":"2025-02-26T13:16:50.656Z","updated_at":"2025-06-27T03:11:24.213Z","avatar_url":"https://github.com/Prajwalsrinvas.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# LLM API Cost Calculator 🤖🧮\n\nA Streamlit app to calculate, compare, and visualize the costs of various LLM APIs. The app includes real-time pricing data, interactive visualization, and advanced features like token estimation and query parameter support.\n\n[LLM API Cost Calculator demo.webm](https://github.com/user-attachments/assets/b7bd21b6-ade2-4d56-b008-203e0724a464)\n\n![image](https://github.com/user-attachments/assets/7921cef2-507e-4521-8647-8ad7b76cd141)\n\n![image](https://github.com/user-attachments/assets/1ebec78f-61ce-4250-865a-00ed96a73b2c)\n\n![image](https://github.com/user-attachments/assets/ae342797-8d7b-48af-bf8f-91568afc3b9d)\n\n---\n\n## Features\n\n- **Real-time Pricing Data:**  \n  Fetches up-to-date pricing information from [docsbot.ai](https://docsbot.ai/tools/gpt-openai-api-pricing-calculator) using BeautifulSoup, regex extraction, and caching to minimize redundant API calls. The raw data is optionally saved to a local file (`cost.json`) for debugging.\n\n- **Cost Calculation:**  \n  Computes the total cost based on input tokens, output tokens, and API calls. Costs are calculated on a per‑million tokens basis and further compared against a default model to provide a relative cost metric.\n\n- **Relative Cost Comparison:**  \n  Compares costs of various models using a user-selected default model (e.g., **GPT-4o mini**) as the baseline.\n\n- **Provider Filtering:**  \n  Allows filtering the results by LLM provider, with automatic normalization of provider names (e.g., \"OpenAI / Azure\" is treated as \"OpenAI\").\n\n- **Interactive Visualization:**  \n  Displays cost data in both a detailed table and an interactive horizontal bar chart built with Plotly.\n\n- **Currency Conversion:**  \n  Supports both USD and INR. A live USD-to-INR exchange rate is fetched and applied, with a fallback rate provided if the fetch fails.\n\n- **Token Estimation Dialog:**  \n  Provides a dialog (powered by the `tiktoken` module) for estimating token counts from sample input and output texts. Estimated values are stored in session state and automatically populate the token input fields.\n\n- **Detailed Token Cost Breakdown:**  \n  Offers an option to display individual input and output token costs in the results table.\n\n- **Query Parameters Support:**  \n  Reads URL query parameters (`input_tokens`, `output_tokens`, and `api_calls`) to pre-populate the respective input fields, ensuring a seamless user experience.\n\n- **Caching \u0026 Session State:**  \n  Utilizes Streamlit’s caching for data fetching and exchange rate lookups. The app leverages session state to retain token estimation results between interactions.\n\n---\n\n## Dependencies\n\n- pandas\n- plotly\n- requests\n- beautifulsoup4\n- streamlit (supports st.cache_data, st.dialog, and session state)\n- tiktoken (optional, for token estimation)\n\n---\n\n## Key Functions\n\n- **`fetch_llm_api_cost()`**  \n  Fetches and parses pricing data from the remote website using caching. The raw JSON data is optionally written to `cost.json` for debugging.\n\n- **`load_data()`**  \n  Loads and preprocesses the fetched pricing data into a pandas DataFrame, including normalization of provider names.\n\n- **`calculate_costs()`**  \n  Computes the total and relative costs based on user inputs (input tokens, output tokens, and API calls), applies currency conversion, and optionally includes a detailed breakdown of token costs.\n\n- **`create_total_cost_chart()`**  \n  Generates an interactive horizontal bar chart visualizing the total cost per model.\n\n- **`estimate_dialog()`**  \n  Opens a dialog for estimating token counts using sample texts. Estimated token counts are stored in session state and automatically populate the token input fields.\n\n---\n\n## Main Application Flow\n\n1. **Data Loading \u0026 Preprocessing:**  \n   - Fetch pricing data and the live USD-to-INR exchange rate.\n   - Normalize provider names and prepare the data for cost calculations.\n\n2. **User Input Sidebar:**  \n   - **Provider and Model Selection:**  \n     Choose which LLM providers to include and select a default model for relative cost comparison.\n   - **Token and API Call Inputs:**  \n     - Manually enter the number of input tokens, output tokens, and API calls.\n     - Alternatively, use URL query parameters (`input_tokens`, `output_tokens`, and `api_calls`) to pre-populate these values. (ex: http://localhost:8501/?input_tokens=2000\u0026output_tokens=300\u0026api_calls=10)\n   - **Token Estimation:**  \n     Click the \"Calculate Token Count\" button to open the token estimation dialog. The resulting token counts are automatically populated into the input fields.\n   - **Display Options:**  \n     - Toggle the display of individual token cost breakdowns.\n     - Select the display currency (USD or INR).\n\n3. **Cost Calculation \u0026 Visualization:**  \n   - Calculate total and relative costs based on the provided inputs.\n   - Present the results in a detailed table and an interactive bar chart.\n\n4. **Performance \u0026 Debugging:**  \n   - Leverages caching to optimize data fetching and exchange rate lookups.\n   - Optionally writes fetched pricing data to `cost.json` for debugging purposes.\n\n---\n\n## Usage\n\nTo run the application locally:\n\n```bash\ncurl -LsSf https://astral.sh/uv/install.sh | sh\nuv venv --python 3.11\nsource .venv/bin/activate\nuv pip install -r requirements.txt\nstreamlit run app.py\n```\n\nInstalls uv -\u003e Installs python (if not present) -\u003e creates and activates venv -\u003e installs requirements  \npip can be used for this too, uv is faster!  \n`streamlit run` command will launch the app in your browser. You can modify token values, filter providers, use URL query parameters to pre-populate inputs, and interactively view cost comparisons and visualizations.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprajwalsrinvas%2Fllm_api_cost_calc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprajwalsrinvas%2Fllm_api_cost_calc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprajwalsrinvas%2Fllm_api_cost_calc/lists"}