costly

Estimate costs of complex LLM workflows in advance before spending money

https://github.com/abhimanyupallavisudhir/costly

Science Score: 44.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
    Found CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Academic email domains
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (10.0%) to scientific vocabulary
Last synced: 6 months ago · JSON representation ·

Repository

Estimate costs of complex LLM workflows in advance before spending money

Basic Info
  • Host: GitHub
  • Owner: abhimanyupallavisudhir
  • License: mit
  • Language: Python
  • Default Branch: master
  • Size: 286 KB
Statistics
  • Stars: 10
  • Watchers: 1
  • Forks: 0
  • Open Issues: 2
  • Releases: 7
Created over 1 year ago · Last pushed 10 months ago
Metadata Files
Readme License Citation

README.md

costly

Estimate costs and running times of complex LLM workflows/experiments/pipelines in advance before spending money, via simulations. Just put @costly() on the load-bearing function; make sure all functions that call it pass **kwargs to it and call your complex function with simulate=True and some cost_log: Costlog object. See examples.ipynb for more details.

(Actually you don't have to pass cost_log and simulate throughout; you can just define a global cost_log and simulate at the top of the file that contains your @costly functions and pass them as defaults to your @costly functions.)

(Pass @costly(disable_costly=True) to disable costly for a function. E.g. you may set disable_costly to be a global variable and pass it to your @costly decorators.)

https://github.com/abhimanyupallavisudhir/costly

Installation

bash pip install costly

Usage

See examples.ipynb for a full walkthrough; some examples below.

```python

from costly import Costlog, costly, CostlyResponse from costly.estimators.llmapiestimation import LLMAPIEstimation as estimator

@costly() def chatgpt(input_string: str, model: str) -> str: from openai import OpenAI

client = OpenAI()
response = client.chat.completions.create(
    model=model, messages=[{"role": "user", "content": input_string}]
)
output_string = response.choices[0].message.content
return output_string

@costly( inputtokens=lambda kwargs: LLMAPIEstimation.messagestoinputtokens( kwargs["messages"], kwargs["model"] ), ) def chatgpt_messages(messages: list[dict[str, str]], model: str) -> str: from openai import OpenAI

client = OpenAI()
response = client.chat.completions.create(model=model, messages=messages)
output_string = response.choices[0].message.content
return output_string

@costly() def chatgpt(input_string: str, model: str) -> str: from openai import OpenAI

client = OpenAI()
response = client.chat.completions.create(
    model=model,
    messages=[
        {"role": "user", "content": input_string},
    ],
)

return CostlyResponse(
    output=response.choices[0].message.content,
    cost_info={
        "input_tokens": response.usage.prompt_tokens,
        "output_tokens": response.usage.completion_tokens,
    },
) # in usage, this will still just return the output, not the whole CostlyResponse object

```

Testing

```bash poetry run pytest -s -m "not slow" poetry run pytest -s -m "slow"

Tests for instructor currently fail. ```

TODO

  • [x] Make it work with async
  • [x] Decide and document what the best way to "propagate" description (for breakdown purposes) through function calls is. Have the user manually write def f(...): ... g(description = kwargs.get("description") + ["f"]? Add a @description("blabla") decorator? Add a @description decorator that automatically appends the function name and arguments into description?
  • [x] Better solution for token counting for Chat messages (search HACK in the repo)
  • [x] make instructor tests pass https://community.openai.com/t/how-to-calculate-the-tokens-when-using-function-call/266573/11
  • [ ] Support for locally run LLMs -- ideally need a cost & time estimator that takes into account your machine details, GPU pricing etc.
  • [ ] support more models

Instructor tests don't really pass but I can kinda live with this. Lmk if anyone has a good way to count tokens from messages that includes tool calling (I'm using this). FAILED tests/test_estimators/test_llm_api_estimation.py::test_estimate_contains_exact_instructor[PERSONINFO_gpt-4o] - AssertionError: ['Input tokens estimate 65 not within 20pc of truth 83'] FAILED tests/test_estimators/test_llm_api_estimation.py::test_estimate_contains_exact_instructor[PERSONINFO_gpt-4o-mini] - AssertionError: ['Input tokens estimate 65 not within 20pc of truth 83'] FAILED tests/test_estimators/test_llm_api_estimation.py::test_estimate_contains_exact_instructor[PERSONINFO_gpt-4-turbo] - AssertionError: ['Input tokens estimate 65 not within 20pc of truth 85'] FAILED tests/test_estimators/test_llm_api_estimation.py::test_estimate_contains_exact_instructor[PERSONINFO_gpt-3.5-turbo] - AssertionError: ['Input tokens estimate 65 not within 20pc of truth 85'] FAILED tests/test_estimators/test_llm_api_estimation.py::test_estimate_contains_exact_instructor[FOOMODEL_gpt-4o] - AssertionError: ['Input tokens estimate 77 not within 20pc of truth 108'] FAILED tests/test_estimators/test_llm_api_estimation.py::test_estimate_contains_exact_instructor[FOOMODEL_gpt-4o-mini] - AssertionError: ['Input tokens estimate 77 not within 20pc of truth 108'] FAILED tests/test_estimators/test_llm_api_estimation.py::test_estimate_contains_exact_instructor[FOOMODEL_gpt-4-turbo] - AssertionError: ['Input tokens estimate 77 not within 20pc of truth 113'] FAILED tests/test_estimators/test_llm_api_estimation.py::test_estimate_contains_exact_instructor[FOOMODEL_gpt-3.5-turbo] - AssertionError: ['Input tokens estimate 77 not within 20pc of truth 113'] FAILED tests/test_estimators/test_llm_api_estimation.py::test_estimate_contains_exact_instructor[BARMODEL_gpt-4o] - AssertionError: ['Input tokens estimate 70 not within 20pc of truth 168'] FAILED tests/test_estimators/test_llm_api_estimation.py::test_estimate_contains_exact_instructor[BARMODEL_gpt-4o-mini] - AssertionError: ['Input tokens estimate 70 not within 20pc of truth 168'] FAILED tests/test_estimators/test_llm_api_estimation.py::test_estimate_contains_exact_instructor[BARMODEL_gpt-4-turbo] - AssertionError: ['Input tokens estimate 70 not within 20pc of truth 178'] FAILED tests/test_estimators/test_llm_api_estimation.py::test_estimate_contains_exact_instructor[BARMODEL_gpt-4] - AssertionError: ['Input tokens estimate 70 not within 20pc of truth 126'] FAILED tests/test_estimators/test_llm_api_estimation.py::test_estimate_contains_exact_instructor[BARMODEL_gpt-3.5-turbo] - AssertionError: ['Input tokens estimate 70 not within 20pc of truth 178']

Owner

  • Name: Abhimanyu Pallavi Sudhir
  • Login: abhimanyupallavisudhir
  • Kind: user
  • Location: UK
  • Company: Warwick University

CS PhD student https://abhimanyu.io http://thewindingnumber.blogspot.com

Citation (CITATION.cff)

cff-version: 1.2.0
message: "If you use this software, please cite it as below."
authors:
  - family-names: Pallavi Sudhir
    given-names: Abhimanyu
    orcid: https://orcid.org/0000-0002-2506-0515
title: "Costly: a Python package for estimating costs of LLM workflows in advance"
version: 2.3.4
date-released: 2025-01-29

GitHub Events

Total
  • Release event: 7
  • Watch event: 3
  • Push event: 14
  • Pull request event: 2
  • Create event: 6
Last Year
  • Release event: 7
  • Watch event: 3
  • Push event: 14
  • Pull request event: 2
  • Create event: 6

Issues and Pull Requests

Last synced: 6 months ago

All Time
  • Total issues: 0
  • Total pull requests: 1
  • Average time to close issues: N/A
  • Average time to close pull requests: less than a minute
  • Total issue authors: 0
  • Total pull request authors: 1
  • Average comments per issue: 0
  • Average comments per pull request: 0.0
  • Merged pull requests: 1
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 0
  • Pull requests: 1
  • Average time to close issues: N/A
  • Average time to close pull requests: less than a minute
  • Issue authors: 0
  • Pull request authors: 1
  • Average comments per issue: 0
  • Average comments per pull request: 0.0
  • Merged pull requests: 1
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • abhimanyupallavisudhir (3)
Pull Request Authors
  • abhimanyupallavisudhir (10)
Top Labels
Issue Labels
good first issue (2) immediate (1)
Pull Request Labels

Packages

  • Total packages: 1
  • Total downloads:
    • pypi 193 last-month
  • Total dependent packages: 0
  • Total dependent repositories: 0
  • Total versions: 31
  • Total maintainers: 1
pypi.org: costly

Estimate costs and running times of complex LLM workflows/experiments/pipelines in advance before spending money, via simulations.

  • Versions: 31
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 193 Last month
Rankings
Dependent packages count: 10.4%
Average: 34.5%
Dependent repos count: 58.6%
Maintainers (1)
Last synced: 6 months ago

Dependencies

pyproject.toml pypi
  • Faker ^28.0.0
  • pydantic ^2.8.2
  • python ^3.11
  • tiktoken ^0.7.0
  • typing-extensions ^4.12.2