Skip to main content

Python SDK for Laghav API prompt compression and model routing

Project description

laghav

Official Python SDK for the Laghav API — compress every prompt, route to the cheapest capable model, and see exactly where your money went.

Installation

pip install laghav

Quick start

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.

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.

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

client.compress()

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

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

Streaming

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:

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

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

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

laghav-0.1.0.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

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

laghav-0.1.0-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

Details for the file laghav-0.1.0.tar.gz.

File metadata

  • Download URL: laghav-0.1.0.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for laghav-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e2781b06be63aaef4e2603bc8085e4efdefafc87343e8c3017ce255be1b068eb
MD5 232e91147b9d515a77ec05b00165d377
BLAKE2b-256 f2191424c646afec48d6d6a77840c60e8af4aaa3ca8725d3a9a366bdf30d0ee1

See more details on using hashes here.

File details

Details for the file laghav-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: laghav-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for laghav-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 263760021cc4e112c510a036479a6c2041ccc85f5d29edfe8369bcb1e1b50d52
MD5 30b965026e5dda1b0a245ead64521e6c
BLAKE2b-256 4800d79f4876a344d65d08b4931304e56885050a2e6a7e33de3ca068f70c9e1a

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