Metadata-Version: 2.4
Name: llm-router-ledger
Version: 0.1.1
Summary: Route LLM calls (OpenAI-compatible providers and local Ollama) and keep a JSONL ledger of every request and response for offline cost reconciliation.
Project-URL: Homepage, https://github.com/nirmalyaghosh/llm-router-ledger
Project-URL: Issues, https://github.com/nirmalyaghosh/llm-router-ledger/issues
Author-email: Nirmalya Ghosh <1637161+nirmalyaghosh@users.noreply.github.com>
License: MIT License
        
        Copyright (c) 2026 Nirmalya Ghosh
        
        Permission is hereby granted, free of charge, to any person obtaining
        a copy of this software and associated documentation files (the
        "Software"), to deal in the Software without restriction, including
        without limitation the rights to use, copy, modify, merge, publish,
        distribute, sublicense, and/or sell copies of the Software, and to
        permit persons to whom the Software is furnished to do so, subject to
        the following conditions:
        
        The above copyright notice and this permission notice shall be
        included in all copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
        EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
        MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
        NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
        BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
        ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
        CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Keywords: ledger,llm,openai,openrouter,router,tracking,usage
Classifier: Development Status :: 2 - Pre-Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Software Development :: Libraries
Requires-Python: >=3.12
Requires-Dist: openai>=1.55.0
Requires-Dist: pydantic>=2.7
Requires-Dist: python-dotenv>=1.0
Requires-Dist: pyyaml>=6.0
Provides-Extra: dev
Requires-Dist: mypy>=1.10; extra == 'dev'
Requires-Dist: pytest>=8.0; extra == 'dev'
Requires-Dist: ruff>=0.5; extra == 'dev'
Requires-Dist: twine>=5.0; extra == 'dev'
Requires-Dist: types-pyyaml; extra == 'dev'
Description-Content-Type: text/markdown

# llm-router-ledger

Route any LLM call through one `send_message()` and keep a JSONL ledger
of every request and response for offline cost reconciliation.

## Provider support

| Provider | Status | Adapter |
|---|---|---|
| OpenRouter | Supported | OpenAI-compat |
| Anthropic | Planned | direct |
| Azure OpenAI | Planned | direct |
| DeepSeek | Planned | direct |
| Gemini | Planned | direct |
| MiniMax | Planned | direct |
| OpenAI | Planned | OpenAI-compat |
| Qwen | Planned | direct |
| Zhipu / GLM | Planned | direct |
| ByteDance Seed | Planned | via OpenRouter |
| Xiaomi MiMo | Planned | via OpenRouter |
| Local Ollama | Supported | OpenAI-compat |

In 0.1.0 OpenRouter and Local Ollama are verified end-to-end. All other
rows describe planned providers and land in their own minor releases.

## Install

```bash
uv pip install llm-router-ledger
```

## Quickstart

Set `OPENROUTER_API_KEY` in `.env` and create `llm_endpoints.yaml` in
the working directory. The fastest path is to copy the example file
and edit it:

```bash
cp examples/llm_endpoints.example.yaml llm_endpoints.yaml
```

```python
from llm_router_ledger import UsageTracker, send_message

tracker = UsageTracker(
    log_path="logs/usage.jsonl",
    project_id="my-blog",
)
text, usage, gen_id = send_message(
    endpoint_name="openrouter-gpt-4.1-nano",
    system="You are concise.",
    user="Explain prompt caching in two sentences.",
    tracker=tracker,
)
```

Or against a local Ollama server, with no API costs:

```python
text, usage, gen_id = send_message(
    endpoint_name="local-llama",
    system="You are concise.",
    user="Explain prompt caching in two sentences.",
    tracker=tracker,
)
```

`send_message()` returns `(response_text, usage_dict, generation_id)`.
`UsageTracker` appends paired `llm_request` / `llm_response` events to
the JSONL log, stamped with `project_id`, `run_tag`, `run_label`, and
`purpose` for later grouping.

## CLI

```bash
llm-router-ledger list                          # show configured endpoints
llm-router-ledger validate llm_endpoints.yaml   # validate the YAML
llm-router-ledger stale --days 30               # endpoints with stale pricing
llm-router-ledger chat --endpoint openrouter-gpt-4.1-nano \
    --system "You are concise." --user "Hello." \
    --log-path logs/usage.jsonl --project-id my-project
```

## Env vars

| Variable | Purpose |
|---|---|
| `LRL_RUN_TAG` | Stamped on every JSONL event. |
| `LRL_RUN_LABEL` | Stamped on every JSONL event. |
| `LRL_CONFIG_PATH` | Default YAML path when `load_config()` is called with no argument. |

## Development

```bash
git clone https://github.com/nirmalyaghosh/llm-router-ledger
cd llm-router-ledger
uv sync --extra dev
pytest tests/unit
```

Verify a local Ollama setup end-to-end with
`python examples/smoke_test_ollama.py` (see prerequisites at the top
of the script).

## License

MIT. See `LICENSE`.
