Metadata-Version: 2.5
Name: pennytrase
Version: 0.3.0
Summary: Local-first cost, outcome and margin tracking for AI agent tasks. Fail-open SDK, zero dependencies.
Project-URL: Homepage, https://github.com/ramsakalle45/pennytrase
Project-URL: Documentation, https://github.com/ramsakalle45/pennytrase#readme
Project-URL: Issues, https://github.com/ramsakalle45/pennytrase/issues
Author: pennytrase contributors
License: MIT
License-File: LICENSE
Keywords: agents,ai,cost-tracking,llm,observability
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.10
Provides-Extra: server
Requires-Dist: fastapi; extra == 'server'
Requires-Dist: uvicorn; extra == 'server'
Description-Content-Type: text/markdown

# pennytrase

Local-only, zero-cost MVP that tracks cost, outcome and margin of AI agent tasks per customer. No cloud, no paid services, no API keys required. Never stores prompts or responses — metadata only.

Dashboard: http://localhost:8000 · API docs: http://localhost:8000/docs · Official guide: http://localhost:8000/guide · Markdown docs: [`docs/`](docs/index.md)

## Install the SDK

```bash
pip install pennytrase            # SDK only, zero dependencies
pip install "pennytrase[server]"  # + FastAPI server, then: pennytrase-server
```

Or copy one file (`src/pennytrase/__init__.py`, stdlib only, zero dependencies) into your repo as `pennytrase.py`.

## Run without Docker

```bash
python -m venv .venv && source .venv/bin/activate
pip install -r requirements.txt
pip install -e .   # installs the pennytrase SDK from src/
uvicorn app.main:app --reload --port 8000
```

## Run with Docker

```bash
docker compose up --build
# persists SQLite in ./data/pennytrase.db
```

## Demo

```bash
# terminal 1: server (pick one run method above)
# terminal 2:
python examples/demo_seed.py      # believable data; Umbrella Corp shows red on top
python examples/example_usage.py  # SDK live demo; kill the server mid-run — it must not crash
```

## API quick ref (full reference with curl examples: [`docs/api.md`](docs/api.md))

- `POST /v1/tasks/start {task_id, customer_id, workflow}`
- `POST /v1/events {task_id, kind: llm|tool, model?, provider?, input_tokens?, output_tokens?, cost_usd?, pricing_override?, flags?, ...}` — LLM cost computed server-side (exact → alias → override → estimate); response carries `pricing_source`.
- `POST /v1/tasks/finish {task_id, status: success|failed, outcome_value?}`
- `PUT /v1/customers/{id} {name?, monthly_revenue?}`
- `GET /v1/tasks/recent` + `GET /v1/tasks/{id}` — per-call explorer feed and drill-down
- `GET /v1/metrics/summary|customer|workflows|trend?days=14` (`?from=YYYY-MM-DD&to=...` supported)
- `GET/PUT /v1/pricing`, `GET /v1/pricing/refresh`, `GET /health`
- `DELETE /v1/customers/{id}` (erasure), `DELETE /v1/tasks/before?date=` (retention)
- Auth: if `PT_API_KEY` env is set, send `X-API-Key` header on all `/v1/*` (reads included); otherwise off (default). Dashboard has a key field for this.

Cost: `in/1e6*in_price + out/1e6*out_price`. Margin = `monthly_revenue prorated (days/30)` − cost. Cost/success = all-task cost ÷ successful tasks. Exact definitions: [`docs/methodology.md`](docs/methodology.md).

## Tests

```bash
pytest -q
```

## How to plug this into my own product

1. Install: `pip install pennytrase` (or copy `src/pennytrase/__init__.py` as `pennytrase.py` — no pip deps).
2. Wrap your agent call:
   ```python
   from pennytrase import Pennytrase
   pt = Pennytrase(base_url="http://localhost:8000")
   with pt.task(customer_id="acme", workflow="support_refund") as t:
       t.llm(model="gpt-4o-mini", response=openai_resp, latency_ms=900)  # auto-extracts usage
       t.tool(name="stripe_refund", cost_usd=0.002)
       t.success(outcome_value=12.0)  # or t.fail(); exception → auto-fail
   # or: @pt.track(customer_id="acme", workflow="support_refund")
   ```
3. Set what each customer pays: `PUT /v1/customers/acme {"monthly_revenue": 800}` (or edit inline in dashboard).
4. Look at the dashboard: worst-margin customer floats to the top; fix price, model, or retry logic. Edit `app/pricing.json` or the Pricing tab when vendors change prices.
5. It is fail-open: if pennytrase is down, your agent keeps running (background queue, 2s timeout, errors swallowed to stderr).
