Metadata-Version: 2.4
Name: dvara-ai
Version: 0.1.0
Summary: Lightweight AI gateway for Python: tracing, cost tracking and a local dashboard for LLM calls.
Keywords: llm,observability,tracing,cost-tracking,openai,anthropic,claude,ai-gateway,sqlite,dashboard
Author: Prasad Thorve
License-Expression: Apache-2.0
License-File: LICENSE
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
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
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Topic :: System :: Monitoring
Classifier: Typing :: Typed
Requires-Dist: typer>=0.12
Requires-Dist: rich>=13
Requires-Dist: dvara-ai[openai,anthropic,ui] ; extra == 'all'
Requires-Dist: anthropic>=0.30 ; extra == 'anthropic'
Requires-Dist: openai>=1.0 ; extra == 'openai'
Requires-Dist: fastapi>=0.110 ; extra == 'ui'
Requires-Dist: uvicorn>=0.29 ; extra == 'ui'
Requires-Python: >=3.10
Project-URL: Homepage, https://github.com/PrasadThorve/Dvara
Project-URL: Repository, https://github.com/PrasadThorve/Dvara
Project-URL: Issues, https://github.com/PrasadThorve/Dvara/issues
Project-URL: Changelog, https://github.com/PrasadThorve/Dvara/blob/dev/CHANGELOG.md
Provides-Extra: all
Provides-Extra: anthropic
Provides-Extra: openai
Provides-Extra: ui
Description-Content-Type: text/markdown

# Dvara

**Tracing, cost tracking and a local dashboard for your LLM calls. One line of code, no server, no account.**

Wrap your OpenAI or Anthropic client with `dvara.wrap(...)` and every call is recorded to a
local SQLite file: model, tokens, cost, latency, errors and your own metadata. Inspect it
from the terminal, from Python (as a list or a pandas DataFrame), or in a dashboard that
runs on your machine.

> **Status:** v0.1, an early release. The API may change before 1.0.
> Long-term, Dvara aims to be a lightweight AI gateway (one API across providers, fallbacks,
> caching).

![Dvara dashboard](https://raw.githubusercontent.com/PrasadThorve/Dvara/dev/docs/images/dashboard-light.png)

## Install

```bash
pip install "dvara-ai[all]"      # or: uv add "dvara-ai[all]"
```

The package is `dvara-ai`; you import it as `dvara`. The core has only two dependencies
(`typer`, `rich`); everything else is an optional extra:

| Extra | Installs | Needed for |
|---|---|---|
| `openai` | `openai` | wrapping `openai.OpenAI` clients |
| `anthropic` | `anthropic` | wrapping `anthropic.Anthropic` clients |
| `ui` | `fastapi`, `uvicorn` | the `dvara ui` dashboard |
| `all` | all of the above | |

Python 3.10 or newer.

## Quickstart

```python
import dvara
from openai import OpenAI
from anthropic import Anthropic

# Wrap once. Your code and the response objects stay exactly the same.
openai_client = dvara.wrap(OpenAI(), metadata={"feature": "support-bot"})
claude = dvara.wrap(Anthropic(), metadata={"feature": "summarizer"})

openai_client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Say hi in five words."}],
)
claude.messages.create(
    model="claude-haiku-5-5",
    max_tokens=100,
    messages=[{"role": "user", "content": "Say hi in five words."}],
)
```

Then look at what happened:

```bash
dvara logs      # recent calls
dvara stats     # totals and per-model breakdown
dvara ui        # dashboard at http://127.0.0.1:4000
```

Or read traces from Python:

```python
traces = dvara.get_traces(limit=50, model="gpt-4o-mini", status="success")
df = dvara.get_traces(as_dataframe=True)   # needs pandas installed
```

A runnable version is in [`examples/quickstart.py`](examples/quickstart.py).

## Command line

```text
$ dvara logs -n 5
┏━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━━━┳━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━┓
┃ Time                ┃ Provider  ┃ Model           ┃    In ┃   Out ┃      Cost ┃ Latency ┃ Status  ┃
┡━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━╇━━━━━━━╇━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━┩
│ 2026-11-29 19:30:05 │ openai    │ gpt-6.2-sol     │ 3,721 │   383 │   $0.0113 │   2.4 s │ success │
│ 2026-11-29 19:08:39 │ anthropic │ claude-haiku-5… │ 1,649 │   169 │ $0.000249 │  521 ms │ success │
│ 2026-11-29 18:46:33 │ openai    │ gpt-5-mini      │   737 │   482 │ $0.001148 │  870 ms │ success │
│ 2026-11-29 18:20:30 │ anthropic │ claude-sonnet-… │ 1,786 │   815 │   $0.0117 │   2.1 s │ success │
│ 2026-11-29 18:02:27 │ anthropic │ claude-opus-5-5 │ 2,321 │ 1,142 │   $0.0321 │   3.5 s │ success │
└─────────────────────┴───────────┴─────────────────┴───────┴───────┴───────────┴─────────┴─────────┘
```

| Command | What it does | Options |
|---|---|---|
| `dvara logs` | Table of the most recent calls | `-n/--limit` (default 20), `--model`, `--status success\|error` |
| `dvara stats` | Total calls, tokens, cost, average latency and error rate, plus a per-model table | `--model` |
| `dvara clear` | Delete all traces (asks first) | `-y/--yes` to skip the question |
| `dvara ui` | Start the local dashboard and open it in your browser | `--port` (default 4000), `--no-browser` |

## Dashboard

`dvara ui` (needs the `ui` extra) serves a single page with summary cards, cost over time
(hourly for the last 48 hours or daily for the last 30 days), cost by model, and a
filterable table of calls. Click a row to see its metadata, error and, if you logged them,
the prompt and response. New calls appear automatically every 5 seconds. Light and dark
mode follow your system setting.

![Dvara dashboard in dark mode](https://raw.githubusercontent.com/PrasadThorve/Dvara/dev/docs/images/dashboard-dark.png)

The dashboard only listens on `127.0.0.1` and only answers requests addressed to
`127.0.0.1` or `localhost`, so it isn't reachable from your network. It is read-only, and
works offline (Chart.js ships inside the package).

## What gets recorded

Each call becomes one row in the `traces` table:

| Field | Notes |
|---|---|
| `id`, `timestamp` | Random hex ID; UTC ISO-8601 time the call started |
| `provider`, `model` | `openai` or `anthropic`; the model the API reports (e.g. `gpt-4o-mini-2024-07-18`) |
| `input_tokens`, `output_tokens`, `total_tokens` | Normalized across providers |
| `cost_usd` | `NULL` if the model's price is unknown |
| `latency_ms` | Time around the API call only |
| `status`, `error_type`, `error_message` | `success` or `error`, with the exception's class and message |
| `metadata` | Whatever you passed to `wrap(..., metadata=...)`, as JSON |
| `prompt`, `response` | **`NULL` unless you pass `log_content=True`** |
| `fallback_used` | Always 0 (reserved for v0.3) |

**Privacy:** prompt and response text are not stored unless you opt in with
`dvara.wrap(client, log_content=True)`. Nothing ever leaves your machine; Dvara makes no
network calls of its own.

**Location:** `~/.dvara/traces.db`. Set `DVARA_DB_PATH` to use a different file, for
example one per project.

### Open the database yourself

It's a plain SQLite file, so any SQLite tool works:

- [DB Browser for SQLite](https://sqlitebrowser.org/): open `~/.dvara/traces.db`.
- [Datasette](https://datasette.io/): `datasette ~/.dvara/traces.db` for a browsable web UI with SQL.
- `sqlite3 ~/.dvara/traces.db "SELECT model, SUM(cost_usd) FROM traces GROUP BY model"`

## Cost tracking

Costs come from a built-in price table (USD per million input and output tokens) taken
from the official [OpenAI](https://developers.openai.com/api/docs/pricing) and
[Anthropic](https://platform.claude.com/docs/en/about-claude/pricing) pricing pages. The
date it was last checked is in `dvara.pricing.PRICES_UPDATED`.

- Dated model names find their base model: `gpt-4o-mini-2024-07-18` uses the `gpt-4o-mini`
  price. The longest matching name wins.
- Models that charge more for long prompts (Claude Haiku 5.5 above 100K input tokens,
  newer GPT models above 272K) use the higher price for those calls.
- Unknown models get `cost_usd = NULL`, never a guess and never an error. Add or override
  prices yourself:

```python
dvara.set_price("my-finetuned-model", input_per_1m=0.30, output_per_1m=1.20)
```

Not included in v0.1: prompt-cache read/write rates, batch discounts, fast mode and
data-residency surcharges. If you use prompt caching heavily, actual costs will differ.

## Overhead

Tracing adds about **27 µs per call**. That's measured on a mocked client that returns
instantly, so it's only Dvara's own work: reading usage, looking up the price and one SQLite
write (WAL mode, one reused connection). For comparison, a real LLM call takes hundreds of
milliseconds or more. Measured on an Apple M5, macOS 26.6, Python 3.12; run
`uv run pytest tests/test_overhead.py -s` to measure on your machine.

## Guarantees

- **Tracing never breaks your call.** If writing a trace fails, Dvara logs a warning on the
  `dvara` logger and you still get the real response.
- **Errors are untouched.** If the API call raises, Dvara records an error trace and
  re-raises the original exception unchanged.
- **Same objects.** You get back the SDK's own response objects, not copies or wrappers.

## Limitations in v0.1

- Streaming calls (`stream=True`) pass through but aren't traced.
- Async clients (`AsyncOpenAI`, `AsyncAnthropic`) aren't supported yet.
- Clients created with `client.with_options(...)`, and `.with_raw_response` calls, aren't traced.
- Only `chat.completions.create` (OpenAI) and `messages.create` (Anthropic) are traced.
- Anthropic prompt-cache tokens aren't counted in `input_tokens` or cost.

These are planned for future releases.

## Development

```bash
uv sync --all-extras                        # install everything
uv run pytest                               # tests use mocked clients: no keys, no network
uv run python scripts/seed_demo_data.py     # fake traces for working on the dashboard
uv run dvara ui
```

The seed script writes to `~/.dvara/traces.db` unless you set `DVARA_DB_PATH`.

## License

[Apache-2.0](LICENSE)
