Skip to main content

FinOps for LLM agents — unit economics and cost accountability in the developer workflow

Project description

stepcost (Python SDK)

FinOps for LLM agents — a typed, priced cost graph for every agent step. See ../../SPEC.md for the product and technical design.

Install

pip install stepcost        # from PyPI (or: pip install -e . from a clone)
pip install -e ".[dev]"     # + pytest, ruff

Requires Python ≥ 3.11. Runtime deps: pydantic (only).

Quickstart (no API key, $0)

python examples/quickstart.py
stepcost report ~/.stepcost/quickstart.db

examples/quickstart.py instruments a 3-step agent (plan → retrieve → execute) with simulated usage and writes a priced cost graph to SQLite. stepcost report renders the per-step tree, by-step / by-kind / by-customer rollups, and waste flags — no spend, no network.

Instrument your agent

from stepcost import StepCost, agent_step, llm_call

cc = StepCost(project="my-app", sink="sqlite:///~/.stepcost/my-app.db")

with cc.trace(feature_id="chat", customer_id="acme") as trace:
    with agent_step("answer"), llm_call(model="gpt-4o-mini", provider="openai") as call:
        resp = openai_client.chat.completions.create(...)
        call.record(resp)               # invoice-safe token + $ extraction

print(trace.total_usd, trace.by_step)   # also .by_kind
  • call.record(resp) extracts usage from OpenAI (Chat Completions, Responses API, embeddings) and Anthropic responses — cached / reasoning tokens and 5m- vs 1h-TTL cache writes split correctly, no double-count. Unrecognized usage shapes raise instead of silently recording $0.
  • No extractor for your provider? Use record_usage(input_tokens=…, output_tokens=…, embedding_tokens=…).
  • Sinks: sink="stdout" or sink="sqlite:///path.db".
  • Safe by construction: spans persist at trace exit and process exit (no manual flush() needed), sink failures re-queue instead of dropping spans, and concurrent asyncio tasks under one trace parent their spans correctly.
  • Models missing from the price table trigger a runtime warning and an "unpriced spans" section in stepcost report — never a silent $0.

Report CLI

stepcost report <db>                 # account summary: by feature/customer/kind, top traces, waste
stepcost report <db> --trace <id>    # one trace's cost tree (per-node tokens + $)
stepcost report <db> --json          # machine-readable

Privacy

Metadata-only by default (PayloadCapture.NONE): token counts, model, and your business dimensions — never prompt/response content. Fully offline; SQLite stays local.

Develop

pytest        # 50 tests
ruff check stepcost tests scripts
python scripts/bench_overhead.py     # p99 span-emit overhead (<5ms gate)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

stepcost-0.2.0.tar.gz (43.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

stepcost-0.2.0-py3-none-any.whl (40.6 kB view details)

Uploaded Python 3

File details

Details for the file stepcost-0.2.0.tar.gz.

File metadata

  • Download URL: stepcost-0.2.0.tar.gz
  • Upload date:
  • Size: 43.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.13

File hashes

Hashes for stepcost-0.2.0.tar.gz
Algorithm Hash digest
SHA256 e61bef37b79897ec037e9857d00bd836f43fa6ad2006661a14af43a362afa3e5
MD5 12047c9e49ddd3caec1cfb94ed5e0866
BLAKE2b-256 8b1de006c3e1c0ae903acb379ff92c1ba30e4eecd78b765166969de432ac1aa9

See more details on using hashes here.

File details

Details for the file stepcost-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: stepcost-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 40.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.13

File hashes

Hashes for stepcost-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 71f4e5cab8da0137ece7875fa26bcf24be7ed1dbac0a35be8cd393538c85e35d
MD5 00b11329d2cc7470a3a80b06517073d8
BLAKE2b-256 49f9accbc9129c7f521e3a59455ba0862496f413a1a95bd6a9a85139fbb053e8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page