Skip to content

Custom clients & primitives

If a library calls chat.completions.create, responses.create or messages.create on the official SDKs, install() already covers it. That includes OpenAI-compatible servers such as vLLM, Ollama and LM Studio reached through openai.OpenAI(base_url=...). For anything else, put the guard’s building blocks around your own call:

import parry
guard = parry.get_guard() or parry.install()
def generate(prompt: str) -> str:
guard.check_budget("my-model", prompt_tokens=parry.count_tokens(prompt, "my-model"))
guard.acquire("myprovider", "my-model") # waits, or raises RateLimited
response = my_client.generate(prompt)
guard.record_usage("myprovider", "my-model", response.in_tokens, response.out_tokens)
return response.text

Price the model so the budget can see its spend:

guard.set_pricing("my-model", input_per_million=0.5, output_per_million=1.5)
guard.set_rate_limit("my-model", requests_per_minute=120, provider="myprovider")

Everything parry does is built from four thread-safe Rust types and two functions, which you can use directly.

from parry import Cache
cache = Cache(max_entries=1_000, ttl_seconds=600)
key = Cache.make_key("my-namespace", '{"model": "x", "prompt": "hi"}') # blake3 of canonical JSON
cache.set(key, "serialised value", tokens=42, cost_usd=0.0003)
cache.get(key) # 'serialised value'
cache.lookup(key) # ('serialised value', 42, 0.0003)
cache.stats() # {'hits': 2, 'misses': 0, 'entries': 1, ...}