Metadata-Version: 2.4
Name: laghav
Version: 0.1.0
Summary: Python SDK for Laghav API prompt compression and model routing
Home-page: https://github.com/laghav-ai/laghav-python
Author: Laghav team
Author-email: support@laghav.ai
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Requires-Python: >=3.8
Description-Content-Type: text/markdown
Requires-Dist: requests>=2.25.0
Requires-Dist: pydantic>=2.0.0
Provides-Extra: langchain
Requires-Dist: langchain-core>=0.1.0; extra == "langchain"
Provides-Extra: llama-index
Requires-Dist: llama-index-core>=0.10.0; extra == "llama-index"
Dynamic: author
Dynamic: author-email
Dynamic: classifier
Dynamic: description
Dynamic: description-content-type
Dynamic: home-page
Dynamic: provides-extra
Dynamic: requires-dist
Dynamic: requires-python
Dynamic: summary

# laghav

Official Python SDK for the [Laghav](https://laghav.ai) API — compress every prompt, route to the cheapest capable model, and see exactly where your money went.

## Installation

```bash
pip install laghav
```

## Quick start

```python
from laghav import LaghavClient

client = LaghavClient(api_key="lgh_live_...")

response = client.complete(
    messages=[{"role": "user", "content": "Explain LLM compression in one sentence."}],
    model="auto",
)
print(response.choices[0].message.content)
print(f"Saved ${response.laghav_meta.saved_usd:.6f} on this call")
```

## Methods

### `client.complete()`

Full completion with compression, routing, caching, and typed `LaghavResponse`.

```python
response = client.complete(
    messages=[{"role": "user", "content": "..."}],
    model="auto",          # or "claude-haiku-3", "gpt-4o-mini", etc.
    max_tokens=1000,
    laghav_options={
        "compress": True,
        "route": True,
        "cache": True,
    },
)
# response is LaghavResponse (Pydantic model)
meta = response.laghav_meta   # LaghavMeta — all fields typed
```

### `client.ask()`

Single-prompt convenience wrapper — returns the response string directly.

```python
answer = client.ask("What is prompt compression?")
```

### `client.compress()`

Compress text without invoking an LLM (hits `/v1/playground`).

```python
compressed = client.compress("Hello there! I hope you're doing well ...")
```

### Streaming

```python
for chunk in client.complete(messages=[...], stream=True):
    print(chunk.choices[0].delta.content or "", end="", flush=True)
    if chunk.laghav_meta:       # final chunk carries the meta
        print(f"\nSaved: ${chunk.laghav_meta.saved_usd:.6f}")
```

## `@shield` decorator

Automatically compress any function's prompt argument before your own LLM call:

```python
from laghav import shield, configure_global_client

configure_global_client(api_key="lgh_live_...", base_url="https://api.laghav.ai")

@shield(compress=True)
def my_llm_call(prompt: str) -> str:
    # prompt is already compressed when this runs
    return openai_client.chat.completions.create(...)

my_llm_call("Hello there! Could you possibly help me understand...")
```

## Error handling

```python
from laghav.errors import RateLimitError, BudgetExceededError, LaghavError

try:
    response = client.complete(messages=[...])
except RateLimitError as e:
    print(f"Rate limited — retry after {e.retry_after}s")
except BudgetExceededError as e:
    print(f"Budget cap reached: {e.budget_id}")
except LaghavError as e:
    print(f"API error [{e.status_code}] {e.code}: {e.message}")
```

## Environment variables

| Variable | Default | Description |
|---|---|---|
| `LAGHAV_API_KEY` | — | API key (required if not passed to constructor) |
| `LAGHAV_BASE_URL` | `https://api.laghav.ai` | Override for local development |
